From ef3a897581a73bbdd3792953d61409e3050758bd Mon Sep 17 00:00:00 2001 From: Axel Kittenberger Date: Tue, 3 Apr 2018 08:59:04 +0200 Subject: [PATCH] structering the core some more --- core/core.c | 161 ---------------------------------------------------- core/log.c | 77 +++++++++++++++++++++++++ core/log.h | 3 + core/time.h | 4 ++ 4 files changed, 84 insertions(+), 161 deletions(-) diff --git a/core/core.c b/core/core.c index 2669e64..b0dcee5 100644 --- a/core/core.c +++ b/core/core.c @@ -113,12 +113,6 @@ char * lsyncd_config_file = NULL; bool first_time = true; -/* -| The kernel's clock ticks per second. -*/ -extern long clocks_per_sec; - - /*:::::::::::::::::::. :: Helper Routines '::::::::::::::::::::*/ @@ -143,83 +137,6 @@ static int mci = 0; int callError; -/* -| A user observance became read-ready. -*/ -static void -user_obs_ready( - lua_State * L, - int fd, - void * extra -) -{ - // pushes the ready table on table - lua_pushlightuserdata( L, ( void * ) user_obs_ready ); - lua_gettable( L, LUA_REGISTRYINDEX ); - - // pushes the error handler - lua_pushlightuserdata( L, (void *) &callError ); - lua_gettable( L, LUA_REGISTRYINDEX ); - - // pushes the user func - lua_pushnumber( L, fd ); - lua_gettable( L, -3 ); - - // gives the ufunc the fd - lua_pushnumber( L, fd ); - - // calls the user function - if( lua_pcall( L, 1, 0, -3 ) ) exit( -1 ); - - lua_pop( L, 2 ); -} - - -/* -| A user observance became write-ready -*/ -static void -user_obs_writey( - lua_State * L, - int fd, - void * extra -) -{ - // pushes the writey table on table - lua_pushlightuserdata( L, (void *) user_obs_writey ); - lua_gettable( L, LUA_REGISTRYINDEX ); - - // pushes the error handler - lua_pushlightuserdata(L, (void *) &callError); - lua_gettable( L, LUA_REGISTRYINDEX ); - - // pushes the user func - lua_pushnumber( L, fd ); - lua_gettable( L, -3 ); - - // gives the user func the fd - lua_pushnumber( L, fd ); - - // calls the user function - if( lua_pcall( L, 1, 0, -3 ) ) exit(-1); - - lua_pop( L, 2 ); -} - -/* -| Tidies up a user observance -| FIXME - give the user a chance to do something in that case! -*/ -static void -user_obs_tidy( - int fd, - void * extra -) -{ - close( fd ); -} - - /*:::::::::::::::::::::::::::::::. :: Library calls for the mantle '::::::::::::::::::::::::::::::::*/ @@ -228,84 +145,6 @@ user_obs_tidy( int l_stackdump( lua_State* L ); -/* -| Logs a message. -| -| Params on Lua stack: -| -| 1: loglevel of massage -| 2: the string to log -*/ -static int -l_log( lua_State *L ) -{ - const char * cat; // log category - const char * message; // log message - int priority; // log priority - - cat = luaL_checkstring( L, 1 ); - priority = check_logcat( cat ); - - // skips filtered messages - if( priority > settings.log_level ) return 0; - - // replaces non string values - { - int i; - int top = lua_gettop(L); - for( i = 1; i <= top; i++ ) - { - int t = lua_type( L, i ); - - switch( t ) - { - case LUA_TTABLE : - lua_pushfstring( - L, - "(Table: %p)", - lua_topointer( L, i ) - ); - - lua_replace( L, i ); - break; - - case LUA_TBOOLEAN : - if( lua_toboolean( L, i ) ) - { lua_pushstring( L, "(true)" ); } - else - { lua_pushstring( L, "(false)" ); } - - lua_replace(L, i); - break; - - case LUA_TUSERDATA: - { - clock_t *c = ( clock_t * ) luaL_checkudata( L, i, "Lsyncd.jiffies" ); - - double d = *c; - d /= clocks_per_sec; - lua_pushfstring( L, "(Timestamp: %f)", d ); - lua_replace( L, i ); - } - break; - - case LUA_TNIL: - lua_pushstring( L, "(nil)" ); - lua_replace( L, i ); - break; - } - } - } - - // concates if there is more than one string parameter - lua_concat( L, lua_gettop( L ) - 1 ); - - message = luaL_checkstring( L, 2 ); - logstring0( priority, cat, message ); - - return 0; -} - /* | Executes a subprocess. Does not wait for it to return. diff --git a/core/log.c b/core/log.c index 784448f..04f4e4b 100644 --- a/core/log.c +++ b/core/log.c @@ -27,6 +27,7 @@ #include "log.h" #include "mem.h" +#include "time.h" #include "lsyncd.h" @@ -295,3 +296,79 @@ log_free( ) } +/* +| Logs a message. +| +| Params on Lua stack: +| +| 1: loglevel of massage +| 2: the string to log +*/ +int l_log( lua_State * L ) +{ + char const * cat; // log category + char const * message; // log message + int priority; // log priority + + cat = luaL_checkstring( L, 1 ); + priority = check_logcat( cat ); + + // skips filtered messages + if( priority > settings.log_level ) return 0; + + // replaces non string values + { + int i; + int top = lua_gettop(L); + for( i = 1; i <= top; i++ ) + { + int t = lua_type( L, i ); + + switch( t ) + { + case LUA_TTABLE : + + lua_pushfstring( L, "(Table: %p)", lua_topointer( L, i ) ); + + lua_replace( L, i ); + + break; + + case LUA_TBOOLEAN : + if( lua_toboolean( L, i ) ) + lua_pushstring( L, "(true)" ); + else + lua_pushstring( L, "(false)" ); + + lua_replace( L, i ); + + break; + + case LUA_TUSERDATA: + { + clock_t *c = ( clock_t * ) luaL_checkudata( L, i, "Lsyncd.jiffies" ); + + double d = *c; + d /= clocks_per_sec; + lua_pushfstring( L, "(Timestamp: %f)", d ); + lua_replace( L, i ); + } + break; + + case LUA_TNIL: + lua_pushstring( L, "(nil)" ); + lua_replace( L, i ); + break; + } + } + } + + // concates if there is more than one string parameter + lua_concat( L, lua_gettop( L ) - 1 ); + + message = luaL_checkstring( L, 2 ); + logstring0( priority, cat, message ); + + return 0; +} + diff --git a/core/log.h b/core/log.h index 8c12c89..e909af2 100644 --- a/core/log.h +++ b/core/log.h @@ -43,4 +43,7 @@ printlogf0( // Frees logging stuff extern void log_free( ); + +extern int l_log( lua_State * L ); + #endif diff --git a/core/time.h b/core/time.h index 2c6150a..17ff047 100644 --- a/core/time.h +++ b/core/time.h @@ -17,6 +17,10 @@ #define time_after_eq(a,b) ((long)(a) - (long)(b) >= 0) #define time_before_eq(a,b) time_after_eq(b,a) + +// The kernel's clock ticks per second. +extern long clocks_per_sec; + // Returns the current time. extern clock_t now( );