#include <log.h>
Collaboration diagram for EMAN::Log:
Public Types | |
ERROR_LOG | |
WARNING_LOG | |
DEBUG_LOG | |
VARIABLE_LOG | |
enum | LogLevel { ERROR_LOG, WARNING_LOG, DEBUG_LOG, VARIABLE_LOG } |
Public Member Functions | |
int | begin (int argc, char *argv[], int ppid) |
begin() and start() are used by command-line programs | |
void | end (int ref, const string &file="-", const string &text="") |
void | error (const char *format,...) |
log an error message. | |
void | warn (const char *format,...) |
log a warning message. | |
void | debug (const char *format,...) |
log a debug message. | |
void | variable (const char *format,...) |
log a very-detailed-level debug message. | |
void | set_level (int level) |
void | set_logfile (const char *filename) |
set log output file. | |
void | loc (LogLevel level, const string &file, int linenum, const string &func) |
Static Public Member Functions | |
static Log * | logger () |
Private Member Functions | |
Log () | |
Log (const Log &) | |
~Log () | |
void | vlog (const char *format, LogLevel level, va_list arg) |
Private Attributes | |
FILE * | out |
LogLevel | log_level |
string | default_emandir |
string | default_emanlog |
string | location |
Static Private Attributes | |
static Log * | instance = 0 |
1) The logs can either go to standard output (default), or go to a user-given file. 2) 4 verbose log levels are defined. by default, ERROR_LOG is used. 3) Typical usage: Log::logger()->set_level(Log::WARNING_LEVEL); Log::logger()->error("cannot open file");
Definition at line 72 of file log.h.
enum EMAN::Log::LogLevel |
Definition at line 75 of file log.h.
00076 { 00077 ERROR_LOG, // error message 00078 WARNING_LOG, // warning message 00079 DEBUG_LOG, // debug message, usually at function level. 00080 VARIABLE_LOG // very-detailed-level debug message 00081 };
Log::Log | ( | ) | [private] |
Definition at line 53 of file log.cpp.
References default_emandir, default_emanlog, ERROR_LOG, location, log_level, and out.
Referenced by logger().
00054 { 00055 out = 0; 00056 log_level = ERROR_LOG; 00057 #ifdef WIN32 00058 char c[2]; 00059 c[0] = getenv("WINDIR")[0]; 00060 c[1] = '\0'; 00061 default_emandir = string(c) + string(":\\.eman"); 00062 #else 00063 default_emandir = string(getenv("HOME")) + "/.eman"; 00064 mkdir(default_emandir.c_str(), 0xffff); 00065 #endif 00066 default_emanlog = ".emanlog"; 00067 location = ""; 00068 }
Log::~Log | ( | ) | [private] |
int Log::begin | ( | int | argc, | |
char * | argv[], | |||
int | ppid | |||
) |
begin() and start() are used by command-line programs
Definition at line 181 of file log.cpp.
References default_emandir, default_emanlog, in, and EMAN::Util::sbasename().
00182 { 00183 time_t tm = time(0); 00184 const char *pwd = getenv("PWD"); 00185 #ifdef _WIN32 00186 int ref = _getpid(); 00187 #else 00188 int ref = getpid(); 00189 #endif 00190 00191 string filename = Util::sbasename(argv[0]); 00192 00193 char s[4048]; 00194 #ifndef WIN32 00195 sprintf(s, "%d\t%d\t%d\t%d\t%s", ref, (int)tm, 0, ppid ? ppid : getppid(), filename.c_str()); 00196 #else 00197 sprintf(s, "%d\t%d\t%d\t%d\t%s", ref, (int)tm, 0, ppid, filename.c_str()); 00198 #endif 00199 for (int i = 1; i < argc; i++) { 00200 sprintf(s + strlen(s), " %s", argv[i]); 00201 } 00202 sprintf(s + strlen(s), "\n"); 00203 00204 FILE *eman_file = fopen(default_emanlog.c_str(), "a"); 00205 if (!eman_file) { 00206 return 0; 00207 } 00208 00209 //Util::file_lock_wait(eman_file); 00210 fprintf(eman_file, "%s", s); 00211 fclose(eman_file); 00212 00213 string dirlist = default_emandir + "./dirlist"; 00214 FILE *in = fopen(dirlist.c_str(), "r"); 00215 if (in) { 00216 char s[1024]; 00217 int f = 0; 00218 while (fscanf(in, " %1023s", s) == 1) { 00219 if (strcmp(s, pwd) == 0) { 00220 f = 1; 00221 break; 00222 } 00223 } 00224 00225 fclose(in); 00226 if (!f) { 00227 in = 0; 00228 } 00229 } 00230 00231 if (!in) { 00232 FILE *dirout = fopen(dirlist.c_str(), "a"); 00233 if (dirout) { 00234 fprintf(dirout, "%s\n", pwd); 00235 fclose(dirout); 00236 } 00237 } 00238 00239 return ref; 00240 }
void Log::debug | ( | const char * | format, | |
... | ||||
) |
void Log::end | ( | int | ref, | |
const string & | file = "-" , |
|||
const string & | text = "" | |||
) |
Definition at line 243 of file log.cpp.
References out.
00244 { 00245 FILE *out = fopen(".emanlog", "a"); 00246 00247 if (out) { 00248 time_t tm = time(0); 00249 //Util::file_lock_wait(out); 00250 fprintf(out, "%d\t%ld\t%s\t%s\n", ref, tm, file.c_str(), text.c_str()); 00251 fclose(out); 00252 } 00253 }
void Log::error | ( | const char * | format, | |
... | ||||
) |
log an error message.
log level = ERROR_LOG. Its args are the same as printf().
Definition at line 160 of file log.cpp.
References ERROR_LOG, and vlog().
Referenced by EMAN::CCDNormProcessor::process_inplace().
00161 { 00162 va_list arg; 00163 va_start(arg, format); 00164 vlog(format, ERROR_LOG, arg); 00165 va_end(arg); 00166 }
void Log::loc | ( | LogLevel | level, | |
const string & | file, | |||
int | linenum, | |||
const string & | func | |||
) |
Definition at line 92 of file log.cpp.
References EMAN::Util::int2str(), location, log_level, and EMAN::Util::sbasename().
00093 { 00094 if (log_level < level) { 00095 return; 00096 } 00097 00098 location = Util::sbasename(filename) + ":" + Util::int2str(linenum); 00099 if (func != "") { 00100 location +=" " + func + "()"; 00101 } 00102 }
Log * Log::logger | ( | ) | [static] |
Definition at line 84 of file log.cpp.
References instance, and Log().
Referenced by EMAN::CCDNormProcessor::process_inplace(), and EMAN::Util::set_log_level().
void Log::set_level | ( | int | level | ) |
Definition at line 169 of file log.cpp.
References log_level.
Referenced by EMAN::CCDNormProcessor::process_inplace(), and EMAN::Util::set_log_level().
void Log::set_logfile | ( | const char * | filename | ) |
void Log::variable | ( | const char * | format, | |
... | ||||
) |
log a very-detailed-level debug message.
log level = VARIABLE_LOG. Its args are the same as printf().
Definition at line 136 of file log.cpp.
References VARIABLE_LOG, and vlog().
00137 { 00138 va_list arg; 00139 va_start(arg, format); 00140 vlog(format, VARIABLE_LOG, arg); 00141 va_end(arg); 00142 }
void Log::vlog | ( | const char * | format, | |
LogLevel | level, | |||
va_list | arg | |||
) | [private] |
Definition at line 104 of file log.cpp.
References ERROR_LOG, key, location, log_level, out, and WARNING_LOG.
Referenced by debug(), error(), variable(), and warn().
00105 { 00106 if (log_level < level) { 00107 return; 00108 } 00109 00110 const char *key = ""; 00111 00112 switch (level) { 00113 case WARNING_LOG: 00114 key = "Warning: "; 00115 break; 00116 case ERROR_LOG: 00117 key = "Error: "; 00118 break; 00119 default: 00120 key = ""; 00121 } 00122 00123 FILE *file = stdout; 00124 if (out) { 00125 file = out; 00126 } 00127 00128 fprintf(file, "%s", key); 00129 vfprintf(file, format, arg); 00130 if (location != "") { 00131 fprintf(file, " at %s", location.c_str()); 00132 } 00133 fprintf(file, "\n"); 00134 }
void Log::warn | ( | const char * | format, | |
... | ||||
) |
log a warning message.
log level = WARNING_LOG. Its args are the same as printf().
Definition at line 152 of file log.cpp.
References vlog(), and WARNING_LOG.
00153 { 00154 va_list arg; 00155 va_start(arg, format); 00156 vlog(format, WARNING_LOG, arg); 00157 va_end(arg); 00158 }
string EMAN::Log::default_emandir [private] |
string EMAN::Log::default_emanlog [private] |
Log * Log::instance = 0 [static, private] |
string EMAN::Log::location [private] |
LogLevel EMAN::Log::log_level [private] |
FILE* EMAN::Log::out [private] |