aboutsummaryrefslogtreecommitdiff
path: root/src/lua/lua_logger.c
blob: 2145d408633f1cb321b402519e7ce7c0bcde68f1 (plain) (blame)
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
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
/*
 * Copyright 2025 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 "lua_common.h"
#include "libserver/maps/map.h"
#include "libserver/maps/map_private.h"

/***
 * @module rspamd_logger
 * Rspamd logger module is used to log messages from LUA API to the main rspamd logger.
 * It supports legacy and modern interfaces allowing highly customized an convenient log functions.
 * Here is an example of logger usage:
 * @example
local rspamd_logger = require "rspamd_logger"

local a = 'string'
local b = 1.5
local c = 100
local d = {
	'aa',
	1,
	'bb'
}
local e = {
	key = 'value',
	key2 = 1.0
}

-- New extended interface
-- Positional arguments: %<number> (e.g., %1, %2, %3)
-- Sequential arguments: %s (uses the next argument)
-- Type specifiers can be combined with positional or sequential:
--   %d   - signed integer
--   %ud  - unsigned integer
--   %f   - double (floating point)
--   %.Nf - double with N decimal places (e.g., %.2f for 2 decimals)

-- Default formatting (automatic type detection)
rspamd_logger.info('a=%1, b=%2, c=%3, d=%4, e=%s', a, b, c, d, e)
-- Output: a=string, b=1.500000, c=100, d={[1] = aa, [2] = 1, [3] = bb} e={[key]=value, [key2]=1.0}

-- Using type specifiers
rspamd_logger.info('count=%1d, price=%.2f, name=%3', c, b, a)
-- Output: count=100, price=1.50, name=string

-- Sequential formatting with types
rspamd_logger.info('int=%d, float=%.3f, str=%s', c, b, a)
-- Output: int=100, float=1.500, str=string

-- Create string using logger API
local str = rspamd_logger.slog('value=%1d, percent=%.1f%%', c, b)
print(str)
-- Output: value=100, percent=1.5%
 */

/* Logger methods */
/***
 * @function logger.err(msg)
 * Log message as an error
 * @param {string} msg string to be logged
 */
LUA_FUNCTION_DEF(logger, err);
/***
 * @function logger.warn(msg)
 * Log message as a warning
 * @param {string} msg string to be logged
 */
LUA_FUNCTION_DEF(logger, warn);
/***
 * @function logger.info(msg)
 * Log message as an informational message
 * @param {string} msg string to be logged
 */
LUA_FUNCTION_DEF(logger, info);
/***
 * @function logger.message(msg)
 * Log message as an notice message
 * @param {string} msg string to be logged
 */
LUA_FUNCTION_DEF(logger, message);
/***
 * @function logger.debug(msg)
 * Log message as a debug message
 * @param {string} msg string to be logged
 */
LUA_FUNCTION_DEF(logger, debug);
/***
 * @function logger.errx(fmt[, args)
 * Extended interface to make an error log message
 * @param {string} fmt format string supporting:
 *   - Positional arguments: %<number> (e.g., %1, %2, %3)
 *   - Sequential arguments: %s
 *   - Type specifiers: %d (int), %ud (unsigned), %f (float), %.Nf (float with precision)
 *   - Combined: %1d, %2f, %.2f
 * @param {any} args list of arguments to be formatted
 */
LUA_FUNCTION_DEF(logger, errx);
/***
 * @function logger.warn(fmt[, args)
 * Extended interface to make a warning log message
 * @param {string} fmt format string supporting:
 *   - Positional arguments: %<number> (e.g., %1, %2, %3)
 *   - Sequential arguments: %s
 *   - Type specifiers: %d (int), %ud (unsigned), %f (float), %.Nf (float with precision)
 *   - Combined: %1d, %2f, %.2f
 * @param {any} args list of arguments to be formatted
 */
LUA_FUNCTION_DEF(logger, warnx);
/***
 * @function logger.infox(fmt[, args)
 * Extended interface to make an informational log message
 * @param {string} fmt format string supporting:
 *   - Positional arguments: %<number> (e.g., %1, %2, %3)
 *   - Sequential arguments: %s
 *   - Type specifiers: %d (int), %ud (unsigned), %f (float), %.Nf (float with precision)
 *   - Combined: %1d, %2f, %.2f
 * @param {any} args list of arguments to be formatted
 */
LUA_FUNCTION_DEF(logger, infox);
/***
 * @function logger.messagex(fmt[, args)
 * Extended interface to make a notice log message
 * @param {string} fmt format string supporting:
 *   - Positional arguments: %<number> (e.g., %1, %2, %3)
 *   - Sequential arguments: %s
 *   - Type specifiers: %d (int), %ud (unsigned), %f (float), %.Nf (float with precision)
 *   - Combined: %1d, %2f, %.2f
 * @param {any} args list of arguments to be formatted
 */
