From 1b9739969443c78639cf6ee67bb804ad8bd9ff27 Mon Sep 17 00:00:00 2001 From: kakwa Date: Wed, 10 Apr 2019 22:47:38 +0200 Subject: [PATCH] better handling for logging to stdout * disable stdout buffering when logging to stdout * add 'log_to_syslog' parameter in configuration file to enable/disable logging to syslog * add 'log_to_stdout' parameter in configuration file to enable/disable logging to stdout slight clean-up and reformatting also (thanks to clang-format new version) --- conf/uts-server.cnf | 6 ++++++ inc/context.h | 11 +++++++++-- src/lib/http.c | 20 +++++++++++++------- src/lib/rfc3161.c | 5 +++-- src/lib/utils.c | 25 ++++++++++++++++++++----- 5 files changed, 51 insertions(+), 16 deletions(-) diff --git a/conf/uts-server.cnf b/conf/uts-server.cnf index 6dff509..d3dae19 100644 --- a/conf/uts-server.cnf +++ b/conf/uts-server.cnf @@ -103,6 +103,12 @@ tcp_nodelay = 0 # Loglevel (debug, info, notice, warn, err, emerg, crit) log_level = info +# Enable logging to syslog (default: yes) +log_to_syslog = yes + +# Enable logging to stdout (default: no) +#log_to_stdout = no + # TSA configuration parameters. [ tsa ] diff --git a/inc/context.h b/inc/context.h index e4f4dcf..ff88fa1 100644 --- a/inc/context.h +++ b/inc/context.h @@ -9,8 +9,9 @@ #define HTTP_OPTIONS 1 #define LOGLEVEL_OPTIONS 2 -#define TSA_OPTIONS 3 -#define PATH_HTTP_OPTIONS 4 +#define LOGHANDLER_OPTIONS 3 +#define TSA_OPTIONS 4 +#define PATH_HTTP_OPTIONS 5 #define MAIN_CONF_SECTION "main" #define RFC3161_OPTIONS_LEN \ @@ -26,6 +27,10 @@ typedef struct { uint64_t query_counter; // flag for debugging bool stdout_dbg; + // flag for logging to stdout + bool stdout_logging; + // flag for logging to stdout + bool syslog_logging; // log level int loglevel; // number of threads @@ -64,6 +69,8 @@ static struct rfc3161_option rfc3161_options[] = { {"access_control_allow_origin", HTTP_OPTIONS, "*"}, {"tcp_nodelay", HTTP_OPTIONS, "0"}, {"log_level", LOGLEVEL_OPTIONS, "info"}, + {"log_to_syslog", LOGHANDLER_OPTIONS, "yes"}, + {"log_to_stdout", LOGHANDLER_OPTIONS, "no"}, {"ssl_certificate", PATH_HTTP_OPTIONS, NULL}, {"ssl_ca_path", PATH_HTTP_OPTIONS, NULL}, {"ssl_ca_file", PATH_HTTP_OPTIONS, NULL}, diff --git a/src/lib/http.c b/src/lib/http.c index c85bcfa..9ab3c18 100644 --- a/src/lib/http.c +++ b/src/lib/http.c @@ -92,9 +92,10 @@ void log_request(const struct mg_request_info *request_info, char *request_id, } } - uts_logger(context, LOG_INFO, "Request[%s], remote_addr[%s] ssl[%d] " - "uri[%s] http_resp_code[%d] duration[%d us] " - "user-agent[%s] content-type[%s]", + uts_logger(context, LOG_INFO, + "Request[%s], remote_addr[%s] ssl[%d] " + "uri[%s] http_resp_code[%d] duration[%d us] " + "user-agent[%s] content-type[%s]", request_id, null_undef(request_info->remote_addr), request_info->is_ssl, null_undef(request_info->local_uri), response_code, timer, null_undef(user_agent), @@ -178,10 +179,11 @@ int rfc3161_handler(struct mg_connection *conn, void *context) { // respond according to create_response return code switch (resp_code) { case 200: - mg_printf(conn, "HTTP/1.1 200 OK\r\n" - "Content-Type: application/timestamp-reply\r\n" - "Content-Length: %d\r\n" - "\r\n", + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/timestamp-reply\r\n" + "Content-Length: %d\r\n" + "\r\n", (int)content_length); mg_write(conn, content, content_length); log_hex(ct, LOG_DEBUG, "response hexdump content", content, @@ -241,6 +243,10 @@ int http_server_start(char *conffile, char *conf_wd, bool stdout_dbg) { if (!set_params(ct, conffile, conf_wd)) return EXIT_FAILURE; + // Disable stdout buffering if logging to stdout + if (ct->stdout_logging || ct->stdout_dbg) + setbuf(stdout, NULL); + // Prepare callbacks structure. We have only one callback, the rest are // NULL. memset(&callbacks, 0, sizeof(callbacks)); diff --git a/src/lib/rfc3161.c b/src/lib/rfc3161.c index d923c8f..e8b6865 100644 --- a/src/lib/rfc3161.c +++ b/src/lib/rfc3161.c @@ -319,8 +319,9 @@ end: ret = 200; break; case TS_STATUS_GRANTED_WITH_MODS: - uts_logger(ct, LOG_NOTICE, "timestamp request granted with " - "modification", + uts_logger(ct, LOG_NOTICE, + "timestamp request granted with " + "modification", *serial_id); ret = 200; break; diff --git a/src/lib/utils.c b/src/lib/utils.c index 4adb262..90e06cb 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -162,7 +161,7 @@ void uts_logger(rfc3161_context *ct, int priority, char *fmt, ...) { fclose(stream); // if in debugging mode, also log to stdout - if (ct->stdout_dbg) { + if (ct->stdout_logging || ct->stdout_dbg) { switch (priority) { case LOG_EMERG: printf("LOG_EMER : %s\n", out); @@ -197,7 +196,8 @@ void uts_logger(rfc3161_context *ct, int priority, char *fmt, ...) { ; } } - syslog(priority, "%s", out); + if (ct->syslog_logging) + syslog(priority, "%s", out); free(out); } @@ -301,6 +301,21 @@ int set_params(rfc3161_context *ct, char *conf_file, char *conf_wd) { } break; ; + case LOGHANDLER_OPTIONS: + if (strcmp(name, "log_to_syslog") == 0) { + if (strcmp(value, "yes")) + ct->syslog_logging = 0; + else + ct->syslog_logging = 1; + } + if (strcmp(name, "log_to_stdout") == 0) { + if (strcmp(value, "yes")) + ct->stdout_logging = 0; + else + ct->stdout_logging = 1; + } + break; + ; } } // parse the options to get the civetweb options and a few other things @@ -318,8 +333,8 @@ int set_params(rfc3161_context *ct, char *conf_file, char *conf_wd) { uts_logger(ct, LOG_DEBUG, "configuration param['%s'] = '%s'", name, null_undef(value)); switch (type) { - // if it's an http (civetweb) option, put it in the http_options buffer - // like civetweb is expected it. + // if it's an http (civetweb) option, put it in the http_options + // buffer like civetweb is expected it. case HTTP_OPTIONS: if (value != NULL) { ct->http_options[http_counter] = name;