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)
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 ]

View File

@ -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},

View File

@ -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));

View File

@ -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;

View File

@ -8,7 +8,6 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>
@ -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;