openModeller  Version 1.5.0
Log.cpp
Go to the documentation of this file.
1 
28 #include <openmodeller/Log.hh>
29 
30 
32 
33 //Needed for strlen calls otherwise gcc 4.3.2 throws error
34 #include <string.h>
35 
36 using std::ostream;
37 using std::ios_base;
38 using std::fstream;
39 using std::endl;
40 using std::string;
41 //
42 // Static calls to enforce singleton behaviour
43 //
44 Log *Log::mpInstance = 0;
46 {
47  if (mpInstance == 0)
48  {
49  mpInstance = new Log();
50  }
51  return mpInstance;
52 }
53 
54 //
55 // WIN32 function defines
56 //
57 #ifdef WIN32
58 #define snprintf _snprintf
59 #define vsnprintf _vsnprintf
60 #endif
61 
62 /****************************************************************/
63 /******************** StdFileLogCallback ************************/
64 
66 public:
67 
68  StdFileLogCallback( std::string fileName ) {
69 
70  file.open( fileName.c_str(), ios_base::out );
71 
72  if ( file.fail() ) {
73 
74  fprintf( stderr, "Could not open log file!\n" );
75  }
76  }
77 
79 
80  file.close();
81  }
82 
83  void operator()( Log::Level level, const std::string& msg ) {
84 
85  if ( file.is_open() ) {
86 
87  file << msg.c_str();
88  file.flush();
89  }
90  }
91 
92  std::fstream file;
93 };
94 
95 /****************************************************************/
96 /******************** OstreamCallback ***************************/
97 
99  os( os )
100 {}
101 
102 void
103 Log::OstreamCallback::operator()(Level level, const string& msg ) {
104  os << msg;
105 }
106 
107 /****************************************************************/
108 /******************** output function ***************************/
109 const char *
111  {
112  "[Debug] ",
113  "",
114  "[Info] ",
115  "[Warn] ",
116  "[Error] "
117  };
118 
119 void
120 Log::FormatAndWrite( Log::LogCallback& lc, Log::Level level, std::string pref, const char* format, va_list ap ) {
121 
122  const int buf_size = 1024;
123  char buf[buf_size];
124 
125  // Print in 'buf'.
126  //
127  // Header.
128  snprintf( buf, buf_size, "%s%s", LevelLabels[level], pref.c_str() );
129 
130  // Print message after header.
131  int len = strlen( buf );
132  char *end = buf + len;
133  vsnprintf( end, buf_size - len, format, ap );
134 
135  lc( level, buf );
136 }
137 
138 /****************************************************************/
139 /****************************** Log *****************************/
140 
141 std::string
142 Log::format( const char *fmt, ... )
143 {
144  std::string ret_str("");
145 
146  if ( NULL != fmt ) {
147 
148  va_list marker;
149 
150  // initalize variable arguments
151  va_start( marker, fmt );
152 
153  const int buf_size = 1024;
154  char buf[buf_size];
155  char *end = buf;
156 
157  vsnprintf( end, buf_size, fmt, marker );
158 
159  // reset variable arguments
160  va_end( marker );
161 
162  ret_str = buf;
163  }
164 
165  return ret_str;
166 }
167 
168 /*******************/
169 /*** constructor ***/
170 
172  callback( new Log::OstreamCallback( std::cerr ) )
173 {
174  _level = Log::Debug ;
175  setPrefix( "" );
176  _deleteCallback = true;
177 }
178 
179 
180 /******************/
181 /*** destructor ***/
182 
184 {
185  if ( callback && _deleteCallback ) {
186 
187  delete callback;
188  }
189 }
190 
191 /**************************/
192 /*** set all parameters ***/
193 //void
194 //Log::set( Log::Level level, FILE* out, char const *pref )
195 void
196 Log::set( Log::Level level, std::string fileName, char const *pref )
197 {
198  setLevel( level );
199  setCallback( new StdFileLogCallback( fileName ) );
200  setPrefix( pref );
201  _deleteCallback = true;
202 }
203 
204 /********************/
205 /*** set Callback ***/
206 void
208 {
209  if ( callback && _deleteCallback ) {
210 
211  delete callback;
212  _deleteCallback = false;
213  }
214 
215  callback = lc;
216 }
217 
218 
219 /******************/
220 /*** set Prefix ***/
221 void
222 Log::setPrefix( const char *pref )
223 {
224  _pref.assign( pref );
225 
226  // If prefix is non-empty, we need a trailing space
227  if ( _pref.size() > 0 ) {
228 
229  _pref.append(" ");
230  }
231 }
232 
233 
234 /*************/
235 /*** debug ***/
236 void
237 Log::debug( const char *format, ... )
238 {
239  if ( _level > Debug || ! callback ) {
240 
241  return;
242  }
243 
244  va_list ap;
245  va_start( ap, format );
246  FormatAndWrite( *callback, Debug, _pref, format, ap );
247  va_end( ap );
248 
249  return;
250 }
251 
252 
253 /************/
254 /*** info ***/
255 void
256 Log::info( const char *format, ... )
257 {
258  if ( _level > Info || !callback )
259  return;
260 
261  va_list ap;
262  va_start( ap, format );
263  FormatAndWrite( *callback, Info, _pref, format, ap );
264  va_end( ap );
265 
266  return;
267 }
268 
269 
270 /************/
271 /*** warn ***/
272 void
273 Log::warn( const char *format, ... )
274 {
275  if ( _level > Warn || !callback )
276  return;
277 
278  va_list ap;
279  va_start( ap, format );
280  FormatAndWrite( *callback, Warn, _pref, format, ap );
281  va_end( ap );
282 
283  return;
284 }
285 
286 
287 /*************/
288 /*** error ***/
289 void
290 Log::error( const char *format, ... )
291 {
292  if ( _level > Error || !callback )
293  return;
294 
295  va_list ap;
296  va_start( ap, format );
297  FormatAndWrite( *callback, Error, _pref, format, ap );
298  va_end( ap );
299 
300  return;
301 }
302 
303 
304 /*******************/
305 /*** operator () ***/
306 void
307 Log::operator()( const char *format, ... )
308 {
309  if ( _level > Default || !callback )
310  return;
311 
312  va_list ap;
313  va_start( ap, format );
314  FormatAndWrite( *callback, Default, _pref, format, ap );
315  va_end( ap );
316 
317  return;
318 }
Level _level
Definition: Log.hh:129
void warn(const char *format,...)
'Warn' level.
Definition: Log.cpp:273
void setPrefix(const char *pref)
Definition: Log.cpp:222
void setCallback(LogCallback *lc)
Definition: Log.cpp:207
static std::string format(const char *fmt,...)
Definition: Log.cpp:142
const char * LevelLabels[5]
Definition: Log.cpp:110
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
Definition: Log.hh:48
bool _deleteCallback
Definition: Log.hh:133
std::string _pref
Definition: Log.hh:131
Level
Definition: Log.hh:54
void setLevel(Level level)
Definition: Log.hh:107
Definition: Log.hh:57
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
void operator()(Log::Level level, const std::string &msg)
Definition: Log.cpp:83
Log()
Definition: Log.cpp:171
void operator()(Level level, const std::string &msg)
Definition: Log.cpp:103
LogCallback * callback
Definition: Log.hh:127
static Log * mpInstance
Definition: Log.hh:125
StdFileLogCallback(std::string fileName)
Definition: Log.cpp:68
std::fstream file
Definition: Log.cpp:92
void set(Level level, std::string fileName, char const *pref="")
Definition: Log.cpp:196
Definition: Log.hh:58
OstreamCallback(std::ostream &os)
Definition: Log.cpp:98
void operator()(const char *format,...)
Definition: Log.cpp:307
void info(const char *format,...)
'Info' level.
Definition: Log.cpp:256
~StdFileLogCallback()
Definition: Log.cpp:78
void FormatAndWrite(Log::LogCallback &lc, Log::Level level, std::string pref, const char *format, va_list ap)
Definition: Log.cpp:120
void debug(const char *format,...)
'Debug' level.
Definition: Log.cpp:237
~Log()
Definition: Log.cpp:183