LUA_FUNCTION_DEF(logger, messagex);
/***
 * @function logger.debugx(fmt[, args)
 * Extended interface to make a debug log message
 * @param {string} fmt format string supporting:
 *   - Positional arguments: %<number> (e.g., %1, %2, %3)
 *   - Sequential arguments: %s
 *   - Type specifiers: %d (int), %ud (unsigned), %f (float), %.Nf (float with precision)
 *   - Combined: %1d, %2f, %.2f
 * @param {any} args list of arguments to be formatted
 */
LUA_FUNCTION_DEF(logger, debugx);

/***
 * @function logger.debugm(module, id, fmt[, args)
 * Extended interface to make a debug log message
 * @param {string} module debug module
 * @param {task|cfg|pool|string} id id to log
 * @param {string} fmt format string supporting type specifiers (%d, %ud, %f, %.Nf)
 * @param {any} args list of arguments to be formatted
 */
LUA_FUNCTION_DEF(logger, debugm);
/***
 * @function logger.slog(fmt[, args)
 * Create string replacing percent params with corresponding arguments
 * @param {string} fmt format string supporting:
 *   - Positional arguments: %<number> (e.g., %1, %2, %3)
 *   - Sequential arguments: %s
 *   - Type specifiers: %d (int), %ud (unsigned), %f (float), %.Nf (float with precision)
 *   - Combined: %1d, %2f, %.2f
 * @param {any} args list of arguments to be formatted
 * @return {string} string with percent parameters substituted
 */
LUA_FUNCTION_DEF(logger, slog);

/***
 * @function logger.logx(level, module, id, fmt[, args)
 * Extended interface to make a generic log message on any level
 * @param {number} log level as a number (see GLogLevelFlags enum for values)
 * @param {task|cfg|pool|string} id id to log
 * @param {string} fmt format string supporting type specifiers (%d, %ud, %f, %.Nf)
 * @param {any} args list of arguments to be formatted
 */
LUA_FUNCTION_DEF(logger, logx);

/***
 * @function logger.log_level()
 * Returns log level for a logger
 * @return {string} current log level
 */
LUA_FUNCTION_DEF(logger, log_level);

static const struct luaL_reg loggerlib_f[] = {
	LUA_INTERFACE_DEF(logger, err),
	LUA_INTERFACE_DEF(logger, warn),
	LUA_INTERFACE_DEF(logger, message),
	{"msg", lua_logger_message},
	LUA_INTERFACE_DEF(logger, info),
	LUA_INTERFACE_DEF(logger, debug),
	LUA_INTERFACE_DEF(logger, errx),
	LUA_INTERFACE_DEF(logger, warnx),
	LUA_INTERFACE_DEF(logger, infox),
	LUA_INTERFACE_DEF(logger, messagex),
	{"msgx", lua_logger_messagex},
	LUA_INTERFACE_DEF(logger, debugx),
	LUA_INTERFACE_DEF(logger, debugm),
	LUA_INTERFACE_DEF(logger, slog),
	LUA_INTERFACE_DEF(logger, logx),
	LUA_INTERFACE_DEF(logger, log_level),
	{"__tostring", rspamd_lua_class_tostring},
	{NULL, NULL}};

static gsize
lua_logger_out_type(lua_State *L, int pos, char *outbuf,
					gsize len, struct lua_logger_trace *trace,
					enum lua_logger_escape_type esc_type);

static void
lua_common_log_line(GLogLevelFlags level,
					lua_State *L,
					const char *msg,
					const char *uid,
					const char *module,
					int stack_level)
{
	lua_Debug d;
	char func_buf[128], *p;

	if (lua_getstack(L, stack_level, &d) == 1) {
		(void) lua_getinfo(L, "Sl", &d);
		if ((p = strrchr(d.short_src, '/')) == NULL) {
			p = d.short_src;
		}
		else {
			p++;
		}

		if (strlen(p) > 30) {
			rspamd_snprintf(func_buf, sizeof(func_buf), "%27s...:%d", p,
							d.currentline);
		}
		else {
			rspamd_snprintf(func_buf, sizeof(func_buf), "%s:%d", p,
							d.currentline);
		}

		p = func_buf;
	}
	else {
		p = (char *) G_STRFUNC;
	}

	rspamd_common_log_function(NULL,
							   level,
							   module,
							   uid,
							   p,
							   "%s",
							   msg);
}

/*** Logger interface ***/
static int
lua_logger_err(lua_State *L)
{
	return lua_logger_errx(L);
}

static int
lua_logger_warn(lua_State *L)
{
	return lua_logger_warnx(L);
}

static int
lua_logger_info(lua_State *L)
{
	return lua_logger_infox(L);
}

static int
lua_logger_message(lua_State *L)
{
	return lua_logger_messagex(L);
}

static int
lua_logger_debug(lua_State *L)
{
	return lua_logger_debugx(L);
}

