EMAN2
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
EMAN::Log Class Reference

Log defines a way to output logging information. More...

#include <log.h>

Collaboration diagram for EMAN::Log:
Collaboration graph
[legend]

Public Types

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 More...
 
void end (int ref, const string &file="-", const string &text="")
 
void error (const char *format,...)
 log an error message. More...
 
void warn (const char *format,...)
 log a warning message. More...
 
void debug (const char *format,...)
 log a debug message. More...
 
void variable (const char *format,...)
 log a very-detailed-level debug message. More...
 
void set_level (int level)
 
void set_logfile (const char *filename)
 set log output file. More...
 
void loc (LogLevel level, const string &file, int linenum, const string &func)
 

Static Public Member Functions

static Loglogger ()
 

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 Loginstance = 0
 

Detailed Description

Log defines a way to output logging information.

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 68 of file log.h.

Member Enumeration Documentation

◆ LogLevel

Enumerator
ERROR_LOG 
WARNING_LOG 
DEBUG_LOG 
VARIABLE_LOG 

Definition at line 71 of file log.h.

72 {
73 ERROR_LOG, // error message
74 WARNING_LOG, // warning message
75 DEBUG_LOG, // debug message, usually at function level.
76 VARIABLE_LOG // very-detailed-level debug message
77 };
@ WARNING_LOG
Definition: log.h:74
@ DEBUG_LOG
Definition: log.h:75
@ ERROR_LOG
Definition: log.h:73
@ VARIABLE_LOG
Definition: log.h:76

Constructor & Destructor Documentation

◆ Log() [1/2]

Log::Log ( )
private

Definition at line 49 of file log.cpp.

50{
51 out = 0;
53#ifdef WIN32
54 char c[2];
55 c[0] = getenv("WINDIR")[0];
56 c[1] = '\0';
57 default_emandir = string(c) + string(":\\.eman");
58#else
59 default_emandir = string(getenv("HOME")) + "/.eman";
60 mkdir(default_emandir.c_str(), 0xffff);
61#endif
62 default_emanlog = ".emanlog";
63 location = "";
64}
string default_emandir
Definition: log.h:121
LogLevel log_level
Definition: log.h:120
string default_emanlog
Definition: log.h:122
string location
Definition: log.h:123
FILE * out
Definition: log.h:119

References default_emandir, default_emanlog, ERROR_LOG, location, log_level, and out.

Referenced by logger().

◆ Log() [2/2]

Log::Log ( const Log )
private

Definition at line 66 of file log.cpp.

67{
68}

◆ ~Log()

Log::~Log ( )
private

Definition at line 70 of file log.cpp.

71{
72 if (out) {
73 fclose(out);
74 out = 0;
75 }
76}

References out.

Member Function Documentation

◆ begin()

int Log::begin ( int  argc,
char *  argv[],
int  ppid 
)

begin() and start() are used by command-line programs

Definition at line 177 of file log.cpp.

178{
179 time_t tm = time(0);
180 const char *pwd = getenv("PWD");
181 int ref = getpid();
182
183 string filename = Util::sbasename(argv[0]);
184
185 char s[4048];
186#ifndef WIN32
187 sprintf(s, "%d\t%d\t%d\t%d\t%s", ref, (int)tm, 0, ppid ? ppid : getppid(), filename.c_str());
188#else
189 sprintf(s, "%d\t%d\t%d\t%d\t%s", ref, (int)tm, 0, ppid, filename.c_str());
190#endif
191 for (int i = 1; i < argc; i++) {
192 sprintf(s + strlen(s), " %s", argv[i]);
193 }
194 sprintf(s + strlen(s), "\n");
195
196 FILE *eman_file = fopen(default_emanlog.c_str(), "a");
197 if (!eman_file) {
198 return 0;
199 }
200
201 //Util::file_lock_wait(eman_file);
202 fprintf(eman_file, "%s", s);
203 fclose(eman_file);
204
205 string dirlist = default_emandir + "./dirlist";
206 FILE *in = fopen(dirlist.c_str(), "r");
207 if (in) {
208 char s[1024];
209 int f = 0;
210 while (fscanf(in, " %1023s", s) == 1) {
211 if (strcmp(s, pwd) == 0) {
212 f = 1;
213 break;
214 }
215 }
216
217 fclose(in);
218 if (!f) {
219 in = 0;
220 }
221 }
222
223 if (!in) {
224 FILE *dirout = fopen(dirlist.c_str(), "a");
225 if (dirout) {
226 fprintf(dirout, "%s\n", pwd);
227 fclose(dirout);
228 }
229 }
230
231 return ref;
232}
static string sbasename(const string &filename)
Get a filename's basename.
Definition: util.cpp:505

References default_emandir, default_emanlog, and EMAN::Util::sbasename().

◆ debug()

void Log::debug ( const char *  format,
  ... 
)

log a debug message.

log level = DEBUG_LOG. Its args are the same as printf().

Definition at line 140 of file log.cpp.

