1
0
mirror of https://github.com/kakwa/uts-server synced 2024-12-04 23:15:54 +01:00

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)
This commit is contained in:
kakwa 2019-04-10 22:47:38 +02:00
parent 4d174251ff
commit 1b97399694
5 changed files with 51 additions and 16 deletions

View File

@ -103,6 +103,12 @@ tcp_nodelay = 0
# Loglevel (debug, info, notice, warn, err, emerg, crit) # Loglevel (debug, info, notice, warn, err, emerg, crit)
log_level = info 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 configuration parameters.
[ tsa ] [ tsa ]

View File

@ -9,8 +9,9 @@
#define HTTP_OPTIONS 1 #define HTTP_OPTIONS 1
#define LOGLEVEL_OPTIONS 2 #define LOGLEVEL_OPTIONS 2
#define TSA_OPTIONS 3 #define LOGHANDLER_OPTIONS 3
#define PATH_HTTP_OPTIONS 4 #define TSA_OPTIONS 4
#define PATH_HTTP_OPTIONS 5
#define MAIN_CONF_SECTION "main" #define MAIN_CONF_SECTION "main"
#define RFC3161_OPTIONS_LEN \ #define RFC3161_OPTIONS_LEN \
@ -26,6 +27,10 @@ typedef struct {
uint64_t query_counter; uint64_t query_counter;
// flag for debugging // flag for debugging
bool stdout_dbg; bool stdout_dbg;
// flag for logging to stdout
bool stdout_logging;
// flag for logging to stdout
bool syslog_logging;
// log level // log level
int loglevel; int loglevel;
// number of threads // number of threads
@ -64,6 +69,8 @@ static struct rfc3161_option rfc3161_options[] = {
{"access_control_allow_origin", HTTP_OPTIONS, "*"}, {"access_control_allow_origin", HTTP_OPTIONS, "*"},
{"tcp_nodelay", HTTP_OPTIONS, "0"}, {"tcp_nodelay", HTTP_OPTIONS, "0"},
{"log_level", LOGLEVEL_OPTIONS, "info"}, {"log_level", LOGLEVEL_OPTIONS, "info"},
{"log_to_syslog", LOGHANDLER_OPTIONS, "yes"},
{"log_to_stdout", LOGHANDLER_OPTIONS, "no"},
{"ssl_certificate", PATH_HTTP_OPTIONS, NULL}, {"ssl_certificate", PATH_HTTP_OPTIONS, NULL},
{"ssl_ca_path", PATH_HTTP_OPTIONS, NULL}, {"ssl_ca_path", PATH_HTTP_OPTIONS, NULL},
{"ssl_ca_file", PATH_HTTP_OPTIONS, NULL}, {"ssl_ca_file", PATH_HTTP_OPTIONS, NULL},

View File

@ -92,7 +92,8 @@ 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] " uts_logger(context, LOG_INFO,
"Request[%s], remote_addr[%s] ssl[%d] "
"uri[%s] http_resp_code[%d] duration[%d us] " "uri[%s] http_resp_code[%d] duration[%d us] "
"user-agent[%s] content-type[%s]", "user-agent[%s] content-type[%s]",
request_id, null_undef(request_info->remote_addr), request_id, null_undef(request_info->remote_addr),
@ -178,7 +179,8 @@ int rfc3161_handler(struct mg_connection *conn, void *context) {
// respond according to create_response return code // respond according to create_response return code
switch (resp_code) { switch (resp_code) {
case 200: case 200:
mg_printf(conn, "HTTP/1.1 200 OK\r\n" mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
"Content-Type: application/timestamp-reply\r\n" "Content-Type: application/timestamp-reply\r\n"
"Content-Length: %d\r\n" "Content-Length: %d\r\n"
"\r\n", "\r\n",
@ -241,6 +243,10 @@ int http_server_start(char *conffile, char *conf_wd, bool stdout_dbg) {
if (!set_params(ct, conffile, conf_wd)) if (!set_params(ct, conffile, conf_wd))
return EXIT_FAILURE; 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 // Prepare callbacks structure. We have only one callback, the rest are
// NULL. // NULL.
memset(&callbacks, 0, sizeof(callbacks)); memset(&callbacks, 0, sizeof(callbacks));

View File

@ -319,7 +319,8 @@ end:
ret = 200; ret = 200;
break; break;
case TS_STATUS_GRANTED_WITH_MODS: case TS_STATUS_GRANTED_WITH_MODS:
uts_logger(ct, LOG_NOTICE, "timestamp request granted with " uts_logger(ct, LOG_NOTICE,
"timestamp request granted with "
"modification", "modification",
*serial_id); *serial_id);
ret = 200; ret = 200;

View File

@ -8,7 +8,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <syslog.h> #include <syslog.h>
#include <unistd.h> #include <unistd.h>
@ -162,7 +161,7 @@ void uts_logger(rfc3161_context *ct, int priority, char *fmt, ...) {
fclose(stream); fclose(stream);
// if in debugging mode, also log to stdout // if in debugging mode, also log to stdout
if (ct->stdout_dbg) { if (ct->stdout_logging || ct->stdout_dbg) {
switch (priority) { switch (priority) {
case LOG_EMERG: case LOG_EMERG:
printf("LOG_EMER : %s\n", out); printf("LOG_EMER : %s\n", out);
@ -197,6 +196,7 @@ void uts_logger(rfc3161_context *ct, int priority, char *fmt, ...) {
; ;
} }
} }
if (ct->syslog_logging)
syslog(priority, "%s", out); syslog(priority, "%s", out);
free(out); free(out);
} }
@ -301,6 +301,21 @@ int set_params(rfc3161_context *ct, char *conf_file, char *conf_wd) {
} }
break; 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 // 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, uts_logger(ct, LOG_DEBUG, "configuration param['%s'] = '%s'", name,
null_undef(value)); null_undef(value));
switch (type) { switch (type) {
// if it's an http (civetweb) option, put it in the http_options buffer // if it's an http (civetweb) option, put it in the http_options
// like civetweb is expected it. // buffer like civetweb is expected it.
case HTTP_OPTIONS: case HTTP_OPTIONS:
if (value != NULL) { if (value != NULL) {
ct->http_options[http_counter] = name; ct->http_options[http_counter] = name;