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
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: */

View file

@ -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.
*

View file

@ -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;
}
}

View file

@ -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));

View file

@ -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);
/**
* }@

View file

@ -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;
}

View file

@ -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);
/**
* }@

View file

@ -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;
}

View file

@ -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;
}

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;
struct csync_memstat_s m;
FILE* fp;

View file

@ -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);