-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_cpp.cpp
74 lines (68 loc) · 1.49 KB
/
logger_cpp.cpp
1
2
3
4
5
6
7
8
9
10
11
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <iostream>
#include "logger.hpp"
Logger &Logger::get_instance()
{
static Logger instance(std::cout, LEVEL_DEBUG);
return instance;
}
Logger::Logger(std::ostream &os, const LEVEL level)
: os_(os)
, level_(level)
{
}
static void output_prefix(std::ostream &os, const Logger::LEVEL level)
{
switch (level) {
case Logger::LEVEL_DEBUG:
os << "DEBUG: ";
break;
case Logger::LEVEL_INFO:
os << "INFO: ";
break;
case Logger::LEVEL_WARN:
os << "WARN: ";
break;
case Logger::LEVEL_ERROR:
os << "ERROR: ";
break;
case Logger::LEVEL_FATAL:
os << "FATAL: ";
break;
}
}
void Logger::log(const Logger::LEVEL level, const char *format, ...)
{
va_list args;
va_start(args, format);
vlog(level, format, args);
va_end(args);
}
#define DEFUN_LOGGER(name, level) \
void Logger::name(const char *format, ...) \
{ \
va_list args; \
va_start(args, format); \
vlog(level, format, args); \
va_end(args); \
}
DEFUN_LOGGER(debug, LEVEL_DEBUG);
DEFUN_LOGGER(info, LEVEL_INFO);
DEFUN_LOGGER(warn, LEVEL_WARN);
DEFUN_LOGGER(error, LEVEL_ERROR);
DEFUN_LOGGER(fatal, LEVEL_FATAL);
void Logger::vlog(const Logger::LEVEL level, const char *format, va_list args)
{
if (level >= level_) {
char buf[128];
#ifdef _MSC_VER
vsnprintf_s(buf, sizeof buf, format, args);
#else
vsnprintf(buf, sizeof buf, format, args);
#endif
output_prefix(os_, level);
os_ << buf << std::endl;
}
}