static inline bool
lua_logger_char_safe(int t, unsigned int esc_type)
{
	if (t & 0x80) {
		if (esc_type & LUA_ESCAPE_8BIT) {
			return false;
		}

		return true;
	}

	if (esc_type & LUA_ESCAPE_UNPRINTABLE) {
		if (!g_ascii_isprint(t) && !g_ascii_isspace(t)) {
			return false;
		}
	}

	if (esc_type & LUA_ESCAPE_NEWLINES) {
		if (t == '\r' || t == '\n') {
			return false;
		}
	}

	return true;
}

/* Format specifier types */
enum lua_logger_format_type {
	LUA_FMT_STRING = 0, /* %s - default, any type */
	LUA_FMT_INT,        /* %d - signed integer */
	LUA_FMT_UINT,       /* %ud - unsigned integer */
	LUA_FMT_DOUBLE,     /* %f - double with optional precision */
};

/* Format a number as integer */
static gsize
lua_logger_out_int(lua_State *L, int pos, char *outbuf, gsize len, gboolean is_unsigned)
{
	if (lua_type(L, pos) == LUA_TNUMBER) {
		lua_Number num = lua_tonumber(L, pos);
		if (is_unsigned) {
			guint64 uval = (guint64) num;
			return rspamd_snprintf(outbuf, len, "%uL", uval);
		}
		else {
			gint64 ival = (gint64) num;
			return rspamd_snprintf(outbuf, len, "%L", ival);
		}
	}
	else if (lua_type(L, pos) == LUA_TSTRING) {
		/* Try to convert string to number */
		gsize slen;
		const char *str = lua_tolstring(L, pos, &slen);
		if (is_unsigned) {
			gulong uval;
			if (rspamd_strtoul(str, slen, &uval)) {
				return rspamd_snprintf(outbuf, len, "%uL", uval);
			}
		}
		else {
			glong ival;
			if (rspamd_strtol(str, slen, &ival)) {
				return rspamd_snprintf(outbuf, len, "%L", ival);
			}
		}
	}
	/* Fallback for non-numeric types */
	return rspamd_snprintf(outbuf, len, is_unsigned ? "0" : "0");
}

/* Format a number as double with precision */
static gsize
lua_logger_out_double(lua_State *L, int pos, char *outbuf, gsize len, int precision)
{
	gsize r;
	char *p;

	if (lua_type(L, pos) == LUA_TNUMBER) {
		lua_Number num = lua_tonumber(L, pos);
		if (precision >= 0) {
			return rspamd_snprintf(outbuf, len, "%.*f", precision, (double) num);
		}
		else {
			/* Default: smart formatting without trailing zeros */
			r = rspamd_snprintf(outbuf, len, "%.6f", (double) num);
			/* Remove trailing zeros, but keep at least one digit after decimal point */
			if (r > 0 && outbuf[0] != '\0') {
				p = outbuf + r - 1;
				while (p > outbuf && *p == '0') {
					p--;
				}
				/* Keep at least one digit after decimal point */
				if (*p == '.') {
					p++;
				}
				p++;
				*p = '\0';
				return p - outbuf;
			}
			return r;
		}
	}
	else if (lua_type(L, pos) == LUA_TSTRING) {
		/* Try to convert string to number */
		gsize slen;
		const char *str = lua_tolstring(L, pos, &slen);
		char *endptr;
		double dval = g_ascii_strtod(str, &endptr);
		if (endptr != str && (*endptr == '\0' || endptr == str + slen)) {
			if (precision >= 0) {
				return rspamd_snprintf(outbuf, len, "%.*f", precision, dval);
			}
			else {
				/* Default: smart formatting without trailing zeros */
				r = rspamd_snprintf(outbuf, len, "%.6f", dval);
				/* Remove trailing zeros, but keep at least one digit after decimal point */
				if (r > 0 && outbuf[0] != '\0') {
					p = outbuf + r - 1;
					while (p > outbuf && *p == '0') {
						p--;
					}
					/* Keep at least one digit after decimal point */
					if (*p == '.') {
						p++;
					}
					p++;
					*p = '\0';
					return p - outbuf;
				}
				return r;
			}
		}
	}
	/* Fallback for non-numeric types */
	if (precision >= 0) {
		return rspamd_snprintf(outbuf, len, "%.*f", precision, 0.0);
	}
	return rspamd_snprintf(outbuf, len, "0.0");
}

#define LUA_MAX_ARGS 32
/* Gracefully handles argument mismatches by substituting missing args and noting extra args */
static glong
lua_logger_log_format_str(lua_State *L, int offset, char *logbuf, gsize remain,
						  const char *fmt,
						  enum lua_logger_escape_type esc_type)
{
	const char *c;
	gsize r;
	int digit;
	char *d = logbuf;
	unsigned int arg_num, cur_arg = 0, arg_max = lua_gettop(L) - offset;
	gboolean args_used[LUA_MAX_ARGS];
	unsigned int used_args_count = 0;
	enum lua_logger_format_type fmt_type;
	int precision;

