From 445ced1dd80313f4a67302564b17f5e05db3c022 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20P=C3=ADsa=C5=99?= Date: Sun, 30 May 2010 14:54:48 +0200 Subject: [PATCH] Add log_file configuration option --- doc/shigofumi.xml | 20 +++++++++++++- src/shigofumi.c | 81 +++++++++++++++++++++++++++++++++---------------------- 2 files changed, 68 insertions(+), 33 deletions(-) diff --git a/doc/shigofumi.xml b/doc/shigofumi.xml index d738dba..3cb4875 100644 --- a/doc/shigofumi.xml +++ b/doc/shigofumi.xml @@ -328,7 +328,7 @@ timeout Non-negative integer setting network time-out in - miliseconds. Use 0 not to limit any network operation. + milliseconds. Use 0 not to limit any network operation. Default value is 10,000 ms. @@ -336,6 +336,24 @@ + Log Options + + + log_file + + String value selecting file to append + ISDS log. The log catches libisds internal + debugging protocol. It does not cover messages produces by + shigofumi itself. This feature is designed to debug underlying + libraries and protocols like ISDS + SOAP or cURL's HTTP(S). + If undefined, shigofumi logs to standard error output. + + + + + + Other Options diff --git a/src/shigofumi.c b/src/shigofumi.c index dd92f9d..7d85b16 100644 --- a/src/shigofumi.c +++ b/src/shigofumi.c @@ -30,6 +30,7 @@ #define CONFIG_CADIRECTORY "ca_directory" #define CONFIG_CRLFILE "crl_file" #define CONFIG_TIMEOUT "timeout" +#define CONFIG_LOGFILE "log_file" #define CONFIG_NORMALIZEMIMETYPE "normalize_mime_type" #define CZPDEPOSIT_URL "https://www.czechpoint.cz/uschovna/" @@ -46,6 +47,7 @@ cfg_opt_t configuration_syntax[] = { CFG_STR(CONFIG_CADIRECTORY, NULL, CFGF_NODEFAULT), CFG_STR(CONFIG_CRLFILE, NULL, CFGF_NODEFAULT), CFG_INT(CONFIG_TIMEOUT, TIMEOUT, CFGF_NONE), + CFG_STR(CONFIG_LOGFILE, NULL, CFGF_NODEFAULT), CFG_BOOL(CONFIG_NORMALIZEMIMETYPE, cfg_true, CFGF_NONE), CFG_END() }; @@ -178,6 +180,44 @@ static void finish_isds_operation(struct isds_ctx *ctx, isds_error err) { } +void logger(isds_log_facility facility, isds_log_level level, + const char *message, int length, void *data) { + int fd; + ssize_t written, left = length; + + if (!data) return; + fd = *((int *) data); + /*printf("\033[32mLOG(%02d,%02d): ", facility, level); + printf("%.*s", length, message); + printf("\033[m");*/ + + while (left) { + written = write(fd, message + length - left, left); + if (written == -1) { + fprintf(stderr, + _("Could not save log message into log file: %s\n" + "Log message discarded!\n"), + strerror(errno)); + /*close(fd); + fd = -1;*/ + return; + } + left-=written; + } +} + + +/* Redirect ISDS log to file if @file is not NULL. */ +static int do_log_to_file(const char *file) { + if (file && *file) { + logger_fd = open_file_for_writing(file, 0); + if (logger_fd == -1) return -1; + isds_set_log_callback(logger, &logger_fd); + } + return 0; +} + + /* Clamp long int to unsigned int */ static unsigned int normalize_timeout(long int raw) { if (raw < 0) { @@ -227,6 +267,13 @@ static int shi_init(const char *config_file) { return -1; } + /* Set ISDS logging */ + value = cfg_getstr(configuration, CONFIG_LOGFILE); + if (do_log_to_file(value)) { + fprintf(stderr, _("Could not redirect ISDS log to file `%s'\n"), value); + return -1; + } + /* Set ISDS context up */ cisds = isds_ctx_create(); if (!cisds) { @@ -924,33 +971,6 @@ static int shi_login(int argc, const char **argv) { } -void logger(isds_log_facility facility, isds_log_level level, - const char *message, int length, void *data) { - int fd; - ssize_t written, left = length; - - if (!data) return; - fd = *((int *) data); - /*printf("\033[32mLOG(%02d,%02d): ", facility, level); - printf("%.*s", length, message); - printf("\033[m");*/ - - while (left) { - written = write(fd, message + length - left, left); - if (written == -1) { - fprintf(stderr, - _("Could not save log message into log file: %s\n" - "Log message discarded!\n"), - strerror(errno)); - /*close(fd); - fd = -1;*/ - return; - } - left-=written; - } -} - - static void shi_debug_usage(const char *command) { printf(_( "Usage: %s -l LEVEL [-f FACILITY...] [{-e | -o FILE}]\n" @@ -1030,11 +1050,8 @@ static int shi_debug(int argc, const char **argv) { } } } - if (file && *file) { - logger_fd = open_file_for_writing(file, 0); - if (logger_fd == -1) return -1; - isds_set_log_callback(logger, &logger_fd); - } + if (do_log_to_file(file)) + return -1; /* Set log levels */ isds_set_logging(log_facility, log_level); -- 2.11.4.GIT