aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_logger.c
blob: 87a45c1d5b302af11f0b3c178b9db56d556b623b (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
/*-
 * Copyright 2016 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 "libutil/map.h"
#include "libutil/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 = 1
local d = {
	'aa',
	1,
	'bb'
}
local e = {
	key = 'value',
	key2 = 1.0
}

-- New extended interface
-- %<number> means numeric arguments and %s means the next argument
-- for example %1, %2, %s: %s would mean the third argument

rspamd_logger.infox('a=%1, b=%2, c=%3, d=%4, e=%s', a, b, c, d, e)
-- Output: a=string, b=1.50000, c=1, d={[1] = aa, [2] = 1, [3] = bb} e={[key]=value, [key2]=1.0}

-- Legacy interface (can handle merely strings)
rspamd_logger.info('Old stupid API')

-- Create string using logger API
local str = rspamd_logger.slog('a=%1, b=%2, c=%3, d=%4, e=%5', a, b, c, d, e)

print(str)
-- Output: a=string, b=1.50000, c=1, d={[1] = aa, [2] = 1, [3] = bb} e={[key]=value, [key2]=1.0}
 */

/* 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, arguments are encoded as %<number>
 * @param {any} args list of arguments to be replaced in %<number> positions
 */
LUA_FUNCTION_DEF (logger, errx);
/***
 * @function logger.warn(fmt[, args)
 * Extended interface to make a warning log message
 * @param {string} fmt format string, arguments are encoded as %<number>
 * @param {any} args list of arguments to be replaced in %<number> positions
 */
LUA_FUNCTION_DEF (logger, warnx);
/***
 * @function logger.infox(fmt[, args)
 * Extended interface to make an informational log message
 * @param {string} fmt format string, arguments are encoded as %<number>
 * @param {any} args list of arguments to be replaced in %<number> positions
 */
LUA_FUNCTION_DEF (logger, infox);
/***
 * @function logger.infox(fmt[, args)
 * Extended interface to make an informational log message
 * @param {string} fmt format string, arguments are encoded as %<number>
 * @param {any} args list of arguments to be replaced in %<number> positions
 */
LUA_FUNCTION_DEF (logger, messagex);
/***
 * @function logger.debugx(fmt[, args)
 * Extended interface to make a debug log message
 * @param {string} fmt format string, arguments are encoded as %<number>
 * @param {any} args list of arguments to be replaced in %<number> positions
 */
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, arguments are encoded as %<number>
 * @param {any} args list of arguments to be replaced in %<number> positions
 */
LUA_FUNCTION_DEF (logger, debugm);
/***
 * @function logger.slog(fmt[, args)
 * Create string replacing percent params with corresponding arguments
 * @param {string} fmt format string, arguments are encoded as %<number>
 * @param {any} args list of arguments to be replaced in %<number> positions
 * @return {string} string with percent parameters substituted
 */
LUA_FUNCTION_DEF (logger, slog);

/***
 * @function logger.logx(level, 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, arguments are encoded as %<number>
 * @param {any} args list of arguments to be replaced in %<number> positions
 */
LUA_FUNCTION_DEF (logger, logx);

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),
		{"__tostring", rspamd_lua_class_tostring},
		{NULL, NULL}
};

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

	if (lua_getstack (L, 1, &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);
		}

		if (level == G_LOG_LEVEL_DEBUG) {
			rspamd_conditional_debug (NULL,
					NULL,
					module,
					uid,
					func_buf,
					"%s",
					msg);
		}
		else {
			rspamd_common_log_function (NULL,
					level,
					module,
					uid,
					func_buf,
					"%s",
					msg);
		}
	}
	else {
		if (level == G_LOG_LEVEL_DEBUG) {
			rspamd_conditional_debug (NULL,
					NULL,
					module,
					uid,
					G_STRFUNC,
					"%s",
					msg);
		}
		else {
			rspamd_common_log_function (NULL,
					level,
					module,
					uid,
					G_STRFUNC,
					"%s",
					msg);
		}
	}
}

/*** Logger interface ***/
static gint
lua_logger_err (lua_State *L)
{
	LUA_TRACE_POINT;
	const gchar *msg;
	msg = luaL_checkstring (L, 1);
	lua_common_log_line (G_LOG_LEVEL_CRITICAL, L, msg, NULL, NULL);
	return 0;
}

static gint
lua_logger_warn (lua_State *L)
{
	LUA_TRACE_POINT;
	const gchar *msg;
	msg = luaL_checkstring (L, 1);
	lua_common_log_line (G_LOG_LEVEL_WARNING, L, msg, NULL, NULL);
	return 0;
}

static gint
lua_logger_info (lua_State *L)
{
	LUA_TRACE_POINT;
	const gchar *msg;
	msg = luaL_checkstring (L, 1);
	lua_common_log_line (G_LOG_LEVEL_INFO, L, msg, NULL, NULL);
	return 0;
}

static gint
lua_logger_message (lua_State *L)
{
	LUA_TRACE_POINT;
	const gchar *msg;
	msg = luaL_checkstring (L, 1);
	lua_common_log_line (G_LOG_LEVEL_MESSAGE, L, msg, NULL, NULL);
	return 0;
}

