suricata
util-lua-log.c
Go to the documentation of this file.
1/* Copyright (C) 2025 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18#include "suricata-common.h"
19#include "util-lua-log.h"
20#include "util-lua.h"
21#include "util-debug.h"
22
23#include "lauxlib.h"
24
25static bool LuaGetAr(lua_State *L, lua_Debug *ar)
26{
27 if (lua_getstack(L, 1, ar) && lua_getinfo(L, "nSl", ar)) {
28 return true;
29 }
30 return false;
31}
32
33static int LuaLogInfo(lua_State *L)
34{
35 const char *msg = luaL_checkstring(L, 1);
36 lua_Debug ar;
37 if (LuaGetAr(L, &ar)) {
38 const char *funcname = ar.name ? ar.name : ar.what;
39 SCLogInfoRaw(ar.short_src, funcname, ar.currentline, "%s", msg);
40 } else {
41 SCLogInfo("%s", msg);
42 }
43 return 0;
44}
45
46static int LuaLogNotice(lua_State *L)
47{
48 const char *msg = luaL_checkstring(L, 1);
49 lua_Debug ar;
50 if (LuaGetAr(L, &ar)) {
51 const char *funcname = ar.name ? ar.name : ar.what;
52 SCLogNoticeRaw(ar.short_src, funcname, ar.currentline, "%s", msg);
53 } else {
54 SCLogNotice("%s", msg);
55 }
56
57 return 0;
58}
59
60static int LuaLogWarning(lua_State *L)
61{
62 const char *msg = luaL_checkstring(L, 1);
63 lua_Debug ar;
64 if (LuaGetAr(L, &ar)) {
65 const char *funcname = ar.name ? ar.name : ar.what;
66 SCLogWarningRaw(ar.short_src, funcname, ar.currentline, "%s", msg);
67 } else {
68 SCLogWarning("%s", msg);
69 }
70 return 0;
71}
72
73static int LuaLogError(lua_State *L)
74{
75 const char *msg = luaL_checkstring(L, 1);
76 lua_Debug ar;
77 if (LuaGetAr(L, &ar)) {
78 const char *funcname = ar.name ? ar.name : ar.what;
79 SCLogErrorRaw(ar.short_src, funcname, ar.currentline, "%s", msg);
80 } else {
81 SCLogError("%s", msg);
82 }
83 return 0;
84}
85
86static int LuaLogDebug(lua_State *L)
87{
88#ifdef DEBUG
89 const char *msg = luaL_checkstring(L, 1);
90 lua_Debug ar;
91 if (LuaGetAr(L, &ar)) {
92 const char *funcname = ar.name ? ar.name : ar.what;
93 SCLogDebugRaw(ar.short_src, funcname, ar.currentline, "%s", msg);
94 } else {
95 SCLogDebug("%s", msg);
96 }
97#endif
98 return 0;
99}
100
101static int LuaLogConfig(lua_State *L)
102{
103 const char *msg = luaL_checkstring(L, 1);
104 lua_Debug ar;
105 if (LuaGetAr(L, &ar)) {
106 const char *funcname = ar.name ? ar.name : ar.what;
107 SCLogConfigRaw(ar.short_src, funcname, ar.currentline, "%s", msg);
108 } else {
109 SCLogConfig("%s", msg);
110 }
111 return 0;
112}
113
114static int LuaLogPerf(lua_State *L)
115{
116 const char *msg = luaL_checkstring(L, 1);
117 lua_Debug ar;
118 if (LuaGetAr(L, &ar)) {
119 const char *funcname = ar.name ? ar.name : ar.what;
120 SCLogPerfRaw(ar.short_src, funcname, ar.currentline, "%s", msg);
121 } else {
122 SCLogPerf("%s", msg);
123 }
124 return 0;
125}
126
127static const struct luaL_Reg loglib[] = {
128 // clang-format off
129 { "info", LuaLogInfo },
130 { "notice", LuaLogNotice },
131 { "warning", LuaLogWarning },
132 { "error", LuaLogError },
133 { "debug", LuaLogDebug },
134 { "config", LuaLogConfig },
135 { "perf", LuaLogPerf },
136 { NULL, NULL }
137 // clang-format on
138};
139
141{
142 luaL_newlib(L, loglib);
143 return 1;
144}
struct lua_State lua_State
#define SCLogConfigRaw(file, func, line,...)
Definition util-debug.h:231
#define SCLogInfoRaw(file, func, line,...)
Definition util-debug.h:226
#define SCLogPerf(...)
Definition util-debug.h:234
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition util-debug.h:243
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition util-debug.h:255
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition util-debug.h:225
#define SCLogErrorRaw(file, func, line,...)
Definition util-debug.h:269
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
#define SCLogNoticeRaw(file, func, line,...)
Definition util-debug.h:245
#define SCLogPerfRaw(file, func, line,...)
Definition util-debug.h:235
#define SCLogWarningRaw(file, func, line,...)
Definition util-debug.h:257
#define SCLogConfig(...)
Definition util-debug.h:229
int SCLuaLoadLogLib(lua_State *L)