/* * anylike * * anylike is a ... * * * Copyright (C) 2009-2010 Markus Grueneis * Christian Pointner * * This file is part of anylike. * * anylike is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * anylike is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with anylike. If not, see . */ #include #include #include "log.h" #include "l_log.h" static int l_log_init(lua_State *L) { log_init(); return 0; } static int l_log_close(lua_State *L) { log_close(); return 0; } static int l_log_add_target(lua_State *L) { int ret = log_add_target(luaL_checklstring(L,1,NULL)); if(ret) { lua_pushboolean(L, 0); switch(ret) { case -2: lua_pushstring(L, "memory error at log_add_target"); break; case -3: lua_pushstring(L, "unknown log target"); break; case -4: lua_pushstring(L, "this log target is only allowed once"); break; default: lua_pushstring(L, "syntax error"); break; } return 2; } lua_pushboolean(L, 1); return 1; } /* void log_printf(log_prio_t prio, const char* fmt, ...); */ static int l_log_print(lua_State *L) { const char* prio_str = luaL_checklstring(L,1,NULL); log_prio_t prio; if(prio_str) { if(!strncmp("ERROR", prio_str, 6)) prio = ERROR; else if(!strncmp("WARNING", prio_str, 6)) prio = WARNING; else if(!strncmp("NOTICE", prio_str, 6)) prio = NOTICE; else if(!strncmp("INFO", prio_str, 6)) prio = INFO; else if(!strncmp("DEBUG", prio_str, 6)) prio = DEBUG; else { lua_pushstring(L, "invalid log priority"); lua_error(L); } } else { lua_pushstring(L, "invalid log priority"); lua_error(L); } log_printf(prio, "%s", luaL_checklstring(L, 2, NULL)); return 0; } /* void log_print_hex_dump(log_prio_t prio, const u_int8_t* buf, u_int32_t len); */ static const struct luaL_reg loglib [] = { { "init", l_log_init }, { "close", l_log_close }, { "add_target", l_log_add_target }, { "print", l_log_print }, { NULL, NULL } }; LUALIB_API int luaopen_log (lua_State *L) { luaL_openlib(L, "log", loglib, 0); return 1; }