From 91d92bfa160585baed4229fa8da6b5183d043fd9 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Fri, 1 Mar 2013 09:59:55 +0100 Subject: [PATCH] log: Add functions to set userdata for the logging callback. --- src/csync.c | 12 +++++------- src/csync.h | 19 +++++++++++++++++-- src/csync_config.c | 4 ++-- src/csync_lock.c | 14 +++++++------- src/csync_lock.h | 4 ++-- src/csync_log.c | 30 ++++++++++++++++++------------ src/csync_log.h | 7 +++---- src/csync_propagate.c | 6 +++--- src/csync_statedb.c | 4 ++-- src/csync_util.c | 2 +- src/csync_util.h | 2 +- 11 files changed, 61 insertions(+), 43 deletions(-) diff --git a/src/csync.c b/src/csync.c index c3913ab3b..e624b9146 100644 --- a/src/csync.c +++ b/src/csync.c @@ -181,7 +181,7 @@ int csync_init(CSYNC *ctx) { } #ifndef _WIN32 - if (csync_lock(ctx, lock) < 0) { + if (csync_lock(lock) < 0) { rc = -1; goto out; } @@ -330,7 +330,7 @@ int csync_update(CSYNC *ctx) { return -1; } - csync_memstat_check(ctx); + csync_memstat_check(); /* update detection for local replica */ csync_gettime(&start); @@ -344,7 +344,7 @@ int csync_update(CSYNC *ctx) { CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Update detection for local replica took %.2f seconds walking %zu files.", c_secdiff(finish, start), c_rbtree_size(ctx->local.tree)); - csync_memstat_check(ctx); + csync_memstat_check(); if (rc < 0) { return -1; @@ -364,7 +364,7 @@ int csync_update(CSYNC *ctx) { "Update detection for remote replica took %.2f seconds " "walking %zu files.", c_secdiff(finish, start), c_rbtree_size(ctx->remote.tree)); - csync_memstat_check(ctx); + csync_memstat_check(); if (rc < 0) { return -1; @@ -626,7 +626,7 @@ int csync_destroy(CSYNC *ctx) { #ifndef _WIN32 /* remove the lock file */ if (asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE) > 0) { - csync_lock_remove(ctx, lock); + csync_lock_remove(lock); } #endif @@ -854,5 +854,3 @@ int csync_set_iconv_codec(const char *from) return 0; } #endif - -/* vim: set ts=8 sw=2 et cindent: */ diff --git a/src/csync.h b/src/csync.h index 384e20b40..1fe1dca44 100644 --- a/src/csync.h +++ b/src/csync.h @@ -126,8 +126,7 @@ typedef struct csync_s CSYNC; typedef int (*csync_auth_callback) (const char *prompt, char *buf, size_t len, int echo, int verify, void *userdata); -typedef void (*csync_log_callback) (CSYNC *ctx, - int verbosity, +typedef void (*csync_log_callback) (int verbosity, const char *function, const char *buffer, void *userdata); @@ -358,6 +357,22 @@ csync_log_callback csync_get_log_callback(void); */ int csync_set_log_callback(csync_log_callback cb); +/** + * @brief get the userdata set for the logging callback. + * + * @return The userdata or NULL. + */ +void *csync_get_log_userdata(void); + +/** + * @brief Set the userdata passed to the logging callback. + * + * @param[in] data The userdata to set. + * + * @return 0 on success, less than 0 if an error occured. + */ +int csync_set_log_userdata(void *data); + /** * @brief Get the path of the statedb file used. * diff --git a/src/csync_config.c b/src/csync_config.c index 609ccf1dd..9dea67381 100644 --- a/src/csync_config.c +++ b/src/csync_config.c @@ -30,7 +30,7 @@ #define CSYNC_LOG_CATEGORY_NAME "csync.config" #include "csync_log.h" -static int _csync_config_copy_default (CSYNC *ctx, const char *config) { +static int _csync_config_copy_default (const char *config) { int re = 0; #ifdef _WIN32 /* For win32, try to copy the conf file from the directory from where the app was started. */ @@ -73,7 +73,7 @@ int csync_config_load(CSYNC *ctx, const char *config) { /* copy default config, if no config exists */ if (! c_isfile(config)) { - if (_csync_config_copy_default(ctx, config) < 0) { + if (_csync_config_copy_default(config) < 0) { return -1; } } diff --git a/src/csync_lock.c b/src/csync_lock.c index 0a6dbda8c..7a47b7600 100644 --- a/src/csync_lock.c +++ b/src/csync_lock.c @@ -40,7 +40,7 @@ #define CSYNC_LOG_CATEGORY_NAME "csync.lock" #include "csync_log.h" -static int _csync_lock_create(CSYNC *ctx, const char *lockfile) { +static int _csync_lock_create(const char *lockfile) { int fd = -1; pid_t pid = 0; int rc = -1; @@ -119,7 +119,7 @@ out: return rc; } -static pid_t _csync_lock_read(CSYNC *ctx, const char *lockfile) { +static pid_t _csync_lock_read(const char *lockfile) { char errbuf[256] = {0}; char buf[8] = {0}; long int tmp; @@ -173,22 +173,22 @@ static pid_t _csync_lock_read(CSYNC *ctx, const char *lockfile) { return pid; } -int csync_lock(CSYNC *ctx, const char *lockfile) { +int csync_lock(const char *lockfile) { /* Check if lock already exists. */ - if (_csync_lock_read(ctx, lockfile) > 0) { + if (_csync_lock_read(lockfile) > 0) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Aborting, another synchronization process is running."); return -1; } CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "Creating lock file: %s", lockfile); - return _csync_lock_create(ctx, lockfile); + return _csync_lock_create(lockfile); } -void csync_lock_remove(CSYNC *ctx, const char *lockfile) { +void csync_lock_remove(const char *lockfile) { char errbuf[256] = {0}; /* You can't remove the lock if it is from another process */ - if (_csync_lock_read(ctx, lockfile) == getpid()) { + if (_csync_lock_read(lockfile) == getpid()) { CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Removing lock file: %s", lockfile); if (unlink(lockfile) < 0) { strerror_r(errno, errbuf, sizeof(errbuf)); diff --git a/src/csync_lock.h b/src/csync_lock.h index 638c82ca0..2cb44363f 100644 --- a/src/csync_lock.h +++ b/src/csync_lock.h @@ -47,7 +47,7 @@ * @return 0 if the lock was successfull, less than 0 if the lock file * couldn't be created or if it is already locked. */ -int csync_lock(CSYNC *ctx, const char *lockfile); +int csync_lock(const char *lockfile); /** * @brief Remove the lockfile @@ -57,7 +57,7 @@ int csync_lock(CSYNC *ctx, const char *lockfile); * * @param lockfile The lock file to remove. */ -void csync_lock_remove(CSYNC *ctx, const char *lockfile); +void csync_lock_remove(const char *lockfile); /** * }@ diff --git a/src/csync_log.c b/src/csync_log.c index bf5161777..5a1df643a 100644 --- a/src/csync_log.c +++ b/src/csync_log.c @@ -35,6 +35,7 @@ CSYNC_THREAD int csync_log_level; CSYNC_THREAD csync_log_callback csync_log_cb; +CSYNC_THREAD void *csync_log_userdata; static int current_timestring(int hires, char *buf, size_t len) { @@ -78,8 +79,7 @@ static void csync_log_stderr(int verbosity, fprintf(stderr, " %s\n", buffer); } -static void csync_log_function(CSYNC *ctx, - int verbosity, +static void csync_log_function(int verbosity, const char *function, const char *buffer) { @@ -89,11 +89,10 @@ static void csync_log_function(CSYNC *ctx, snprintf(buf, sizeof(buf), "%s: %s", function, buffer); - log_fn(ctx, - verbosity, + log_fn(verbosity, function, buf, - csync_get_userdata(ctx)); + csync_get_log_userdata()); return; } @@ -101,23 +100,18 @@ static void csync_log_function(CSYNC *ctx, } -void csync_log(CSYNC *ctx, - int verbosity, +void csync_log(int verbosity, const char *function, const char *format, ...) { char buffer[1024]; va_list va; - if (ctx == NULL) { - return; - } - if (verbosity <= csync_get_log_level()) { va_start(va, format); vsnprintf(buffer, sizeof(buffer), format, va); va_end(va); - csync_log_function(ctx, verbosity, function, buffer); + csync_log_function(verbosity, function, buffer); } } @@ -149,3 +143,15 @@ csync_log_callback csync_get_log_callback(void) { return csync_log_cb; } +void *csync_get_log_userdata(void) +{ + return csync_log_userdata; +} + +int csync_set_log_userdata(void *data) +{ + csync_log_userdata = data; + + return 0; +} + diff --git a/src/csync_log.h b/src/csync_log.h index e6494fc7d..43a21744c 100644 --- a/src/csync_log.h +++ b/src/csync_log.h @@ -55,12 +55,11 @@ enum csync_log_priority_e { }; #define CSYNC_LOG(priority, ...) \ - csync_log(ctx, priority, __FUNCTION__, __VA_ARGS__) + csync_log(priority, __FUNCTION__, __VA_ARGS__) -void csync_log(CSYNC *ctx, - int verbosity, +void csync_log(int verbosity, const char *function, - const char *format, ...) PRINTF_ATTRIBUTE(4, 5); + const char *format, ...) PRINTF_ATTRIBUTE(3, 4); /** * }@ diff --git a/src/csync_propagate.c b/src/csync_propagate.c index 7476a74f5..08215faa0 100644 --- a/src/csync_propagate.c +++ b/src/csync_propagate.c @@ -386,7 +386,7 @@ out: return rc; } -static int _backup_path(CSYNC *ctx, char** duri, const char* uri, const char* path) +static int _backup_path(char** duri, const char* uri, const char* path) { int rc=0; C_PATHINFO *info=NULL; @@ -436,7 +436,7 @@ static int _csync_backup_file(CSYNC *ctx, csync_file_stat_t *st) { goto out; } - if (_backup_path(ctx, &duri, ctx->remote.uri,st->path) < 0) { + if (_backup_path(&duri, ctx->remote.uri,st->path) < 0) { rc = -1; goto out; } @@ -448,7 +448,7 @@ static int _csync_backup_file(CSYNC *ctx, csync_file_stat_t *st) { goto out; } - if ( _backup_path(ctx, &duri, ctx->local.uri, st->path) < 0) { + if ( _backup_path(&duri, ctx->local.uri, st->path) < 0) { rc = -1; goto out; } diff --git a/src/csync_statedb.c b/src/csync_statedb.c index 9ffc22db4..3b7d91389 100644 --- a/src/csync_statedb.c +++ b/src/csync_statedb.c @@ -49,7 +49,7 @@ int csync_get_statedb_exists(CSYNC *ctx) { return ctx->statedb.exists; } -static int _csync_statedb_check(CSYNC *ctx, const char *statedb) { +static int _csync_statedb_check(const char *statedb) { int fd = -1, rc; ssize_t r; char buf[BUF_SIZE] = {0}; @@ -112,7 +112,7 @@ int csync_statedb_load(CSYNC *ctx, const char *statedb) { c_strlist_t *result = NULL; char *statedb_tmp = NULL; - if (_csync_statedb_check(ctx, statedb) < 0) { + if (_csync_statedb_check(statedb) < 0) { rc = -1; goto out; } diff --git a/src/csync_util.c b/src/csync_util.c index 0aadad985..81ce3bb8f 100644 --- a/src/csync_util.c +++ b/src/csync_util.c @@ -82,7 +82,7 @@ const char *csync_instruction_str(enum csync_instructions_e instr) } -void csync_memstat_check(CSYNC *ctx) { +void csync_memstat_check(void) { int s = 0; struct csync_memstat_s m; FILE* fp; diff --git a/src/csync_util.h b/src/csync_util.h index 8d7fc8180..b67d70393 100644 --- a/src/csync_util.h +++ b/src/csync_util.h @@ -27,7 +27,7 @@ const char *csync_instruction_str(enum csync_instructions_e instr); -void csync_memstat_check(CSYNC *ctx); +void csync_memstat_check(void); int csync_merge_file_trees(CSYNC *ctx);