bindings for error logging, phase 1.

better error reporting for json parse errors.
This commit is contained in:
Enno Rehling 2014-06-13 13:02:03 -07:00
parent 97c17b1afa
commit e27bde06ac
9 changed files with 141 additions and 6 deletions

View File

@ -547,6 +547,8 @@
Filters="*.bmp"/> Filters="*.bmp"/>
<Folder <Folder
Name="Other Files" Name="Other Files"
Filters=""/> Filters="">
<F N="../src/log.pkg"/>
</Folder>
</Files> </Files>
</Project> </Project>

View File

@ -48,6 +48,7 @@ MACRO(TOLUA_BINDING PKGFILE FILES)
ENDMACRO(TOLUA_BINDING) ENDMACRO(TOLUA_BINDING)
IF(NOT MSVC) IF(NOT MSVC)
TOLUA_BINDING(log.pkg util/log.h)
TOLUA_BINDING(config.pkg bind_config.h) TOLUA_BINDING(config.pkg bind_config.h)
TOLUA_BINDING(process.pkg bind_process.h) TOLUA_BINDING(process.pkg bind_process.h)
TOLUA_BINDING(eressea.pkg bind_eressea.h) TOLUA_BINDING(eressea.pkg bind_eressea.h)
@ -86,6 +87,7 @@ set(SERVER_SRC
console.c console.c
helpers.c helpers.c
config.pkg.c config.pkg.c
log.pkg.c
process.pkg.c process.pkg.c
eressea.pkg.c eressea.pkg.c
settings.pkg.c settings.pkg.c

View File

@ -5,18 +5,33 @@
#include <kernel/jsonconf.h> #include <kernel/jsonconf.h>
#include <util/log.h> #include <util/log.h>
#include <cJSON.h> #include <cJSON.h>
#include <string.h>
void config_parse(const char *json) int config_parse(const char *json)
{ {
cJSON * conf = cJSON_Parse(json); cJSON * conf = cJSON_Parse(json);
if (conf) { if (conf) {
json_config(conf); json_config(conf);
cJSON_Delete(conf); cJSON_Delete(conf);
return 0;
} else { } 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<ep;++line) {
lp = xp;
xp = strchr(xp, '\n');
}
xp = (ep > 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;
} }

View File

@ -4,8 +4,8 @@
extern "C" { extern "C" {
#endif #endif
void config_parse(const char *json); int config_parse(const char *json);
void config_read(const char *filename); int config_read(const char *filename);
#ifdef __cplusplus #ifdef __cplusplus
} }

0
src/bind_log.c Normal file
View File

0
src/bind_log.h Normal file
View File

View File

@ -86,6 +86,7 @@ TOLUA_PKG(eressea);
TOLUA_PKG(process); TOLUA_PKG(process);
TOLUA_PKG(settings); TOLUA_PKG(settings);
TOLUA_PKG(config); TOLUA_PKG(config);
TOLUA_PKG(log);
int log_lua_error(lua_State * L) int log_lua_error(lua_State * L)
{ {
@ -1060,6 +1061,7 @@ int tolua_bindings_open(lua_State * L)
tolua_process_open(L); tolua_process_open(L);
tolua_settings_open(L); tolua_settings_open(L);
tolua_config_open(L); tolua_config_open(L);
tolua_log_open(L);
/* register user types */ /* register user types */
tolua_usertype(L, TOLUA_CAST "spell"); tolua_usertype(L, TOLUA_CAST "spell");

9
src/log.pkg Normal file
View File

@ -0,0 +1,9 @@
$#include <util/log.h>
module eressea {
module log {
void log_error @ error(const char *message);
void log_warning @ warning(const char *message);
}
}

105
src/log.pkg.c Normal file
View File

@ -0,0 +1,105 @@
/*
** Lua binding: log
*/
#include "tolua.h"
#ifndef __cplusplus
#include <stdlib.h>
#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 <string.h>
/* Exported function */
TOLUA_API int tolua_log_open (lua_State* tolua_S);
LUALIB_API int luaopen_log (lua_State* tolua_S);
#include <util/log.h>
/* 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;
}