log: Add functions to set userdata for the logging callback.

This commit is contained in:
Andreas Schneider 2013-03-01 09:59:55 +01:00
parent 2d6514b509
commit 91d92bfa16
11 changed files with 61 additions and 43 deletions

View file

@ -181,7 +181,7 @@ int csync_init(CSYNC *ctx) {
} }
#ifndef _WIN32 #ifndef _WIN32
if (csync_lock(ctx, lock) < 0) { if (csync_lock(lock) < 0) {
rc = -1; rc = -1;
goto out; goto out;
} }
@ -330,7 +330,7 @@ int csync_update(CSYNC *ctx) {
return -1; return -1;
} }
csync_memstat_check(ctx); csync_memstat_check();
/* update detection for local replica */ /* update detection for local replica */
csync_gettime(&start); csync_gettime(&start);
@ -344,7 +344,7 @@ int csync_update(CSYNC *ctx) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
"Update detection for local replica took %.2f seconds walking %zu files.", "Update detection for local replica took %.2f seconds walking %zu files.",
c_secdiff(finish, start), c_rbtree_size(ctx->local.tree)); c_secdiff(finish, start), c_rbtree_size(ctx->local.tree));
csync_memstat_check(ctx); csync_memstat_check();
if (rc < 0) { if (rc < 0) {
return -1; return -1;
@ -364,7 +364,7 @@ int csync_update(CSYNC *ctx) {
"Update detection for remote replica took %.2f seconds " "Update detection for remote replica took %.2f seconds "
"walking %zu files.", "walking %zu files.",
c_secdiff(finish, start), c_rbtree_size(ctx->remote.tree)); c_secdiff(finish, start), c_rbtree_size(ctx->remote.tree));
csync_memstat_check(ctx); csync_memstat_check();
if (rc < 0) { if (rc < 0) {
return -1; return -1;
@ -626,7 +626,7 @@ int csync_destroy(CSYNC *ctx) {
#ifndef _WIN32 #ifndef _WIN32
/* remove the lock file */ /* remove the lock file */
if (asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE) > 0) { if (asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE) > 0) {
csync_lock_remove(ctx, lock); csync_lock_remove(lock);
} }
#endif #endif
@ -854,5 +854,3 @@ int csync_set_iconv_codec(const char *from)
return 0; return 0;
} }
#endif #endif
/* vim: set ts=8 sw=2 et cindent: */

View file

@ -126,8 +126,7 @@ typedef struct csync_s CSYNC;
typedef int (*csync_auth_callback) (const char *prompt, char *buf, size_t len, typedef int (*csync_auth_callback) (const char *prompt, char *buf, size_t len,
int echo, int verify, void *userdata); int echo, int verify, void *userdata);
typedef void (*csync_log_callback) (CSYNC *ctx, typedef void (*csync_log_callback) (int verbosity,
int verbosity,
const char *function, const char *function,
const char *buffer, const char *buffer,
void *userdata); void *userdata);
@ -358,6 +357,22 @@ csync_log_callback csync_get_log_callback(void);
*/ */
int csync_set_log_callback(csync_log_callback cb); 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. * @brief Get the path of the statedb file used.
* *

View file

@ -30,7 +30,7 @@
#define CSYNC_LOG_CATEGORY_NAME "csync.config" #define CSYNC_LOG_CATEGORY_NAME "csync.config"
#include "csync_log.h" #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; int re = 0;
#ifdef _WIN32 #ifdef _WIN32
/* For win32, try to copy the conf file from the directory from where the app was started. */ /* 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 */ /* copy default config, if no config exists */
if (! c_isfile(config)) { if (! c_isfile(config)) {
if (_csync_config_copy_default(ctx, config) < 0) { if (_csync_config_copy_default(config) < 0) {
return -1; return -1;
} }
} }

View file

