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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. /***
  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 gsize
  252. lua_logger_out_str (lua_State *L, gint pos, gchar *outbuf, gsize len,
  253. struct lua_logger_trace *trace)
  254. {
  255. gsize slen, flen;
  256. const gchar *str = lua_tolstring (L, pos, &slen);
  257. static const gchar hexdigests[16] = "0123456789abcdef";
  258. gsize r = 0, s;
  259. if (str) {
  260. gboolean normal = TRUE;
  261. flen = MIN (slen, len - 1);
  262. for (r = 0; r < flen; r ++) {
  263. if (!(g_ascii_isprint (str[r]) || (str[r] & 0x80))) {
  264. normal = FALSE;
  265. break;
  266. }
  267. }
  268. if (normal) {
  269. r = rspamd_strlcpy (outbuf, str, flen + 1);
  270. }
  271. else {
  272. /* Need to escape non printed characters */
  273. r = 0;
  274. s = 0;
  275. while (slen > 0 && len > 1) {
  276. if (!g_ascii_isprint (str[s])) {
  277. if (str[s] & 0x80) {
  278. outbuf[r++] = str[s];
  279. }
  280. else if (len >= 3) {
  281. outbuf[r++] = '\\';
  282. outbuf[r++] = hexdigests[((str[s] >> 4) & 0xF)];
  283. outbuf[r++] = hexdigests[((str[s]) & 0xF)];
  284. len -= 2;
  285. }
  286. else {
  287. outbuf[r++] = '?';
  288. }
  289. }
  290. else {
  291. outbuf[r++] = str[s];
  292. }
  293. s++;
  294. slen --;
  295. len --;
  296. }
  297. outbuf[r] = '\0';
  298. }
  299. }
  300. return r;
  301. }
  302. static gsize
  303. lua_logger_out_num (lua_State *L, gint pos, gchar *outbuf, gsize len,
  304. struct lua_logger_trace *trace)
  305. {
  306. gdouble num = lua_tonumber (L, pos);
  307. glong inum;
  308. gsize r = 0;
  309. if ((gdouble) (glong) num == num) {
  310. inum = num;
  311. r = rspamd_snprintf (outbuf, len + 1, "%l", inum);
  312. }
  313. else {
  314. r = rspamd_snprintf (outbuf, len + 1, "%f", num);
  315. }
  316. return r;
  317. }
  318. static gsize
  319. lua_logger_out_boolean (lua_State *L, gint pos, gchar *outbuf, gsize len,
  320. struct lua_logger_trace *trace)
  321. {
  322. gboolean val = lua_toboolean (L, pos);
  323. gsize r = 0;
  324. r = rspamd_strlcpy (outbuf, val ? "true" : "false", len + 1);
  325. return r;
  326. }
  327. static gsize
  328. lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len,
  329. struct lua_logger_trace *trace)
  330. {
  331. gint r, top;
  332. const gchar *str = NULL;
  333. gboolean converted_to_str = FALSE;
  334. top = lua_gettop (L);
  335. if (!lua_getmetatable (L, pos)) {
  336. return 0;
  337. }
  338. lua_pushstring (L, "__index");
  339. lua_gettable (L, -2);
  340. if (!lua_istable (L, -1)) {
  341. lua_settop (L, top);
  342. return 0;
  343. }
  344. lua_pushstring (L, "__tostring");
  345. lua_gettable (L, -2);
  346. if (lua_isfunction (L, -1)) {
  347. lua_pushvalue (L, pos);
  348. if (lua_pcall (L, 1, 1, 0) != 0) {
  349. lua_settop (L, top);
  350. return 0;
  351. }
  352. str = lua_tostring (L, -1);
  353. if (str) {
  354. converted_to_str = TRUE;
  355. }
  356. }
  357. else {
  358. lua_pop (L, 1);
  359. lua_pushstring (L, "class");
  360. lua_gettable (L, -2);
  361. if (lua_isstring (L, -1)) {
  362. str = lua_tostring (L, -1);
  363. converted_to_str = TRUE;
  364. }
  365. }
  366. if (converted_to_str) {
  367. r = rspamd_snprintf (outbuf, len + 1, "%s", str);
  368. }
  369. else {
  370. /* Print raw pointer */
  371. r = rspamd_snprintf (outbuf, len + 1, "%s(%p)", str, lua_touserdata (L, pos));
  372. }
  373. lua_settop (L, top);
  374. return r;
  375. }
  376. #define MOVE_BUF(d, remain, r) \
  377. (d) += (r); (remain) -= (r); \
  378. if ((remain) == 0) { lua_pop (L, 1); break; }
  379. static gsize
  380. lua_logger_out_table (lua_State *L, gint pos, gchar *outbuf, gsize len,
  381. struct lua_logger_trace *trace)
  382. {
  383. gchar *d = outbuf;
  384. gsize remain = len, r;
  385. gboolean first = TRUE;
  386. gconstpointer self = NULL;
  387. gint i, tpos;
  388. if (!lua_istable (L, pos) || remain == 0) {
  389. return 0;
  390. }
  391. self = lua_topointer (L, pos);
  392. /* Check if we have seen this pointer */
  393. for (i = 0; i < TRACE_POINTS; i ++) {
  394. if (trace->traces[i] == self) {
  395. r = rspamd_snprintf (d, remain + 1, "ref(%p)", self);
  396. d += r;
  397. return (d - outbuf);
  398. }
  399. }
  400. trace->traces[trace->cur_level % TRACE_POINTS] = self;
  401. lua_pushvalue (L, pos);
  402. r = rspamd_snprintf (d, remain + 1, "{");
  403. remain -= r;
  404. d += r;
  405. /* Get numeric keys (ipairs) */
  406. for (i = 1; ; i++) {
  407. lua_rawgeti (L, -1, i);
  408. if (lua_isnil (L, -1)) {
  409. lua_pop (L, 1);
  410. break;
  411. }
  412. if (!first) {
  413. r = rspamd_snprintf (d, remain + 1, ", ");
  414. MOVE_BUF(d, remain, r);
  415. }
  416. r = rspamd_snprintf (d, remain + 1, "[%d] = ", i);
  417. MOVE_BUF(d, remain, r);
  418. tpos = lua_gettop (L);
  419. if (lua_topointer (L, tpos) == self) {
  420. r = rspamd_snprintf (d, remain + 1, "__self");
  421. }
  422. else {
  423. r = lua_logger_out_type (L, tpos, d, remain, trace);
  424. }
  425. MOVE_BUF(d, remain, r);
  426. first = FALSE;
  427. lua_pop (L, 1);
  428. }
  429. /* Get string keys (pairs) */
  430. for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 1)) {
  431. /* 'key' is at index -2 and 'value' is at index -1 */
  432. if (lua_type (L, -2) == LUA_TNUMBER) {
  433. continue;
  434. }
  435. if (!first) {
  436. r = rspamd_snprintf (d, remain + 1, ", ");
  437. MOVE_BUF(d, remain, r);
  438. }
  439. r = rspamd_snprintf (d, remain + 1, "[%s] = ",
  440. lua_tostring (L, -2));
  441. MOVE_BUF(d, remain, r);
  442. tpos = lua_gettop (L);
  443. if (lua_topointer (L, tpos) == self) {
  444. r = rspamd_snprintf (d, remain + 1, "__self");
  445. }
  446. else {
  447. r = lua_logger_out_type (L, tpos, d, remain, trace);
  448. }
  449. MOVE_BUF(d, remain, r);
  450. first = FALSE;
  451. }
  452. lua_pop (L, 1);
  453. r = rspamd_snprintf (d, remain + 1, "}");
  454. d += r;
  455. return (d - outbuf);
  456. }
  457. #undef MOVE_BUF
  458. gsize
  459. lua_logger_out_type (lua_State *L, gint pos, gchar *outbuf, gsize len,
  460. struct lua_logger_trace *trace)
  461. {
  462. gint type;
  463. gsize r = 0;
  464. if (len == 0) {
  465. return 0;
  466. }
  467. type = lua_type (L, pos);
  468. trace->cur_level ++;
  469. switch (type) {
  470. case LUA_TNUMBER:
  471. r = lua_logger_out_num (L, pos, outbuf, len, trace);
  472. break;
  473. case LUA_TBOOLEAN:
  474. r = lua_logger_out_boolean (L, pos, outbuf, len, trace);
  475. break;
  476. case LUA_TTABLE:
  477. r = lua_logger_out_table (L, pos, outbuf, len, trace);
  478. break;
  479. case LUA_TUSERDATA:
  480. r = lua_logger_out_userdata (L, pos, outbuf, len, trace);
  481. break;
  482. case LUA_TFUNCTION:
  483. r = rspamd_snprintf (outbuf, len + 1, "function");
  484. break;
  485. case LUA_TNIL:
  486. r = rspamd_snprintf (outbuf, len + 1, "nil");
  487. break;
  488. case LUA_TNONE:
  489. r = rspamd_snprintf (outbuf, len + 1, "no value");
  490. break;
  491. default:
  492. /* Try to push everything as string using tostring magic */
  493. r = lua_logger_out_str (L, pos, outbuf, len, trace);
  494. break;
  495. }
  496. trace->cur_level --;
  497. return r;
  498. }
  499. static const gchar *
  500. lua_logger_get_id (lua_State *L, gint pos, GError **err)
  501. {
  502. const gchar *uid = NULL, *clsname;
  503. if (lua_getmetatable (L, pos) != 0) {
  504. uid = "";
  505. lua_pushstring (L, "__index");
  506. lua_gettable (L, -2);
  507. lua_pushstring (L, "class");
  508. lua_gettable (L, -2);
  509. clsname = lua_tostring (L, -1);
  510. if (strcmp (clsname, "rspamd{task}") == 0) {
  511. struct rspamd_task *task = lua_check_task (L, pos);
  512. if (task) {
  513. uid = task->task_pool->tag.uid;
  514. }
  515. else {
  516. g_set_error (err, g_quark_from_static_string ("lua_logger"),
  517. EINVAL, "invalid rspamd{task}");
  518. }
  519. }
  520. else if (strcmp (clsname, "rspamd{mempool}") == 0) {
  521. rspamd_mempool_t *pool;
  522. pool = rspamd_lua_check_mempool (L, pos);
  523. if (pool) {
  524. uid = pool->tag.uid;
  525. }
  526. else {
  527. g_set_error (err, g_quark_from_static_string ("lua_logger"),
  528. EINVAL, "invalid rspamd{mempool}");
  529. }
  530. }
  531. else if (strcmp (clsname, "rspamd{config}") == 0) {
  532. struct rspamd_config *cfg;
  533. cfg = lua_check_config (L, pos);
  534. if (cfg) {
  535. if (cfg->checksum) {
  536. uid = cfg->checksum;
  537. }
  538. }
  539. else {
  540. g_set_error (err, g_quark_from_static_string ("lua_logger"),
  541. EINVAL, "invalid rspamd{config}");
  542. }
  543. }
  544. else if (strcmp (clsname, "rspamd{map}") == 0) {
  545. struct rspamd_lua_map *map;
  546. map = lua_check_map (L, pos);
  547. if (map) {
  548. if (map->map) {
  549. uid = map->map->tag;
  550. }
  551. else {
  552. uid = "embedded";
  553. }
  554. }
  555. else {
  556. g_set_error (err, g_quark_from_static_string ("lua_logger"),
  557. EINVAL, "invalid rspamd{map}");
  558. }
  559. }
  560. else {
  561. g_set_error (err, g_quark_from_static_string ("lua_logger"),
  562. EINVAL, "unknown class: %s", clsname);
  563. }
  564. /* Metatable, __index, classname */
  565. lua_pop (L, 3);
  566. }
  567. else {
  568. g_set_error (err, g_quark_from_static_string ("lua_logger"),
  569. EINVAL, "no metatable found for userdata");
  570. }
  571. return uid;
  572. }
  573. static gboolean
  574. lua_logger_log_format (lua_State *L, gint fmt_pos, gboolean is_string,
  575. gchar *logbuf, gsize remain)
  576. {
  577. gchar *d;
  578. const gchar *s, *c;
  579. gsize r, cpylen = 0;
  580. guint arg_num = 0, cur_arg;
  581. bool num_arg = false;
  582. struct lua_logger_trace tr;
  583. enum {
  584. copy_char = 0,
  585. got_percent,
  586. parse_arg_num
  587. } state = copy_char;
  588. d = logbuf;
  589. s = lua_tostring (L, fmt_pos);
  590. c = s;
  591. cur_arg = fmt_pos;
  592. if (s == NULL) {
  593. return FALSE;
  594. }
  595. while (remain > 0 && *s != '\0') {
  596. switch (state) {
  597. case copy_char:
  598. if (*s == '%') {
  599. state = got_percent;
  600. s++;
  601. if (cpylen > 0) {
  602. memcpy (d, c, cpylen);
  603. d += cpylen;
  604. }
  605. cpylen = 0;
  606. }
  607. else {
  608. s++;
  609. cpylen ++;
  610. remain--;
  611. }
  612. break;
  613. case got_percent:
  614. if (g_ascii_isdigit (*s) || *s == 's') {
  615. state = parse_arg_num;
  616. c = s;
  617. }
  618. else {
  619. *d++ = *s++;
  620. c = s;
  621. state = copy_char;
  622. }
  623. break;
  624. case parse_arg_num:
  625. if (g_ascii_isdigit (*s)) {
  626. s++;
  627. num_arg = true;
  628. }
  629. else {
  630. if (num_arg) {
  631. arg_num = strtoul (c, NULL, 10);
  632. arg_num += fmt_pos - 1;
  633. /* Update the current argument */
  634. cur_arg = arg_num;
  635. }
  636. else {
  637. /* We have non numeric argument, e.g. %s */
  638. arg_num = cur_arg ++;
  639. s ++;
  640. }
  641. if (arg_num < 1 || arg_num > (guint) lua_gettop (L) + 1) {
  642. msg_err ("wrong argument number: %ud", arg_num);
  643. return FALSE;
  644. }
  645. memset (&tr, 0, sizeof (tr));
  646. r = lua_logger_out_type (L, arg_num + 1, d, remain, &tr);
  647. g_assert (r <= remain);
  648. remain -= r;
  649. d += r;
  650. state = copy_char;
  651. c = s;
  652. }
  653. break;
  654. }
  655. }
  656. if (state == parse_arg_num) {
  657. if (num_arg) {
  658. arg_num = strtoul (c, NULL, 10);
  659. arg_num += fmt_pos - 1;
  660. }
  661. else {
  662. /* We have non numeric argument, e.g. %s */
  663. arg_num = cur_arg;
  664. }
  665. if (arg_num < 1 || arg_num > (guint) lua_gettop (L) + 1) {
  666. msg_err ("wrong argument number: %ud", arg_num);
  667. return FALSE;
  668. }
  669. memset (&tr, 0, sizeof (tr));
  670. r = lua_logger_out_type (L, arg_num + 1, d, remain, &tr);
  671. g_assert (r <= remain);
  672. remain -= r;
  673. d += r;
  674. }
  675. else if (state == copy_char) {
  676. if (cpylen > 0 && remain > 0) {
  677. memcpy (d, c, cpylen);
  678. d += cpylen;
  679. }
  680. }
  681. *d = '\0';
  682. return TRUE;
  683. }
  684. static gint
  685. lua_logger_do_log (lua_State *L,
  686. GLogLevelFlags level,
  687. gboolean is_string,
  688. gint start_pos)
  689. {
  690. gchar logbuf[RSPAMD_LOGBUF_SIZE - 128];
  691. const gchar *uid = NULL;
  692. gint fmt_pos = start_pos;
  693. gint ret;
  694. GError *err = NULL;
  695. if (lua_type (L, start_pos) == LUA_TSTRING) {
  696. fmt_pos = start_pos;
  697. }
  698. else if (lua_type (L, start_pos) == LUA_TUSERDATA) {
  699. fmt_pos = start_pos + 1;
  700. uid = lua_logger_get_id (L, start_pos, &err);
  701. if (uid == NULL) {
  702. ret = luaL_error (L, "bad userdata for logging: %s",
  703. err ? err->message : "unknown error");
  704. if (err) {
  705. g_error_free (err);
  706. }
  707. return ret;
  708. }
  709. }
  710. else {
  711. /* Bad argument type */
  712. return luaL_error (L, "bad format string type: %s",
  713. lua_typename (L, lua_type (L, start_pos)));
  714. }
  715. ret = lua_logger_log_format (L, fmt_pos, is_string,
  716. logbuf, sizeof (logbuf) - 1);
  717. if (ret) {
  718. if (is_string) {
  719. lua_pushstring (L, logbuf);
  720. return 1;
  721. }
  722. else {
  723. lua_common_log_line (level, L, logbuf, uid, "lua", 1);
  724. }
  725. }
  726. else {
  727. if (is_string) {
  728. lua_pushnil (L);
  729. return 1;
  730. }
  731. }
  732. return 0;
  733. }
  734. static gint
  735. lua_logger_errx (lua_State *L)
  736. {
  737. LUA_TRACE_POINT;
  738. return lua_logger_do_log (L, G_LOG_LEVEL_CRITICAL, FALSE, 1);
  739. }
  740. static gint
  741. lua_logger_warnx (lua_State *L)
  742. {
  743. LUA_TRACE_POINT;
  744. return lua_logger_do_log (L, G_LOG_LEVEL_WARNING, FALSE, 1);
  745. }
  746. static gint
  747. lua_logger_infox (lua_State *L)
  748. {
  749. LUA_TRACE_POINT;
  750. return lua_logger_do_log (L, G_LOG_LEVEL_INFO, FALSE, 1);
  751. }
  752. static gint
  753. lua_logger_messagex (lua_State *L)
  754. {
  755. LUA_TRACE_POINT;
  756. return lua_logger_do_log (L, G_LOG_LEVEL_MESSAGE, FALSE, 1);
  757. }
  758. static gint
  759. lua_logger_debugx (lua_State *L)
  760. {
  761. LUA_TRACE_POINT;
  762. return lua_logger_do_log (L, G_LOG_LEVEL_DEBUG, FALSE, 1);
  763. }
  764. static gint
  765. lua_logger_logx (lua_State *L)
  766. {
  767. LUA_TRACE_POINT;
  768. GLogLevelFlags flags = lua_tonumber (L, 1);
  769. const gchar *modname = lua_tostring (L, 2), *uid = NULL;
  770. gchar logbuf[RSPAMD_LOGBUF_SIZE - 128];
  771. gboolean ret;
  772. gint stack_pos = 1;
  773. if (lua_type (L, 3) == LUA_TSTRING) {
  774. uid = luaL_checkstring (L, 3);
  775. }
  776. else {
  777. uid = lua_logger_get_id (L, 3, NULL);
  778. }
  779. if (uid && modname) {
  780. if (lua_type (L, 4) == LUA_TSTRING) {
  781. ret = lua_logger_log_format (L, 4, FALSE, logbuf, sizeof (logbuf) - 1);
  782. }
  783. else if (lua_type (L, 4) == LUA_TNUMBER) {
  784. stack_pos = lua_tonumber (L, 4);
  785. ret = lua_logger_log_format (L, 5, FALSE, logbuf, sizeof (logbuf) - 1);
  786. }
  787. else {
  788. return luaL_error (L, "invalid argument on pos 4");
  789. }
  790. if (ret) {
  791. lua_common_log_line (flags, L, logbuf, uid, modname, stack_pos);
  792. }
  793. }
  794. else {
  795. return luaL_error (L, "invalid arguments");
  796. }
  797. return 0;
  798. }
  799. static gint
  800. lua_logger_debugm (lua_State *L)
  801. {
  802. LUA_TRACE_POINT;
  803. gchar logbuf[RSPAMD_LOGBUF_SIZE - 128];
  804. const gchar *uid = NULL, *module = NULL;
  805. gint stack_pos = 1;
  806. gboolean ret;
  807. module = luaL_checkstring (L, 1);
  808. if (lua_type (L, 2) == LUA_TSTRING) {
  809. uid = luaL_checkstring (L, 2);
  810. }
  811. else {
  812. uid = lua_logger_get_id (L, 2, NULL);
  813. }
  814. if (uid && module) {
  815. if (lua_type (L, 3) == LUA_TSTRING) {
  816. ret = lua_logger_log_format (L, 3, FALSE, logbuf, sizeof (logbuf) - 1);
  817. }
  818. else if (lua_type (L, 3) == LUA_TNUMBER) {
  819. stack_pos = lua_tonumber (L, 3);
  820. ret = lua_logger_log_format (L, 4, FALSE, logbuf, sizeof (logbuf) - 1);
  821. }
  822. else {
  823. return luaL_error (L, "invalid argument on pos 3");
  824. }
  825. if (ret) {
  826. lua_common_log_line (G_LOG_LEVEL_DEBUG, L, logbuf, uid, module, stack_pos);
  827. }
  828. }
  829. else {
  830. return luaL_error (L, "invalid arguments");
  831. }
  832. return 0;
  833. }
  834. static gint
  835. lua_logger_slog (lua_State *L)
  836. {
  837. return lua_logger_do_log (L, 0, TRUE, 1);
  838. }
  839. /*** Init functions ***/
  840. static gint
  841. lua_load_logger (lua_State *L)
  842. {
  843. lua_newtable (L);
  844. luaL_register (L, NULL, loggerlib_f);
  845. return 1;
  846. }
  847. void
  848. luaopen_logger (lua_State *L)
  849. {
  850. rspamd_lua_add_preload (L, "rspamd_logger", lua_load_logger);
  851. }