141{
142 va_list arg;
143 va_start(arg, format);
144 vlog(format, DEBUG_LOG, arg);
145 va_end(arg);
146}
void vlog(const char *format, LogLevel level, va_list arg)
Definition: log.cpp:100

References DEBUG_LOG, and vlog().

◆ end()

void Log::end ( int  ref,
const string &  file = "-",
const string &  text = "" 
)

Definition at line 235 of file log.cpp.

236{
237 FILE *out = fopen(".emanlog", "a");
238
239 if (out) {
240 time_t tm = time(0);
241 //Util::file_lock_wait(out);
242 fprintf(out, "%d\t%ld\t%s\t%s\n", ref, tm, file.c_str(), text.c_str());
243 fclose(out);
244 }
245}

References out.

◆ error()

void Log::error ( const char *  format,
  ... 
)

log an error message.

log level = ERROR_LOG. Its args are the same as printf().

Definition at line 156 of file log.cpp.

157{
158 va_list arg;
159 va_start(arg, format);
160 vlog(format, ERROR_LOG, arg);
161 va_end(arg);
162}

References ERROR_LOG, and vlog().

Referenced by EMAN::CCDNormProcessor::process_inplace().

◆ loc()

void Log::loc ( LogLevel  level,
const string &  file,
int  linenum,
const string &  func 
)

Definition at line 88 of file log.cpp.

89{
90 if (log_level < level) {
91 return;
92 }
93
94 location = Util::sbasename(filename) + ":" + Util::int2str(linenum);
95 if (func != "") {
96 location +=" " + func + "()";
97 }
98}
static string int2str(int n)
Get a string format of an integer, e.g.
Definition: util.cpp:315

References EMAN::Util::int2str(), location, log_level, and EMAN::Util::sbasename().

◆ logger()

Log * Log::logger ( )
static

Definition at line 80 of file log.cpp.

81{
82 if (!instance) {
83 instance = new Log();
84 }
85 return instance;
86}
static Log * instance
Definition: log.h:118
Log()
Definition: log.cpp:49

References instance, and Log().

Referenced by EMAN::CCDNormProcessor::process_inplace(), and EMAN::Util::set_log_level().

◆ set_level()

void Log::set_level ( int  level)

Definition at line 165 of file log.cpp.

166{
167 log_level = (LogLevel)level;
168}
LogLevel
Definition: log.h:72

References log_level.

Referenced by EMAN::CCDNormProcessor::process_inplace(), and EMAN::Util::set_log_level().

◆ set_logfile()

void Log::set_logfile ( const char *  filename)

set log output file.

If this function is not called, output is standart output.

Definition at line 170 of file log.cpp.

171{
172 if (filename && !out) {
173 out = fopen(filename, "wb");
174 }
175}

References out.

◆ variable()

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 132 of file log.cpp.

133{
134 va_list arg;
135 va_start(arg, format);
136 vlog(format, VARIABLE_LOG, arg);
137 va_end(arg);
138}

References VARIABLE_LOG, and vlog().

◆ vlog()

void Log::vlog ( const char *  format,
LogLevel  level,
va_list  arg 
)
private

Definition at line 100 of file log.cpp.

101{
102 if (log_level < level) {
103 return;
104 }
105
106 const char *key = "";
107
108 switch (level) {
109 case WARNING_LOG:
110 key = "Warning: ";
111 break;
112 case ERROR_LOG:
113 key = "Error: ";
114 break;
115 default:
116 key = "";
117 }
118
119 FILE *file = stdout;
120 if (out) {
121 file = out;
122 }
123
124 fprintf(file, "%s", key);
125 vfprintf(file, format, arg);
126 if (location != "") {
127 fprintf(file, " at %s", location.c_str());
128 }
129 fprintf(file, "\n");
130}

References ERROR_LOG, location, log_level, out, and WARNING_LOG.

Referenced by debug(), error(), variable(), and warn().

◆ warn()

void Log::warn ( const char *  format,
  ... 
)

log a warning message.

log level = WARNING_LOG. Its args are the same as printf().

Definition at line 148 of file log.cpp.

149{
150 va_list arg;
151 va_start(arg, format);
152 vlog(format, WARNING_LOG, arg);
153 va_end(arg);
154}

References vlog(), and WARNING_LOG.

Member Data Documentation

◆ default_emandir

string EMAN::Log::default_emandir
private

Definition at line 121 of file log.h.

Referenced by begin(), and Log().

◆ default_emanlog

string EMAN::Log::default_emanlog
private

Definition at line 122 of file log.h.

Referenced by begin(), and Log().

◆ instance

Log * Log::instance = 0
staticprivate

Definition at line 118 of file log.h.

Referenced by logger().

◆ location

string EMAN::Log::location
private

Definition at line 123 of file log.h.

Referenced by loc(), Log(), and vlog().

◆ log_level

LogLevel EMAN::Log::log_level
private

Definition at line 120 of file log.h.

Referenced by loc(), Log(), set_level(), and vlog().

◆ out

FILE* EMAN::Log::out
private

Definition at line 119 of file log.h.

Referenced by end(), Log(), set_logfile(), vlog(), and ~Log().


The documentation for this class was generated from the following files: