aboutsummaryrefslogtreecommitdiffstats
path: root/src/libserver/logger/logger.c
blob: 58cb31969da1f22c15fdf10cb5c58ff12f4eccee (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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
/*
 * 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.
 */
#include "config.h"
#include "logger.h"
#include "rspamd.h"
#include "libserver/maps/map.h"
#include "libserver/maps/map_helpers.h"
#include "ottery.h"
#include "unix-std.h"
#include "logger_private.h"


static rspamd_logger_t *default_logger = NULL;
static rspamd_logger_t *emergency_logger = NULL;
static struct rspamd_log_modules *log_modules = NULL;

static const gchar lf_chr = '\n';

guint rspamd_task_log_id = (guint) -1;
RSPAMD_CONSTRUCTOR(rspamd_task_log_init)
{
	rspamd_task_log_id = rspamd_logger_add_debug_module("task");
}

rspamd_logger_t *
rspamd_log_default_logger(void)
{
	return default_logger;
}

rspamd_logger_t *
rspamd_log_emergency_logger(void)
{
	return emergency_logger;
}

void rspamd_log_set_log_level(rspamd_logger_t *logger, gint level)
{
	if (logger == NULL) {
		logger = default_logger;
	}

	logger->log_level = level;
}

gint rspamd_log_get_log_level(rspamd_logger_t *logger)
{
	if (logger == NULL) {
		logger = default_logger;
	}

	return logger->log_level;
}

void rspamd_log_set_log_flags(rspamd_logger_t *logger, gint flags)
{
	g_assert(logger != NULL);

	logger->flags = flags;
}

void rspamd_log_close(rspamd_logger_t *logger)
{
	g_assert(logger != NULL);

	if (logger->closed) {
		return;
	}

	logger->closed = TRUE;

	if (logger->debug_ip) {
		rspamd_map_helper_destroy_radix(logger->debug_ip);
	}

	if (logger->pk) {
		rspamd_pubkey_unref(logger->pk);
	}

	if (logger->keypair) {
		rspamd_keypair_unref(logger->keypair);
	}

	logger->ops.dtor(logger, logger->ops.specific);

	/* TODO: Do we really need that ? */
	if (logger == default_logger) {
		default_logger = NULL;
	}

	if (logger == emergency_logger) {
		emergency_logger = NULL;
	}

	if (!logger->pool) {
		g_free(logger);
	}
}

bool rspamd_log_reopen(rspamd_logger_t *rspamd_log, struct rspamd_config *cfg,
					   uid_t uid, gid_t gid)
{
	void *nspec;
	GError *err = NULL;

	g_assert(rspamd_log != NULL);

	nspec = rspamd_log->ops.reload(rspamd_log, cfg, rspamd_log->ops.specific,
								   uid, gid, &err);

	if (nspec != NULL) {
		rspamd_log->ops.specific = nspec;
	}
	else {
	}

	return nspec != NULL;
}

static void
rspamd_emergency_logger_dtor(gpointer d)
{
	rspamd_logger_t *logger = (rspamd_logger_t *) d;

	rspamd_log_close(logger);
}

rspamd_logger_t *
rspamd_log_open_emergency(rspamd_mempool_t *pool, gint flags)
{
	rspamd_logger_t *logger;
	GError *err = NULL;

	g_assert(default_logger == NULL);
	g_assert(emergency_logger == NULL);

	if (pool) {
		logger = rspamd_mempool_alloc0(pool, sizeof(rspamd_logger_t));
		logger->mtx = rspamd_mempool_get_mutex(pool);
	}
	else {
		logger = g_malloc0(sizeof(rspamd_logger_t));
	}

	logger->flags = flags;
	logger->pool = pool;
	logger->process_type = "main";
	logger->pid = getpid();

	const struct rspamd_logger_funcs *funcs = &console_log_funcs;
	memcpy(&logger->ops, funcs, sizeof(*funcs));

	logger->ops.specific = logger->ops.init(logger, NULL, -1, -1, &err);

	if (logger->ops.specific == NULL) {
		rspamd_fprintf(stderr, "fatal error: cannot init console logging: %e\n",
					   err);
		g_error_free(err);

		exit(EXIT_FAILURE);
	}

	default_logger = logger;
	emergency_logger = logger;

	rspamd_mempool_add_destructor(pool, rspamd_emergency_logger_dtor,
								  emergency_logger);

	return logger;
}

rspamd_logger_t *
rspamd_log_open_specific(rspamd_mempool_t *pool,
						 struct rspamd_config *cfg,
						 const gchar *ptype,
						 uid_t uid, gid_t gid)
{
	rspamd_logger_t *logger;
	GError *err = NULL;

	if (pool) {
		logger = rspamd_mempool_alloc0(pool, sizeof(rspamd_logger_t));
		logger->mtx = rspamd_mempool_get_mutex(pool);
	}
	else {
		logger = g_malloc0(sizeof(rspamd_logger_t));
	}

	logger->pool = pool;

	if (cfg) {
		if (cfg->log_error_elts > 0 && pool) {
			logger->errlog = rspamd_mempool_alloc0_shared(pool,
														  sizeof(*logger->errlog));
			logger->errlog->pool = pool;
			logger->errlog->max_elts = cfg->log_error_elts;
			logger->errlog->elt_len = cfg->log_error_elt_maxlen;
			logger->errlog->elts = rspamd_mempool_alloc0_shared(pool,
																sizeof(struct rspamd_logger_error_elt) * cfg->log_error_elts +
																	cfg->log_error_elt_maxlen * cfg->log_error_elts);
		}

		logger->log_level = cfg->log_level;
		logger->flags = cfg->log_flags;

		if (!(logger->flags & RSPAMD_LOG_FLAG_ENFORCED)) {
			logger->log_level = cfg->log_level;
		}
	}

	const struct rspamd_logger_funcs *funcs = NULL;

	if (cfg) {
		switch (cfg->log_type) {
		case RSPAMD_LOG_CONSOLE:
			funcs = &console_log_funcs;
			break;
		case RSPAMD_LOG_SYSLOG:
			funcs = &syslog_log_funcs;
			break;
		case RSPAMD_LOG_FILE:
			funcs = &file_log_funcs;
			break;
		}
	}
	else {
		funcs = &console_log_funcs;
	}

	g_assert(funcs != NULL);
	memcpy(&logger->ops, funcs, sizeof(*funcs));

	logger->ops.specific = logger->ops.init(logger, cfg, uid, gid, &err);

	if (emergency_logger && logger->ops.specific == NULL) {
		rspamd_common_log_function(emergency_logger, G_LOG_LEVEL_CRITICAL,
								   "logger", NULL, G_STRFUNC,
								   "cannot open specific logger: %e", err);
		g_error_free(err);

		return NULL;
	}

	logger->pid = getpid();
	logger->process_type = ptype;
	logger->enabled = TRUE;

	/* Set up conditional logging */
	if (cfg) {
		if (cfg->debug_ip_map != NULL) {
			/* Try to add it as map first of all */
			if (logger->debug_ip) {
				rspamd_map_helper_destroy_radix(logger->debug_ip);
			}

			logger->debug_ip = NULL;
			rspamd_config_radix_from_ucl(cfg,
										 cfg->debug_ip_map,
										 "IP addresses for which debug logs are enabled",
										 &logger->debug_ip,
										 NULL,
										 NULL, "debug ip");
		}

		if (cfg->log_encryption_key) {
			logger->pk = rspamd_pubkey_ref(cfg->log_encryption_key);
			logger->keypair = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX,
												 RSPAMD_CRYPTOBOX_MODE_25519);
			rspamd_pubkey_calculate_nm(logger->pk, logger->keypair);
		}
	}

	default_logger = logger;

	return logger;
}


/**
 * Used after fork() for updating structure params
 */
void rspamd_log_on_fork(GQuark ptype, struct rspamd_config *cfg,
						rspamd_logger_t *logger)
{
	logger->pid = getpid();
	logger->process_type = g_quark_to_string(ptype);

	if (logger->ops.on_fork) {
		GError *err = NULL;

		bool ret = logger->ops.on_fork(logger, cfg, logger->ops.specific, &err);

		if (!ret && emergency_logger) {
			rspamd_common_log_function(emergency_logger, G_LOG_LEVEL_CRITICAL,
									   "logger", NULL, G_STRFUNC,
									   "cannot update logging on fork: %e", err);
			g_error_free(err);
		}
	}
}

inline gboolean
rspamd_logger_need_log(rspamd_logger_t *rspamd_log, GLogLevelFlags log_level,
					   gint module_id)
{
	g_assert(rspamd_log != NULL);

	if ((log_level & RSPAMD_LOG_FORCED) ||
		(log_level & (RSPAMD_LOG_LEVEL_MASK & G_LOG_LEVEL_MASK)) <= rspamd_log->log_level) {
		return TRUE;
	}

	if (module_id != -1 && isset(log_modules->bitset, module_id)) {
		return TRUE;
	}

	return FALSE;
}

static gchar *
rspamd_log_encrypt_message(const gchar *begin, const gchar *end, gsize *enc_len,
						   rspamd_logger_t *rspamd_log)
{
	guchar *out;
	gchar *b64;
	guchar *p, *nonce, *mac;
	const guchar *comp;
	guint len, inlen;

	g_assert(end > begin);
	/* base64 (pubkey | nonce | message) */
	inlen = rspamd_cryptobox_nonce_bytes(RSPAMD_CRYPTOBOX_MODE_25519) +
			rspamd_cryptobox_pk_bytes(RSPAMD_CRYPTOBOX_MODE_25519) +
			rspamd_cryptobox_mac_bytes(RSPAMD_CRYPTOBOX_MODE_25519) +
			(end - begin);
	out = g_malloc(inlen);

	p = out;
	comp = rspamd_pubkey_get_pk(rspamd_log->pk, &len);
	memcpy(p, comp, len);
	p += len;
	ottery_rand_bytes(p, rspamd_cryptobox_nonce_bytes(RSPAMD_CRYPTOBOX_MODE_25519));
	nonce = p;
	p += rspamd_cryptobox_nonce_bytes(RSPAMD_CRYPTOBOX_MODE_25519);
	mac = p;
	p += rspamd_cryptobox_mac_bytes(RSPAMD_CRYPTOBOX_MODE_25519);
	memcpy(p, begin, end - begin);
	comp = rspamd_pubkey_get_nm(rspamd_log->pk, rspamd_log->keypair);
	g_assert(comp != NULL);
	rspamd_cryptobox_encrypt_nm_inplace(p, end - begin, nonce, comp, mac,
										RSPAMD_CRYPTOBOX_MODE_25519);
	b64 = rspamd_encode_base64(out, inlen, 0, enc_len);
	g_free(out);

	return b64;
}