static gint
lua_logger_debug (lua_State *L)
{
	LUA_TRACE_POINT;
	const gchar *msg;
	msg = luaL_checkstring (L, 1);
	lua_common_log_line (G_LOG_LEVEL_DEBUG, L, msg, NULL, NULL);
	return 0;
}

static gsize
lua_logger_out_str (lua_State *L, gint pos, gchar *outbuf, gsize len)
{
	gsize slen;
	const gchar *str = lua_tolstring (L, pos, &slen);
	gsize r = 0;

	if (str) {
		r = rspamd_strlcpy (outbuf, str, MIN (slen, len) + 1);
	}

	return r;
}

static gsize
lua_logger_out_num (lua_State *L, gint pos, gchar *outbuf, gsize len)
{
	gdouble num = lua_tonumber (L, pos);
	glong inum;
	gsize r = 0;

	if ((gdouble) (glong) num == num) {
		inum = num;
		r = rspamd_snprintf (outbuf, len + 1, "%l", inum);
	}
	else {
		r = rspamd_snprintf (outbuf, len + 1, "%f", num);
	}

	return r;
}

static gsize
lua_logger_out_boolean (lua_State *L, gint pos, gchar *outbuf, gsize len)
{
	gboolean val = lua_toboolean (L, pos);
	gsize r = 0;

	r = rspamd_strlcpy (outbuf, val ? "true" : "false", len + 1);

	return r;
}

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

	top = lua_gettop (L);

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

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

	if (!lua_istable (L, -1)) {
		lua_settop (L, top);

		return 0;
	}

	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_pushstring (L, "class");
		lua_gettable (L, -2);

		if (!lua_isstring (L, -1)) {
			lua_settop (L, top);

			return 0;
		}

		str = lua_tostring (L, -1);
	}

	if (converted_to_str) {
		r = rspamd_snprintf (outbuf, len + 1, "%s", str);
	}
	else {
		/* Print raw pointer */
		r = rspamd_snprintf (outbuf, len + 1, "%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) == 0) { lua_pop (L, 1); break; }

static gsize
lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len)
{
	gchar *d = outbuf;
	gsize remain = len, r;
	gboolean first = TRUE;
	gint i;

	if (!lua_istable (L, pos) || remain == 0) {
		return 0;
	}

	lua_pushvalue (L, pos);
	r = rspamd_snprintf (d, remain + 1, "{");
	remain -= r;
	d += r;

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

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

		if (!first) {
			r = rspamd_snprintf (d, remain + 1, ", ");
			MOVE_BUF(d, remain, r);
		}

		r = rspamd_snprintf (d, remain + 1, "[%d] = ", i);
		MOVE_BUF(d, remain, r);
		r = lua_logger_out_type (L, lua_gettop (L), d, remain);
		MOVE_BUF(d, remain, r);

		first = FALSE;
		lua_pop (L, 1);
	}

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

		if (lua_type (L, -2) == LUA_TNUMBER) {
			continue;
		}

		if (!first) {
			r = rspamd_snprintf (d, remain + 1, ", ");
			MOVE_BUF(d, remain, r);
		}

		r = rspamd_snprintf (d, remain + 1, "[%s] = ",
				lua_tostring (L, -2));
		MOVE_BUF(d, remain, r);
		r = lua_logger_out_type (L, lua_gettop (L), d, remain);
		MOVE_BUF(d, remain, r);

		first = FALSE;
	}

	lua_pop (L, 1);

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

	return (d - outbuf);
}

#undef MOVE_BUF

gsize
lua_logger_out_type (lua_State *L, gint pos, gchar *outbuf, gsize len)
{
	gint type;
	gsize r = 0;

	if (len == 0) {
		return 0;
	}

	type = lua_type (L, pos);

	switch (type) {
		case LUA_TNUMBER:
			r = lua_logger_out_num (L, pos, outbuf, len);
			break;
		case LUA_TBOOLEAN:
			r = lua_logger_out_boolean (L, pos, outbuf, len);
			break;
		case LUA_TTABLE:
			r = lua_logger_out_table (L, pos, outbuf, len);
			break;
		case LUA_TUSERDATA:
			r = lua_logger_out_userdata (L, pos, outbuf, len);
			break;
		case LUA_TFUNCTION:
			r = rspamd_snprintf (outbuf, len + 1, "function");
			break;
		case LUA_TNIL:
			r = rspamd_snprintf (outbuf, len + 1, "nil");
			break;
		case LUA_TNONE:
			r = rspamd_snprintf (outbuf, len + 1, "no value");
			break;
		default:
			/* Try to push everything as string using tostring magic */
			r = lua_logger_out_str (L, pos, outbuf, len);
			break;
	}

	return r;
}