	memset(args_used, 0, sizeof(args_used));
	while (remain > 1 && *fmt) {
		if (*fmt == '%') {
			++fmt;
			/* Check for %% (escaped percent) */
			if (*fmt == '%') {
				*d++ = '%';
				++fmt;
				--remain;
				continue;
			}
			c = fmt;
			fmt_type = LUA_FMT_STRING;
			precision = -1;

			/* Check for precision specifier (%.Nf) */
			if (*fmt == '.') {
				++fmt;
				precision = 0;
				while ((digit = g_ascii_digit_value(*fmt)) >= 0) {
					precision = precision * 10 + digit;
					++fmt;
				}
				/* Expect 'f' after precision */
				if (*fmt == 'f') {
					fmt_type = LUA_FMT_DOUBLE;
					++fmt;
					++cur_arg;
				}
				else {
					/* Invalid format, reset */
					fmt = c;
				}
			}
			/* Check for format type specifiers */
			else if (*fmt == 's') {
				fmt_type = LUA_FMT_STRING;
				++fmt;
				++cur_arg;
			}
			else if (*fmt == 'd') {
				fmt_type = LUA_FMT_INT;
				++fmt;
				++cur_arg;
			}
			else if (*fmt == 'u') {
				++fmt;
				if (*fmt == 'd') {
					fmt_type = LUA_FMT_UINT;
					++fmt;
					++cur_arg;
				}
				else {
					/* Just 'u' without 'd', treat as literal */
					fmt = c;
				}
			}
			else if (*fmt == 'f') {
				fmt_type = LUA_FMT_DOUBLE;
				++fmt;
				++cur_arg;
			}
			/* Check for positional argument (%<number>) */
			else if (g_ascii_isdigit(*fmt)) {
				arg_num = 0;
				while ((digit = g_ascii_digit_value(*fmt)) >= 0) {
					++fmt;
					arg_num = arg_num * 10 + digit;
					if (arg_num >= LUA_MAX_ARGS) {
						/* Avoid ridiculously large numbers */
						fmt = c;
						break;
					}
				}

				if (fmt > c) {
					/* Check for type specifier after number */
					if (*fmt == 'd') {
						fmt_type = LUA_FMT_INT;
						++fmt;
					}
					else if (*fmt == 'u') {
						++fmt;
						if (*fmt == 'd') {
							fmt_type = LUA_FMT_UINT;
							++fmt;
						}
						else {
							--fmt; /* Backtrack */
						}
					}
					else if (*fmt == 'f') {
						fmt_type = LUA_FMT_DOUBLE;
						++fmt;
					}
					/* else: default to LUA_FMT_STRING */

					/* Update the current argument */
					cur_arg = arg_num;
				}
			}

			if (fmt > c) {
				if (cur_arg < 1 || cur_arg > arg_max) {
					/* Missing argument - substitute placeholder */
					r = rspamd_snprintf(d, remain, "<MISSING ARGUMENT>");
				}
				else {
					/* Valid argument - output it based on format type */
					switch (fmt_type) {
					case LUA_FMT_INT:
						r = lua_logger_out_int(L, offset + cur_arg, d, remain, FALSE);
						break;
					case LUA_FMT_UINT:
						r = lua_logger_out_int(L, offset + cur_arg, d, remain, TRUE);
						break;
					case LUA_FMT_DOUBLE:
						r = lua_logger_out_double(L, offset + cur_arg, d, remain, precision);
						break;
					case LUA_FMT_STRING:
					default:
						r = lua_logger_out(L, offset + cur_arg, d, remain, esc_type);
						break;
					}
					/* Track which arguments are used */
					if (cur_arg <= LUA_MAX_ARGS && !args_used[cur_arg - 1]) {
						args_used[cur_arg - 1] = TRUE;
						used_args_count++;
					}
				}

				g_assert(r < remain);
				remain -= r;
				d += r;
				continue;
			}

			/* Copy % if we couldn't parse a format specifier */
			if (fmt == c) {
				--fmt;
			}
			else {
				/* We parsed something but didn't match a valid format */
				--fmt;
			}
		}

		*d++ = *fmt++;
		--remain;
	}

	/* Check for extra arguments and append warning if any */
	if (used_args_count > 0 && used_args_count < arg_max && remain > 1) {
		unsigned int extra_args = arg_max - used_args_count;
		r = rspamd_snprintf(d, remain, " <EXTRA %d ARGUMENTS>", (int) extra_args);
		remain -= r;
		d += r;
	}

	*d = 0;

	return d - logbuf;
}

#undef LUA_MAX_ARGS

static gsize
lua_logger_out_str(lua_State *L, int pos,
				   char *outbuf, gsize len,
				   enum lua_logger_escape_type esc_type)
{
	static const char hexdigests[] = "0123456789abcdef";
	gsize slen;
	const unsigned char *str = lua_tolstring(L, pos, &slen);
	unsigned char c;
	char *out = outbuf;

