You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_logger.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "lua_common.h"
  17. #include "libutil/map.h"
  18. #include "libutil/map_private.h"
  19. /***
  20. * @module rspamd_logger
  21. * Rspamd logger module is used to log messages from LUA API to the main rspamd logger.
  22. * It supports legacy and modern interfaces allowing highly customized an convenient log functions.
  23. * Here is an example of logger usage:
  24. * @example
  25. local rspamd_logger = require "rspamd_logger"
  26. local a = 'string'
  27. local b = 1.5
  28. local c = 1
  29. local d = {
  30. 'aa',
  31. 1,
  32. 'bb'
  33. }
  34. local e = {
  35. key = 'value',
  36. key2 = 1.0
  37. }
  38. -- New extended interface
  39. -- %<number> means numeric arguments and %s means the next argument
  40. -- for example %1, %2, %s: %s would mean the third argument
  41. rspamd_logger.infox('a=%1, b=%2, c=%3, d=%4, e=%s', a, b, c, d, e)
  42. -- Output: a=string, b=1.50000, c=1, d={[1] = aa, [2] = 1, [3] = bb} e={[key]=value, [key2]=1.0}
  43. -- Legacy interface (can handle merely strings)
  44. rspamd_logger.info('Old stupid API')
  45. -- Create string using logger API
  46. local str = rspamd_logger.slog('a=%1, b=%2, c=%3, d=%4, e=%5', a, b, c, d, e)
  47. print(str)
  48. -- Output: a=string, b=1.50000, c=1, d={[1] = aa, [2] = 1, [3] = bb} e={[key]=value, [key2]=1.0}
  49. */
  50. /* Logger methods */
  51. /***
  52. * @function logger.err(msg)
  53. * Log message as an error
  54. * @param {string} msg string to be logged
  55. */
  56. LUA_FUNCTION_DEF (logger, err);
  57. /***
  58. * @function logger.warn(msg)
  59. * Log message as a warning
  60. * @param {string} msg string to be logged
  61. */
  62. LUA_FUNCTION_DEF (logger, warn);
  63. /***
  64. * @function logger.info(msg)
  65. * Log message as an informational message
  66. * @param {string} msg string to be logged
  67. */
  68. LUA_FUNCTION_DEF (logger, info);
  69. /***
  70. * @function logger.message(msg)
  71. * Log message as an notice message
  72. * @param {string} msg string to be logged
  73. */
  74. LUA_FUNCTION_DEF (logger, message);
  75. /***
  76. * @function logger.debug(msg)
  77. * Log message as a debug message
  78. * @param {string} msg string to be logged
  79. */
  80. LUA_FUNCTION_DEF (logger, debug);
  81. /***
  82. * @function logger.errx(fmt[, args)
  83. * Extended interface to make an error log message
  84. * @param {string} fmt format string, arguments are encoded as %<number>
  85. * @param {any} args list of arguments to be replaced in %<number> positions
  86. */
  87. LUA_FUNCTION_DEF (logger, errx);
  88. /***
  89. * @function logger.warn(fmt[, args)
  90. * Extended interface to make a warning log message
  91. * @param {string} fmt format string, arguments are encoded as %<number>
  92. * @param {any} args list of arguments to be replaced in %<number> positions
  93. */
  94. LUA_FUNCTION_DEF (logger, warnx);
  95. /***
  96. * @function logger.infox(fmt[, args)
  97. * Extended interface to make an informational log message
  98. * @param {string} fmt format string, arguments are encoded as %<number>
  99. * @param {any} args list of arguments to be replaced in %<number> positions
  100. */
  101. LUA_FUNCTION_DEF (logger, infox);
  102. /***
  103. * @function logger.infox(fmt[, args)
  104. * Extended interface to make an informational log message
  105. * @param {string} fmt format string, arguments are encoded as %<number>
  106. * @param {any} args list of arguments to be replaced in %<number> positions
  107. */
  108. LUA_FUNCTION_DEF (logger, messagex);
  109. /***
  110. * @function logger.debugx(fmt[, args)
  111. * Extended interface to make a debug log message
  112. * @param {string} fmt format string, arguments are encoded as %<number>
  113. * @param {any} args list of arguments to be replaced in %<number> positions
  114. */
  115. LUA_FUNCTION_DEF (logger, debugx);
  116. /***
  117. * @function logger.debugm(module, id, fmt[, args)
  118. * Extended interface to make a debug log message
  119. * @param {string} module debug module
  120. * @param {task|cfg|pool|string} id id to log
  121. * @param {string} fmt format string, arguments are encoded as %<number>
  122. * @param {any} args list of arguments to be replaced in %<number> positions
  123. */
  124. LUA_FUNCTION_DEF (logger, debugm);
  125. /***
  126. * @function logger.slog(fmt[, args)
  127. * Create string replacing percent params with corresponding arguments
  128. * @param {string} fmt format string, arguments are encoded as %<number>
  129. * @param {any} args list of arguments to be replaced in %<number> positions
  130. * @return {string} string with percent parameters substituted
  131. */
  132. LUA_FUNCTION_DEF (logger, slog);
  133. static const struct luaL_reg loggerlib_f[] = {
  134. LUA_INTERFACE_DEF (logger, err),
  135. LUA_INTERFACE_DEF (logger, warn),
  136. LUA_INTERFACE_DEF (logger, message),
  137. {"msg", lua_logger_message},
  138. LUA_INTERFACE_DEF (logger, info),
  139. LUA_INTERFACE_DEF (logger, debug),
  140. LUA_INTERFACE_DEF (logger, errx),
  141. LUA_INTERFACE_DEF (logger, warnx),
  142. LUA_INTERFACE_DEF (logger, infox),
  143. LUA_INTERFACE_DEF (logger, messagex),
  144. {"msgx", lua_logger_messagex},
  145. LUA_INTERFACE_DEF (logger, debugx),
  146. LUA_INTERFACE_DEF (logger, debugm),
  147. LUA_INTERFACE_DEF (logger, slog),
  148. {"__tostring", rspamd_lua_class_tostring},
  149. {NULL, NULL}
  150. };
  151. static void
  152. lua_common_log_line (GLogLevelFlags level, lua_State *L,
  153. const gchar *msg, const gchar *uid, const gchar *module)
  154. {
  155. lua_Debug d;
  156. gchar func_buf[128], *p;
  157. if (lua_getstack (L, 1, &d) == 1) {
  158. (void) lua_getinfo (L, "Sl", &d);
  159. if ((p = strrchr (d.short_src, '/')) == NULL) {
  160. p = d.short_src;
  161. }
  162. else {
  163. p++;
  164. }
  165. if (strlen (p) > 30) {
  166. rspamd_snprintf (func_buf, sizeof (func_buf), "%27s...:%d", p,
  167. d.currentline);
  168. }
  169. else {
  170. rspamd_snprintf (func_buf, sizeof (func_buf), "%s:%d", p,
  171. d.currentline);
  172. }
  173. if (level == G_LOG_LEVEL_DEBUG) {
  174. rspamd_conditional_debug (NULL,
  175. NULL,
  176. module,
  177. uid,
  178. func_buf,
  179. "%s",
  180. msg);
  181. }
  182. else {
  183. rspamd_common_log_function (NULL,
  184. level,
  185. module,
  186. uid,
  187. func_buf,
  188. "%s",
  189. msg);
  190. }
  191. }
  192. else {
  193. if (level == G_LOG_LEVEL_DEBUG) {
  194. rspamd_conditional_debug (NULL,
  195. NULL,
  196. module,
  197. uid,
  198. G_STRFUNC,
  199. "%s",
  200. msg);
  201. }
  202. else {
  203. rspamd_common_log_function (NULL,
  204. level,
  205. module,
  206. uid,
  207. G_STRFUNC,
  208. "%s",
  209. msg);
  210. }
  211. }
  212. }
  213. /*** Logger interface ***/
  214. static gint
  215. lua_logger_err (lua_State *L)
  216. {
  217. const gchar *msg;
  218. msg = luaL_checkstring (L, 1);
  219. lua_common_log_line (G_LOG_LEVEL_CRITICAL, L, msg, NULL, NULL);
  220. return 0;
  221. }
  222. static gint
  223. lua_logger_warn (lua_State *L)
  224. {
  225. const gchar *msg;
  226. msg = luaL_checkstring (L, 1);
  227. lua_common_log_line (G_LOG_LEVEL_WARNING, L, msg, NULL, NULL);
  228. return 0;
  229. }
  230. static gint
  231. lua_logger_info (lua_State *L)
  232. {
  233. const gchar *msg;
  234. msg = luaL_checkstring (L, 1);
  235. lua_common_log_line (G_LOG_LEVEL_INFO, L, msg, NULL, NULL);
  236. return 0;
  237. }
  238. static gint
  239. lua_logger_message (lua_State *L)
  240. {
  241. const gchar *msg;
  242. msg = luaL_checkstring (L, 1);
  243. lua_common_log_line (G_LOG_LEVEL_MESSAGE, L, msg, NULL, NULL);
  244. return 0;
  245. }
  246. static gint
  247. lua_logger_debug (lua_State *L)
  248. {
  249. const gchar *msg;
  250. msg = luaL_checkstring (L, 1);
  251. lua_common_log_line (G_LOG_LEVEL_DEBUG, L, msg, NULL, NULL);
  252. return 0;
  253. }
  254. static gsize
  255. lua_logger_out_str (lua_State *L, gint pos, gchar *outbuf, gsize len)
  256. {
  257. gsize slen;
  258. const gchar *str = lua_tolstring (L, pos, &slen);
  259. gsize r = 0;
  260. if (str) {
  261. r = rspamd_strlcpy (outbuf, str, MIN (slen, len) + 1);
  262. }
  263. return r;
  264. }
  265. static gsize
  266. lua_logger_out_num (lua_State *L, gint pos, gchar *outbuf, gsize len)
  267. {
  268. gdouble num = lua_tonumber (L, pos);
  269. glong inum;
  270. gsize r = 0;
  271. if ((gdouble) (glong) num == num) {
  272. inum = num;
  273. r = rspamd_snprintf (outbuf, len + 1, "%l", inum);
  274. }
  275. else {
  276. r = rspamd_snprintf (outbuf, len + 1, "%f", num);
  277. }
  278. return r;
  279. }
  280. static gsize
  281. lua_logger_out_boolean (lua_State *L, gint pos, gchar *outbuf, gsize len)
  282. {
  283. gboolean val = lua_toboolean (L, pos);
  284. gsize r = 0;
  285. r = rspamd_strlcpy (outbuf, val ? "true" : "false", len + 1);
  286. return r;
  287. }
  288. static gsize
  289. lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len)
  290. {
  291. gint r, top;
  292. const gchar *str = NULL;
  293. gboolean converted_to_str = FALSE;
  294. top = lua_gettop (L);
  295. if (!lua_getmetatable (L, pos)) {
  296. return 0;
  297. }
  298. lua_pushstring (L, "__index");
  299. lua_gettable (L, -2);
  300. if (!lua_istable (L, -1)) {
  301. lua_settop (L, top);
  302. return 0;
  303. }
  304. lua_pushstring (L, "__tostring");
  305. lua_gettable (L, -2);
  306. if (lua_isfunction (L, -1)) {
  307. lua_pushvalue (L, pos);
  308. if (lua_pcall (L, 1, 1, 0) != 0) {
  309. lua_settop (L, top);
  310. return 0;
  311. }
  312. str = lua_tostring (L, -1);
  313. if (str) {
  314. converted_to_str = TRUE;
  315. }
  316. }
  317. else {
  318. lua_pushstring (L, "class");
  319. lua_gettable (L, -2);
  320. if (!lua_isstring (L, -1)) {
  321. lua_settop (L, top);
  322. return 0;
  323. }
  324. str = lua_tostring (L, -1);
  325. }
  326. if (converted_to_str) {
  327. r = rspamd_snprintf (outbuf, len + 1, "%s", str);
  328. }
  329. else {
  330. /* Print raw pointer */
  331. r = rspamd_snprintf (outbuf, len + 1, "%s(%p)", str, lua_touserdata (L, pos));
  332. }
  333. lua_settop (L, top);
  334. return r;
  335. }
  336. #define MOVE_BUF(d, remain, r) \
  337. (d) += (r); (remain) -= (r); \
  338. if ((remain) == 0) { lua_pop (L, 1); break; }
  339. static gsize
  340. lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len)
  341. {
  342. gchar *d = outbuf;
  343. gsize remain = len, r;
  344. gboolean first = TRUE;
  345. gint i;
  346. if (!lua_istable (L, pos) || remain == 0) {
  347. return 0;
  348. }
  349. lua_pushvalue (L, pos);
  350. r = rspamd_snprintf (d, remain + 1, "{");
  351. remain -= r;
  352. d += r;
  353. /* Get numeric keys (ipairs) */
  354. for (i = 1; ; i++) {
  355. lua_rawgeti (L, -1, i);
  356. if (lua_isnil (L, -1)) {
  357. lua_pop (L, 1);
  358. break;
  359. }
  360. if (!first) {
  361. r = rspamd_snprintf (d, remain + 1, ", ");
  362. MOVE_BUF(d, remain, r);
  363. }
  364. r = rspamd_snprintf (d, remain + 1, "[%d] = ", i);
  365. MOVE_BUF(d, remain, r);
  366. r = lua_logger_out_type (L, lua_gettop (L), d, remain);
  367. MOVE_BUF(d, remain, r);
  368. first = FALSE;
  369. lua_pop (L, 1);
  370. }
  371. /* Get string keys (pairs) */
  372. for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
  373. /* 'key' is at index -2 and 'value' is at index -1 */
  374. if (lua_type (L, -2) == LUA_TNUMBER) {
  375. continue;
  376. }
  377. if (!first) {
  378. r = rspamd_snprintf (d, remain + 1, ", ");
  379. MOVE_BUF(d, remain, r);
  380. }
  381. r = rspamd_snprintf (d, remain + 1, "[%s] = ",
  382. lua_tostring (L, -2));
  383. MOVE_BUF(d, remain, r);
  384. r = lua_logger_out_type (L, lua_gettop (L), d, remain);
  385. MOVE_BUF(d, remain, r);
  386. first = FALSE;
  387. }
  388. lua_pop (L, 1);
  389. r = rspamd_snprintf (d, remain + 1, "}");
  390. d += r;
  391. return (d - outbuf);
  392. }
  393. #undef MOVE_BUF
  394. gsize
  395. lua_logger_out_type (lua_State *L, gint pos, gchar *outbuf, gsize len)
  396. {
  397. gint type;
  398. gsize r = 0;
  399. if (len == 0) {
  400. return 0;
  401. }
  402. type = lua_type (L, pos);
  403. switch (type) {
  404. case LUA_TNUMBER:
  405. r = lua_logger_out_num (L, pos, outbuf, len);
  406. break;
  407. case LUA_TBOOLEAN:
  408. r = lua_logger_out_boolean (L, pos, outbuf, len);
  409. break;
  410. case LUA_TTABLE:
  411. r = lua_logger_out_table (L, pos, outbuf, len);
  412. break;
  413. case LUA_TUSERDATA:
  414. r = lua_logger_out_userdata (L, pos, outbuf, len);
  415. break;
  416. case LUA_TFUNCTION:
  417. r = rspamd_snprintf (outbuf, len + 1, "function");
  418. break;
  419. case LUA_TNIL:
  420. r = rspamd_snprintf (outbuf, len + 1, "nil");
  421. break;
  422. case LUA_TNONE:
  423. r = rspamd_snprintf (outbuf, len + 1, "no value");
  424. break;
  425. default:
  426. /* Try to push everything as string using tostring magic */
  427. r = lua_logger_out_str (L, pos, outbuf, len);
  428. break;
  429. }
  430. return r;
  431. }
  432. static const gchar *
  433. lua_logger_get_id (lua_State *L, gint pos)
  434. {
  435. const gchar *uid = NULL, *clsname;
  436. if (lua_getmetatable (L, pos) != 0) {
  437. uid = "";
  438. lua_pushstring (L, "__index");
  439. lua_gettable (L, -2);
  440. lua_pushstring (L, "class");
  441. lua_gettable (L, -2);
  442. clsname = lua_tostring (L, -1);
  443. if (strcmp (clsname, "rspamd{task}") == 0) {
  444. struct rspamd_task *task = lua_check_task (L, pos);
  445. if (task) {
  446. uid = task->task_pool->tag.uid;
  447. }
  448. }
  449. else if (strcmp (clsname, "rspamd{mempool}") == 0) {
  450. rspamd_mempool_t *pool;
  451. pool = rspamd_lua_check_mempool (L, pos);
  452. if (pool) {
  453. uid = pool->tag.uid;
  454. }
  455. }
  456. else if (strcmp (clsname, "rspamd{config}") == 0) {
  457. struct rspamd_config *cfg;
  458. cfg = lua_check_config (L, pos);
  459. if (cfg) {
  460. uid = cfg->checksum;
  461. }
  462. }
  463. else if (strcmp (clsname, "rspamd{map}") == 0) {
  464. struct rspamd_lua_map *map;
  465. map = lua_check_map (L, pos);
  466. if (map) {
  467. if (map->map) {
  468. uid = map->map->tag;
  469. }
  470. else {
  471. uid = "embedded";
  472. }
  473. }
  474. }
  475. /* Metatable, __index, classname */
  476. lua_pop (L, 3);
  477. }
  478. return uid;
  479. }
  480. static gboolean
  481. lua_logger_log_format (lua_State *L, gint fmt_pos, gboolean is_string,
  482. gchar *logbuf, gsize remain)
  483. {
  484. gchar *d;
  485. const gchar *s, *c;
  486. gsize r, cpylen = 0;
  487. guint arg_num = 0, cur_arg;
  488. bool num_arg = false;
  489. enum {
  490. copy_char = 0,
  491. got_percent,
  492. parse_arg_num
  493. } state = copy_char;
  494. d = logbuf;
  495. s = lua_tostring (L, fmt_pos);
  496. c = s;
  497. cur_arg = fmt_pos;
  498. if (s == NULL) {
  499. return FALSE;
  500. }
  501. while (remain > 0 && *s != '\0') {
  502. switch (state) {
  503. case copy_char:
  504. if (*s == '%') {
  505. state = got_percent;
  506. s++;
  507. if (cpylen > 0) {
  508. memcpy (d, c, cpylen);
  509. d += cpylen;
  510. }
  511. cpylen = 0;
  512. }
  513. else {
  514. s++;
  515. cpylen ++;
  516. remain--;
  517. }
  518. break;
  519. case got_percent:
  520. if (g_ascii_isdigit (*s) || *s == 's') {
  521. state = parse_arg_num;
  522. c = s;
  523. }
  524. else {
  525. *d++ = *s++;
  526. c = s;
  527. state = copy_char;
  528. }
  529. break;
  530. case parse_arg_num:
  531. if (g_ascii_isdigit (*s)) {
  532. s++;
  533. num_arg = true;
  534. }
  535. else {
  536. if (num_arg) {
  537. arg_num = strtoul (c, NULL, 10);
  538. arg_num += fmt_pos - 1;
  539. /* Update the current argument */
  540. cur_arg = arg_num;
  541. }
  542. else {
  543. /* We have non numeric argument, e.g. %s */
  544. arg_num = cur_arg ++;
  545. s ++;
  546. }
  547. if (arg_num < 1 || arg_num > (guint) lua_gettop (L) + 1) {
  548. msg_err ("wrong argument number: %ud", arg_num);
  549. return FALSE;
  550. }
  551. r = lua_logger_out_type (L, arg_num + 1, d, remain);
  552. g_assert (r <= remain);
  553. remain -= r;
  554. d += r;
  555. state = copy_char;
  556. c = s;
  557. }
  558. break;
  559. }
  560. }
  561. if (state == parse_arg_num) {
  562. if (num_arg) {
  563. arg_num = strtoul (c, NULL, 10);
  564. arg_num += fmt_pos - 1;
  565. }
  566. else {
  567. /* We have non numeric argument, e.g. %s */
  568. arg_num = cur_arg;
  569. }
  570. if (arg_num < 1 || arg_num > (guint) lua_gettop (L) + 1) {
  571. msg_err ("wrong argument number: %ud", arg_num);
  572. return FALSE;
  573. }
  574. r = lua_logger_out_type (L, arg_num + 1, d, remain);
  575. g_assert (r <= remain);
  576. remain -= r;
  577. d += r;
  578. }
  579. else if (state == copy_char) {
  580. if (cpylen > 0 && remain > 0) {
  581. memcpy (d, c, cpylen);
  582. d += cpylen;
  583. }
  584. }
  585. *d = '\0';
  586. return TRUE;
  587. }
  588. static gint
  589. lua_logger_logx (lua_State *L, GLogLevelFlags level, gboolean is_string)
  590. {
  591. gchar logbuf[RSPAMD_LOGBUF_SIZE - 128];
  592. const gchar *uid = NULL;
  593. gint fmt_pos = 1;
  594. gboolean ret;
  595. if (lua_type (L, 1) == LUA_TSTRING) {
  596. fmt_pos = 1;
  597. }
  598. else if (lua_type (L, 1) == LUA_TUSERDATA) {
  599. fmt_pos = 2;
  600. uid = lua_logger_get_id (L, 1);
  601. if (uid == NULL) {
  602. return luaL_error (L, "bad userdata for logging");
  603. }
  604. }
  605. else {
  606. /* Bad argument type */
  607. msg_err ("bad format string type: %s", lua_typename (L, lua_type (L,
  608. 1)));
  609. lua_error (L);
  610. return 0;
  611. }
  612. ret = lua_logger_log_format (L, fmt_pos, is_string,
  613. logbuf, sizeof (logbuf) - 1);
  614. if (ret) {
  615. if (is_string) {
  616. lua_pushstring (L, logbuf);
  617. return 1;
  618. }
  619. else {
  620. lua_common_log_line (level, L, logbuf, uid, "lua");
  621. }
  622. }
  623. else {
  624. if (is_string) {
  625. lua_pushnil (L);
  626. return 1;
  627. }
  628. }
  629. return 0;
  630. }
  631. static gint
  632. lua_logger_errx (lua_State *L)
  633. {
  634. return lua_logger_logx (L, G_LOG_LEVEL_CRITICAL, FALSE);
  635. }
  636. static gint
  637. lua_logger_warnx (lua_State *L)
  638. {
  639. return lua_logger_logx (L, G_LOG_LEVEL_WARNING, FALSE);
  640. }
  641. static gint
  642. lua_logger_infox (lua_State *L)
  643. {
  644. return lua_logger_logx (L, G_LOG_LEVEL_INFO, FALSE);
  645. }
  646. static gint
  647. lua_logger_messagex (lua_State *L)
  648. {
  649. return lua_logger_logx (L, G_LOG_LEVEL_MESSAGE, FALSE);
  650. }
  651. static gint
  652. lua_logger_debugx (lua_State *L)
  653. {
  654. return lua_logger_logx (L, G_LOG_LEVEL_DEBUG, FALSE);
  655. }
  656. static gint
  657. lua_logger_debugm (lua_State *L)
  658. {
  659. gchar logbuf[RSPAMD_LOGBUF_SIZE - 128];
  660. const gchar *uid = NULL, *module = NULL;
  661. gboolean ret;
  662. module = luaL_checkstring (L, 1);
  663. if (lua_type (L, 2) == LUA_TSTRING) {
  664. uid = luaL_checkstring (L, 2);
  665. }
  666. else {
  667. uid = lua_logger_get_id (L, 2);
  668. }
  669. if (uid && module && lua_type (L, 3) == LUA_TSTRING) {
  670. ret = lua_logger_log_format (L, 3, FALSE, logbuf, sizeof (logbuf) - 1);
  671. if (ret) {
  672. lua_common_log_line (G_LOG_LEVEL_DEBUG, L, logbuf, uid, module);
  673. }
  674. }
  675. else {
  676. return luaL_error (L, "invalid arguments");
  677. }
  678. return 0;
  679. }
  680. static gint
  681. lua_logger_slog (lua_State *L)
  682. {
  683. return lua_logger_logx (L, 0, TRUE);
  684. }
  685. /*** Init functions ***/
  686. static gint
  687. lua_load_logger (lua_State *L)
  688. {
  689. lua_newtable (L);
  690. luaL_register (L, NULL, loggerlib_f);
  691. return 1;
  692. }
  693. void
  694. luaopen_logger (lua_State *L)
  695. {
  696. rspamd_lua_add_preload (L, "rspamd_logger", lua_load_logger);
  697. }