ULogger.h
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef ULOGGER_H
21 #define ULOGGER_H
22 
23 #include "find_object/FindObjectExp.h" // DLL export/import defines
24 
27 
28 #include <stdio.h>
29 #include <time.h>
30 #include <string>
31 #include <vector>
32 
33 #include <stdarg.h>
34 
35 #if _MSC_VER
36  #undef min
37  #undef max
38 #endif
39 
49 /*
50  * Convenient macros for logging...
51  */
52 #define ULOGGER_LOG(level, ...) ULogger::write(level, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
53 
54 #define ULOGGER_DEBUG(...) ULOGGER_LOG(ULogger::kDebug, __VA_ARGS__)
55 #define ULOGGER_INFO(...) ULOGGER_LOG(ULogger::kInfo, __VA_ARGS__)
56 #define ULOGGER_WARN(...) ULOGGER_LOG(ULogger::kWarning, __VA_ARGS__)
57 #define ULOGGER_ERROR(...) ULOGGER_LOG(ULogger::kError, __VA_ARGS__)
58 #define ULOGGER_FATAL(...) ULOGGER_LOG(ULogger::kFatal, __VA_ARGS__)
59 
60 #define UDEBUG(...) ULOGGER_DEBUG(__VA_ARGS__)
61 #define UINFO(...) ULOGGER_INFO(__VA_ARGS__)
62 #define UWARN(...) ULOGGER_WARN(__VA_ARGS__)
63 #define UERROR(...) ULOGGER_ERROR(__VA_ARGS__)
64 #define UFATAL(...) ULOGGER_FATAL(__VA_ARGS__)
65 
66 #define UASSERT(condition) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met!", #condition)
67 #define UASSERT_MSG(condition, msg_str) if(!(condition)) ULogger::write(ULogger::kFatal, __FILE__, __LINE__, __FUNCTION__, "Condition (%s) not met! [%s]", #condition, msg_str)
68 
199 {
200 
201 public:
205  static const std::string kDefaultLogFileName;
206 
213  enum Type{kTypeNoLog, kTypeConsole, kTypeFile};
214 
221  enum Level{kDebug, kInfo, kWarning, kError, kFatal};
222 
234  static void setType(Type type, const std::string &fileName = kDefaultLogFileName, bool append = true);
235  static Type type() {return type_;}
236 
237  // Setters
242  static void setPrintTime(bool printTime) {printTime_ = printTime;}
243  static bool isPrintTime() {return printTime_;}
244 
249  static void setPrintLevel(bool printLevel) {printLevel_ = printLevel;}
250  static bool isPrintLevel() {return printLevel_;}
251 
256  static void setPrintEndline(bool printEndline) {printEndline_ = printEndline;}
257  static bool isPrintEndLine() {return printEndline_;}
258 
264  static void setPrintColored(bool printColored) {printColored_ = printColored;}
265  static bool isPrintColored() {return printColored_;}
266 
271  static void setPrintWhere(bool printWhere) {printWhere_ = printWhere;}
272  static bool isPrintWhere() {return printWhere_;}
273 
278  static void setPrintWhereFullPath(bool printWhereFullPath) {printWhereFullPath_ = printWhereFullPath;}
279  static bool isPrintWhereFullPath() {return printWhereFullPath_;}
280 
287  static void setBuffered(bool buffered);
288  static bool isBuffered() {return buffered_;}
289 
301  static void setLevel(ULogger::Level level) {level_ = level;}
302  static ULogger::Level level() {return level_;}
303 
310  static void setExitLevel(ULogger::Level exitLevel) {exitLevel_ = exitLevel;}
311  static ULogger::Level exitLevel() {return exitLevel_;}
312 
319  static void setEventLevel(ULogger::Level eventSentLevel) {eventLevel_ = eventSentLevel;}
320  static ULogger::Level eventLevel() {return eventLevel_;}
321 
325  static void reset();
326 
331  static void flush();
332 
339  static void write(const char* msg, ...);
340 
341  /*
342  * Write a message to logger: use UDEBUG(), UINFO(), UWARN(), UERROR() or UFATAL() instead.
343  * @param level the log level of this message
344  * @param file the file path
345  * @param line the line in the file
346  * @param function the function name in which the message is logged
347  * @param msg the message to write
348  * @param ... the variable arguments
349  */
350  static void write(ULogger::Level level,
351  const char * file,
352  int line,
353  const char *function,
354  const char* msg,
355  ...);
356 
362  static int getTime(std::string &timeStr);
363 
364 protected:
365  /*
366  * This method is used to have a reference on the
367  * Logger. When no Logger exists, one is
368  * created. There is only one instance in the application.
369  * Must be protected by loggerMutex_.
370  * See the Singleton pattern for further explanation.
371  *
372  * @return the reference on the Logger
373  */
374  static ULogger* getInstance();
375 
376  /*
377  * Called only once in getInstance(). It can't be instanciated
378  * by the user.
379  *
380  * @see getInstance()
381  */
382  ULogger() {}
383 
384  /*
385  * Only called by a Destroyer.
386  * @see Destroyer
387  */
388  virtual ~ULogger();
389 
390  /*
391  * Flush buffered messages
392  */
393  void _flush();
394 
395  /*
396  * A Destroyer is used to remove a dynamicaly created
397  * Singleton. It is friend here to have access to the
398  * destructor.
399  *
400  * @see Destroyer
401  */
402  friend class UDestroyer<ULogger>;
403 
404  /*
405  * The log file name.
406  */
407  static std::string logFileName_;
408 
409  /*
410  * Default true, it doesn't overwrite the file.
411  */
412  static bool append_;
413 
414 private:
415  /*
416  * Create an instance according to type. See the Abstract factory
417  * pattern for further explanation.
418  * @see type_
419  * @return the reference on the new logger
420  */
421  static ULogger* createInstance();
422 
423  /*
424  * Write a message on the output with the format :
425  * "A message". Inherited class
426  * must override this method to output the message. It
427  * does nothing by default.
428  * @param msg the message to write.
429  * @param arg the variable arguments
430  */
431  virtual void _write(const char* msg, va_list arg) {} // Do nothing by default
432  virtual void _writeStr(const char* msg) {} // Do nothing by default
433 
434 private:
435  /*
436  * The Logger instance pointer.
437  */
439 
440  /*
441  * The Logger's destroyer
442  */
444 
445  /*
446  * If the logger prints the time for each message.
447  * Default is true.
448  */
449  static bool printTime_;
450 
451  /*
452  * If the logger prints the level for each message.
453  * Default is true.
454  */
455  static bool printLevel_;
456 
457  /*
458  * If the logger prints the end line for each message.
459  * Default is true.
460  */
461  static bool printEndline_;
462 
463  /*
464  * If the logger prints text with color.
465  * Default is true.
466  */
467  static bool printColored_;
468 
469  /*
470  * If the logger prints where the message is logged (fileName::function():line).
471  * Default is true.
472  */
473  static bool printWhere_;
474 
475  /*
476  * If the logger prints the full path of the source file
477  * where the message is written. Only works when
478  * "printWhere_" is true.
479  * Default is false.
480  */
481  static bool printWhereFullPath_;
482 
483  /*
484  * If the logger limit the size of the "where" path to
485  * characters. If the path is over 8 characters, a "~"
486  * is added. Only works when "printWhereFullPath_" is false.
487  * Default is false.
488  */
489  static bool limitWhereLength_;
490 
491  /*
492  * The type of the logger.
493  */
494  static Type type_;
495 
496  /*
497  * The severity of the log.
498  */
499  static Level level_;
500 
501  /*
502  * The severity at which the application exits.
503  * Note : A FATAL level will always exit whatever the level specified here.
504  */
506 
507  /*
508  * The severity at which the message is also sent in a ULogEvent.
509  */
511 
512  static const char * levelName_[5];
513 
514  /*
515  * Mutex used when accessing public functions.
516  */
518 
519  /*
520  * If the logger prints messages only when ULogger::flush() is called.
521  * Default is false.
522  */
523  static bool buffered_;
524 
525  static std::string bufferedMsgs_;
526 
527  /*
528  * State attribute. This state happens when an exit level
529  * message is received.
530  * Messages received during this state are not logged.
531  * @see exitLevel_
532  */
533  static bool exitingState_;
534 };
535 
536 #endif // ULOGGER_H
static Level eventLevel_
Definition: ULogger.h:510
static bool buffered_
Definition: ULogger.h:523
static const std::string kDefaultLogFileName
Definition: ULogger.h:205
static void setPrintLevel(bool printLevel)
Definition: ULogger.h:249
static bool isBuffered()
Definition: ULogger.h:288
Base * createInstance(const std::string &derived_class_name, ClassLoader *loader)
virtual void _writeStr(const char *msg)
Definition: ULogger.h:432
static std::string bufferedMsgs_
Definition: ULogger.h:525
#define FINDOBJECT_EXP
Definition: FindObjectExp.h:38
static bool printEndline_
Definition: ULogger.h:461
static bool limitWhereLength_
Definition: ULogger.h:489
static ULogger * instance_
Definition: ULogger.h:438
static ULogger::Level eventLevel()
Definition: ULogger.h:320
static void setLevel(ULogger::Level level)
Definition: ULogger.h:301
static Level level_
Definition: ULogger.h:499
static bool printLevel_
Definition: ULogger.h:455
static bool isPrintLevel()
Definition: ULogger.h:250
static Level exitLevel_
Definition: ULogger.h:505
static bool printWhereFullPath_
Definition: ULogger.h:481
static void setPrintTime(bool printTime)
Definition: ULogger.h:242
static Type type()
Definition: ULogger.h:235
Definition: UMutex.h:54
static void setEventLevel(ULogger::Level eventSentLevel)
Definition: ULogger.h:319
static bool exitingState_
Definition: ULogger.h:533
static void setPrintEndline(bool printEndline)
Definition: ULogger.h:256
static ULogger::Level exitLevel()
Definition: ULogger.h:311
static std::string logFileName_
Definition: ULogger.h:407
static bool printColored_
Definition: ULogger.h:467
static void setPrintWhere(bool printWhere)
Definition: ULogger.h:271
static void setPrintWhereFullPath(bool printWhereFullPath)
Definition: ULogger.h:278
static ULogger::Level level()
Definition: ULogger.h:302
static bool isPrintEndLine()
Definition: ULogger.h:257
static bool isPrintWhereFullPath()
Definition: ULogger.h:279
static bool isPrintWhere()
Definition: ULogger.h:272
static bool append_
Definition: ULogger.h:412
static bool isPrintColored()
Definition: ULogger.h:265
ULogger()
Definition: ULogger.h:382
ROSCPP_DECL std::string append(const std::string &left, const std::string &right)
static bool printWhere_
Definition: ULogger.h:473
static bool printTime_
Definition: ULogger.h:449
static bool isPrintTime()
Definition: ULogger.h:243
static void setExitLevel(ULogger::Level exitLevel)
Definition: ULogger.h:310
static Type type_
Definition: ULogger.h:494
static UDestroyer< ULogger > destroyer_
Definition: ULogger.h:443
static UMutex loggerMutex_
Definition: ULogger.h:517
static void setPrintColored(bool printColored)
Definition: ULogger.h:264
virtual void _write(const char *msg, va_list arg)
Definition: ULogger.h:431


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 19:22:26