	if (str) {
		while (slen > 0 && len > 1) {
			c = *str++;
			if (lua_logger_char_safe(c, esc_type)) {
				*out++ = c;
			}
			else if (len > 3) {
				/* Need to escape non-printed characters */
				*out++ = '\\';
				*out++ = hexdigests[c >> 4];
				*out++ = hexdigests[c & 0xF];
				len -= 2;
			}
			else {
				*out++ = '?';
			}
			--slen;
			--len;
		}
	}
	*out = 0;

	return out - outbuf;
}

static gsize
lua_logger_out_num(lua_State *L, int pos, char *outbuf, gsize len)
{
	double num = lua_tonumber(L, pos);
	glong inum = (glong) num;

	if ((double) inum == num) {
		return rspamd_snprintf(outbuf, len, "%l", inum);
	}

	return rspamd_snprintf(outbuf, len, "%f", num);
}

static gsize
lua_logger_out_boolean(lua_State *L, int pos, char *outbuf, gsize len)
{
	gboolean val = lua_toboolean(L, pos);

	return rspamd_snprintf(outbuf, len, val ? "true" : "false");
}

static gsize
lua_logger_out_userdata(lua_State *L, int pos, char *outbuf, gsize len)
{
	gsize r = 0;
	int top;
	const char *str = NULL;
	gboolean converted_to_str = FALSE;

	top = lua_gettop(L);
	if (pos < 0) {
		pos += top + 1; /* Convert to absolute */
	}

	if (!lua_getmetatable(L, pos)) {
		return 0;
	}

	lua_pushstring(L, "__index");
	lua_gettable(L, -2);

	if (!lua_istable(L, -1)) {

		if (lua_isfunction(L, -1)) {
			/* Functional metatable, try to get __tostring directly */
			lua_pushstring(L, "__tostring");
			lua_gettable(L, -3);

			if (lua_isfunction(L, -1)) {
				lua_pushvalue(L, pos);

				if (lua_pcall(L, 1, 1, 0) == 0) {
					str = lua_tostring(L, -1);
					if (str) {
						r = rspamd_snprintf(outbuf, len, "%s", str);
					}
				}
			}
		}
		lua_settop(L, top);

		return r;
	}

	lua_pushstring(L, "__tostring");
	lua_gettable(L, -2);

	if (lua_isfunction(L, -1)) {
		lua_pushvalue(L, pos);

		if (lua_pcall(L, 1, 1, 0) != 0) {
			lua_settop(L, top);

			return 0;
		}

		str = lua_tostring(L, -1);

		if (str) {
			converted_to_str = TRUE;
		}
	}
	else {
		lua_pop(L, 1);
		lua_pushstring(L, "class");
		lua_gettable(L, -2);

		if (lua_isstring(L, -1)) {
			str = lua_tostring(L, -1);
			converted_to_str = TRUE;
		}
	}

	if (converted_to_str) {
		r = rspamd_snprintf(outbuf, len, "%s", str);
	}
	else {
		/* Print raw pointer */
		r = rspamd_snprintf(outbuf, len, "%s(%p)", str, lua_touserdata(L, pos));
	}

	lua_settop(L, top);

	return r;
}

#define MOVE_BUF(d, remain, r) \
	(d) += (r);                \
	(remain) -= (r);           \
	if ((remain) <= 1) {       \
		lua_settop(L, top);    \
		goto table_oob;        \
	}

static gsize
lua_logger_out_table(lua_State *L, int pos, char *outbuf, gsize len,
					 struct lua_logger_trace *trace,
					 enum lua_logger_escape_type esc_type)
{
	char *d = outbuf, *str;
	gsize remain = len;
	glong r;
	gboolean first = TRUE;
	gconstpointer self = NULL;
	int i, last_seq = 0, top;
	double num;
	glong inum;

	/* Type and length checks are done in logger_out_type() */

	self = lua_topointer(L, pos);

	/* Check if we have seen this pointer */
	for (i = 0; i < TRACE_POINTS; i++) {
		if (trace->traces[i] == self) {
			if ((trace->cur_level + TRACE_POINTS - 1) % TRACE_POINTS == i) {
				return rspamd_snprintf(d, remain, "__self");
			}
			return rspamd_snprintf(d, remain, "ref(%p)", self);
		}
	}

	trace->traces[trace->cur_level % TRACE_POINTS] = self;
	++trace->cur_level;

	top = lua_gettop(L);
	if (pos < 0) {
		pos += top + 1; /* Convert to absolute */
	}

	r = rspamd_snprintf(d, remain, "{");
	MOVE_BUF(d, remain, r);

