From 7656082dc2f185ab67c66ce2e85c90a6dc50c5a2 Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 13 Apr 2024 02:16:21 +0000 Subject: [PATCH] syslog module works git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@8 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- DevForge/log.c | 34 +++++++++++++++++++++++++++++----- DevForge/main.c | 1 + Module/syslog.c | 17 ++++++++++++++++- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/DevForge/log.c b/DevForge/log.c index 593b64a..288d03b 100644 --- a/DevForge/log.c +++ b/DevForge/log.c @@ -30,13 +30,37 @@ #include "df_log.h" +#include "df_config.h" +#include "devforge.h" + #include #include +#include +#include +#include void devforge_log(const char* name, const char* log) { - char timestr[256]; - time_t t = time(NULL); - struct tm* tm = localtime(&t); - strftime(timestr, 255, "%Y-%m-%d %H:%M:%S%z", tm); - fprintf(stderr, "%s %8s %s\n", timestr, name, log); + bool fallback = true; + if(loaded_mods != NULL){ + int i; + for(i = 0; loaded_mods[i] != NULL; i++){ + const char* type = (const char*)dlsym(loaded_mods[i], "mod_type"); + if(strcmp(type, DF_MOD_LOG) == 0){ + void(*mod_log)(const char*, const char*) = (void(*)(const char*, const char*))dlsym(loaded_mods[i], "mod_log"); + if(mod_log != NULL){ + fallback = false; + mod_log(name, log); + break; + } + } + } + } + + if(fallback){ + char timestr[256]; + time_t t = time(NULL); + struct tm* tm = localtime(&t); + strftime(timestr, 255, "%Y-%m-%d %H:%M:%S%z", tm); + fprintf(stderr, "%s %8s %s\n", timestr, name, log); + } } diff --git a/DevForge/main.c b/DevForge/main.c index cb913b8..9288555 100644 --- a/DevForge/main.c +++ b/DevForge/main.c @@ -71,4 +71,5 @@ int main(int argc, char** argv) { int ret = devforge_load_config(PREFIX "/etc/devforge.conf"); if(ret != 0) return ret; } + devforge_log(DF_INFO, "Hello, world!"); } diff --git a/Module/syslog.c b/Module/syslog.c index cc4e42c..c013f36 100644 --- a/Module/syslog.c +++ b/Module/syslog.c @@ -31,7 +31,10 @@ #include "../DevForge/devforge.h" #include "../DevForge/df_log.h" -const char type[] = DF_MOD_LOG; +#include +#include + +const char mod_type[] = DF_MOD_LOG; void(*putlog)(const char* name, const char* log); @@ -39,3 +42,15 @@ void mod_init(void(*_putlog)(const char* name, const char* log)){ putlog = _putlog; putlog(DF_INFO, "Syslog Module init"); } + +void mod_log(const char* name, const char* log){ + if(strcmp(name, DF_INFO) == 0){ + syslog(LOG_INFO, log); + }else if(strcmp(name, DF_LOG) == 0){ + syslog(LOG_NOTICE, log); + }else if(strcmp(name, DF_ERROR) == 0){ + syslog(LOG_ERR, log); + }else if(strcmp(name, DF_WARN) == 0){ + syslog(LOG_WARNING, log); + } +} -- 2.43.0