-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.cc
116 lines (96 loc) · 2.13 KB
/
log.cc
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright: (C) 2016 ESIMTEK ALL RIGHTS RESERVED
// author: Yonghua Zheng
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "log.h"
#include "syslog.h"
Log* Log::logger = NULL;
void Log::Instantiate(const char* logmeth, int level, const char* prefix)
{
if (!Log::logger) {
if (!logmeth)
logmeth = "syslog";
if (!strncmp(logmeth, "syslog", 6))
Log::logger = new Syslog(level, prefix);
else
Log::logger = new Log(level, prefix);
}
}
Log::Log(int level, const char* prefix)
: prio(level), tag(prefix)
{
}
Log::~Log()
{
}
void Log::SetPriority(int priority)
{
prio = priority;
}
void Log::log(int priority, const char* tag, const char* buffer)
{
fprintf(stdout, "%s: %s\n", tag, buffer);
}
void Log::log(int priority, const char* format, va_list ap)
{
char buffer[256];
if (priority > prio)
return;
vsnprintf(buffer, sizeof(buffer), format, ap);
log(priority, tag, buffer);
}
void Log::log(int priority, const char* format, ...)
{
char buffer[256];
if (priority > prio)
return;
va_list ap;
va_start(ap, format);
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
log(priority, tag, buffer);
}
void Log::Error(const char* format, ...)
{
if (Log::logger) {
va_list ap;
va_start(ap, format);
Log::logger->log(LOG_PRIO_ERROR, format, ap);
va_end(ap);
}
}
void Log::Info(const char* format, ...)
{
if (Log::logger) {
va_list ap;
va_start(ap, format);
Log::logger->log(LOG_PRIO_ERROR, format, ap);
va_end(ap);
}
}
void Log::Debug(const char* format, ...)
{
if (Log::logger) {
va_list ap;
va_start(ap, format);
Log::logger->log(LOG_PRIO_ERROR, format, ap);
va_end(ap);
}
}
void Log::Trace(const char* format, ...)
{
if (Log::logger) {
va_list ap;
va_start(ap, format);
Log::logger->log(LOG_PRIO_ERROR, format, ap);
va_end(ap);
}
}
void Log::Dispose()
{
if (Log::logger) {
delete Log::logger;
Log::logger = NULL;
}
}