static void
rspamd_log_write_ringbuffer(rspamd_logger_t *rspamd_log,
							const gchar *module, const gchar *id,
							const gchar *data, glong len)
{
	guint32 row_num;
	struct rspamd_logger_error_log *elog;
	struct rspamd_logger_error_elt *elt;

	if (!rspamd_log->errlog) {
		return;
	}

	elog = rspamd_log->errlog;

	g_atomic_int_compare_and_exchange(&elog->cur_row, elog->max_elts, 0);
#if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30))
	row_num = g_atomic_int_add(&elog->cur_row, 1);
#else
	row_num = g_atomic_int_exchange_and_add(&elog->cur_row, 1);
#endif

	if (row_num < elog->max_elts) {
		elt = (struct rspamd_logger_error_elt *) (((guchar *) elog->elts) +
												  (sizeof(*elt) + elog->elt_len) * row_num);
		g_atomic_int_set(&elt->completed, 0);
	}
	else {
		/* Race condition */
		elog->cur_row = 0;
		return;
	}

	elt->pid = rspamd_log->pid;
	elt->ptype = g_quark_from_string(rspamd_log->process_type);
	elt->ts = rspamd_get_calendar_ticks();

	if (id) {
		rspamd_strlcpy(elt->id, id, sizeof(elt->id));
	}
	else {
		rspamd_strlcpy(elt->id, "", sizeof(elt->id));
	}

	if (module) {
		rspamd_strlcpy(elt->module, module, sizeof(elt->module));
	}
	else {
		rspamd_strlcpy(elt->module, "", sizeof(elt->module));
	}

	rspamd_strlcpy(elt->message, data, MIN(len + 1, elog->elt_len));
	g_atomic_int_set(&elt->completed, 1);
}

bool rspamd_common_logv(rspamd_logger_t *rspamd_log, gint level_flags,
						const gchar *module, const gchar *id, const gchar *function,
						const gchar *fmt, va_list args)
{
	gchar *end;
	gint level = level_flags & (RSPAMD_LOG_LEVEL_MASK & G_LOG_LEVEL_MASK), mod_id;
	bool ret = false;
	gchar logbuf[RSPAMD_LOGBUF_SIZE], *log_line;
	gsize nescaped;

	if (G_UNLIKELY(rspamd_log == NULL)) {
		rspamd_log = default_logger;
	}

	log_line = logbuf;

	if (G_UNLIKELY(rspamd_log == NULL)) {
		/* Just fprintf message to stderr */
		if (level >= G_LOG_LEVEL_INFO) {
			end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, args);
			rspamd_fprintf(stderr, "%*s\n", (gint) (end - log_line),
						   log_line);
		}
	}
	else {
		if (level == G_LOG_LEVEL_DEBUG) {
			mod_id = rspamd_logger_add_debug_module(module);
		}
		else {
			mod_id = -1;
		}

		if (rspamd_logger_need_log(rspamd_log, level_flags, mod_id)) {
			end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, args);

			if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_RSPAMADM)) {
				if ((nescaped = rspamd_log_line_need_escape(logbuf, end - logbuf)) != 0) {
					gsize unescaped_len = end - logbuf;
					gchar *logbuf_escaped = g_alloca(unescaped_len + nescaped * 4);
					log_line = logbuf_escaped;

					end = rspamd_log_line_hex_escape(logbuf, unescaped_len,
													 logbuf_escaped, unescaped_len + nescaped * 4);
				}
			}

			if ((level_flags & RSPAMD_LOG_ENCRYPTED) && rspamd_log->pk) {
				gchar *encrypted;
				gsize enc_len;

				encrypted = rspamd_log_encrypt_message(log_line, end, &enc_len,
													   rspamd_log);
				ret = rspamd_log->ops.log(module, id,
										  function,
										  level_flags,
										  encrypted,
										  enc_len,
										  rspamd_log,
										  rspamd_log->ops.specific);
				g_free(encrypted);
			}
			else {
				ret = rspamd_log->ops.log(module, id,
										  function,
										  level_flags,
										  log_line,
										  end - log_line,
										  rspamd_log,
										  rspamd_log->ops.specific);
			}

			switch (level) {
			case G_LOG_LEVEL_CRITICAL:
				rspamd_log->log_cnt[0]++;
				rspamd_log_write_ringbuffer(rspamd_log, module, id, log_line,
											end - log_line);
				break;
			case G_LOG_LEVEL_WARNING:
				rspamd_log->log_cnt[1]++;
				break;
			case G_LOG_LEVEL_INFO:
				rspamd_log->log_cnt[2]++;
				break;
			case G_LOG_LEVEL_DEBUG:
				rspamd_log->log_cnt[3]++;
				break;
			default:
				break;
			}
		}
	}

	return ret;
}

/**
 * This log functions select real logger and write message if level is less or equal to configured log level
 */
bool rspamd_common_log_function(rspamd_logger_t *rspamd_log,
								gint level_flags,
								const gchar *module, const gchar *id,
								const gchar *function,
								const gchar *fmt,
								...)
{
	va_list vp;

	va_start(vp, fmt);
	bool ret = rspamd_common_logv(rspamd_log, level_flags, module, id, function, fmt, vp);
	va_end(vp);

	return ret;
}

bool rspamd_default_logv(gint level_flags, const gchar *module, const gchar *id,
						 const gchar *function,
						 const gchar *fmt, va_list args)
{
	return rspamd_common_logv(NULL, level_flags, module, id, function, fmt, args);
}

bool rspamd_default_log_function(gint level_flags,
								 const gchar *module, const gchar *id,
								 const gchar *function, const gchar *fmt, ...)
{

	va_list vp;

	va_start(vp, fmt);
	bool ret = rspamd_default_logv(level_flags, module, id, function, fmt, vp);
	va_end(vp);

	return ret;
}


/**
 * Main file interface for logging
 */
/**
 * Write log line depending on ip
 */
bool rspamd_conditional_debug(rspamd_logger_t *rspamd_log,
							  rspamd_inet_addr_t *addr, const gchar *module, const gchar *id,
							  const gchar *function, const gchar *fmt, ...)
{
	static gchar logbuf[LOGBUF_LEN];
	va_list vp;
	gchar *end;
	gint mod_id;

	if (rspamd_log == NULL) {
		rspamd_log = default_logger;
	}

	mod_id = rspamd_logger_add_debug_module(module);

	if (rspamd_logger_need_log(rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) ||
		rspamd_log->is_debug) {
		if (rspamd_log->debug_ip && addr != NULL) {
			if (rspamd_match_radix_map_addr(rspamd_log->debug_ip,
											addr) == NULL) {
				return false;
			}
		}

		va_start(vp, fmt);
		end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, vp);
		*end = '\0';
		va_end(vp);
		return rspamd_log->ops.log(module, id,
								   function,
								   G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED,
								   logbuf,
								   end - logbuf,
								   rspamd_log,
								   rspamd_log->ops.specific);
	}

	return false;
}

bool rspamd_conditional_debug_fast(rspamd_logger_t *rspamd_log,
								   rspamd_inet_addr_t *addr,
								   gint mod_id, const gchar *module, const gchar *id,
								   const gchar *function, const gchar *fmt, ...)
{
	static gchar logbuf[LOGBUF_LEN];
	va_list vp;
	gchar *end;

	if (rspamd_log == NULL) {
		rspamd_log = default_logger;
	}

	if (rspamd_logger_need_log(rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) ||
		rspamd_log->is_debug) {
		if (rspamd_log->debug_ip && addr != NULL) {
			if (rspamd_match_radix_map_addr(rspamd_log->debug_ip, addr) == NULL) {
				return false;
			}
		}

		va_start(vp, fmt);
		end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, vp);
		*end = '\0';
		va_end(vp);
		return rspamd_log->ops.log(module, id,
								   function,
								   G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED,
								   logbuf,
								   end - logbuf,
								   rspamd_log,
								   rspamd_log->ops.specific);
	}

	return false;
}

bool rspamd_conditional_debug_fast_num_id(rspamd_logger_t *rspamd_log,
										  rspamd_inet_addr_t *addr,
										  gint mod_id, const gchar *module, guint64 id,
										  const gchar *function, const gchar *fmt, ...)
{
	static gchar logbuf[LOGBUF_LEN], idbuf[64];
	va_list vp;
	gchar *end;

	if (rspamd_log == NULL) {
		rspamd_log = default_logger;
	}

	if (rspamd_logger_need_log(rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) ||
		rspamd_log->is_debug) {
		if (rspamd_log->debug_ip && addr != NULL) {
			if (rspamd_match_radix_map_addr(rspamd_log->debug_ip, addr) == NULL) {
				return false;
			}
		}

		rspamd_snprintf(idbuf, sizeof(idbuf), "%XuL", id);
		va_start(vp, fmt);
		end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, vp);
		*end = '\0';
		va_end(vp);
		return rspamd_log->ops.log(module, idbuf,
								   function,
								   G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED,
								   logbuf,
								   end - logbuf,
								   rspamd_log,
								   rspamd_log->ops.specific);
	}

	return false;
}

/**
 * Wrapper for glib logger
 */
void rspamd_glib_log_function(const gchar *log_domain,
							  GLogLevelFlags log_level,
							  const gchar *message,
							  gpointer arg)
{
	rspamd_logger_t *rspamd_log = (rspamd_logger_t *) arg;

	if (rspamd_log->enabled &&
		rspamd_logger_need_log(rspamd_log, log_level, -1)) {
		rspamd_log->ops.log("glib", NULL,
							NULL,
							log_level,
							message,
							strlen(message),
							rspamd_log,
							rspamd_log->ops.specific);
	}
}

void rspamd_glib_printerr_function(const gchar *message)
{
	rspamd_common_log_function(NULL, G_LOG_LEVEL_CRITICAL, "glib",
							   NULL, G_STRFUNC,
							   "%s", message);
}

/**
 * Temporary turn on debugging
 */
void rspamd_log_debug(rspamd_logger_t *rspamd_log)
{
	rspamd_log->is_debug = TRUE;
}

/**
 * Turn off temporary debugging
 */
void rspamd_log_nodebug(rspamd_logger_t *rspamd_log)
{
	rspamd_log->is_debug = FALSE;
}

const guint64 *
rspamd_log_counters(rspamd_logger_t *logger)
{
	if (logger) {
		return logger->log_cnt;
	}

	return NULL;
}

static gint
rspamd_log_errlog_cmp(const ucl_object_t **o1, const ucl_object_t **o2)
{
	const ucl_object_t *ts1, *ts2;

	ts1 = ucl_object_lookup(*o1, "ts");
	ts2 = ucl_object_lookup(*o2, "ts");

	if (ts1 && ts2) {
		gdouble t1 = ucl_object_todouble(ts1), t2 = ucl_object_todouble(ts2);

		if (t1 > t2) {
			return -1;
		}
		else if (t2 > t1) {
			return 1;
		}
	}

	return 0;
}

ucl_object_t *
rspamd_log_errorbuf_export(const rspamd_logger_t *logger)
{
	struct rspamd_logger_error_elt *cpy, *cur;
	ucl_object_t *top = ucl_object_typed_new(UCL_ARRAY);
	guint i;

	if (logger->errlog == NULL) {
		return top;
	}

	cpy = g_malloc0_n(logger->errlog->max_elts,
					  sizeof(*cpy) + logger->errlog->elt_len);
	memcpy(cpy, logger->errlog->elts, logger->errlog->max_elts * (sizeof(*cpy) + logger->errlog->elt_len));

	for (i = 0; i < logger->errlog->max_elts; i++) {
		cur = (struct rspamd_logger_error_elt *) ((guchar *) cpy +
												  i * ((sizeof(*cpy) + logger->errlog->elt_len)));
		if (cur->completed) {
			ucl_object_t *obj = ucl_object_typed_new(UCL_OBJECT);

			ucl_object_insert_key(obj, ucl_object_fromdouble(cur->ts),
								  "ts", 0, false);
			ucl_object_insert_key(obj, ucl_object_fromint(cur->pid),
								  "pid", 0, false);
			ucl_object_insert_key(obj,
								  ucl_object_fromstring(g_quark_to_string(cur->ptype)),
								  "type", 0, false);
			ucl_object_insert_key(obj, ucl_object_fromstring(cur->id),
								  "id", 0, false);
			ucl_object_insert_key(obj, ucl_object_fromstring(cur->module),
								  "module", 0, false);
			ucl_object_insert_key(obj, ucl_object_fromstring(cur->message),
								  "message", 0, false);

			ucl_array_append(top, obj);
		}
	}

	ucl_object_array_sort(top, rspamd_log_errlog_cmp);
	g_free(cpy);

	return top;
}

static guint
rspamd_logger_allocate_mod_bit(void)
{
	if (log_modules->bitset_allocated * NBBY > log_modules->bitset_len + 1) {
		log_modules->bitset_len++;
		return log_modules->bitset_len - 1;
	}
	else {
		/* Need to expand */
		log_modules->bitset_allocated *= 2;
		log_modules->bitset = g_realloc(log_modules->bitset,
										log_modules->bitset_allocated);

		return rspamd_logger_allocate_mod_bit();
	}
}

RSPAMD_DESTRUCTOR(rspamd_debug_modules_dtor)
{
	if (log_modules) {
		g_hash_table_unref(log_modules->modules);
		g_free(log_modules->bitset);
		g_free(log_modules);
	}
}

gint rspamd_logger_add_debug_module(const gchar *mname)
{
	struct rspamd_log_module *m;

	if (mname == NULL) {
		return -1;
	}

	if (log_modules == NULL) {
		/*
		 * This is usually called from constructors, so we call init check
		 * each time to avoid dependency issues between ctors calls
		 */
		log_modules = g_malloc0(sizeof(*log_modules));
		log_modules->modules = g_hash_table_new_full(rspamd_strcase_hash,
													 rspamd_strcase_equal, g_free, g_free);
		log_modules->bitset_allocated = 16;
		log_modules->bitset_len = 0;
		log_modules->bitset = g_malloc0(log_modules->bitset_allocated);
	}

	if ((m = g_hash_table_lookup(log_modules->modules, mname)) == NULL) {
		m = g_malloc0(sizeof(*m));
		m->mname = g_strdup(mname);
		m->id = rspamd_logger_allocate_mod_bit();
		clrbit(log_modules->bitset, m->id);
		g_hash_table_insert(log_modules->modules, m->mname, m);
	}

	return m->id;
}

void rspamd_logger_configure_modules(GHashTable *mods_enabled)
{
	GHashTableIter it;
	gpointer k, v;
	guint id;

	/* Clear all in bitset_allocated -> this are bytes not bits */
	memset(log_modules->bitset, 0, log_modules->bitset_allocated);
	/* On first iteration, we go through all modules enabled and add missing ones */
	g_hash_table_iter_init(&it, mods_enabled);

	while (g_hash_table_iter_next(&it, &k, &v)) {
		rspamd_logger_add_debug_module((const gchar *) k);
	}

	g_hash_table_iter_init(&it, mods_enabled);

	while (g_hash_table_iter_next(&it, &k, &v)) {
		id = rspamd_logger_add_debug_module((const gchar *) k);

		if (isclr(log_modules->bitset, id)) {
			msg_info("enable debugging for module %s (%d)", (const gchar *) k,
					 id);
			setbit(log_modules->bitset, id);
		}
	}
}

struct rspamd_logger_funcs *
rspamd_logger_set_log_function(rspamd_logger_t *logger,
							   struct rspamd_logger_funcs *nfuncs)
{
	/* TODO: write this */

	return NULL;
}


gchar *
rspamd_log_line_hex_escape(const guchar *src, gsize srclen,
						   gchar *dst, gsize dstlen)
{
	static const gchar hexdigests[16] = "0123456789ABCDEF";
	gchar *d = dst;

	static guint32 escape[] = {
		0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */

		/* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
		0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0100 */

		/* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */
		0x00000000, /* 0001 0000 0000 0000  0000 0000 0000 0000 */

		/*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
		0x80000000, /* 1000 0000 0000 0000  0000 0000 0000 0000 */

		/* Allow all 8bit characters (assuming they are valid utf8) */
		0x00000000,
		0x00000000,
		0x00000000,
		0x00000000,
	};

	while (srclen && dstlen) {
		if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
			if (dstlen >= 4) {
				*d++ = '\\';
				*d++ = 'x';
				*d++ = hexdigests[*src >> 4];
				*d++ = hexdigests[*src & 0xf];
				src++;
				dstlen -= 4;
			}
			else {
				/* Overflow */
				break;
			}
		}
		else {
			*d++ = *src++;
			dstlen--;
		}

		srclen--;
	}

	return d;
}

gsize rspamd_log_line_need_escape(const guchar *src, gsize srclen)
{
	static guint32 escape[] = {
		0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */

		/* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
		0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0100 */

		/* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */
		0x00000000, /* 0001 0000 0000 0000  0000 0000 0000 0000 */

		/*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
		0x80000000, /* 1000 0000 0000 0000  0000 0000 0000 0000 */

		/* Allow all 8bit characters (assuming they are valid utf8) */
		0x00000000,
		0x00000000,
		0x00000000,
		0x00000000,
	};
	gsize n = 0;

	while (srclen) {
		if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
			n++;
		}

		src++;
		srclen--;
	}

	return n;
}

const gchar *
rspamd_get_log_severity_string(gint level_flags)
{
	unsigned int bitnum;
	static const char *level_strs[G_LOG_LEVEL_USER_SHIFT] = {
		"", /* G_LOG_FLAG_RECURSION */
		"", /* G_LOG_FLAG_FATAL */
		"crit",
		"error",
		"warn",
		"notice",
		"info",
		"debug"};
	level_flags &= ((1u << G_LOG_LEVEL_USER_SHIFT) - 1u) & ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL);
#ifdef __GNUC__
	/* We assume gcc >= 3 and clang >= 5 anyway */
	bitnum = __builtin_ffs(level_flags) - 1;
#else
	bitnum = ffs(level_flags) - 1;
#endif
	return level_strs[bitnum];
}

static inline void
log_time(gdouble now, rspamd_logger_t *rspamd_log, gchar *timebuf,
		 size_t len)
{
	time_t sec = (time_t) now;
	gsize r;
	struct tm tms;

	rspamd_localtime(sec, &tms);
	r = strftime(timebuf, len, "%F %H:%M:%S", &tms);

	if (rspamd_log->flags & RSPAMD_LOG_FLAG_USEC) {
		gchar usec_buf[16];

		rspamd_snprintf(usec_buf, sizeof(usec_buf), "%.5f",
						now - (gdouble) sec);
		rspamd_snprintf(timebuf + r, len - r,
						"%s", usec_buf + 1);
	}
}

gsize rspamd_log_fill_iov(struct iovec *iov,
						  double ts,
						  const gchar *module,
						  const gchar *id,
						  const gchar *function,
						  gint level_flags,
						  const gchar *message,
						  gsize mlen,
						  rspamd_logger_t *logger)
{
	bool log_color = (logger->flags & RSPAMD_LOG_FLAG_COLOR);
	bool log_severity = (logger->flags & RSPAMD_LOG_FLAG_SEVERITY);
	bool log_rspamadm = (logger->flags & RSPAMD_LOG_FLAG_RSPAMADM);
	bool log_systemd = (logger->flags & RSPAMD_LOG_FLAG_SYSTEMD);
	bool log_json = (logger->flags & RSPAMD_LOG_FLAG_JSON);

	if (log_json) {
		/* Some sanity to avoid too many branches */
		log_color = false;
		log_severity = true;
		log_systemd = false;
	}

	gint r;

	if (iov == NULL) {
		/* This is the case when we just return a number of IOV required for the logging */
		if (log_json) {
			return 3; /* Preamble, message, "}\n" */
		}
		else {
			if (log_rspamadm) {
				if (logger->log_level == G_LOG_LEVEL_DEBUG) {
					return 4;
				}
				else {
					return 3; /* No time component */
				}
			}
			else if (log_systemd) {
				return 3;
			}
			else {
				if (log_color) {
					return 5;
				}
				else {
					return 4;
				}
			}
		}
	}
	else {
		static gchar timebuf[64], modulebuf[64];
		static gchar tmpbuf[256];

		if (!log_json && !log_systemd) {
			log_time(ts, logger, timebuf, sizeof(timebuf));
		}

		if (G_UNLIKELY(log_json)) {
			/* Perform JSON logging */
			r = rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "{\"ts\": %L, "
														"\"pid\": %P, "
														"\"severity\": \"%s\", "
														"\"worker_type\": \"%s\", "
														"\"id\": \"%s\", "
														"\"module\": \"%s\", "
														"\"function\": \"%s\", "
														"\"message\": \"",
								(gint64) (ts * 1e6),
								logger->pid,
								rspamd_get_log_severity_string(level_flags),
								logger->process_type,
								id,
								module,
								function);
			iov[0].iov_base = tmpbuf;
			iov[0].iov_len = r;
			iov[1].iov_base = (void *) message;
			iov[1].iov_len = mlen;
			iov[2].iov_base = (void *) "\"}\n";
			iov[2].iov_len = sizeof("\"}\n") - 1;
		}
		else if (G_LIKELY(!log_rspamadm)) {
			if (!log_systemd) {
				if (log_color) {
					if (level_flags & (G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE)) {
						/* White */
						r = rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "\033[0;37m");
					}
					else if (level_flags & G_LOG_LEVEL_WARNING) {
						/* Magenta */
						r = rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "\033[0;32m");
					}
					else if (level_flags & G_LOG_LEVEL_CRITICAL) {
						/* Red */
						r = rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "\033[1;31m");
					}
					else {
						r = 0;
					}
				}
				else {
					r = 0;
				}

				if (log_severity) {
					r += rspamd_snprintf(tmpbuf + r,
										 sizeof(tmpbuf) - r,
										 "%s [%s] #%P(%s) ",
										 timebuf,
										 rspamd_get_log_severity_string(level_flags),
										 logger->pid,
										 logger->process_type);
				}
				else {
					r += rspamd_snprintf(tmpbuf + r,
										 sizeof(tmpbuf) - r,
										 "%s #%P(%s) ",
										 timebuf,
										 logger->pid,
										 logger->process_type);
				}
			}
			else {
				r = 0;
				r += rspamd_snprintf(tmpbuf + r,
									 sizeof(tmpbuf) - r,
									 "(%s) ",
									 logger->process_type);
			}

			int mremain, mr;
			char *m;

			modulebuf[0] = '\0';
			mremain = sizeof(modulebuf);
			m = modulebuf;

			if (id != NULL) {
				guint slen = strlen(id);
				slen = MIN(RSPAMD_LOG_ID_LEN, slen);
				mr = rspamd_snprintf(m, mremain, "<%*.s>; ", slen,
									 id);
				m += mr;
				mremain -= mr;
			}
			if (module != NULL) {
				mr = rspamd_snprintf(m, mremain, "%s; ", module);
				m += mr;
				mremain -= mr;
			}
			if (function != NULL) {
				mr = rspamd_snprintf(m, mremain, "%s: ", function);
				m += mr;
				mremain -= mr;
			}
			else {
				mr = rspamd_snprintf(m, mremain, ": ");
				m += mr;
				mremain -= mr;
			}

			/* Ensure that we have a space at the end */
			if (m > modulebuf && *(m - 1) != ' ') {
				*(m - 1) = ' ';
			}

			/* Construct IOV for log line */
			iov[0].iov_base = tmpbuf;
			iov[0].iov_len = r;
			iov[1].iov_base = modulebuf;
			iov[1].iov_len = m - modulebuf;
			iov[2].iov_base = (void *) message;
			iov[2].iov_len = mlen;
			iov[3].iov_base = (void *) &lf_chr;
			iov[3].iov_len = 1;

			if (log_color) {
				iov[4].iov_base = "\033[0m";
				iov[4].iov_len = sizeof("\033[0m") - 1;

				return 5;
			}

			return 4;
		}
		else {
			/* Rspamadm case */
			int niov = 0;

			if (logger->log_level == G_LOG_LEVEL_DEBUG) {
				iov[niov].iov_base = (void *) timebuf;
				iov[niov++].iov_len = strlen(timebuf);
				iov[niov].iov_base = (void *) " ";
				iov[niov++].iov_len = 1;
			}

			iov[niov].iov_base = (void *) message;
			iov[niov++].iov_len = mlen;
			iov[niov].iov_base = (void *) &lf_chr;
			iov[niov++].iov_len = 1;

			return niov;
		}
	}
}