-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.c
More file actions
224 lines (189 loc) · 6.67 KB
/
log.c
File metadata and controls
224 lines (189 loc) · 6.67 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/***************************************************************************
* Copyright (C) 2015 by Michael Ambrus *
* ambrmi09@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <strings.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <liblog/log.h>
#include "config.h"
#include "local.h"
#if !defined(WIN32) && !defined(__CYGWIN__) && defined(LIBLOG_ENABLE_SYSLOG)
#include <syslog.h>
#endif
#if defined(WIN32) || defined(__CYGWIN__)
#define SYSTEM_SYSLOG_INABLE
#endif
#define ENV_LOG_LEVEL "LOG_LEVEL"
#define LOG_LINE_MAX_CHARS 2048
/* Global variables that affect assure */
log_level log_ASSURE = LOG_LEVEL_WARNING;
log_level log_ASSERT = LOG_LEVEL_ERROR;
/* Local variables */
static char proc_name[NAME_MAX] = LOG_AS_PROCESS_NAME_DFLT;
#ifdef LIBLOG_ENABLE_SYSLOG
static int syslog_open = 0;
#endif
/*****************************************************************************
log_level binds when global variable initialization is run in .start, i.e.
_before_ CTOR which is initialized in initfini.c
*****************************************************************************/
log_level log_filter_level = DEF_LOG_LEVEL;
int log_getenv_loglevel(void)
{
int log_level = DEF_LOG_LEVEL;
if ((getenv(ENV_LOG_LEVEL) != NULL)
&& (strlen(getenv(ENV_LOG_LEVEL)) > 0)) {
int valid_value;
log_level = str2loglevel(getenv(ENV_LOG_LEVEL), &valid_value);
if (!valid_value) {
log_level = DEF_LOG_LEVEL;
}
}
return log_level;
}
log_level str2loglevel(const char *str, int *ok)
{
log_level level = LOG_LEVEL_ERROR;
int valid = 1;
if ((strnlen(str, NAME_MAX) == 1)
&& (((str[0] - '0') <= LOG_LEVEL_CRITICAL)
&& ((str[0] - '0') >= LOG_LEVEL_VERBOSE))) {
level = atoi(str);
goto done;
}
if (!strcasecmp(str, "critical")) {
level = LOG_LEVEL_CRITICAL;
} else if (!strcasecmp(str, "error")) {
level = LOG_LEVEL_ERROR;
} else if (!strcasecmp(str, "warning")) {
level = LOG_LEVEL_WARNING;
} else if (!strcasecmp(str, "info")) {
level = LOG_LEVEL_INFO;
} else if (!strcasecmp(str, "debug")) {
level = LOG_LEVEL_DEBUG;
} else if (!strcasecmp(str, "verbose")) {
level = LOG_LEVEL_VERBOSE;
} else {
valid = 0;
}
done:
*ok = valid;
return level;
}
void log_set_process_name(const char *name)
{
char *sptr;
sptr = strrchr(name, '/');
if (sptr)
strncpy(proc_name, sptr + 1, NAME_MAX);
else
strncpy(proc_name, name, NAME_MAX);
#ifdef LIBLOG_ENABLE_SYSLOG
log_syslog_config(ENABLE_SYSLOG_STDERR);
#endif
}
void log_write(log_level level, const char *format, ...)
{
/* Only process this message if its level exceeds the current threshold */
if (level >= log_filter_level) {
va_list args;
/* Write the log message */
va_start(args, format);
#if defined(SYSTEM_SYSLOG_INABLE) || ! defined(LIBLOG_ENABLE_SYSLOG)
{
char tstr[LOG_LINE_MAX_CHARS];
char buffer[LOG_LINE_MAX_CHARS];
struct tm tm;
struct timeval now;
gettimeofday(&now, NULL);
strftime(tstr, LOG_LINE_MAX_CHARS, "%y%m%d-%H%M%S",
localtime_r(&(now.tv_sec), &tm));
snprintf(buffer, LOG_LINE_MAX_CHARS, "%s.%06lu %s[%d]: %s", tstr,
now.tv_usec, proc_name, getpid(), format);
vfprintf(stderr, buffer, args);
}
#else
{
int syslog_level;
switch (level) {
case LOG_LEVEL_VERBOSE:
case LOG_LEVEL_DEBUG:
syslog_level = LOG_DEBUG;
break;
case LOG_LEVEL_INFO:
syslog_level = LOG_INFO;
break;
case LOG_LEVEL_WARNING:
syslog_level = LOG_WARNING;
break;
case LOG_LEVEL_ERROR:
syslog_level = LOG_ERR;
break;
case LOG_LEVEL_CRITICAL:
syslog_level = LOG_CRIT;
break;
default:
/* Shouldn't be used, so just route it to a low level */
syslog_level = LOG_DEBUG;
break;
}
vsyslog(syslog_level | LOG_USER, format, args);
}
#endif
va_end(args);
}
}
void log_set_verbosity(log_level level)
{
log_filter_level = level;
}
#ifdef LIBLOG_ENABLE_SYSLOG
void log_syslog_config(int incl_stderr)
{
if (syslog_open) {
closelog();
syslog_open = 0;
}
if (incl_stderr) {
openlog(proc_name,
LOG_CONS | LOG_NDELAY | LOG_NOWAIT | LOG_PERROR | LOG_PID,
LOG_USER);
} else {
openlog(proc_name,
LOG_CONS | LOG_NDELAY | LOG_NOWAIT | LOG_PID, LOG_USER);
}
syslog_open = 1;
}
#endif
/* Test what log-level is effectuated and where it ends up */
void log_test()
{
LOGV("liblog-test [VERBOSE] %d\n", LOG_LEVEL_VERBOSE);
LOGD("liblog-test [DEBUG] %d\n", LOG_LEVEL_DEBUG);
LOGI("liblog-test [INFO] %d\n", LOG_LEVEL_INFO);
LOGW("liblog-test [WARNING] %d\n", LOG_LEVEL_WARNING);
LOGE("liblog-test [ERROR] %d\n", LOG_LEVEL_ERROR);
}