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

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