Log.h 1.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#pragma once

#if defined(Q_CC_GNU)
#define PRINTF_FORMAT(IDX, FIRST) __attribute__ ((format (printf, IDX, FIRST)))
#else
#define PRINTF_FORMAT(IDX, FIRST)
#endif

#include <stdarg.h>

gladish's avatar
gladish committed
11
namespace common
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
{
  class Logger
  {
  public:
    enum Level
    {
      DSB_LOGLEVEL_OFF = -1,
      DSB_LOGLEVEL_DEBUG = 1,
      DSB_LOGLEVEL_INFO = 2,
      DSB_LOGLEVEL_WARN = 3,
      DSB_LOGLEVEL_ERROR = 4,
      DSB_LOGLEVEL_FATAL = 5
    };

    static void Write(char const* module, Level level, const char* file,
        int line, const char* format, ...) PRINTF_FORMAT(6, 7);

    static bool IsLevelEnabled(const char* module, Level level);

  private:
    static void VaLog(const char* module, Level level, const char* file, int line, const char* format, va_list args);
  };
}

#define DSB_DECLARE_LOGNAME(LOGNAME) char const __dsb_logger_module_name__[] = { #LOGNAME };


#define DSBLOG_WITH_LEVEL(LEVEL, FORMAT, ...) \
gladish's avatar
gladish committed
40 41
    do { if (common::Logger::IsLevelEnabled(__dsb_logger_module_name__, LEVEL)) { \
        common::Logger::Write(__dsb_logger_module_name__, LEVEL, __FILE__, __LINE__ - 2, FORMAT, ##__VA_ARGS__); \
42 43 44
    } } while (0)

#define DSBLOG(LEVEL, NAME, FORMAT, ...) \
gladish's avatar
gladish committed
45 46
    do { if (common::Logger::IsLevelEnabled(NAME, common::Logger::LEVEL)) { \
        common::Logger::Write(NAME, common::Logger::LEVEL, __FILE__, __LINE__ - 2, FORMAT, ##__VA_ARGS__); \
47 48 49 50 51 52 53 54
    } } while (0)


#define DSBLOG_DEBUG(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_DEBUG, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_INFO(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_INFO, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_WARN(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_WARN, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_ERROR(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_ERROR, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)

55 56
#define DSBLOG_NOT_IMPLEMENTED() DSBLOG_WARN("%s has not been implemented", __func__);