@ -40,7 +40,7 @@
#define CSYNC_LOG_CATEGORY_NAME "csync.lock" #define CSYNC_LOG_CATEGORY_NAME "csync.lock"
#include "csync_log.h" #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; int fd = -1;
pid_t pid = 0; pid_t pid = 0;
int rc = -1; int rc = -1;
@ -119,7 +119,7 @@ out:
return rc; 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 errbuf[256] = {0};
char buf[8] = {0}; char buf[8] = {0};
long int tmp; long int tmp;
@ -173,22 +173,22 @@ static pid_t _csync_lock_read(CSYNC *ctx, const char *lockfile) {
return pid; return pid;
} }
int csync_lock(CSYNC *ctx, const char *lockfile) { int csync_lock(const char *lockfile) {
/* Check if lock already exists. */ /* 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."); CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Aborting, another synchronization process is running.");
return -1; return -1;
} }
CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "Creating lock file: %s", lockfile); 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}; char errbuf[256] = {0};
/* You can't remove the lock if it is from another process */ /* 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); CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Removing lock file: %s", lockfile);
if (unlink(lockfile) < 0) { if (unlink(lockfile) < 0) {
strerror_r(errno, errbuf, sizeof(errbuf)); strerror_r(errno, errbuf, sizeof(errbuf));

View file

@ -47,7 +47,7 @@
* @return 0 if the lock was successfull, less than 0 if the lock file * @return 0 if the lock was successfull, less than 0 if the lock file
* couldn't be created or if it is already locked. * 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 * @brief Remove the lockfile
@ -57,7 +57,7 @@ int csync_lock(CSYNC *ctx, const char *lockfile);
* *
* @param lockfile The lock file to remove. * @param lockfile The lock file to remove.
*/ */
void csync_lock_remove(CSYNC *ctx, const char *lockfile); void csync_lock_remove(const char *lockfile);
/** /**
* }@ * }@

View file

@ -35,6 +35,7 @@
CSYNC_THREAD int csync_log_level; CSYNC_THREAD int csync_log_level;
CSYNC_THREAD csync_log_callback csync_log_cb; 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) 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); fprintf(stderr, " %s\n", buffer);
} }
static void csync_log_function(CSYNC *ctx, static void csync_log_function(int verbosity,
int verbosity,
const char *function, const char *function,
const char *buffer) const char *buffer)
{ {
@ -89,11 +89,10 @@ static void csync_log_function(CSYNC *ctx,
snprintf(buf, sizeof(buf), "%s: %s", function, buffer); snprintf(buf, sizeof(buf), "%s: %s", function, buffer);
log_fn(ctx, log_fn(verbosity,
verbosity,
function, function,
buf, buf,
csync_get_userdata(ctx)); csync_get_log_userdata());
return; return;
} }
@ -101,23 +100,18 @@ static void csync_log_function(CSYNC *ctx,
} }
void csync_log(CSYNC *ctx, void csync_log(int verbosity,
int verbosity,
const char *function, const char *function,
const char *format, ...) const char *format, ...)
{ {
char buffer[1024]; char buffer[1024];
va_list va; va_list va;
if (ctx == NULL) {
return;
}
if (verbosity <= csync_get_log_level()) { if (verbosity <= csync_get_log_level()) {
va_start(va, format); va_start(va, format);
vsnprintf(buffer, sizeof(buffer), format, va); vsnprintf(buffer, sizeof(buffer), format, va);
va_end(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; 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;
}

View file

@ -55,12 +55,11 @@ enum csync_log_priority_e {
}; };
#define CSYNC_LOG(priority, ...) \ #define CSYNC_LOG(priority, ...) \
csync_log(ctx, priority, __FUNCTION__, __VA_ARGS__) csync_log(priority, __FUNCTION__, __VA_ARGS__)
void csync_log(CSYNC *ctx, void csync_log(int verbosity,
int verbosity,
const char *function, const char *function,
const char *format, ...) PRINTF_ATTRIBUTE(4, 5); const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
/** /**
* }@ * }@

View file

@ -386,7 +386,7 @@ out:
return rc; 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; int rc=0;
C_PATHINFO *info=NULL; C_PATHINFO *info=NULL;
@ -436,7 +436,7 @@ static int _csync_backup_file(CSYNC *ctx, csync_file_stat_t *st) {
goto out; 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; rc = -1;
goto out; goto out;
} }
@ -448,7 +448,7 @@ static int _csync_backup_file(CSYNC *ctx, csync_file_stat_t *st) {
goto out; 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; rc = -1;
goto out; goto out;
} }

View file

@ -49,7 +49,7 @@ int csync_get_statedb_exists(CSYNC *ctx) {
return ctx->statedb.exists; 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; int fd = -1, rc;
ssize_t r; ssize_t r;
char buf[BUF_SIZE] = {0}; char buf[BUF_SIZE] = {0};
@ -112,7 +112,7 @@ int csync_statedb_load(CSYNC *ctx, const char *statedb) {
c_strlist_t *result = NULL; c_strlist_t *result = NULL;
char *statedb_tmp = NULL; char *statedb_tmp = NULL;
if (_csync_statedb_check(ctx, statedb) < 0) { if (_csync_statedb_check(statedb) < 0) {
rc = -1; rc = -1;
goto out; goto out;
} }

View file

@ -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; int s = 0;
struct csync_memstat_s m; struct csync_memstat_s m;
FILE* fp; FILE* fp;

View file

@ -27,7 +27,7 @@
const char *csync_instruction_str(enum csync_instructions_e instr); 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); int csync_merge_file_trees(CSYNC *ctx);