static const gchar *
lua_logger_get_id (lua_State *L, gint pos)
{
	const gchar *uid = NULL, *clsname;

	if (lua_getmetatable (L, pos) != 0) {
		uid = "";
		lua_pushstring (L, "__index");
		lua_gettable (L, -2);

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

		clsname = lua_tostring (L, -1);

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

			if (task) {
				uid = task->task_pool->tag.uid;
			}
		}
		else if (strcmp (clsname, "rspamd{mempool}") == 0) {
			rspamd_mempool_t  *pool;

			pool = rspamd_lua_check_mempool (L, pos);

			if (pool) {
				uid = pool->tag.uid;
			}
		}
		else if (strcmp (clsname, "rspamd{config}") == 0) {
			struct rspamd_config *cfg;

			cfg = lua_check_config (L, pos);

			if (cfg) {
				uid = cfg->checksum;
			}
		}
		else if (strcmp (clsname, "rspamd{map}") == 0) {
			struct rspamd_lua_map *map;

			map = lua_check_map (L, pos);

			if (map) {
				if (map->map) {
					uid = map->map->tag;
				}
				else {
					uid = "embedded";
				}
			}
		}


		/* Metatable, __index, classname */
		lua_pop (L, 3);
	}

	return uid;
}

static gboolean
lua_logger_log_format (lua_State *L, gint fmt_pos, gboolean is_string,
		gchar *logbuf, gsize remain)
{
	gchar *d;
	const gchar *s, *c;
	gsize r, cpylen = 0;
	guint arg_num = 0, cur_arg;
	bool num_arg = false;
	enum {
		copy_char = 0,
		got_percent,
		parse_arg_num
	} state = copy_char;

	d = logbuf;
	s = lua_tostring (L, fmt_pos);
	c = s;
	cur_arg = fmt_pos;

	if (s == NULL) {
		return FALSE;
	}

	while (remain > 0 && *s != '\0') {
		switch (state) {
		case copy_char:
			if (*s == '%') {
				state = got_percent;
				s++;
				if (cpylen > 0) {
					memcpy (d, c, cpylen);
					d += cpylen;
				}
				cpylen = 0;
			}
			else {
				s++;
				cpylen ++;
				remain--;
			}
			break;
		case got_percent:
			if (g_ascii_isdigit (*s) || *s == 's') {
				state = parse_arg_num;
				c = s;
			}
			else {
				*d++ = *s++;
				c = s;
				state = copy_char;
			}
			break;
		case parse_arg_num:
			if (g_ascii_isdigit (*s)) {
				s++;
				num_arg = true;
			}
			else {
				if (num_arg) {
					arg_num = strtoul (c, NULL, 10);
					arg_num += fmt_pos - 1;
					/* Update the current argument */
					cur_arg = arg_num;
				}
				else {
					/* We have non numeric argument, e.g. %s */
					arg_num = cur_arg ++;
					s ++;
				}

				if (arg_num < 1 || arg_num > (guint) lua_gettop (L) + 1) {
					msg_err ("wrong argument number: %ud", arg_num);

					return FALSE;
				}

				r = lua_logger_out_type (L, arg_num + 1, d, remain);
				g_assert (r <= remain);
				remain -= r;
				d += r;
				state = copy_char;
				c = s;
			}
			break;
		}
	}

	if (state == parse_arg_num) {
		if (num_arg) {
			arg_num = strtoul (c, NULL, 10);
			arg_num += fmt_pos - 1;
		}
		else {
			/* We have non numeric argument, e.g. %s */
			arg_num = cur_arg;
		}

		if (arg_num < 1 || arg_num > (guint) lua_gettop (L) + 1) {
			msg_err ("wrong argument number: %ud", arg_num);

			return FALSE;
		}

		r = lua_logger_out_type (L, arg_num + 1, d, remain);
		g_assert (r <= remain);
		remain -= r;
		d += r;
	}
	else if (state == copy_char) {
		if (cpylen > 0 && remain > 0) {
			memcpy (d, c, cpylen);
			d += cpylen;
		}
	}

	*d = '\0';


	return TRUE;
}

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

	if (lua_type (L, start_pos) == LUA_TSTRING) {
		fmt_pos = start_pos;
	}
	else if (lua_type (L, start_pos) == LUA_TUSERDATA) {
		fmt_pos = start_pos + 1;

		uid = lua_logger_get_id (L, start_pos);

		if (uid == NULL) {
			return luaL_error (L, "bad userdata for logging");
		}
	}
	else {
		/* Bad argument type */
		msg_err ("bad format string type: %s", lua_typename (L, lua_type (L, 1)));
		return lua_error (L);
	}

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

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

			return 1;
		}
	}

	return 0;
}

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

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

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

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

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

static gint
lua_logger_logx (lua_State *L)
{
	LUA_TRACE_POINT;
	GLogLevelFlags flags = lua_tonumber (L, 1);

	return lua_logger_do_log (L, (flags & G_LOG_LEVEL_MASK), FALSE, 2);
}


static gint
lua_logger_debugm (lua_State *L)
{
	LUA_TRACE_POINT;
	gchar logbuf[RSPAMD_LOGBUF_SIZE - 128];
	const gchar *uid = NULL, *module = NULL;
	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);
	}

	if (uid && module && lua_type (L, 3) == LUA_TSTRING) {
		ret = lua_logger_log_format (L, 3, FALSE, logbuf, sizeof (logbuf) - 1);

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

	return 0;
}



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

/*** Init functions ***/

static gint
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);
}