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

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