aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/logger.h
blob: 2523efad3234a967645541b76948c4fb1146b155 (plain)
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
#ifndef RSPAMD_LOGGER_H
#define RSPAMD_LOGGER_H

#include "config.h"
#include "cfg_file.h"
#include "radix.h"
#include "util.h"


typedef void (*rspamd_log_func_t) (const gchar *log_domain,
		const gchar *module, const gchar *id, const gchar *function,
		GLogLevelFlags log_level, const gchar *message,
		gboolean forced, gpointer arg);

typedef struct rspamd_logger_s rspamd_logger_t;

/**
 * Init logger
 */
void rspamd_set_logger (struct rspamd_config *cfg,
		GQuark ptype,
		struct rspamd_main *main);

/**
 * Open log file or initialize other structures
 */
gint rspamd_log_open (rspamd_logger_t *logger);

/**
 * Close log file or destroy other structures
 */
void rspamd_log_close (rspamd_logger_t *logger);

/**
 * Close and open log again
 */
gint rspamd_log_reopen (rspamd_logger_t *logger);

/**
 * Open log file or initialize other structures for privileged processes
 */
gint rspamd_log_open_priv (rspamd_logger_t *logger, uid_t uid, gid_t gid);

/**
 * Close log file or destroy other structures for privileged processes
 */
void rspamd_log_close_priv (rspamd_logger_t *logger, uid_t uid, gid_t gid);

/**
 * Close and open log again for privileged processes
 */
gint rspamd_log_reopen_priv (rspamd_logger_t *logger, uid_t uid, gid_t gid);

/**
 * Set log pid
 */
void rspamd_log_update_pid (GQuark ptype, rspamd_logger_t *logger);

/**
 * Flush log buffer for some types of logging
 */
void rspamd_log_flush (rspamd_logger_t *logger);

/**
 * Log function that is compatible for glib messages
 */
void rspamd_glib_log_function (const gchar *log_domain,
		GLogLevelFlags log_level, const gchar *message, gpointer arg);

/**
 * Log function for printing glib assertions
 */
void rspamd_glib_printerr_function (const gchar *message);

/**
 * Function with variable number of arguments support
 */
void rspamd_common_log_function (rspamd_logger_t *logger,
		GLogLevelFlags log_level,
		const gchar *module, const gchar *id,
		const gchar *function, const gchar *fmt, ...);

void rspamd_common_logv (rspamd_logger_t *logger, GLogLevelFlags log_level,
		const gchar *module, const gchar *id, const gchar *function,
		const gchar *fmt, va_list args);

/**
 * Conditional debug function
 */
void rspamd_conditional_debug (rspamd_logger_t *logger,
		rspamd_inet_addr_t *addr, const gchar *module, const gchar *id,
		const gchar *function, const gchar *fmt, ...);

/**
 * Function with variable number of arguments support that uses static default logger
 */
void rspamd_default_log_function (GLogLevelFlags log_level,
		const gchar *module, const gchar *id,
		const gchar *function,
		const gchar *fmt,
		...);

/**
 * Varargs version of default log function
 * @param log_level
 * @param function
 * @param fmt
 * @param args
 */
void rspamd_default_logv (GLogLevelFlags log_level,
		const gchar *module, const gchar *id,
		const gchar *function,
		const gchar *fmt,
		va_list args);

/**
 * Temporary turn on debug
 */
void rspamd_log_debug (rspamd_logger_t *logger);

/**
 * Turn off debug
 */
void rspamd_log_nodebug (rspamd_logger_t *logger);

/**
 * Turn off locking on logger (useful to avoid races)
 */
void rspamd_log_nolock (rspamd_logger_t *logger);

/**
 * Turn on locking to avoid log output mix
 */
void rspamd_log_lock (rspamd_logger_t *logger);

/**
 * Return array of counters (4 numbers):
 * 0 - errors
 * 1 - warnings
 * 2 - info messages
 * 3 - debug messages
 */
const guint64* rspamd_log_counters (rspamd_logger_t *logger);

/* Typical functions */

/* Logging in postfix style */
#define msg_err(...)    rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
        NULL, NULL, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_warn(...)   rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
        NULL, NULL, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_info(...)   rspamd_default_log_function (G_LOG_LEVEL_INFO, \
        NULL, NULL, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_debug(...)  rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \
        NULL, NULL, \
        G_STRFUNC, \
        __VA_ARGS__)
#define debug_task(...) rspamd_conditional_debug (NULL, \
        task->from_addr, \
        task->task_pool->tag.tagname, task->task_pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)

/* Use the following macros if you have `task` in the function */
#define msg_err_task(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
        task->task_pool->tag.tagname, task->task_pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_warn_task(...)   rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
        task->task_pool->tag.tagname, task->task_pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_info_task(...)   rspamd_default_log_function (G_LOG_LEVEL_INFO, \
        task->task_pool->tag.tagname, task->task_pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_debug_task(...)  rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \
        task->task_pool->tag.tagname, task->task_pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)
/* Check for NULL pointer first */
#define msg_err_task_check(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
        task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_warn_task_check(...)   rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
        task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_info_task_check(...)   rspamd_default_log_function (G_LOG_LEVEL_INFO, \
        task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_debug_task_check(...)  rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \
        task ? task->task_pool->tag.tagname : NULL, task ? task->task_pool->tag.uid : NULL, \
        G_STRFUNC, \
        __VA_ARGS__)

/* Use the following macros if you have `pool` in the function */
#define msg_err_pool(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
        pool->tag.tagname, pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_warn_pool(...)   rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
        pool->tag.tagname, pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_info_pool(...)   rspamd_default_log_function (G_LOG_LEVEL_INFO, \
        pool->tag.tagname, pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_debug_pool(...)  rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \
        pool->tag.tagname, pool->tag.uid, \
        G_STRFUNC, \
        __VA_ARGS__)
/* Check for NULL pointer first */
#define msg_err_pool_check(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
        pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_warn_pool_check(...)   rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
		pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_info_pool_check(...)   rspamd_default_log_function (G_LOG_LEVEL_INFO, \
		pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \
        G_STRFUNC, \
        __VA_ARGS__)
#define msg_debug_pool_check(...)  rspamd_default_log_function (G_LOG_LEVEL_DEBUG, \
		pool ? pool->tag.tagname : NULL, pool ? pool->tag.uid : NULL, \
        G_STRFUNC, \
        __VA_ARGS__)
#endif