	/* Get numeric keys (ipairs) */
	for (i = 1;; i++) {
		lua_rawgeti(L, pos, i);

		if (lua_isnil(L, -1)) {
			lua_pop(L, 1);
			last_seq = i;
			break;
		}

		if (first) {
			first = FALSE;
			str = "[%d] = ";
		}
		else {
			str = ", [%d] = ";
		}
		r = rspamd_snprintf(d, remain, str, i);
		MOVE_BUF(d, remain, r);

		r = lua_logger_out_type(L, -1, d, remain, trace, esc_type);
		MOVE_BUF(d, remain, r);

		lua_pop(L, 1);
	}

	/* Get string keys (pairs) */
	for (lua_pushnil(L); lua_next(L, pos); lua_pop(L, 1)) {
		/* 'key' is at index -2 and 'value' is at index -1 */

		/* Preserve key */
		lua_pushvalue(L, -2);
		if (last_seq > 0) {
			if (lua_type(L, -1) == LUA_TNUMBER) {
				num = lua_tonumber(L, -1); /* no conversion here */
				inum = (glong) num;
				if ((double) inum == num && inum > 0 && inum < last_seq) {
					/* Already seen */
					lua_pop(L, 1);
					continue;
				}
			}
		}

		if (first) {
			first = FALSE;
			str = "[%2] = %1";
		}
		else {
			str = ", [%2] = %1";
		}
		r = lua_logger_log_format_str(L, top + 1, d, remain, str, esc_type);
		/* lua_logger_log_format_str now handles errors gracefully */
		MOVE_BUF(d, remain, r);

		/* Remove key */
		lua_pop(L, 1);
	}

	r = rspamd_snprintf(d, remain, "}");
	d += r;

table_oob:
	--trace->cur_level;

	return (d - outbuf);
}

#undef MOVE_BUF

static gsize
lua_logger_out_type(lua_State *L, int pos,
					char *outbuf, gsize len,
					struct lua_logger_trace *trace,
					enum lua_logger_escape_type esc_type)
{
	if (len == 0) {
		return 0;
	}

	int type = lua_type(L, pos);

	switch (type) {
	case LUA_TNUMBER:
		return lua_logger_out_num(L, pos, outbuf, len);
	case LUA_TBOOLEAN:
		return lua_logger_out_boolean(L, pos, outbuf, len);
	case LUA_TTABLE:
		return lua_logger_out_table(L, pos, outbuf, len, trace, esc_type);
	case LUA_TUSERDATA:
		return lua_logger_out_userdata(L, pos, outbuf, len);
	case LUA_TFUNCTION:
		return rspamd_snprintf(outbuf, len, "function");
	case LUA_TLIGHTUSERDATA:
		return rspamd_snprintf(outbuf, len, "0x%p", lua_topointer(L, pos));
	case LUA_TNIL:
		return rspamd_snprintf(outbuf, len, "nil");
	case LUA_TNONE:
		return rspamd_snprintf(outbuf, len, "no value");
	}

	/* Try to push everything as string using tostring magic */
	return lua_logger_out_str(L, pos, outbuf, len, esc_type);
}

gsize lua_logger_out(lua_State *L, int pos,
					 char *outbuf, gsize len,
					 enum lua_logger_escape_type esc_type)
{
	struct lua_logger_trace tr;
	memset(&tr, 0, sizeof(tr));

	return lua_logger_out_type(L, pos, outbuf, len, &tr, esc_type);
}

