]> source.dussan.org Git - rspamd.git/commitdiff
Move logger code to a separate module.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 4 Apr 2015 22:47:56 +0000 (23:47 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 4 Apr 2015 22:47:56 +0000 (23:47 +0100)
src/lua/CMakeLists.txt
src/lua/lua_common.c
src/lua/lua_common.h
src/lua/lua_logger.c [new file with mode: 0644]

index 18e56508eeb2b135fd9483b061b1dbf0b0d9188f..c47cd700d55557bc2fc604c1229fbc9b6fecd081 100644 (file)
@@ -1,5 +1,6 @@
 # Lua support makefile
 SET(LUASRC                       ${CMAKE_CURRENT_SOURCE_DIR}/lua_common.c
+                                         ${CMAKE_CURRENT_SOURCE_DIR}/lua_logger.c
                                          ${CMAKE_CURRENT_SOURCE_DIR}/lua_task.c
                                          ${CMAKE_CURRENT_SOURCE_DIR}/lua_config.c
                                          ${CMAKE_CURRENT_SOURCE_DIR}/lua_classifier.c
index d970325d14f72b5de2b32affe94bce8b649dcd7a..1b065c2e618a612aa3e29a97b755e5d63a5aaa5e 100644 (file)
@@ -32,21 +32,6 @@ const luaL_reg null_reg[] = {
        {NULL, NULL}
 };
 
-/* Logger methods */
-LUA_FUNCTION_DEF (logger, err);
-LUA_FUNCTION_DEF (logger, warn);
-LUA_FUNCTION_DEF (logger, info);
-LUA_FUNCTION_DEF (logger, debug);
-
-static const struct luaL_reg loggerlib_f[] = {
-       LUA_INTERFACE_DEF (logger, err),
-       LUA_INTERFACE_DEF (logger, warn),
-       LUA_INTERFACE_DEF (logger, info),
-       LUA_INTERFACE_DEF (logger, debug),
-       {"__tostring", rspamd_lua_class_tostring},
-       {NULL, NULL}
-};
-
 
 /* Util functions */
 /**
@@ -159,111 +144,6 @@ rspamd_lua_table_get (lua_State *L, const gchar *index)
        return result;
 }
 
-static void
-lua_common_log (GLogLevelFlags level, lua_State *L, const gchar *msg)
-{
-       lua_Debug d;
-       gchar func_buf[128], *p;
-
-       if (lua_getstack (L, 1, &d) == 1) {
-               (void)lua_getinfo (L, "Sl", &d);
-               if ((p = strrchr (d.short_src, '/')) == NULL) {
-                       p = d.short_src;
-               }
-               else {
-                       p++;
-               }
-               rspamd_snprintf (func_buf, sizeof (func_buf), "%s:%d", p,
-                       d.currentline);
-               if (level == G_LOG_LEVEL_DEBUG) {
-                       rspamd_conditional_debug (rspamd_main->logger,
-                               NULL,
-                               func_buf,
-                               "%s",
-                               msg);
-               }
-               else {
-                       rspamd_common_log_function (rspamd_main->logger,
-                               level,
-                               func_buf,
-                               "%s",
-                               msg);
-               }
-       }
-       else {
-               if (level == G_LOG_LEVEL_DEBUG) {
-                       rspamd_conditional_debug (rspamd_main->logger,
-                               NULL,
-                               __FUNCTION__,
-                               "%s",
-                               msg);
-               }
-               else {
-                       rspamd_common_log_function (rspamd_main->logger,
-                               level,
-                               __FUNCTION__,
-                               "%s",
-                               msg);
-               }
-       }
-}
-
-/*** Logger interface ***/
-static gint
-lua_logger_err (lua_State * L)
-{
-       const gchar *msg;
-       msg = luaL_checkstring (L, 1);
-       lua_common_log (G_LOG_LEVEL_CRITICAL, L, msg);
-       return 1;
-}
-
-static gint
-lua_logger_warn (lua_State * L)
-{
-       const gchar *msg;
-       msg = luaL_checkstring (L, 1);
-       lua_common_log (G_LOG_LEVEL_WARNING, L, msg);
-       return 1;
-}
-
-static gint
-lua_logger_info (lua_State * L)
-{
-       const gchar *msg;
-       msg = luaL_checkstring (L, 1);
-       lua_common_log (G_LOG_LEVEL_INFO, L, msg);
-       return 1;
-}
-
-static gint
-lua_logger_debug (lua_State * L)
-{
-       const gchar *msg;
-       msg = luaL_checkstring (L, 1);
-       lua_common_log (G_LOG_LEVEL_DEBUG, L, msg);
-       return 1;
-}
-
-
-/*** Init functions ***/
-
-static gint
-lua_load_logger (lua_State *L)
-{
-       lua_newtable (L);
-       luaL_register (L, NULL, loggerlib_f);
-
-       return 1;
-}
-
-static void
-luaopen_logger (lua_State * L)
-{
-       rspamd_lua_add_preload (L, "rspamd_logger", lua_load_logger);
-}
-
-
 static void
 lua_add_actions_global (lua_State *L)
 {
index d084f2096be96953a77ef6cb0498fa3f9c655a34..f9d365ae0c259ded95bfe24dac7583e4d4938717 100644 (file)
@@ -188,6 +188,7 @@ void luaopen_dns_resolver (lua_State * L);
 void luaopen_rsa (lua_State * L);
 void luaopen_ip (lua_State * L);
 void luaopen_expression (lua_State * L);
+void luaopen_logger (lua_State * L);
 
 gint rspamd_lua_call_filter (const gchar *function, struct rspamd_task *task);
 gint rspamd_lua_call_chain_filter (const gchar *function,
diff --git a/src/lua/lua_logger.c b/src/lua/lua_logger.c
new file mode 100644 (file)
index 0000000..0047190
--- /dev/null
@@ -0,0 +1,146 @@
+/*
+ * Copyright (c) 2015, Vsevolod Stakhov
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *      * Redistributions of source code must retain the above copyright
+ *        notice, this list of conditions and the following disclaimer.
+ *      * Redistributions in binary form must reproduce the above copyright
+ *        notice, this list of conditions and the following disclaimer in the
+ *        documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "lua_common.h"
+
+
+/* Logger methods */
+LUA_FUNCTION_DEF (logger, err);
+LUA_FUNCTION_DEF (logger, warn);
+LUA_FUNCTION_DEF (logger, info);
+LUA_FUNCTION_DEF (logger, debug);
+
+static const struct luaL_reg loggerlib_f[] = {
+       LUA_INTERFACE_DEF (logger, err),
+       LUA_INTERFACE_DEF (logger, warn),
+       LUA_INTERFACE_DEF (logger, info),
+       LUA_INTERFACE_DEF (logger, debug),
+       {"__tostring", rspamd_lua_class_tostring},
+       {NULL, NULL}
+};
+
+static void
+lua_common_log (GLogLevelFlags level, lua_State *L, const gchar *msg)
+{
+       lua_Debug d;
+       gchar func_buf[128], *p;
+
+       if (lua_getstack (L, 1, &d) == 1) {
+               (void)lua_getinfo (L, "Sl", &d);
+               if ((p = strrchr (d.short_src, '/')) == NULL) {
+                       p = d.short_src;
+               }
+               else {
+                       p++;
+               }
+               rspamd_snprintf (func_buf, sizeof (func_buf), "%s:%d", p,
+                       d.currentline);
+               if (level == G_LOG_LEVEL_DEBUG) {
+                       rspamd_conditional_debug (rspamd_main->logger,
+                               NULL,
+                               func_buf,
+                               "%s",
+                               msg);
+               }
+               else {
+                       rspamd_common_log_function (rspamd_main->logger,
+                               level,
+                               func_buf,
+                               "%s",
+                               msg);
+               }
+       }
+       else {
+               if (level == G_LOG_LEVEL_DEBUG) {
+                       rspamd_conditional_debug (rspamd_main->logger,
+                               NULL,
+                               __FUNCTION__,
+                               "%s",
+                               msg);
+               }
+               else {
+                       rspamd_common_log_function (rspamd_main->logger,
+                               level,
+                               __FUNCTION__,
+                               "%s",
+                               msg);
+               }
+       }
+}
+
+/*** Logger interface ***/
+static gint
+lua_logger_err (lua_State * L)
+{
+       const gchar *msg;
+       msg = luaL_checkstring (L, 1);
+       lua_common_log (G_LOG_LEVEL_CRITICAL, L, msg);
+       return 1;
+}
+
+static gint
+lua_logger_warn (lua_State * L)
+{
+       const gchar *msg;
+       msg = luaL_checkstring (L, 1);
+       lua_common_log (G_LOG_LEVEL_WARNING, L, msg);
+       return 1;
+}
+
+static gint
+lua_logger_info (lua_State * L)
+{
+       const gchar *msg;
+       msg = luaL_checkstring (L, 1);
+       lua_common_log (G_LOG_LEVEL_INFO, L, msg);
+       return 1;
+}
+
+static gint
+lua_logger_debug (lua_State * L)
+{
+       const gchar *msg;
+       msg = luaL_checkstring (L, 1);
+       lua_common_log (G_LOG_LEVEL_DEBUG, L, msg);
+       return 1;
+}
+
+
+/*** Init functions ***/
+
+static gint
+lua_load_logger (lua_State *L)
+{
+       lua_newtable (L);
+       luaL_register (L, NULL, loggerlib_f);
+
+       return 1;
+}
+
+void
+luaopen_logger (lua_State * L)
+{
+       rspamd_lua_add_preload (L, "rspamd_logger", lua_load_logger);
+}