From e27bde06ac53effb8140f7776087557490ab8f82 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 13 Jun 2014 13:02:03 -0700 Subject: [PATCH] bindings for error logging, phase 1. better error reporting for json parse errors. --- se/eressea.vpj | 4 +- src/CMakeLists.txt | 2 + src/bind_config.c | 21 +++++++-- src/bind_config.h | 4 +- src/bind_log.c | 0 src/bind_log.h | 0 src/bindings.c | 2 + src/log.pkg | 9 ++++ src/log.pkg.c | 105 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 src/bind_log.c create mode 100644 src/bind_log.h create mode 100644 src/log.pkg create mode 100644 src/log.pkg.c diff --git a/se/eressea.vpj b/se/eressea.vpj index 58c297c2c..f131f3443 100644 --- a/se/eressea.vpj +++ b/se/eressea.vpj @@ -547,6 +547,8 @@ Filters="*.bmp"/> + Filters=""> + + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 64ad949e6..9fecadc88 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,6 +48,7 @@ MACRO(TOLUA_BINDING PKGFILE FILES) ENDMACRO(TOLUA_BINDING) IF(NOT MSVC) +TOLUA_BINDING(log.pkg util/log.h) TOLUA_BINDING(config.pkg bind_config.h) TOLUA_BINDING(process.pkg bind_process.h) TOLUA_BINDING(eressea.pkg bind_eressea.h) @@ -86,6 +87,7 @@ set(SERVER_SRC console.c helpers.c config.pkg.c + log.pkg.c process.pkg.c eressea.pkg.c settings.pkg.c diff --git a/src/bind_config.c b/src/bind_config.c index 46fd9b973..537dd1501 100644 --- a/src/bind_config.c +++ b/src/bind_config.c @@ -5,18 +5,33 @@ #include #include #include +#include -void config_parse(const char *json) +int config_parse(const char *json) { cJSON * conf = cJSON_Parse(json); if (conf) { json_config(conf); cJSON_Delete(conf); + return 0; } else { - log_error("json parse error: %s\n", cJSON_GetErrorPtr()); + int line; + char buffer[10]; + const char *xp = json, *lp, *ep = cJSON_GetErrorPtr(); + for (line=0;xp && xp json + 10) ? ep - 10 : json; + strncpy(buffer, xp, sizeof(buffer)); + buffer[9] = 0; + log_error("json parse error in line %d, position %d, near `%s`\n", line, ep-lp, buffer); + return 1; } } -void config_read(const char *filename) +int config_read(const char *filename) { + return 1; } + diff --git a/src/bind_config.h b/src/bind_config.h index 7ddc91c92..cae19a485 100644 --- a/src/bind_config.h +++ b/src/bind_config.h @@ -4,8 +4,8 @@ extern "C" { #endif -void config_parse(const char *json); -void config_read(const char *filename); +int config_parse(const char *json); +int config_read(const char *filename); #ifdef __cplusplus } diff --git a/src/bind_log.c b/src/bind_log.c new file mode 100644 index 000000000..e69de29bb diff --git a/src/bind_log.h b/src/bind_log.h new file mode 100644 index 000000000..e69de29bb diff --git a/src/bindings.c b/src/bindings.c index 748049e02..74ee744d1 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -86,6 +86,7 @@ TOLUA_PKG(eressea); TOLUA_PKG(process); TOLUA_PKG(settings); TOLUA_PKG(config); +TOLUA_PKG(log); int log_lua_error(lua_State * L) { @@ -1060,6 +1061,7 @@ int tolua_bindings_open(lua_State * L) tolua_process_open(L); tolua_settings_open(L); tolua_config_open(L); + tolua_log_open(L); /* register user types */ tolua_usertype(L, TOLUA_CAST "spell"); diff --git a/src/log.pkg b/src/log.pkg new file mode 100644 index 000000000..c25f8317c --- /dev/null +++ b/src/log.pkg @@ -0,0 +1,9 @@ +$#include + +module eressea { + module log { + void log_error @ error(const char *message); + void log_warning @ warning(const char *message); + } +} + diff --git a/src/log.pkg.c b/src/log.pkg.c new file mode 100644 index 000000000..9950f59b2 --- /dev/null +++ b/src/log.pkg.c @@ -0,0 +1,105 @@ +/* +** Lua binding: log +*/ + +#include "tolua.h" + +#ifndef __cplusplus +#include +#endif +#ifdef __cplusplus + extern "C" int tolua_bnd_takeownership (lua_State* L); // from tolua_map.c +#else + int tolua_bnd_takeownership (lua_State* L); /* from tolua_map.c */ +#endif +#include + +/* Exported function */ +TOLUA_API int tolua_log_open (lua_State* tolua_S); +LUALIB_API int luaopen_log (lua_State* tolua_S); + +#include + +/* function to register type */ +static void tolua_reg_types (lua_State* tolua_S) +{ +} + +/* function: log_error */ +static int tolua_log_eressea_log_error00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isstring(tolua_S,1,0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const char* message = ((const char*) tolua_tostring(tolua_S,1,0)); + { + log_error(message); + } + } + return 0; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'error'.",&tolua_err); + return 0; +#endif +} + +/* function: log_warning */ +static int tolua_log_eressea_log_warning00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isstring(tolua_S,1,0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const char* message = ((const char*) tolua_tostring(tolua_S,1,0)); + { + log_warning(message); + } + } + return 0; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'warning'.",&tolua_err); + return 0; +#endif +} + +/* Open lib function */ +LUALIB_API int luaopen_log (lua_State* tolua_S) +{ + tolua_open(tolua_S); + tolua_reg_types(tolua_S); + tolua_module(tolua_S,NULL,0); + tolua_beginmodule(tolua_S,NULL); + tolua_module(tolua_S,"eressea",0); + tolua_beginmodule(tolua_S,"eressea"); + tolua_module(tolua_S,"log",0); + tolua_beginmodule(tolua_S,"log"); + tolua_function(tolua_S,"error",tolua_log_eressea_log_error00); + tolua_function(tolua_S,"warning",tolua_log_eressea_log_warning00); + tolua_endmodule(tolua_S); + tolua_endmodule(tolua_S); + tolua_endmodule(tolua_S); + return 1; +} +/* Open tolua function */ +TOLUA_API int tolua_log_open (lua_State* tolua_S) +{ + lua_pushcfunction(tolua_S, luaopen_log); + lua_pushstring(tolua_S, "log"); + lua_call(tolua_S, 1, 0); + return 1; +}