diff --git a/src/fusedav_config.c b/src/fusedav_config.c index 2b92554..5e8ea1f 100644 --- a/src/fusedav_config.c +++ b/src/fusedav_config.c @@ -78,7 +78,7 @@ static int fusedav_opt_proc(void *data, const char *arg, int key, struct fuse_ar break; case KEY_IGNORE: - return 0; + return 0; case KEY_HELP: fprintf(stderr, @@ -130,6 +130,7 @@ static void print_config(struct fusedav_config *config) { log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "log_level %d", config->log_level); log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "log_level_by_section %s", config->log_level_by_section); log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "log_prefix %s", config->log_prefix); + log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "log_destination %s", config->log_destination); log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "max_file_size %d", config->max_file_size); log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "statsd_host %s", config->statsd_host); log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "statsd_port %s", config->statsd_port); @@ -156,6 +157,7 @@ run_as_gid=6f7a106722f74cc7bd96d4d06785ed78 log_level=5 log_level_by_section=0 log_prefix=6f7a106722f74cc7bd96d4d06785ed78 +log_destination=journal max_file_size=256 statsd_host=127.0.0.1 statsd_port=8126 @@ -198,6 +200,7 @@ static void parse_configs(struct fusedav_config *config, GError **gerr) { keytuple(fusedav, log_level, INT), keytuple(fusedav, log_level_by_section, STRING), keytuple(fusedav, log_prefix, STRING), + keytuple(fusedav, log_destination, STRING), keytuple(fusedav, max_file_size, INT), keytuple(fusedav, statsd_host, STRING), keytuple(fusedav, statsd_port, STRING), @@ -290,6 +293,7 @@ void configure_fusedav(struct fusedav_config *config, struct fuse_args *args, ch config->nodaemon = false; config->max_file_size = 256; // 256M config->log_level = 5; // default log_level: LOG_NOTICE + config->log_destination = JOURNAL; asprintf(&config->statsd_host, "%s", "127.0.0.1"); asprintf(&config->statsd_port, "%s", "8126"); @@ -312,7 +316,7 @@ void configure_fusedav(struct fusedav_config *config, struct fuse_args *args, ch asprintf(&user_agent, "FuseDAV/%s %s", PACKAGE_VERSION, config->log_prefix); - log_init(config->log_level, config->log_level_by_section, config->log_prefix); + log_init(config->log_level, config->log_level_by_section, config->log_prefix, config->log_destination); log_print(LOG_DEBUG, SECTION_CONFIG_DEFAULT, "log_level: %d.", config->log_level); if (stats_init(config->statsd_host, config->statsd_port) < 0) { diff --git a/src/fusedav_config.h b/src/fusedav_config.h index d32b390..fc66d25 100644 --- a/src/fusedav_config.h +++ b/src/fusedav_config.h @@ -52,6 +52,7 @@ struct fusedav_config { int log_level; char *log_level_by_section; char *log_prefix; + char *log_destination; int max_file_size; char *statsd_host; char *statsd_port; diff --git a/src/log.c b/src/log.c index 6dd6d3b..c1c7efb 100644 --- a/src/log.c +++ b/src/log.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "log.h" #include "log_sections.h" @@ -53,10 +54,19 @@ __thread unsigned int LOG_DYNAMIC = LOG_INFO; // max size for strings in log_key_value array #define KVITEM_SIZE 64 +static const char *log_template = "{\"MESSAGE\": \"%s%s\", " + "\"PRIORITY\": %d, " + "\"USER_AGENT\": \"%s\", " + "\"SITE\": \"%s\", " + "\"ENVIRONMENT\": \"%s\", " + "\"HOST_ADDRESS\": \"%s\", " + "\"TID\": \"%lu\", " + "\"PACKAGE_VERSION\": \"%s\"}\n"; + static unsigned int global_log_level = 5; static unsigned int section_log_levels[SECTIONS] = {0}; static const char *log_key_value[KVITEMS]; - +static enum log_destination log_destination = JOURNAL; static const char *errlevel[] = {"EMERG: ", "ALERT: ", "CRIT: ", "ERR: ", "WARN: ", "NOTICE: ", "INFO: ", "DEBUG: "}; // From the base url get the site id and site env @@ -85,7 +95,7 @@ static void initialize_site(void) { } /* The log_prefix comes from fusedav.conf; the base_url from curl and fuse. */ -void log_init(unsigned int log_level, const char *log_level_by_section, const char *user_agent_abbrev) { +void log_init(unsigned int log_level, const char *log_level_by_section, const char *user_agent_abbrev, const char *destination) { unsigned int vlen; @@ -100,6 +110,10 @@ void log_init(unsigned int log_level, const char *log_level_by_section, const ch log_key_value[USER_AGENT_ABBREV] = "(null)"; } + if ((destination != NULL) && strncmp(destination, "stdout", sizeof("stdout")) == 0) { + log_destination = STDOUT; + } + initialize_site(); if (log_level_by_section == NULL) return; @@ -168,9 +182,22 @@ int logging(unsigned int log_level, unsigned int section) { } static int print_it(const char const *formatwithtid, const char const *msg, int log_level) { - int ret; - // fusedav-server standardizing on names BINDING, SITE, and ENVIRONMENT - ret = sd_journal_send("MESSAGE=%s%s", formatwithtid, msg, + if (log_destination == STDOUT) { + char * escaped = g_strescape(msg, NULL); + printf(log_template, + formatwithtid, escaped, + log_level, + get_user_agent(), + log_key_value[SITE], + log_key_value[ENVIRONMENT], + log_key_value[HOST_ADDRESS], + syscall(SYS_gettid), + PACKAGE_VERSION, + NULL); + free(escaped); + return 0; + } + return sd_journal_send("MESSAGE=%s%s", formatwithtid, msg, "PRIORITY=%d", log_level, "USER_AGENT=%s", get_user_agent(), "SITE=%s", log_key_value[SITE], @@ -179,7 +206,6 @@ static int print_it(const char const *formatwithtid, const char const *msg, int "TID=%lu", syscall(SYS_gettid), "PACKAGE_VERSION=%s", PACKAGE_VERSION, NULL); - return ret; } #define max_msg_sz 2048 @@ -196,7 +222,7 @@ int log_print(unsigned int log_level, unsigned int section, const char *format, assert(formatwithlevel); // print the intended message - ret = print_it(formatwithlevel, msg, log_level); + print_it(formatwithlevel, msg, log_level); // Check and see if we're no longer doing dynamic logging. If so, it will take effect after this call. Then print a message if (turning_off_dynamic_logging()) { diff --git a/src/log.h b/src/log.h index 971436f..8c53386 100644 --- a/src/log.h +++ b/src/log.h @@ -22,7 +22,13 @@ extern __thread unsigned int LOG_DYNAMIC; -void log_init(unsigned int log_level, const char *log_level_by_section, const char *user_agent); +enum log_destination { + JOURNAL, + STDOUT +}; + + +void log_init(unsigned int log_level, const char *log_level_by_section, const char *user_agent, const char *destination); int log_print(unsigned int log_level, unsigned int section, const char *format, ...); int logging(unsigned int log_level, unsigned int section); void set_dynamic_logging(void);