static const char *
lua_logger_get_id(lua_State *L, int pos, GError **err)
{
	const char *uid = NULL, *clsname;

	int top = lua_gettop(L);

	if (lua_getmetatable(L, pos) != 0) {
		/* mt is on top */
		uid = "";
		clsname = NULL;

		/* Fast path: use numeric class id stored at mt[1] */
		lua_rawgeti(L, -1, 1);
		if (lua_type(L, -1) == LUA_TNUMBER) {
			glong cid = (glong) lua_tonumber(L, -1);
			lua_pop(L, 1);
			/* Map numeric ids to class names via pointer equality */
			if (cid == GPOINTER_TO_INT(rspamd_task_classname)) {
				clsname = rspamd_task_classname;
			}
			else if (cid == GPOINTER_TO_INT(rspamd_mempool_classname)) {
				clsname = rspamd_mempool_classname;
			}
			else if (cid == GPOINTER_TO_INT(rspamd_ev_base_classname)) {
				clsname = rspamd_ev_base_classname;
			}
			else if (cid == GPOINTER_TO_INT(rspamd_worker_classname)) {
				clsname = rspamd_worker_classname;
			}
			else if (cid == GPOINTER_TO_INT(rspamd_config_classname)) {
				clsname = rspamd_config_classname;
			}
			else if (cid == GPOINTER_TO_INT(rspamd_resolver_classname)) {
				clsname = rspamd_resolver_classname;
			}
			else if (cid == GPOINTER_TO_INT(rspamd_session_classname)) {
				clsname = rspamd_session_classname;
			}
			else if (cid == GPOINTER_TO_INT(rspamd_map_classname)) {
				clsname = rspamd_map_classname;
			}
		}
		else {
			lua_pop(L, 1);
		}

		/* Slow path: read textual 'class' if needed */
		if (clsname == NULL) {
			lua_pushstring(L, "class");
			lua_gettable(L, -2);
			if (lua_type(L, -1) == LUA_TSTRING) {
				clsname = lua_tostring(L, -1);
			}
			lua_pop(L, 1);
		}

		if (clsname == NULL) {
			/* Fallback to legacy behavior if needed (when __index is a table) */
			lua_pushstring(L, "__index");
			lua_gettable(L, -2);
			if (lua_type(L, -1) == LUA_TTABLE) {
				lua_pushstring(L, "class");
				lua_gettable(L, -2);
				if (lua_type(L, -1) == LUA_TSTRING) {
					clsname = lua_tostring(L, -1);
				}
				lua_pop(L, 1);
			}
			/* Pop __index value */
			lua_pop(L, 1);
		}

		if (strcmp(clsname, rspamd_task_classname) == 0) {
			struct rspamd_task *task = lua_check_task(L, pos);

			if (task) {
				uid = task->task_pool->tag.uid;
			}
			else {
				g_set_error(err, g_quark_from_static_string("lua_logger"),
							EINVAL, "invalid rspamd{task}");
			}
		}
		else if (strcmp(clsname, rspamd_mempool_classname) == 0) {
			rspamd_mempool_t *pool;

			pool = rspamd_lua_check_mempool(L, pos);

			if (pool) {
				uid = pool->tag.uid;
			}
			else {
				g_set_error(err, g_quark_from_static_string("lua_logger"),
							EINVAL, "invalid rspamd{mempool}");
			}
		}
		else if (strcmp(clsname, rspamd_config_classname) == 0) {
			struct rspamd_config *cfg;

			cfg = lua_check_config(L, pos);

			if (cfg) {
				if (cfg->checksum) {
					uid = cfg->checksum;
				}
			}
			else {
				g_set_error(err, g_quark_from_static_string("lua_logger"),
							EINVAL, "invalid rspamd{config}");
			}
		}
		else if (strcmp(clsname, rspamd_map_classname) == 0) {
			struct rspamd_lua_map *map;

			map = lua_check_map(L, pos);

			if (map) {
				if (map->map) {
					uid = map->map->tag;
				}
				else {
					uid = "embedded";
				}
			}
			else {
				g_set_error(err, g_quark_from_static_string("lua_logger"),
							EINVAL, "invalid rspamd{map}");
			}
		}
		else {
			g_set_error(err, g_quark_from_static_string("lua_logger"),
						EINVAL, "unknown class: %s", clsname);
		}


		/* Restore stack to state before getmetatable */
		lua_settop(L, top);
	}
	else {
		g_set_error(err, g_quark_from_static_string("lua_logger"),
					EINVAL, "no metatable found for userdata");
	}

	return uid;
}

static gboolean
lua_logger_log_format(lua_State *L, int fmt_pos, gboolean is_string,
					  char *logbuf, gsize remain)
{
	const char *fmt = lua_tostring(L, fmt_pos);
	if (fmt == NULL) {
		return FALSE;
	}

	/* lua_logger_log_format_str now handles argument mismatches gracefully */
	lua_logger_log_format_str(L, fmt_pos, logbuf, remain, fmt, is_string ? LUA_ESCAPE_UNPRINTABLE : LUA_ESCAPE_LOG);
	return TRUE;
}

static int
lua_logger_do_log(lua_State *L,
				  GLogLevelFlags level,
				  gboolean is_string,
				  int start_pos)
{
	char logbuf[RSPAMD_LOGBUF_SIZE - 128];
	const char *uid = NULL;
	int ret;
	int fmt_pos = start_pos;

	/* Optional id argument */
	if (lua_type(L, fmt_pos) == LUA_TUSERDATA) {
		GError *err = NULL;

		uid = lua_logger_get_id(L, fmt_pos, &err);

		if (uid == NULL) {
			ret = luaL_error(L, "bad userdata for logging: %s",
							 err ? err->message : "unknown error");

			if (err) {
				g_error_free(err);
			}

			return ret;
		}

		++fmt_pos;
	}

	/* Allow calling warnx(fmt, ...) directly without id */
	if (lua_type(L, fmt_pos) != LUA_TSTRING) {
		/* Try start_pos first */
		fmt_pos = start_pos;
	}

	if (lua_type(L, fmt_pos) != LUA_TSTRING) {
		/* Scan forward to find the first string arg (robust to accidental extra args) */
		int top = lua_gettop(L);
		int i;
		for (i = fmt_pos + 1; i <= top; i++) {
			if (lua_type(L, i) == LUA_TSTRING) {
				fmt_pos = i;
				break;
			}
		}

		if (lua_type(L, fmt_pos) != LUA_TSTRING) {
			/* Bad argument type */
			return luaL_error(L, "bad format string type: %s",
							  lua_typename(L, lua_type(L, fmt_pos)));
		}
	}

	ret = lua_logger_log_format(L, fmt_pos, is_string, logbuf, sizeof(logbuf));

	if (ret) {
		if (is_string) {
			lua_pushstring(L, logbuf);
			return 1;
		}
		else {
			lua_common_log_line(level, L, logbuf, uid, "lua", 1);
		}
	}
	else if (is_string) {
		lua_pushnil(L);
		return 1;
	}

	return 0;
}

