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 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. * Copyright 2024 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 "libserver/maps/map.h"
  18. #include "libserver/maps/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.info('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. -- Create string using logger API
  44. local str = rspamd_logger.slog('a=%1, b=%2, c=%3, d=%4, e=%5', a, b, c, d, e)
  45. print(str)
  46. -- Output: a=string, b=1.50000, c=1, d={[1] = aa, [2] = 1, [3] = bb} e={[key]=value, [key2]=1.0}
  47. */
  48. /* Logger methods */
  49. /***
  50. * @function logger.err(msg)
  51. * Log message as an error
  52. * @param {string} msg string to be logged
  53. */
  54. LUA_FUNCTION_DEF(logger, err);
  55. /***
  56. * @function logger.warn(msg)
  57. * Log message as a warning
  58. * @param {string} msg string to be logged
  59. */
  60. LUA_FUNCTION_DEF(logger, warn);
  61. /***
  62. * @function logger.info(msg)
  63. * Log message as an informational message
  64. * @param {string} msg string to be logged
  65. */
  66. LUA_FUNCTION_DEF(logger, info);
  67. /***
  68. * @function logger.message(msg)
  69. * Log message as an notice message
  70. * @param {string} msg string to be logged
  71. */
  72. LUA_FUNCTION_DEF(logger, message);
  73. /***
  74. * @function logger.debug(msg)
  75. * Log message as a debug message
  76. * @param {string} msg string to be logged
  77. */
  78. LUA_FUNCTION_DEF(logger, debug);
  79. /***
  80. * @function logger.errx(fmt[, args)
  81. * Extended interface to make an error log message
  82. * @param {string} fmt format string, arguments are encoded as %<number>
  83. * @param {any} args list of arguments to be replaced in %<number> positions
  84. */
  85. LUA_FUNCTION_DEF(logger, errx);
  86. /***
  87. * @function logger.warn(fmt[, args)
  88. * Extended interface to make a warning log message
  89. * @param {string} fmt format string, arguments are encoded as %<number>
  90. * @param {any} args list of arguments to be replaced in %<number> positions
  91. */
  92. LUA_FUNCTION_DEF(logger, warnx);
  93. /***
  94. * @function logger.infox(fmt[, args)
  95. * Extended interface to make an informational log message
  96. * @param {string} fmt format string, arguments are encoded as %<number>
  97. * @param {any} args list of arguments to be replaced in %<number> positions
  98. */
  99. LUA_FUNCTION_DEF(logger, infox);
  100. /***
  101. * @function logger.infox(fmt[, args)
  102. * Extended interface to make an informational log message
  103. * @param {string} fmt format string, arguments are encoded as %<number>
  104. * @param {any} args list of arguments to be replaced in %<number> positions
  105. */
  106. LUA_FUNCTION_DEF(logger, messagex);
  107. /***
  108. * @function logger.debugx(fmt[, args)
  109. * Extended interface to make a debug log message
  110. * @param {string} fmt format string, arguments are encoded as %<number>
  111. * @param {any} args list of arguments to be replaced in %<number> positions
  112. */
  113. LUA_FUNCTION_DEF(logger, debugx);
  114. /***
  115. * @function logger.debugm(module, id, fmt[, args)
  116. * Extended interface to make a debug log message
  117. * @param {string} module debug module
  118. * @param {task|cfg|pool|string} id id to log
  119. * @param {string} fmt format string, arguments are encoded as %<number>
  120. * @param {any} args list of arguments to be replaced in %<number> positions
  121. */
  122. LUA_FUNCTION_DEF(logger, debugm);
  123. /***
  124. * @function logger.slog(fmt[, args)
  125. * Create string replacing percent params with corresponding arguments
  126. * @param {string} fmt format string, arguments are encoded as %<number>
  127. * @param {any} args list of arguments to be replaced in %<number> positions
  128. * @return {string} string with percent parameters substituted
  129. */
  130. LUA_FUNCTION_DEF(logger, slog);
  131. /***
  132. * @function logger.logx(level, module, id, fmt[, args)
  133. * Extended interface to make a generic log message on any level
  134. * @param {number} log level as a number (see GLogLevelFlags enum for values)
  135. * @param {task|cfg|pool|string} id id to log
  136. * @param {string} fmt format string, arguments are encoded as %<number>
  137. * @param {any} args list of arguments to be replaced in %<number> positions
  138. */
  139. LUA_FUNCTION_DEF(logger, logx);
  140. /***
  141. * @function logger.log_level()
  142. * Returns log level for a logger
  143. * @return {string} current log level
  144. */
  145. LUA_FUNCTION_DEF(logger, log_level);
  146. static const struct luaL_reg loggerlib_f[] = {
  147. LUA_INTERFACE_DEF(logger, err),
  148. LUA_INTERFACE_DEF(logger, warn),
  149. LUA_INTERFACE_DEF(logger, message),
  150. {"msg", lua_logger_message},
  151. LUA_INTERFACE_DEF(logger, info),
  152. LUA_INTERFACE_DEF(logger, debug),
  153. LUA_INTERFACE_DEF(logger, errx),
  154. LUA_INTERFACE_DEF(logger, warnx),
  155. LUA_INTERFACE_DEF(logger, infox),
  156. LUA_INTERFACE_DEF(logger, messagex),
  157. {"msgx", lua_logger_messagex},
  158. LUA_INTERFACE_DEF(logger, debugx),
  159. LUA_INTERFACE_DEF(logger, debugm),
  160. LUA_INTERFACE_DEF(logger, slog),
  161. LUA_INTERFACE_DEF(logger, logx),
  162. LUA_INTERFACE_DEF(logger, log_level),
  163. {"__tostring", rspamd_lua_class_tostring},
  164. {NULL, NULL}};
  165. static void
  166. lua_common_log_line(GLogLevelFlags level,
  167. lua_State *L,
  168. const char *msg,
  169. const char *uid,
  170. const char *module,
  171. int stack_level)
  172. {
  173. lua_Debug d;
  174. char func_buf[128], *p;
  175. if (lua_getstack(L, stack_level, &d) == 1) {
  176. (void) lua_getinfo(L, "Sl", &d);
  177. if ((p = strrchr(d.short_src, '/')) == NULL) {
  178. p = d.short_src;
  179. }
  180. else {
  181. p++;
  182. }
  183. if (strlen(p) > 30) {
  184. rspamd_snprintf(func_buf, sizeof(func_buf), "%27s...:%d", p,
  185. d.currentline);
  186. }
  187. else {
  188. rspamd_snprintf(func_buf, sizeof(func_buf), "%s:%d", p,
  189. d.currentline);
  190. }
  191. rspamd_common_log_function(NULL,
  192. level,
  193. module,
  194. uid,
  195. func_buf,
  196. "%s",
  197. msg);
  198. }
  199. else {
  200. rspamd_common_log_function(NULL,
  201. level,
  202. module,
  203. uid,
  204. G_STRFUNC,
  205. "%s",
  206. msg);
  207. }
  208. }
  209. /*** Logger interface ***/
  210. static int
  211. lua_logger_err(lua_State *L)
  212. {
  213. return lua_logger_errx(L);
  214. }
  215. static int
  216. lua_logger_warn(lua_State *L)
  217. {
  218. return lua_logger_warnx(L);
  219. }
  220. static int
  221. lua_logger_info(lua_State *L)
  222. {
  223. return lua_logger_infox(L);
  224. }
  225. static int
  226. lua_logger_message(lua_State *L)
  227. {
  228. return lua_logger_messagex(L);
  229. }
  230. static int
  231. lua_logger_debug(lua_State *L)
  232. {
  233. return lua_logger_debugx(L);
  234. }
  235. static inline bool
  236. lua_logger_char_safe(int t, unsigned int esc_type)
  237. {
  238. if (t & 0x80) {
  239. if (esc_type & LUA_ESCAPE_8BIT) {
  240. return false;
  241. }
  242. return true;
  243. }
  244. if (esc_type & LUA_ESCAPE_UNPRINTABLE) {
  245. if (!g_ascii_isprint(t) && !g_ascii_isspace(t)) {
  246. return false;
  247. }
  248. }
  249. if (esc_type & LUA_ESCAPE_NEWLINES) {
  250. if (t == '\r' || t == '\n') {
  251. return false;
  252. }
  253. }
  254. return true;
  255. }
  256. static gsize
  257. lua_logger_out_str(lua_State *L, int pos,
  258. char *outbuf, gsize len,
  259. struct lua_logger_trace *trace,
  260. enum lua_logger_escape_type esc_type)
  261. {
  262. gsize slen, flen;
  263. const char *str = lua_tolstring(L, pos, &slen);
  264. static const char hexdigests[16] = "0123456789abcdef";
  265. gsize r = 0, s;
  266. if (str) {
  267. gboolean normal = TRUE;
  268. flen = MIN(slen, len - 1);
  269. for (r = 0; r < flen; r++) {
  270. if (!lua_logger_char_safe(str[r], esc_type)) {
  271. normal = FALSE;
  272. break;
  273. }
  274. }
  275. if (normal) {
  276. r = rspamd_strlcpy(outbuf, str, flen + 1);
  277. }
  278. else {
  279. /* Need to escape non-printed characters */
  280. r = 0;
  281. s = 0;
  282. while (slen > 0 && len > 1) {
  283. if (!lua_logger_char_safe(str[s], esc_type)) {
  284. if (len >= 3) {
  285. outbuf[r++] = '\\';
  286. outbuf[r++] = hexdigests[((str[s] >> 4) & 0xF)];
  287. outbuf[r++] = hexdigests[((str[s]) & 0xF)];
  288. len -= 2;
  289. }
  290. else {
  291. outbuf[r++] = '?';
  292. }
  293. }
  294. else {
  295. outbuf[r++] = str[s];
  296. }
  297. s++;
  298. slen--;
  299. len--;
  300. }
  301. outbuf[r] = '\0';
  302. }
  303. }
  304. return r;
  305. }
  306. static gsize
  307. lua_logger_out_num(lua_State *L, int pos, char *outbuf, gsize len,
  308. struct lua_logger_trace *trace)
  309. {
  310. double num = lua_tonumber(L, pos);
  311. glong inum;
  312. gsize r = 0;
  313. if ((double) (glong) num == num) {
  314. inum = num;
  315. r = rspamd_snprintf(outbuf, len + 1, "%l", inum);
  316. }
  317. else {
  318. r = rspamd_snprintf(outbuf, len + 1, "%f", num);
  319. }
  320. return r;
  321. }
  322. static gsize
  323. lua_logger_out_boolean(lua_State *L, int pos, char *outbuf, gsize len,
  324. struct lua_logger_trace *trace)
  325. {
  326. gboolean val = lua_toboolean(L, pos);
  327. gsize r = 0;
  328. r = rspamd_strlcpy(outbuf, val ? "true" : "false", len + 1);
  329. return r;
  330. }
  331. static gsize
  332. lua_logger_out_userdata(lua_State *L, int pos, char *outbuf, gsize len,
  333. struct lua_logger_trace *trace)
  334. {
  335. int r = 0, top;
  336. const char *str = NULL;
  337. gboolean converted_to_str = FALSE;
  338. top = lua_gettop(L);
  339. if (!lua_getmetatable(L, pos)) {
  340. return 0;
  341. }
  342. lua_pushstring(L, "__index");
  343. lua_gettable(L, -2);
  344. if (!lua_istable(L, -1)) {
  345. if (lua_isfunction(L, -1)) {
  346. /* Functional metatable, try to get __tostring directly */
  347. lua_pushstring(L, "__tostring");
  348. lua_gettable(L, -3);
  349. if (lua_isfunction(L, -1)) {
  350. lua_pushvalue(L, pos);
  351. if (lua_pcall(L, 1, 1, 0) != 0) {
  352. lua_settop(L, top);
  353. return 0;
  354. }
  355. str = lua_tostring(L, -1);
  356. if (str) {
  357. r = rspamd_snprintf(outbuf, len, "%s", str);
  358. }
  359. lua_settop(L, top);
  360. return r;
  361. }
  362. }
  363. lua_settop(L, top);
  364. return 0;
  365. }
  366. lua_pushstring(L, "__tostring");
  367. lua_gettable(L, -2);
  368. if (lua_isfunction(L, -1)) {
  369. lua_pushvalue(L, pos);
  370. if (lua_pcall(L, 1, 1, 0) != 0) {
  371. lua_settop(L, top);
  372. return 0;
  373. }
  374. str = lua_tostring(L, -1);
  375. if (str) {
  376. converted_to_str = TRUE;
  377. }
  378. }
  379. else {
  380. lua_pop(L, 1);
  381. lua_pushstring(L, "class");
  382. lua_gettable(L, -2);
  383. if (lua_isstring(L, -1)) {
  384. str = lua_tostring(L, -1);
  385. converted_to_str = TRUE;
  386. }
  387. }
  388. if (converted_to_str) {
  389. r = rspamd_snprintf(outbuf, len, "%s", str);
  390. }
  391. else {
  392. /* Print raw pointer */
  393. r = rspamd_snprintf(outbuf, len, "%s(%p)", str, lua_touserdata(L, pos));
  394. }
  395. lua_settop(L, top);
  396. return r;
  397. }
  398. #define MOVE_BUF(d, remain, r) \
  399. (d) += (r); \
  400. (remain) -= (r); \
  401. if ((remain) == 0) { \
  402. lua_settop(L, old_top); \
  403. break; \
  404. }
  405. static gsize
  406. lua_logger_out_table(lua_State *L, int pos, char *outbuf, gsize len,
  407. struct lua_logger_trace *trace,
  408. enum lua_logger_escape_type esc_type)
  409. {
  410. char *d = outbuf;
  411. gsize remain = len, r;
  412. gboolean first = TRUE;
  413. gconstpointer self = NULL;
  414. int i, tpos, last_seq = -1, old_top;
  415. if (!lua_istable(L, pos) || remain == 0) {
  416. return 0;
  417. }
  418. old_top = lua_gettop(L);
  419. self = lua_topointer(L, pos);
  420. /* Check if we have seen this pointer */
  421. for (i = 0; i < TRACE_POINTS; i++) {
  422. if (trace->traces[i] == self) {
  423. r = rspamd_snprintf(d, remain + 1, "ref(%p)", self);
  424. d += r;
  425. return (d - outbuf);
  426. }
  427. }
  428. trace->traces[trace->cur_level % TRACE_POINTS] = self;
  429. lua_pushvalue(L, pos);
  430. r = rspamd_snprintf(d, remain + 1, "{");
  431. remain -= r;
  432. d += r;
  433. /* Get numeric keys (ipairs) */
  434. for (i = 1;; i++) {
  435. lua_rawgeti(L, -1, i);
  436. if (lua_isnil(L, -1)) {
  437. lua_pop(L, 1);
  438. break;
  439. }
  440. last_seq = i;
  441. if (!first) {
  442. r = rspamd_snprintf(d, remain + 1, ", ");
  443. MOVE_BUF(d, remain, r);
  444. }
  445. r = rspamd_snprintf(d, remain + 1, "[%d] = ", i);
  446. MOVE_BUF(d, remain, r);
  447. tpos = lua_gettop(L);
  448. if (lua_topointer(L, tpos) == self) {
  449. r = rspamd_snprintf(d, remain + 1, "__self");
  450. }
  451. else {
  452. r = lua_logger_out_type(L, tpos, d, remain, trace, esc_type);
  453. }
  454. MOVE_BUF(d, remain, r);
  455. first = FALSE;
  456. lua_pop(L, 1);
  457. }
  458. /* Get string keys (pairs) */
  459. for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
  460. /* 'key' is at index -2 and 'value' is at index -1 */
  461. if (lua_type(L, -2) == LUA_TNUMBER) {
  462. if (last_seq > 0) {
  463. lua_pushvalue(L, -2);
  464. if (lua_tonumber(L, -1) <= last_seq + 1) {
  465. lua_pop(L, 1);
  466. /* Already seen */
  467. continue;
  468. }
  469. lua_pop(L, 1);
  470. }
  471. }
  472. if (!first) {
  473. r = rspamd_snprintf(d, remain + 1, ", ");
  474. MOVE_BUF(d, remain, r);
  475. }
  476. /* Preserve key */
  477. lua_pushvalue(L, -2);
  478. r = rspamd_snprintf(d, remain + 1, "[%s] = ",
  479. lua_tostring(L, -1));
  480. lua_pop(L, 1); /* Remove key */
  481. MOVE_BUF(d, remain, r);
  482. tpos = lua_gettop(L);
  483. if (lua_topointer(L, tpos) == self) {
  484. r = rspamd_snprintf(d, remain + 1, "__self");
  485. }
  486. else {
  487. r = lua_logger_out_type(L, tpos, d, remain, trace, esc_type);
  488. }
  489. MOVE_BUF(d, remain, r);
  490. first = FALSE;
  491. }
  492. lua_settop(L, old_top);
  493. r = rspamd_snprintf(d, remain + 1, "}");
  494. d += r;
  495. return (d - outbuf);
  496. }
  497. #undef MOVE_BUF
  498. gsize lua_logger_out_type(lua_State *L, int pos,
  499. char *outbuf, gsize len,
  500. struct lua_logger_trace *trace,
  501. enum lua_logger_escape_type esc_type)
  502. {
  503. int type;
  504. gsize r = 0;
  505. if (len == 0) {
  506. return 0;
  507. }
  508. type = lua_type(L, pos);
  509. trace->cur_level++;
  510. switch (type) {
  511. case LUA_TNUMBER:
  512. r = lua_logger_out_num(L, pos, outbuf, len, trace);
  513. break;
  514. case LUA_TBOOLEAN:
  515. r = lua_logger_out_boolean(L, pos, outbuf, len, trace);
  516. break;
  517. case LUA_TTABLE:
  518. r = lua_logger_out_table(L, pos, outbuf, len, trace, esc_type);
  519. break;
  520. case LUA_TUSERDATA:
  521. r = lua_logger_out_userdata(L, pos, outbuf, len, trace);
  522. break;
  523. case LUA_TFUNCTION:
  524. r = rspamd_snprintf(outbuf, len + 1, "function");
  525. break;
  526. case LUA_TLIGHTUSERDATA:
  527. r = rspamd_snprintf(outbuf, len + 1, "0x%p", lua_topointer(L, pos));
  528. break;
  529. case LUA_TNIL:
  530. r = rspamd_snprintf(outbuf, len + 1, "nil");
  531. break;
  532. case LUA_TNONE:
  533. r = rspamd_snprintf(outbuf, len + 1, "no value");
  534. break;
  535. default:
  536. /* Try to push everything as string using tostring magic */
  537. r = lua_logger_out_str(L, pos, outbuf, len, trace, esc_type);
  538. break;
  539. }
  540. trace->cur_level--;
  541. return r;
  542. }
  543. static const char *
  544. lua_logger_get_id(lua_State *L, int pos, GError **err)
  545. {
  546. const char *uid = NULL, *clsname;
  547. if (lua_getmetatable(L, pos) != 0) {
  548. uid = "";
  549. lua_pushstring(L, "__index");
  550. lua_gettable(L, -2);
  551. lua_pushstring(L, "class");
  552. lua_gettable(L, -2);
  553. clsname = lua_tostring(L, -1);
  554. if (strcmp(clsname, rspamd_task_classname) == 0) {
  555. struct rspamd_task *task = lua_check_task(L, pos);
  556. if (task) {
  557. uid = task->task_pool->tag.uid;
  558. }
  559. else {
  560. g_set_error(err, g_quark_from_static_string("lua_logger"),
  561. EINVAL, "invalid rspamd{task}");
  562. }
  563. }
  564. else if (strcmp(clsname, rspamd_mempool_classname) == 0) {
  565. rspamd_mempool_t *pool;
  566. pool = rspamd_lua_check_mempool(L, pos);
  567. if (pool) {
  568. uid = pool->tag.uid;
  569. }
  570. else {
  571. g_set_error(err, g_quark_from_static_string("lua_logger"),
  572. EINVAL, "invalid rspamd{mempool}");
  573. }
  574. }
  575. else if (strcmp(clsname, rspamd_config_classname) == 0) {
  576. struct rspamd_config *cfg;
  577. cfg = lua_check_config(L, pos);
  578. if (cfg) {
  579. if (cfg->checksum) {
  580. uid = cfg->checksum;
  581. }
  582. }
  583. else {
  584. g_set_error(err, g_quark_from_static_string("lua_logger"),
  585. EINVAL, "invalid rspamd{config}");
  586. }
  587. }
  588. else if (strcmp(clsname, rspamd_map_classname) == 0) {
  589. struct rspamd_lua_map *map;
  590. map = lua_check_map(L, pos);
  591. if (map) {
  592. if (map->map) {
  593. uid = map->map->tag;
  594. }
  595. else {
  596. uid = "embedded";
  597. }
  598. }
  599. else {
  600. g_set_error(err, g_quark_from_static_string("lua_logger"),
  601. EINVAL, "invalid rspamd{map}");
  602. }
  603. }
  604. else {
  605. g_set_error(err, g_quark_from_static_string("lua_logger"),
  606. EINVAL, "unknown class: %s", clsname);
  607. }
  608. /* Metatable, __index, classname */
  609. lua_pop(L, 3);
  610. }
  611. else {
  612. g_set_error(err, g_quark_from_static_string("lua_logger"),
  613. EINVAL, "no metatable found for userdata");
  614. }
  615. return uid;
  616. }
  617. static gboolean
  618. lua_logger_log_format(lua_State *L, int fmt_pos, gboolean is_string,
  619. char *logbuf, gsize remain)
  620. {
  621. char *d;
  622. const char *s, *c;
  623. gsize r, cpylen = 0;
  624. unsigned int arg_num = 0, cur_arg;
  625. bool num_arg = false;
  626. struct lua_logger_trace tr;
  627. enum {
  628. copy_char = 0,
  629. got_percent,
  630. parse_arg_num
  631. } state = copy_char;
  632. d = logbuf;
  633. s = lua_tostring(L, fmt_pos);
  634. c = s;
  635. cur_arg = fmt_pos;
  636. if (s == NULL) {
  637. return FALSE;
  638. }
  639. while (remain > 0 && *s != '\0') {
  640. switch (state) {
  641. case copy_char:
  642. if (*s == '%') {
  643. state = got_percent;
  644. s++;
  645. if (cpylen > 0) {
  646. memcpy(d, c, cpylen);
  647. d += cpylen;
  648. }
  649. cpylen = 0;
  650. }
  651. else {
  652. s++;
  653. cpylen++;
  654. remain--;
  655. }
  656. break;
  657. case got_percent:
  658. if (g_ascii_isdigit(*s) || *s == 's') {
  659. state = parse_arg_num;
  660. c = s;
  661. }
  662. else {
  663. *d++ = *s++;
  664. c = s;
  665. state = copy_char;
  666. }
  667. break;
  668. case parse_arg_num:
  669. if (g_ascii_isdigit(*s)) {
  670. s++;
  671. num_arg = true;
  672. }
  673. else {
  674. if (num_arg) {
  675. arg_num = strtoul(c, NULL, 10);
  676. arg_num += fmt_pos - 1;
  677. /* Update the current argument */
  678. cur_arg = arg_num;
  679. }
  680. else {
  681. /* We have non numeric argument, e.g. %s */
  682. arg_num = cur_arg++;
  683. s++;
  684. }
  685. if (arg_num < 1 || arg_num > (unsigned int) lua_gettop(L) + 1) {
  686. msg_err("wrong argument number: %ud", arg_num);
  687. return FALSE;
  688. }
  689. memset(&tr, 0, sizeof(tr));
  690. r = lua_logger_out_type(L, arg_num + 1, d, remain, &tr,
  691. is_string ? LUA_ESCAPE_UNPRINTABLE : LUA_ESCAPE_LOG);
  692. g_assert(r <= remain);
  693. remain -= r;
  694. d += r;
  695. state = copy_char;
  696. c = s;
  697. }
  698. break;
  699. }
  700. }
  701. if (state == parse_arg_num) {
  702. if (num_arg) {
  703. arg_num = strtoul(c, NULL, 10);
  704. arg_num += fmt_pos - 1;
  705. }
  706. else {
  707. /* We have non numeric argument, e.g. %s */
  708. arg_num = cur_arg;
  709. }
  710. if (arg_num < 1 || arg_num > (unsigned int) lua_gettop(L) + 1) {
  711. msg_err("wrong argument number: %ud", arg_num);
  712. return FALSE;
  713. }
  714. memset(&tr, 0, sizeof(tr));
  715. r = lua_logger_out_type(L, arg_num + 1, d, remain, &tr,
  716. is_string ? LUA_ESCAPE_UNPRINTABLE : LUA_ESCAPE_LOG);
  717. g_assert(r <= remain);
  718. remain -= r;
  719. d += r;
  720. }
  721. else if (state == copy_char) {
  722. if (cpylen > 0 && remain > 0) {
  723. memcpy(d, c, cpylen);
  724. d += cpylen;
  725. }
  726. }
  727. *d = '\0';
  728. return TRUE;
  729. }
  730. static int
  731. lua_logger_do_log(lua_State *L,
  732. GLogLevelFlags level,
  733. gboolean is_string,
  734. int start_pos)
  735. {
  736. char logbuf[RSPAMD_LOGBUF_SIZE - 128];
  737. const char *uid = NULL;
  738. int fmt_pos = start_pos;
  739. int ret;
  740. GError *err = NULL;
  741. if (lua_type(L, start_pos) == LUA_TSTRING) {
  742. fmt_pos = start_pos;
  743. }
  744. else if (lua_type(L, start_pos) == LUA_TUSERDATA) {
  745. fmt_pos = start_pos + 1;
  746. uid = lua_logger_get_id(L, start_pos, &err);
  747. if (uid == NULL) {
  748. ret = luaL_error(L, "bad userdata for logging: %s",
  749. err ? err->message : "unknown error");
  750. if (err) {
  751. g_error_free(err);
  752. }
  753. return ret;
  754. }
  755. }
  756. else {
  757. /* Bad argument type */
  758. return luaL_error(L, "bad format string type: %s",
  759. lua_typename(L, lua_type(L, start_pos)));
  760. }
  761. ret = lua_logger_log_format(L, fmt_pos, is_string,
  762. logbuf, sizeof(logbuf) - 1);
  763. if (ret) {
  764. if (is_string) {
  765. lua_pushstring(L, logbuf);
  766. return 1;
  767. }
  768. else {
  769. lua_common_log_line(level, L, logbuf, uid, "lua", 1);
  770. }
  771. }
  772. else {
  773. if (is_string) {
  774. lua_pushnil(L);
  775. return 1;
  776. }
  777. }
  778. return 0;
  779. }
  780. static int
  781. lua_logger_errx(lua_State *L)
  782. {
  783. LUA_TRACE_POINT;
  784. return lua_logger_do_log(L, G_LOG_LEVEL_CRITICAL, FALSE, 1);
  785. }
  786. static int
  787. lua_logger_warnx(lua_State *L)
  788. {
  789. LUA_TRACE_POINT;
  790. return lua_logger_do_log(L, G_LOG_LEVEL_WARNING, FALSE, 1);
  791. }
  792. static int
  793. lua_logger_infox(lua_State *L)
  794. {
  795. LUA_TRACE_POINT;
  796. return lua_logger_do_log(L, G_LOG_LEVEL_INFO, FALSE, 1);
  797. }
  798. static int
  799. lua_logger_messagex(lua_State *L)
  800. {
  801. LUA_TRACE_POINT;
  802. return lua_logger_do_log(L, G_LOG_LEVEL_MESSAGE, FALSE, 1);
  803. }
  804. static int
  805. lua_logger_debugx(lua_State *L)
  806. {
  807. LUA_TRACE_POINT;
  808. return lua_logger_do_log(L, G_LOG_LEVEL_DEBUG, FALSE, 1);
  809. }
  810. static int
  811. lua_logger_logx(lua_State *L)
  812. {
  813. LUA_TRACE_POINT;
  814. GLogLevelFlags flags = lua_tonumber(L, 1);
  815. const char *modname = lua_tostring(L, 2), *uid = NULL;
  816. char logbuf[RSPAMD_LOGBUF_SIZE - 128];
  817. gboolean ret;
  818. int stack_pos = 1;
  819. if (lua_type(L, 3) == LUA_TSTRING) {
  820. uid = luaL_checkstring(L, 3);
  821. }
  822. else if (lua_type(L, 3) == LUA_TUSERDATA) {
  823. uid = lua_logger_get_id(L, 3, NULL);
  824. }
  825. else {
  826. uid = "???";
  827. }
  828. if (uid && modname) {
  829. if (lua_type(L, 4) == LUA_TSTRING) {
  830. ret = lua_logger_log_format(L, 4, FALSE, logbuf, sizeof(logbuf) - 1);
  831. }
  832. else if (lua_type(L, 4) == LUA_TNUMBER) {
  833. stack_pos = lua_tonumber(L, 4);
  834. ret = lua_logger_log_format(L, 5, FALSE, logbuf, sizeof(logbuf) - 1);
  835. }
  836. else {
  837. return luaL_error(L, "invalid argument on pos 4");
  838. }
  839. if (ret) {
  840. lua_common_log_line(flags, L, logbuf, uid, modname, stack_pos);
  841. }
  842. }
  843. else {
  844. return luaL_error(L, "invalid arguments");
  845. }
  846. return 0;
  847. }
  848. static int
  849. lua_logger_debugm(lua_State *L)
  850. {
  851. LUA_TRACE_POINT;
  852. char logbuf[RSPAMD_LOGBUF_SIZE - 128];
  853. const char *uid = NULL, *module = NULL;
  854. int stack_pos = 1;
  855. gboolean ret;
  856. module = luaL_checkstring(L, 1);
  857. if (lua_type(L, 2) == LUA_TSTRING) {
  858. uid = luaL_checkstring(L, 2);
  859. }
  860. else {
  861. uid = lua_logger_get_id(L, 2, NULL);
  862. }
  863. if (uid && module) {
  864. if (lua_type(L, 3) == LUA_TSTRING) {
  865. ret = lua_logger_log_format(L, 3, FALSE, logbuf, sizeof(logbuf) - 1);
  866. }
  867. else if (lua_type(L, 3) == LUA_TNUMBER) {
  868. stack_pos = lua_tonumber(L, 3);
  869. ret = lua_logger_log_format(L, 4, FALSE, logbuf, sizeof(logbuf) - 1);
  870. }
  871. else {
  872. return luaL_error(L, "invalid argument on pos 3");
  873. }
  874. if (ret) {
  875. lua_common_log_line(G_LOG_LEVEL_DEBUG, L, logbuf, uid, module, stack_pos);
  876. }
  877. }
  878. else {
  879. return luaL_error(L, "invalid arguments");
  880. }
  881. return 0;
  882. }
  883. static int
  884. lua_logger_slog(lua_State *L)
  885. {
  886. return lua_logger_do_log(L, 0, TRUE, 1);
  887. }
  888. static int
  889. lua_logger_log_level(lua_State *L)
  890. {
  891. int log_level = rspamd_log_get_log_level(NULL);
  892. lua_pushstring(L, rspamd_get_log_severity_string(log_level));
  893. return 1;
  894. }
  895. /*** Init functions ***/
  896. static int
  897. lua_load_logger(lua_State *L)
  898. {
  899. lua_newtable(L);
  900. luaL_register(L, NULL, loggerlib_f);
  901. return 1;
  902. }
  903. void luaopen_logger(lua_State *L)
  904. {
  905. rspamd_lua_add_preload(L, "rspamd_logger", lua_load_logger);
  906. }