forked from openstreetmap/mod_tile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg_logger.c
142 lines (110 loc) · 3.41 KB
/
g_logger.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
/*
* Copyright (c) 2007 - 2023 by mod_tile contributors (see AUTHORS file)
*
* 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, see http://www.gnu.org/licenses/.
*/
#define _GNU_SOURCE 1
#define G_LOG_USE_STRUCTURED 1
#include <glib.h>
#include <stdio.h>
#include <syslog.h>
extern int foreground;
const char *g_logger_level_name(int log_level)
{
switch (log_level) {
case G_LOG_LEVEL_ERROR:
return "ERROR";
case G_LOG_LEVEL_CRITICAL:
return "CRITICAL";
case G_LOG_LEVEL_WARNING:
return "WARNING";
case G_LOG_LEVEL_MESSAGE:
return "MESSAGE";
case G_LOG_LEVEL_INFO:
return "INFO";
case G_LOG_LEVEL_DEBUG:
return "DEBUG";
default:
return "UNKNOWN";
}
}
void g_logger(int log_level, const char *format, ...)
{
int size;
char *log_message, *log_message_prefixed;
va_list args;
va_start(args, format);
size = vasprintf(&log_message, format, args);
if (size == -1) {
g_error("ERROR: vasprintf failed in g_logger");
}
const GLogField log_fields[] = {{"MESSAGE", log_message, -1}};
size = asprintf(&log_message_prefixed, "%s: %s", g_logger_level_name(log_level), log_message);
if (size == -1) {
g_error("ERROR: asprintf failed in g_logger");
}
const GLogField log_fields_prefixed[] = {{"MESSAGE", log_message_prefixed, -1}};
if (foreground == 1) {
switch (log_level) {
// Levels >= G_LOG_LEVEL_ERROR will terminate the program
case G_LOG_LEVEL_ERROR:
g_log_writer_standard_streams(log_level, log_fields, 1, NULL);
break;
// Levels <= G_LOG_LEVEL_INFO will only show when using G_MESSAGES_DEBUG
case G_LOG_LEVEL_INFO:
g_log_writer_standard_streams(log_level, log_fields, 1, NULL);
break;
default:
g_log_writer_default(log_level, log_fields, 1, NULL);
}
} else if (g_log_writer_is_journald(fileno(stderr))) {
switch (log_level) {
// Levels >= G_LOG_LEVEL_ERROR will terminate the program
case G_LOG_LEVEL_ERROR:
g_log_writer_journald(log_level, log_fields, 1, NULL);
break;
// Levels <= G_LOG_LEVEL_INFO will only show when using G_MESSAGES_DEBUG
case G_LOG_LEVEL_INFO:
g_log_writer_journald(log_level, log_fields, 1, NULL);
break;
default:
g_log_writer_default(log_level, log_fields, 1, NULL);
}
} else {
setlogmask(LOG_UPTO(LOG_INFO));
switch (log_level) {
case G_LOG_LEVEL_ERROR:
syslog(LOG_ERR, log_message_prefixed, NULL);
break;
case G_LOG_LEVEL_CRITICAL:
syslog(LOG_CRIT, log_message_prefixed, NULL);
break;
case G_LOG_LEVEL_WARNING:
syslog(LOG_WARNING, log_message_prefixed, NULL);
break;
case G_LOG_LEVEL_MESSAGE:
syslog(LOG_INFO, log_message_prefixed, NULL);
break;
case G_LOG_LEVEL_INFO:
syslog(LOG_INFO, log_message_prefixed, NULL);
break;
case G_LOG_LEVEL_DEBUG:
syslog(LOG_DEBUG, log_message_prefixed, NULL);
break;
}
}
va_end(args);
free(log_message_prefixed);
free(log_message);
}