static int
lua_logger_errx(lua_State *L)
{
	LUA_TRACE_POINT;
	return lua_logger_do_log(L, G_LOG_LEVEL_CRITICAL, FALSE, 1);
}

static int
lua_logger_warnx(lua_State *L)
{
	LUA_TRACE_POINT;
	return lua_logger_do_log(L, G_LOG_LEVEL_WARNING, FALSE, 1);
}

static int
lua_logger_infox(lua_State *L)
{
	LUA_TRACE_POINT;
	return lua_logger_do_log(L, G_LOG_LEVEL_INFO, FALSE, 1);
}

static int
lua_logger_messagex(lua_State *L)
{
	LUA_TRACE_POINT;
	return lua_logger_do_log(L, G_LOG_LEVEL_MESSAGE, FALSE, 1);
}

static int
lua_logger_debugx(lua_State *L)
{
	LUA_TRACE_POINT;
	return lua_logger_do_log(L, G_LOG_LEVEL_DEBUG, FALSE, 1);
}

static int
lua_logger_logx(lua_State *L)
{
	LUA_TRACE_POINT;
	GLogLevelFlags flags = lua_tonumber(L, 1);
	const char *modname = lua_tostring(L, 2), *uid = NULL;
	char logbuf[RSPAMD_LOGBUF_SIZE - 128];
	gboolean ret;
	int stack_pos = 1;

	if (lua_type(L, 3) == LUA_TSTRING) {
		uid = luaL_checkstring(L, 3);
	}
	else if (lua_type(L, 3) == LUA_TUSERDATA) {
		uid = lua_logger_get_id(L, 3, NULL);
	}
	else {
		uid = "???";
	}

	if (uid && modname) {
		if (lua_type(L, 4) == LUA_TSTRING) {
			ret = lua_logger_log_format(L, 4, FALSE, logbuf, sizeof(logbuf));
		}
		else if (lua_type(L, 4) == LUA_TNUMBER) {
			stack_pos = lua_tonumber(L, 4);
			ret = lua_logger_log_format(L, 5, FALSE, logbuf, sizeof(logbuf));
		}
		else {
			return luaL_error(L, "invalid argument on pos 4");
		}

		if (ret) {
			lua_common_log_line(flags, L, logbuf, uid, modname, stack_pos);
		}
	}
	else {
		return luaL_error(L, "invalid arguments");
	}

	return 0;
}


static int
lua_logger_debugm(lua_State *L)
{
	LUA_TRACE_POINT;
	char logbuf[RSPAMD_LOGBUF_SIZE - 128];
	const char *uid = NULL, *module = NULL;
	int stack_pos = 1;
	gboolean ret;

	module = luaL_checkstring(L, 1);

	if (lua_type(L, 2) == LUA_TSTRING) {
		uid = luaL_checkstring(L, 2);
	}
	else {
		uid = lua_logger_get_id(L, 2, NULL);
	}

	if (uid && module) {
		if (lua_type(L, 3) == LUA_TSTRING) {
			ret = lua_logger_log_format(L, 3, FALSE, logbuf, sizeof(logbuf));
		}
		else if (lua_type(L, 3) == LUA_TNUMBER) {
			stack_pos = lua_tonumber(L, 3);
			ret = lua_logger_log_format(L, 4, FALSE, logbuf, sizeof(logbuf));
		}
		else {
			return luaL_error(L, "invalid argument on pos 3");
		}

		if (ret) {
			lua_common_log_line(G_LOG_LEVEL_DEBUG, L, logbuf, uid, module, stack_pos);
		}
	}
	else {
		return luaL_error(L, "invalid arguments");
	}

	return 0;
}


static int
lua_logger_slog(lua_State *L)
{
	return lua_logger_do_log(L, 0, TRUE, 1);
}

static int
lua_logger_log_level(lua_State *L)
{
	int log_level = rspamd_log_get_log_level(NULL);

	lua_pushstring(L, rspamd_get_log_severity_string(log_level));

	return 1;
}

/*** Init functions ***/

static int
lua_load_logger(lua_State *L)
{
	lua_newtable(L);
	luaL_register(L, NULL, loggerlib_f);

	return 1;
}

void luaopen_logger(lua_State *L)
{
	rspamd_lua_add_preload(L, "rspamd_logger", lua_load_logger);
}