summaryrefslogtreecommitdiffstats
path: root/src/libserver/logger/logger_private.h
blob: 234a207e5c3f4990dcf2e35e2a26b235226c0108 (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
/*
 * Copyright 2023 Vsevolod Stakhov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef RSPAMD_LOGGER_PRIVATE_H
#define RSPAMD_LOGGER_PRIVATE_H

#include "logger.h"

/* How much message should be repeated before it is count to be repeated one */
#define REPEATS_MIN 3
#define REPEATS_MAX 300
#define LOGBUF_LEN 8192

struct rspamd_log_module {
	gchar *mname;
	guint id;
};

struct rspamd_log_modules {
	guchar *bitset;
	guint bitset_len;       /* Number of BITS used in bitset */
	guint bitset_allocated; /* Size of bitset allocated in BYTES */
	GHashTable *modules;
};

struct rspamd_logger_error_elt {
	gint completed;
	GQuark ptype;
	pid_t pid;
	gdouble ts;
	gchar id[RSPAMD_LOG_ID_LEN + 1];
	gchar module[9];
	gchar message[];
};

struct rspamd_logger_error_log {
	struct rspamd_logger_error_elt *elts;
	rspamd_mempool_t *pool;
	guint32 max_elts;
	guint32 elt_len;
	/* Avoid false cache sharing */
	guchar __padding[64 - sizeof(gpointer) * 2 - sizeof(guint64)];
	guint cur_row;
};

/**
 * Static structure that store logging parameters
 * It is NOT shared between processes and is created by main process
 */
struct rspamd_logger_s {
	struct rspamd_logger_funcs ops;
	gint log_level;

	struct rspamd_logger_error_log *errlog;
	struct rspamd_cryptobox_pubkey *pk;
	struct rspamd_cryptobox_keypair *keypair;

	guint flags;
	gboolean closed;
	gboolean enabled;
	gboolean is_debug;
	gboolean no_lock;

	pid_t pid;
	const gchar *process_type;
	struct rspamd_radix_map_helper *debug_ip;
	rspamd_mempool_mutex_t *mtx;
	rspamd_mempool_t *pool;
	guint64 log_cnt[4];
};

/*
 * Common logging prototypes
 */

/*
 * File logging
 */
void *rspamd_log_file_init(rspamd_logger_t *logger, struct rspamd_config *cfg,
						   uid_t uid, gid_t gid, GError **err);
void *rspamd_log_file_reload(rspamd_logger_t *logger, struct rspamd_config *cfg,
							 gpointer arg, uid_t uid, gid_t gid, GError **err);
void rspamd_log_file_dtor(rspamd_logger_t *logger, gpointer arg);
bool rspamd_log_file_log(const gchar *module, const gchar *id,
						 const gchar *function,
						 gint level_flags,
						 const gchar *message,
						 gsize mlen,
						 rspamd_logger_t *rspamd_log,
						 gpointer arg);
bool rspamd_log_file_on_fork(rspamd_logger_t *logger, struct rspamd_config *cfg,
							 gpointer arg, GError **err);

struct rspamd_logger_iov_thrash_stack {
	struct rspamd_logger_iov_thrash_stack *prev;
	char data[0];
};
#define RSPAMD_LOGGER_MAX_IOV 8
struct rspamd_logger_iov_ctx {
	struct iovec iov[RSPAMD_LOGGER_MAX_IOV];
	int niov;
	struct rspamd_logger_iov_thrash_stack *thrash_stack;
};
/**
 * Fills IOV of logger (usable for file/console logging)
 * Warning: this function is NOT reentrant, do not call it twice from a single moment of execution
 * @param iov filled by this function
 * @param module
 * @param id
 * @param function
 * @param level_flags
 * @param message
 * @param mlen
 * @param rspamd_log
 * @return number of iov elements being filled
 */
void rspamd_log_fill_iov(struct rspamd_logger_iov_ctx *iov_ctx,
						 double ts,
						 const gchar *module, const gchar *id,
						 const gchar *function,
						 gint level_flags,
						 const gchar *message,
						 gsize mlen,
						 rspamd_logger_t *rspamd_log);

/**
 * Frees IOV context
 * @param iov_ctx
 */
void rspamd_log_iov_free(struct rspamd_logger_iov_ctx *iov_ctx);
/**
 * Escape log line by replacing unprintable characters to hex escapes like \xNN
 * @param src
 * @param srclen
 * @param dst
 * @param dstlen
 * @return end of the escaped buffer
 */
gchar *rspamd_log_line_hex_escape(const guchar *src, gsize srclen,
								  gchar *dst, gsize dstlen);
/**
 * Returns number of characters to be escaped, e.g. a caller can allocate a new buffer
 * the desired number of characters
 * @param src
 * @param srclen
 * @return number of characters to be escaped
 */
gsize rspamd_log_line_need_escape(const guchar *src, gsize srclen);

static const struct rspamd_logger_funcs file_log_funcs = {
	.init = rspamd_log_file_init,
	.dtor = rspamd_log_file_dtor,
	.reload = rspamd_log_file_reload,
	.log = rspamd_log_file_log,
	.on_fork = rspamd_log_file_on_fork,
};

/*
 * Syslog logging
 */
void *rspamd_log_syslog_init(rspamd_logger_t *logger, struct rspamd_config *cfg,
							 uid_t uid, gid_t gid, GError **err);
void *rspamd_log_syslog_reload(rspamd_logger_t *logger, struct rspamd_config *cfg,
							   gpointer arg, uid_t uid, gid_t gid, GError **err);
void rspamd_log_syslog_dtor(rspamd_logger_t *logger, gpointer arg);
bool rspamd_log_syslog_log(const gchar *module, const gchar *id,
						   const gchar *function,
						   gint level_flags,
						   const gchar *message,
						   gsize mlen,
						   rspamd_logger_t *rspamd_log,
						   gpointer arg);

static const struct rspamd_logger_funcs syslog_log_funcs = {
	.init = rspamd_log_syslog_init,
	.dtor = rspamd_log_syslog_dtor,
	.reload = rspamd_log_syslog_reload,
	.log = rspamd_log_syslog_log,
	.on_fork = NULL,
};

/*
 * Console logging
 */
void *rspamd_log_console_init(rspamd_logger_t *logger, struct rspamd_config *cfg,
							  uid_t uid, gid_t gid, GError **err);
void *rspamd_log_console_reload(rspamd_logger_t *logger, struct rspamd_config *cfg,
								gpointer arg, uid_t uid, gid_t gid, GError **err);
void rspamd_log_console_dtor(rspamd_logger_t *logger, gpointer arg);
bool rspamd_log_console_log(const gchar *module, const gchar *id,
							const gchar *function,
							gint level_flags,
							const gchar *message,
							gsize mlen,
							rspamd_logger_t *rspamd_log,
							gpointer arg);

static const struct rspamd_logger_funcs console_log_funcs = {
	.init = rspamd_log_console_init,
	.dtor = rspamd_log_console_dtor,
	.reload = rspamd_log_console_reload,
	.log = rspamd_log_console_log,
	.on_fork = NULL,
};

#endif