From 1f7551178dcffcb6175593b20361824e6923269c Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 5 Oct 2016 22:21:06 +0200 Subject: [PATCH] Allow variable arguments for logging. --- src/mrimap.c | 4 +--- src/mrlog.c | 46 ++++++++++++++++++++++++++-------------------- src/mrlog.h | 6 +++--- src/mrmailbox.c | 9 ++------- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/mrimap.c b/src/mrimap.c index 415a7054..3ab03cb1 100644 --- a/src/mrimap.c +++ b/src/mrimap.c @@ -365,9 +365,7 @@ static void fetch_from_all_folders(mrimap_t* ths, mrimapthreadval_t* threadval) } else { - char* p = sqlite3_mprintf("Folder \"%s\" ignored.", name_utf8); - mr_log_info(p); - sqlite3_free(p); + mr_log_info("Folder \"%s\" ignored.", name_utf8); } free(name_utf8); diff --git a/src/mrlog.c b/src/mrlog.c index c8692d02..061b7072 100644 --- a/src/mrlog.c +++ b/src/mrlog.c @@ -28,50 +28,56 @@ #include #include +#include #include "mrmailbox.h" #include "mrlog.h" -static void mr_log(char type, const char* msg) +static void mr_log(char type, const char* msg_format_str, va_list argp) { - if( msg == NULL ) { - return; /* this may happen if eg. sqlite3_mprintf() cannot allocate memory - normally, not. */ - } + const char *type_str; + char* msg_full_str, *log_entry_str; - const char* type_str; switch( type ) { case 'i': type_str = "Information"; break; - case 'w': type_str = "Warning"; break; - default: type_str = "ERROR"; break; + case 'w': type_str = "Warning"; break; + default: type_str = "ERROR"; break; } - char* p = sqlite3_mprintf("[%s] %s", type_str, msg); - if( p ) { - printf("%s\n", p); - sqlite3_free(p); - } + msg_full_str = sqlite3_vmprintf(msg_format_str, argp); if( msg_full_str == NULL ) { exit(18); } + log_entry_str = sqlite3_mprintf("[%s] %s", type_str, msg_full_str); if( log_entry_str == NULL ) { exit(19); } + printf("%s\n", log_entry_str); + sqlite3_free(log_entry_str); + sqlite3_free(msg_full_str); } -void mr_log_info(const char* msg) +void mr_log_info(const char* msg, ...) { - mr_log('i', msg); + va_list va; + va_start(va, msg); /* va_start() expects the last non-variable argument as the second parameter */ + mr_log('i', msg, va); + va_end(va); } -void mr_log_warning(const char* msg) +void mr_log_warning(const char* msg, ...) { - mr_log('w', msg); + va_list va; + va_start(va, msg); + mr_log('w', msg, va); + va_end(va); } -void mr_log_error(const char* msg) +void mr_log_error(const char* msg, ...) { - mr_log('e', msg); + va_list va; + va_start(va, msg); + mr_log('e', msg, va); + va_end(va); } - - diff --git a/src/mrlog.h b/src/mrlog.h index f274c810..81716453 100644 --- a/src/mrlog.h +++ b/src/mrlog.h @@ -36,9 +36,9 @@ extern "C" { #include -void mr_log_error (const char* msg); -void mr_log_warning(const char* msg); -void mr_log_info (const char* msg); +void mr_log_error (const char* msg, ...); +void mr_log_warning(const char* msg, ...); +void mr_log_info (const char* msg, ...); #ifdef __cplusplus diff --git a/src/mrmailbox.c b/src/mrmailbox.c index 50a72802..0e90e69a 100644 --- a/src/mrmailbox.c +++ b/src/mrmailbox.c @@ -248,6 +248,7 @@ int mrmailbox_import_spec(mrmailbox_t* ths, const char* spec) /* spec is a file, name = dir_entry->d_name; /* name without path; may also be `.` or `..` */ if( strlen(name)>=4 && strcmp(&name[strlen(name)-4], ".eml")==0 ) { char* path_plus_name = sqlite3_mprintf("%s/%s", spec, name); + mr_log_info("Import: %s", path_plus_name); if( path_plus_name ) { if( mrmailbox_import_file(ths, path_plus_name) ) { /* no abort on single errors errors are logged in any case */ read_cnt++; @@ -258,13 +259,7 @@ int mrmailbox_import_spec(mrmailbox_t* ths, const char* spec) /* spec is a file, } } - { - char* p = sqlite3_mprintf("%i mails read from %s.", read_cnt, spec); - if( p ) { - mr_log_info(p); - sqlite3_free(p); - } - } + mr_log_info("Import: %i mails read from %s.", read_cnt, spec); /* success */ success = 1;