From d5682fe186e6d0a32b394d59ea12bd50f07b29a9 Mon Sep 17 00:00:00 2001 From: lxsang Date: Wed, 12 Feb 2020 12:08:36 +0100 Subject: [PATCH] user syslog for logging --- antd-config.ini | 4 --- http_server.c | 73 ------------------------------------------------- httpd.c | 5 ++++ lib/handle.h | 3 -- lib/utils.h | 5 ++-- 5 files changed, 8 insertions(+), 82 deletions(-) diff --git a/antd-config.ini b/antd-config.ini index 588b4e5..8fe3f44 100644 --- a/antd-config.ini +++ b/antd-config.ini @@ -7,10 +7,6 @@ plugins_ext=.so database=/opt/www/database/ ; tmp dir tmpdir=/opt/www/tmp/ -; server log -server_log = /var/log/antd.log -; server error log -error_log = /var/log/antd_error.log ; max concurent connection maxcon=200 ; server backlocg diff --git a/http_server.c b/http_server.c index 8a1072e..924f4f6 100644 --- a/http_server.c +++ b/http_server.c @@ -27,57 +27,6 @@ config_t *config() { return &server_config; } - -void error_log(const char* fmt, ...) -{ - if(server_config.errorfp) - { - va_list arguments; - char * data; - va_start( arguments, fmt); - int dlen = vsnprintf(0,0,fmt,arguments) + 1; - va_end(arguments); - if ((data = (char*)malloc(dlen*sizeof(char))) != 0) - { - char buf[64]; - server_time(buf,64); - fwrite(buf,strlen(buf),1,server_config.errorfp); - va_start(arguments, fmt); - vsnprintf(data, dlen, fmt, arguments); - va_end(arguments); - fwrite(data,dlen,1,server_config.errorfp); - fflush(server_config.errorfp); - free(data); - } - } -} - -#ifdef DEBUG -void server_log(const char* fmt, ...) -{ - - if(server_config.logfp) - { - va_list arguments; - char * data; - va_start( arguments, fmt); - int dlen = vsnprintf(0,0,fmt,arguments) + 1; - va_end(arguments); - if ((data = (char*)malloc(dlen*sizeof(char))) != 0) - { - char buf[64]; - server_time(buf,64); - fwrite(buf,strlen(buf),1,server_config.logfp); - va_start(arguments, fmt); - vsnprintf(data, dlen, fmt, arguments); - va_end(arguments); - fwrite(data,dlen,1,server_config.logfp); - fflush(server_config.logfp); - free(data); - } - } -} -#endif void destroy_config() { freedict(server_config.handlers); @@ -93,16 +42,6 @@ void destroy_config() free(server_config.ssl_cipher); if(server_config.gzip_types) list_free(&server_config.gzip_types); - if(server_config.errorfp) - { - fclose(server_config.errorfp); - } -#ifdef DEBUG - if(server_config.logfp) - { - fclose(server_config.logfp); - } -#endif if(server_config.mimes) freedict(server_config.mimes); if(server_config.ports) @@ -166,10 +105,6 @@ static int config_handler(void *conf, const char *section, const char *name, { pconfig->n_workers = atoi(value); } - else if (MATCH("SERVER", "error_log")) - { - pconfig->errorfp = fopen(value, "w"); - } #ifdef USE_ZLIB else if (MATCH("SERVER", "gzip_enable")) { @@ -180,12 +115,6 @@ static int config_handler(void *conf, const char *section, const char *name, pconfig->gzip_types = split(value,","); } #endif -#ifdef DEBUG - else if (MATCH("SERVER", "server_log")) - { - pconfig->logfp = fopen(value, "w"); - } -#endif #ifdef USE_OPENSSL else if (MATCH("SERVER", "ssl.cert")) { @@ -290,8 +219,6 @@ void load_config(const char *file) server_config.maxcon = 100; server_config.max_upload_size = 10000000; //10Mb server_config.connection = 0; - server_config.errorfp = NULL; - server_config.logfp = NULL; server_config.mimes = dict(); server_config.enable_ssl = 0; server_config.sslcert = "cert.pem"; diff --git a/httpd.c b/httpd.c index ccc21c6..858bcfb 100644 --- a/httpd.c +++ b/httpd.c @@ -123,6 +123,8 @@ void configure_context(SSL_CTX *ctx) void stop_serve(int dummy) { UNUSED(dummy); + // close log server + closelog (); sigset_t mask; sigemptyset(&mask); //Blocks the SIG_IGN signal (by adding SIG_IGN to newMask) @@ -166,6 +168,9 @@ int main(int argc, char* argv[]) signal(SIGINT, stop_serve); config_t* conf = config(); + // start syslog + setlogmask (LOG_UPTO (LOG_NOTICE)); + openlog (SERVER_NAME, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER); #ifdef USE_OPENSSL if( conf->enable_ssl == 1 ) diff --git a/lib/handle.h b/lib/handle.h index 98e0dec..d55038a 100644 --- a/lib/handle.h +++ b/lib/handle.h @@ -85,9 +85,6 @@ typedef struct { int connection; int n_workers; int max_upload_size; - // log - FILE* errorfp; - FILE* logfp; // ssl int enable_ssl; char* sslcert; diff --git a/lib/utils.h b/lib/utils.h index f475b97..a4ab2eb 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -37,6 +37,7 @@ THE SOFTWARE. #include #include #include +#include #ifdef USE_OPENSSL #include #else @@ -56,12 +57,12 @@ THE SOFTWARE. #define false 0 #ifdef DEBUG - #define LOG(a,...) server_log(": [%s: %d]: " a "\n", __FILE__, \ + #define LOG(a,...) syslog (LOG_NOTICE,"[%s: %d]: " a "\n", __FILE__, \ __LINE__, ##__VA_ARGS__) #else #define LOG(a,...) do{}while(0) #endif -#define ERROR(a,...) error_log(": [%s: %d]: " a "\n", __FILE__, \ +#define ERROR(a,...) syslog (LOG_ERR, "[%s: %d]: " a "\n", __FILE__, \ __LINE__, ##__VA_ARGS__) // add this to the utils #define UNUSED(x) (void)(x)