-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLogger.cpp
100 lines (79 loc) · 1.67 KB
/
Logger.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Logger.cpp
*
* Created on: 05/lug/2017
* Author: Stefano Ceccherini
*/
#include "Logger.h"
#include <cstdio>
#include <iostream>
#include <stdarg.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
extern const char* __progname;
Logger::LOGGER_TYPE Logger::sLogType = LOGGER_TYPE_DEFAULT;
int Logger::sLevel;
void
Logger::SetLevel(int level)
{
sLevel = level;
}
/* static */
void
Logger::Log(int level, const char* const string)
{
if (level > sLevel)
return;
_DoLog(level, string);
}
/* static */
void
Logger::LogFormat(int level, const char* fmtString, ...)
{
if (level > sLevel)
return;
char logString[1024];
va_list argp;
::va_start(argp, fmtString);
::vsnprintf(logString, sizeof(logString), fmtString, argp);
::va_end(argp);
_DoLog(level, logString);
}
/* static */
void
Logger::SetLogger(LOGGER_TYPE loggerType)
{
sLogType = loggerType;
}
/* static */
void
Logger::SetLogger(const std::string& loggerType)
{
if (::strcasecmp(loggerType.c_str(), "STDERR") == 0)
SetLogger(Logger::LOGGER_TYPE_STDERR);
else if (::strcasecmp(loggerType.c_str(), "SYSLOG") == 0)
SetLogger(Logger::LOGGER_TYPE_SYSLOG);
else
SetLogger(Logger::LOGGER_TYPE_DEFAULT);
}
/* static */
void
Logger::_DoLog(int level, const char* string)
{
switch (sLogType) {
case LOGGER_TYPE_SYSLOG:
::syslog(level|LOG_PID|LOG_CONS|LOG_USER, "%s", (const char* const)string);
break;
case LOGGER_TYPE_STDERR:
std::cerr << string << std::endl;
break;
case LOGGER_TYPE_DEFAULT:
default:
if (::isatty(STDIN_FILENO))
std::cerr << string << std::endl;
else
::syslog(level|LOG_PID|LOG_CONS|LOG_USER, "%s", (const char* const)string);
break;
}
}