Skip to content

Commit

Permalink
rf: cleanup error handling and logging (freesurfer#686)
Browse files Browse the repository at this point in the history
* rf: cleanup error handling and logging

* bf: fix dummy mismatch

* use newlines for freeview

* turn off colors for now
  • Loading branch information
ahoopes authored Sep 13, 2019
1 parent 303d28b commit f0d94f0
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 390 deletions.
6 changes: 2 additions & 4 deletions dummy/dummy.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <iostream>

#include "log.h"
#include "timer.h"


int main(int argc, const char **argv) {
logDebug << currentDateTime();
std::cout << currentDateTime();
}
10 changes: 2 additions & 8 deletions freeview/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "LineProf.h"
#include <stdio.h>
#include <stdlib.h>
#include "error.h"
#include <fenv.h>
#include <QFile>
#include <QSurfaceFormat>
Expand All @@ -46,6 +45,7 @@
#endif

#include "fsinit.h"
#include "log.h"
#include "chklc.h"


Expand Down Expand Up @@ -108,16 +108,10 @@ void myMessageOutput(QtMsgType type, const char *msg)
}
#endif

void my_error_exit(int ecode)
{
if (ecode != 0)
throw (ecode);
}

int main(int argc, char *argv[])
{
Progname = argv[0];
ErrorSetExitFunc(my_error_exit);
throwExceptions(true);

putenv((char*)"SURFER_FRONTDOOR=");
if (getenv("FS_DISABLE_LANG") == NULL)
Expand Down
67 changes: 5 additions & 62 deletions include/error.h
Original file line number Diff line number Diff line change
@@ -1,63 +1,6 @@
/**
* @file error.h
* @brief error handling prototypes
*
*/
/*
* Original Author: Bruce Fischl
* CVS Revision Info:
* $Author: greve $
* $Date: 2015/07/27 20:49:35 $
* $Revision: 1.21 $
*
* Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH"
*
* Terms and conditions for use, reproduction, distribution and contribution
* are found in the 'FreeSurfer Software License Agreement' contained
* in the file 'LICENSE' found in the FreeSurfer distribution, and here:
*
* https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense
*
* Reporting: [email protected]
*
*/
#pragma once


#ifndef ERROR_H
#define ERROR_H

#include <stdio.h>
#include <stdarg.h>

int ErrorInit(char *fname,
int (*vfprint)(FILE *fp, const char *fmt, va_list args),
int (*vprint)(const char *fmt, va_list args)) ;
int ErrorSetExitFunc(void (*exit_func)(int ecode)) ;
void ErrorExit(int ecode, const char *fmt, ...) ;
int ErrorPrintf(int ecode, const char *fmt, ...) ;
void SetErrorExitDoneFile(char *DoneFile);
int ErrorWriteDoneFile(char *DoneFile, int errorcode);
#define ErrorReturn(ret, args) { ErrorPrintf args ; return(ret) ; }

#define ESCAPE ErrorExit
#define ErrorSet ErrorPrintf
/* error codes */

#define NO_ERROR 0
#define ERROR_NONE NO_ERROR
#define ERROR_NO_FILE -1
#define ERROR_NOFILE ERROR_NO_FILE
#define ERROR_NO_MEMORY -2
#define ERROR_NOMEMORY ERROR_NO_MEMORY
#define ERROR_UNSUPPORTED -3
#define ERROR_BADPARM -4
#define ERROR_BAD_PARM ERROR_BADPARM
#define ERROR_BADFILE -5
#define ERROR_BAD_FILE ERROR_BADFILE
#define ERROR_SIZE -6
#define ERROR_BADLOOP -7
#define ERROR_OUT_OF_BOUNDS -8

extern int Gerror ; /* global error value */

#endif
// Error functions have been moved to log.h -
// this file is a temporary bridge to avoid modifying every file that
// includes it, but eventally it should be removed.
#include "log.h"
139 changes: 87 additions & 52 deletions include/log.h
Original file line number Diff line number Diff line change
@@ -1,48 +1,12 @@
#ifndef LOG_H
#define LOG_H
#pragma once

#include <iostream>
#include <sstream>

#include "fsinit.h"

/// \class StreamLogger
///
/// A stream-style logging object for printing status messages.
///
/// In general, this class shouldn't used directly. Status messages should
/// be printed via the standard logging macros. For example:
///
/// logWarning << "exceeded standard iteration count";
/// logError << "vertex has no neighbors";
/// logFatal(1) << "could not find file";

class StreamLogger {
public:
enum MessageStatus {Warning, Error, Debug};

StreamLogger(MessageStatus status) : status(status), exitout(false) {};
StreamLogger(MessageStatus status, int exitcode) : status(status), retcode(exitcode), exitout(true) {};
~StreamLogger();

template<typename T>
StreamLogger& operator << (const T& t) {
ss << t;
return *this;
}

// for std::endl
StreamLogger& operator << (std::ostream&(*f)(std::ostream&)) {
f(ss);
return *this;
}

private:
MessageStatus status;
int retcode;
bool exitout;
std::ostringstream ss;
};

// terminal output colors

// terminal colors
namespace term {
const char* black();
const char* red();
Expand All @@ -51,21 +15,92 @@ namespace term {
const char* blue();
const char* purple();
const char* cyan();
const char* light_gray();
const char* white();
const char* light_red();
const char* dim();
// formating
const char* bold();
const char* dim();
const char* underline();
// reset
const char* reset();
}

// macros for easy logging of standard message types
#define logDebug StreamLogger(StreamLogger::Debug)
#define logWarning StreamLogger(StreamLogger::Warning)
#define logError StreamLogger(StreamLogger::Error)
#define logFatal(ret) StreamLogger(StreamLogger::Error, ret)

#endif
// global settings
void throwExceptions(bool setting);
void setErrorLog(const std::string& filename);


namespace detail {

void writeToErrorLog(const std::string& message);
void errorExit(int code);

struct logger
{
template<typename T> logger& operator << (const T& t) { ss << t; return *this; }
logger& operator << (std::ostream&(*f)(std::ostream&)) { f(ss); return *this; }
std::ostringstream ss;
};

}


namespace fs {

struct fatal : public detail::logger
{
int ret;
fatal(int err) : ret(err) {}
~fatal() {
std::cerr << term::red() << "error: " << term::reset() << this->ss.str() << "\n";
detail::writeToErrorLog(this->ss.str());
detail::errorExit(this->ret);
}
};

struct error : public detail::logger
{
~error() {
std::cerr << term::red() << "error: " << term::reset() << this->ss.str() << "\n";
detail::writeToErrorLog(this->ss.str());
}
};

struct warning : public detail::logger
{
~warning() { std::cerr << term::yellow() << "warning: " << term::reset() << this->ss.str() << "\n"; }
};

struct debug : public detail::logger
{
~debug() { std::cerr << term::cyan() << "debug: " << term::reset() << this->ss.str() << "\n"; }
};

}

// temporary definitions
#define logWarning fs::warning()
#define logFatal(ret) fs::fatal(ret)

// old c-style error functions
void ErrorExit(int ecode, const char *fmt, ...);
void ErrorPrintf(int ecode, const char *fmt, ...);
#define ErrorReturn(ret, args) { ErrorPrintf args; return(ret); }
#define ErrorInit(a, b, c) FSinit();

// global error
extern int Gerror;

// error codes
#define NO_ERROR 0
#define ERROR_NONE NO_ERROR
#define ERROR_NO_FILE -1
#define ERROR_NOFILE ERROR_NO_FILE
#define ERROR_NO_MEMORY -2
#define ERROR_NOMEMORY ERROR_NO_MEMORY
#define ERROR_UNSUPPORTED -3
#define ERROR_BADPARM -4
#define ERROR_BAD_PARM ERROR_BADPARM
#define ERROR_BADFILE -5
#define ERROR_BAD_FILE ERROR_BADFILE
#define ERROR_SIZE -6
#define ERROR_BADLOOP -7
#define ERROR_OUT_OF_BOUNDS -8
35 changes: 26 additions & 9 deletions mri_ca_train/mri_ca_train.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,19 @@ int DoSym = 0;
int n_omp_threads;
#endif
char *DoneFile = NULL;
int WriteDoneFile(char *DoneFile, int code);
char *cmdline, cwd[2000];


static void writeDoneFile(char *DoneFile, int errorcode)
{
if (DoneFile == NULL) return;
FILE *fp = fopen(DoneFile, "w");
if (fp == NULL) return;
fprintf(fp, "%d\n", errorcode);
fclose(fp);
}


int
main(int argc, char *argv[])
{
Expand Down Expand Up @@ -162,7 +172,11 @@ main(int argc, char *argv[])
argc -= nargs ;
argv += nargs ;
}
SetErrorExitDoneFile(DoneFile);

// catch exceptions in order to write a done file
throwExceptions(true);
try {

printf("\n");
printf("setenv SUBJECTS_DIR %s\n",getenv("SUBJECTS_DIR"));
printf("cd %s\n",cwd);
Expand Down Expand Up @@ -1037,17 +1051,20 @@ main(int argc, char *argv[])

GCAfree(&gca) ;
msec = start.milliseconds() ;
seconds = nint((float)msec/1000.0f) ;
seconds = nint((float)msec / 1000.0f) ;
minutes = seconds / 60 ;
seconds = seconds % 60 ;
printf("classifier array training took %d minutes"
" and %d seconds.\n", minutes, seconds) ;
printf("classifier array training took %d minutes and %d seconds.\n", minutes, seconds) ;

ErrorWriteDoneFile(DoneFile, 0);
printf("mri_ca_train done\n");
} catch(...) {
writeDoneFile(DoneFile, 1);
printf("mri_ca_train failed\n");
exit(1);
}

exit(0) ;
return(0) ;
writeDoneFile(DoneFile, 0);
printf("mri_ca_train done\n");
return 0;
}
/*----------------------------------------------------------------------
Parameters:
Expand Down
1 change: 0 additions & 1 deletion utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ add_library(utils STATIC
dmatrix.cpp
dti.cpp
dtk.fs.cpp
error.cpp
evschutils.cpp
fcd.cpp
fftutils.cpp
Expand Down
Loading

0 comments on commit f0d94f0

Please sign in to comment.