forked from pgmoneta/pgmoneta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.c
365 lines (321 loc) · 8.81 KB
/
logging.c
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright (C) 2022 Red Hat
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* pgmoneta */
#include <pgmoneta.h>
#include <logging.h>
/* system */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#define LINE_LENGTH 32
FILE* log_file;
static const char* levels[] =
{
"TRACE",
"DEBUG",
"INFO",
"WARN",
"ERROR",
"FATAL"
};
static const char* colors[] =
{
"\x1b[37m",
"\x1b[36m",
"\x1b[32m",
"\x1b[91m",
"\x1b[31m",
"\x1b[35m"
};
int
pgmoneta_init_logging(void)
{
struct configuration* config;
config = (struct configuration*)shmem;
if (config->log_type == PGMONETA_LOGGING_TYPE_FILE)
{
if (strlen(config->log_path) > 0)
{
if (config->log_mode == PGMONETA_LOGGING_MODE_APPEND)
{
log_file = fopen(config->log_path, "a");
}
else
{
log_file = fopen(config->log_path, "w");
}
}
else
{
if (config->log_mode == PGMONETA_LOGGING_MODE_APPEND)
{
log_file = fopen("pgmoneta.log", "a");
}
else
{
log_file = fopen("pgmoneta.log", "w");
}
}
if (!log_file)
{
printf("Failed to open log file %s due to %s\n", strlen(config->log_path) > 0 ? config->log_path : "pgmoneta.log", strerror(errno));
errno = 0;
return 1;
}
}
return 0;
}
/**
*
*/
int
pgmoneta_start_logging(void)
{
struct configuration* config;
config = (struct configuration*)shmem;
if (config->log_type == PGMONETA_LOGGING_TYPE_FILE)
{
if (strlen(config->log_path) > 0)
{
log_file = fopen(config->log_path, "a");
}
else
{
log_file = fopen("pgmoneta.log", "a");
}
if (!log_file)
{
printf("Failed to open log file %s due to %s\n", strlen(config->log_path) > 0 ? config->log_path : "pgmoneta.log", strerror(errno));
errno = 0;
return 1;
}
}
else if (config->log_type == PGMONETA_LOGGING_TYPE_SYSLOG)
{
openlog("pgmoneta", LOG_CONS | LOG_PERROR | LOG_PID, LOG_USER);
}
return 0;
}
/**
*
*/
int
pgmoneta_stop_logging(void)
{
struct configuration* config;
config = (struct configuration*)shmem;
if (config->log_type == PGMONETA_LOGGING_TYPE_FILE)
{
if (log_file != NULL)
{
return fclose(log_file);
}
else
{
return 1;
}
}
else if (config->log_type == PGMONETA_LOGGING_TYPE_SYSLOG)
{
closelog();
}
return 0;
}
void
pgmoneta_log_line(int level, char* file, int line, char* fmt, ...)
{
signed char isfree;
struct configuration* config;
config = (struct configuration*)shmem;
if (config == NULL)
return;
if (level >= config->log_level)
{
retry:
isfree = STATE_FREE;
if (atomic_compare_exchange_strong(&config->log_lock, &isfree, STATE_IN_USE))
{
char buf[256];
va_list vl;
struct tm* tm;
time_t t;
char* filename;
t = time(NULL);
tm = localtime(&t);
filename = strrchr(file, '/');
if (filename != NULL)
{
filename = filename + 1;
}
else
{
filename = file;
}
va_start(vl, fmt);
if (config->log_type == PGMONETA_LOGGING_TYPE_CONSOLE)
{
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm)] = '\0';
fprintf(stdout, "%s %s%-5s\x1b[0m \x1b[90m%s:%d\x1b[0m ",
buf, colors[level - 1], levels[level - 1],
filename, line);
vfprintf(stdout, fmt, vl);
fprintf(stdout, "\n");
fflush(stdout);
}
else if (config->log_type == PGMONETA_LOGGING_TYPE_FILE)
{
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm)] = '\0';
fprintf(log_file, "%s %-5s %s:%d ",
buf, levels[level - 1], filename, line);
vfprintf(log_file, fmt, vl);
fprintf(log_file, "\n");
fflush(log_file);
}
else if (config->log_type == PGMONETA_LOGGING_TYPE_SYSLOG)
{
switch (level)
{
case PGMONETA_LOGGING_LEVEL_DEBUG5:
vsyslog(LOG_DEBUG, fmt, vl);
break;
case PGMONETA_LOGGING_LEVEL_DEBUG1:
vsyslog(LOG_DEBUG, fmt, vl);
break;
case PGMONETA_LOGGING_LEVEL_INFO:
vsyslog(LOG_INFO, fmt, vl);
break;
case PGMONETA_LOGGING_LEVEL_WARN:
vsyslog(LOG_WARNING, fmt, vl);
break;
case PGMONETA_LOGGING_LEVEL_ERROR:
vsyslog(LOG_ERR, fmt, vl);
break;
case PGMONETA_LOGGING_LEVEL_FATAL:
vsyslog(LOG_CRIT, fmt, vl);
break;
default:
vsyslog(LOG_INFO, fmt, vl);
break;
}
}
va_end(vl);
atomic_store(&config->log_lock, STATE_FREE);
}
else
{
/* Sleep for 1ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 1000000L;
nanosleep(&ts, NULL);
goto retry;
}
}
}
void
pgmoneta_log_mem(void* data, size_t size)
{
signed char isfree;
struct configuration* config;
config = (struct configuration*)shmem;
if (config == NULL)
return;
if (config->log_level == PGMONETA_LOGGING_LEVEL_DEBUG5 &&
size > 0 &&
(config->log_type == PGMONETA_LOGGING_TYPE_CONSOLE || config->log_type == PGMONETA_LOGGING_TYPE_FILE))
{
retry:
isfree = STATE_FREE;
if (atomic_compare_exchange_strong(&config->log_lock, &isfree, STATE_IN_USE))
{
char buf[256 * 1024];
int j = 0;
int k = 0;
memset(&buf, 0, sizeof(buf));
for (int i = 0; i < size; i++)
{
if (k == LINE_LENGTH)
{
buf[j] = '\n';
j++;
k = 0;
}
sprintf(&buf[j], "%02X", (signed char) *((char*)data + i));
j += 2;
k++;
}
buf[j] = '\n';
j++;
k = 0;
for (int i = 0; i < size; i++)
{
signed char c = (signed char) *((char*)data + i);
if (k == LINE_LENGTH)
{
buf[j] = '\n';
j++;
k = 0;
}
if (c >= 32 && c <= 127)
{
buf[j] = c;
}
else
{
buf[j] = '?';
}
j++;
k++;
}
if (config->log_type == PGMONETA_LOGGING_TYPE_CONSOLE)
{
fprintf(stdout, "%s", buf);
fprintf(stdout, "\n");
fflush(stdout);
}
else if (config->log_type == PGMONETA_LOGGING_TYPE_FILE)
{
fprintf(log_file, "%s", buf);
fprintf(log_file, "\n");
fflush(log_file);
}
atomic_store(&config->log_lock, STATE_FREE);
}
else
{
/* Sleep for 1ms */
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 1000000L;
nanosleep(&ts, NULL);
goto retry;
}
}
}