From 46428f37d62672a3d17663d1d704a6ebc3f21073 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 17 Oct 2014 09:16:26 +0200 Subject: replaced log( "LEVEL") Lua function with 'log.level' table of C functions for logging --- src/crawl/crawl.conf | 12 ++++++------ src/crawl/crawl.cpp | 43 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/crawl/crawl.conf b/src/crawl/crawl.conf index 29c3620..46599d6 100644 --- a/src/crawl/crawl.conf +++ b/src/crawl/crawl.conf @@ -81,24 +81,24 @@ filters = { function init( ) - log( "NOTICE", "Init.." ) + log.notice( "Init.." ) -- normalizer = urlnormalizers.create( "google_urlnormalizer" ); normalizer = GoogleURLNormalizer:new( ) -- normalizer2 = urlnormalizers.create( "simple_urlnormalizer" ); normalizer2 = SimpleURLNormalizer:new( ) base = tolua.cast( normalizer, "URLNormalizer" ) - log( "DEBUG", "type: " .. tolua.type( base ) ) + log.debug( "type: " .. tolua.type( base ) ) end function destroy( ) - log( "NOTICE", "Destroy.." ) + log.notice( "Destroy.." ) normalizer:delete( ) end function crawl( ) - log( "NOTICE", "Crawling.." ) + log.notice( "Crawling.." ) local baseUrl = base:parseUrl( "http://www.base.com" ) - log( "DEBUG", "base URL is: " .. baseUrl:str( ) ) + log.debug( "base URL is: " .. baseUrl:str( ) ) local url = base:normalize( baseUrl, "/relativedir/relativefile.html" ) - log( "DEBUG", "URL is: " .. url:str( ) ) + log.debug( "URL is: " .. url:str( ) ) end diff --git a/src/crawl/crawl.cpp b/src/crawl/crawl.cpp index 0698b75..24d6427 100755 --- a/src/crawl/crawl.cpp +++ b/src/crawl/crawl.cpp @@ -90,16 +90,15 @@ static vector searchModuleFiles( const vector &modules, const ve return moduleFiles; } -static int lua_log( lua_State *l ) +static int lua_log_level( const LogLevel logLevel, lua_State *l ) { size_t nofParams = lua_gettop( l ); if( nofParams == 0 ) return 0; - const char *logLevel = luaL_checkstring( l, 1 ); ostringstream ss; - for( size_t i = 2; i <= nofParams; i++ ) { + for( size_t i = 1; i <= nofParams; i++ ) { int type = lua_type( l, i ); switch( type ) { @@ -124,13 +123,32 @@ static int lua_log( lua_State *l ) } } - LOG( Logger::fromString( logLevel ) ) << ss.str( ); + LOG( logLevel ) << ss.str( ); lua_pop( l, (int)nofParams ); return 0; } + +#define lua_log( level ) \ +static int lua_log_ ## level( lua_State *l ) \ +{ \ + return lua_log_level( log ## level, l ); \ +} + +lua_log( FATAL ) +lua_log( CRITICAL ) +lua_log( ERROR ) +lua_log( WARNING ) +lua_log( NOTICE ) +lua_log( INFO ) +lua_log( DEBUG ) +lua_log( DEBUG1 ) +lua_log( DEBUG2 ) +lua_log( DEBUG3 ) +lua_log( DEBUG4 ) + int main( int /* argc */, char *argv[] ) { try { @@ -144,7 +162,22 @@ int main( int /* argc */, char *argv[] ) luaVm.loadSource( argv[1] ); // register logging function - lua_pushcclosure( luaVm.handle( ), &lua_log, 0 ); + luaL_Reg reg[12] = { + { "fatal", lua_log_FATAL }, + { "critical", lua_log_CRITICAL }, + { "error", lua_log_ERROR }, + { "warning", lua_log_WARNING }, + { "notice", lua_log_NOTICE }, + { "info", lua_log_INFO }, + { "debug", lua_log_DEBUG }, + { "debug1", lua_log_DEBUG1 }, + { "debug2", lua_log_DEBUG2 }, + { "debug3", lua_log_DEBUG3 }, + { "debug4", lua_log_DEBUG4 }, + { NULL, NULL } + }; + lua_newtable( luaVm.handle( ) ); + luaL_setfuncs( luaVm.handle( ), reg, 0 ); lua_setglobal( luaVm.handle( ), "log" ); // execute main (to get basic configuration in form -- cgit v1.2.3-54-g00ecf