Get rid of csync_log

We use Qt's debugging code everywhere
This commit is contained in:
Olivier Goffart 2017-12-14 16:14:08 +01:00 committed by Dominik Schmidt
parent d948ed11a1
commit 3ddd4b6f16
17 changed files with 12 additions and 504 deletions

View File

@ -331,7 +331,6 @@ int main(int argc, char **argv)
parseOptions(app.arguments(), &options);
csync_set_log_level(options.silent ? 1 : 11);
if (options.silent) {
qInstallMessageHandler(nullMessageHandler);
} else {

View File

@ -62,7 +62,6 @@ endif()
set(csync_SRCS
csync.cpp
csync_exclude.cpp
csync_log.cpp
csync_util.cpp
csync_misc.cpp

View File

@ -86,7 +86,7 @@ int csync_update(CSYNC *ctx) {
timer.start();
ctx->current = LOCAL_REPLICA;
CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "## Starting local discovery ##");
qCInfo(lcCSync, "## Starting local discovery ##");
rc = csync_ftw(ctx, ctx->local.uri, csync_walker, MAX_DEPTH);
if (rc < 0) {
@ -104,7 +104,7 @@ int csync_update(CSYNC *ctx) {
timer.restart();
ctx->current = REMOTE_REPLICA;
CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "## Starting remote discovery ##");
qCInfo(lcCSync, "## Starting remote discovery ##");
rc = csync_ftw(ctx, "", csync_walker, MAX_DEPTH);
if (rc < 0) {

View File

@ -223,10 +223,6 @@ 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) (int verbosity,
const char *function,
const char *buffer);
typedef void (*csync_update_callback) (bool local,
const char *dirUrl,
void *userdata);
@ -304,39 +300,6 @@ csync_auth_callback OCSYNC_EXPORT csync_get_auth_callback(CSYNC *ctx);
*/
int OCSYNC_EXPORT csync_set_auth_callback(CSYNC *ctx, csync_auth_callback cb);
/**
* @brief Set the log level.
*
* @param[in] level The log verbosity.
*
* @return 0 on success, < 0 if an error occurred.
*/
int OCSYNC_EXPORT csync_set_log_level(int level);
/**
* @brief Get the log verbosity
*
* @return The log verbosity, -1 on error.
*/
int OCSYNC_EXPORT csync_get_log_level(void);
/**
* @brief Get the logging callback set.
*
* @return The logging callback set or NULL if an error
* occurred.
*/
csync_log_callback OCSYNC_EXPORT csync_get_log_callback(void);
/**
* @brief Set the logging callback.
*
* @param cb The logging callback.
*
* @return 0 on success, less than 0 if an error occurred.
*/
int OCSYNC_EXPORT csync_set_log_callback(csync_log_callback cb);
/* Used for special modes or debugging */
CSYNC_STATUS OCSYNC_EXPORT csync_get_status(CSYNC *ctx);

View File

@ -1,77 +0,0 @@
/*
* libcsync -- a library to sync a directory with another
*
* Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config_csync.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "csync_private.h"
#include "csync_log.h"
CSYNC_THREAD int csync_log_level;
CSYNC_THREAD csync_log_callback csync_log_cb;
void csync_log(int verbosity,
const char *function,
const char *format, ...)
{
csync_log_callback log_fn = csync_get_log_callback();
if (log_fn && verbosity <= csync_get_log_level()) {
char buffer[1024];
va_list va;
va_start(va, format);
vsnprintf(buffer, sizeof(buffer), format, va);
va_end(va);
log_fn(verbosity, function, buffer);
return;
}
}
int csync_set_log_level(int level) {
if (level < 0) {
return -1;
}
csync_log_level = level;
return 0;
}
int csync_get_log_level(void) {
return csync_log_level;
}
int csync_set_log_callback(csync_log_callback cb) {
if (cb == NULL) {
return -1;
}
csync_log_cb = cb;
return 0;
}
csync_log_callback csync_get_log_callback(void) {
return csync_log_cb;
}

View File

@ -1,75 +0,0 @@
/*
* libcsync -- a library to sync a directory with another
*
* Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file csync_log.h
*
* @brief Logging interface of csync
*
* @defgroup csyncLogInternals csync logging internals
* @ingroup csyncInternalAPI
*
* @{
*/
#ifndef _CSYNC_LOG_H
#define _CSYNC_LOG_H
/* GCC have printf type attribute check. */
#ifndef PRINTF_ATTRIBUTE
#ifdef __GNUC__
#ifdef _WIN32
#define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__gnu_printf__, a, b)))
#else
#define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
#endif
#else
#define PRINTF_ATTRIBUTE(a,b)
#endif /* __GNUC__ */
#endif /* ndef PRINTF_ATTRIBUTE */
enum csync_log_priority_e {
CSYNC_LOG_PRIORITY_NOLOG = 0,
CSYNC_LOG_PRIORITY_FATAL,
CSYNC_LOG_PRIORITY_ALERT,
CSYNC_LOG_PRIORITY_CRIT,
CSYNC_LOG_PRIORITY_ERROR,
CSYNC_LOG_PRIORITY_WARN,
CSYNC_LOG_PRIORITY_NOTICE,
CSYNC_LOG_PRIORITY_INFO,
CSYNC_LOG_PRIORITY_DEBUG,
CSYNC_LOG_PRIORITY_TRACE,
CSYNC_LOG_PRIORITY_NOTSET,
CSYNC_LOG_PRIORITY_UNKNOWN,
};
#define CSYNC_LOG(priority, ...) \
csync_log(priority, __func__, __VA_ARGS__)
void csync_log(int verbosity,
const char *function,
const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
/**
* }@
*/
#endif /* _CSYNC_LOG_H */
/* vim: set ft=c.doxygen ts=4 sw=4 et cindent: */

View File

@ -44,7 +44,6 @@
#include "c_lib.h"
#include "csync_misc.h"
#include "csync_macros.h"
#include "csync_log.h"
#ifdef HAVE_FNMATCH
#include <fnmatch.h>

View File

@ -34,8 +34,8 @@
#include "csync_util.h"
#include "vio/csync_vio.h"
#define CSYNC_LOG_CATEGORY_NAME "csync.util"
#include "csync_log.h"
Q_LOGGING_CATEGORY(lcCSyncUtils, "sync.csync.utils", QtInfoMsg)
typedef struct {
const char *instr_str;
@ -102,8 +102,8 @@ void csync_memstat_check(void) {
return;
}
CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "Memory: %dK total size, %dK resident, %dK shared",
m.size * 4, m.resident * 4, m.shared * 4);
qCInfo(lcCSyncUtils, "Memory: %dK total size, %dK resident, %dK shared",
m.size * 4, m.resident * 4, m.shared * 4);
}
bool (*csync_file_locked_or_open_ext) (const char*) = 0; // filled in by library user
@ -121,7 +121,6 @@ bool csync_file_locked_or_open( const char *dir, const char *fname) {
if (asprintf(&tmp_uri, "%s/%s", dir, fname) < 0) {
return -1;
}
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "csync_file_locked_or_open %s", tmp_uri);
ret = csync_file_locked_or_open_ext(tmp_uri);
SAFE_FREE(tmp_uri);
return ret;

View File

@ -31,11 +31,12 @@
#include "c_string.h"
#include "c_utf8.h"
#include "csync_util.h"
#include "csync_log.h"
#include "csync_vio.h"
#include "vio/csync_vio_local.h"
Q_LOGGING_CATEGORY(lcCSyncVIOLocal, "sync.csync.vio_local", QtInfoMsg)
/*
* directory functions
*/
@ -105,8 +106,7 @@ std::unique_ptr<csync_file_stat_t> csync_vio_local_readdir(csync_vio_handle_t *d
QByteArray fullPath = QByteArray() % const_cast<const char *>(handle->path) % '/' % QByteArray() % const_cast<const char *>(dirent->d_name);
if (file_stat->path.isNull()) {
file_stat->original_path = fullPath;
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "Invalid characters in file/directory name, please rename: \"%s\" (%s)",
dirent->d_name, handle->path);
qCWarning(lcCSyncVIOLocal) << "Invalid characters in file/directory name, please rename:" << dirent->d_name << handle->path;
}
/* Check for availability of d_type, see manpage. */

View File

@ -31,11 +31,11 @@
#include "c_lib.h"
#include "c_utf8.h"
#include "csync_util.h"
#include "csync_log.h"
#include "csync_vio.h"
#include "vio/csync_vio_local.h"
Q_LOGGING_CATEGORY(lcCSyncVIOLocal, "sync.csync.vio_local", QtInfoMsg)
/*
* directory functions
@ -235,13 +235,13 @@ static int _csync_vio_local_stat_mb(const mbchar_t *wuri, csync_file_stat_t *buf
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
NULL );
if( h == INVALID_HANDLE_VALUE ) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_CRIT, "CreateFileW failed on %ls", wuri);
qCCritical(lcCSyncVIOLocal, "CreateFileW failed on %ls", wuri);
errno = GetLastError();
return -1;
}
if(!GetFileInformationByHandle( h, &fileInfo ) ) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_CRIT, "GetFileInformationByHandle failed on %ls", wuri);
qCCritical(lcCSyncVIOLocal, "GetFileInformationByHandle failed on %ls", wuri);
errno = GetLastError();
CloseHandle(h);
return -1;

View File

@ -697,8 +697,6 @@ void DiscoveryJob::start()
_csync_ctx->callbacks.remote_closedir_hook = remote_vio_closedir_hook;
_csync_ctx->callbacks.vio_userdata = this;
csync_set_log_callback(_log_callback);
csync_set_log_level(_log_level);
_lastUpdateProgressCallbackCall.invalidate();
int ret = csync_update(_csync_ctx);

View File

@ -157,8 +157,6 @@ class DiscoveryJob : public QObject
Q_OBJECT
friend class DiscoveryMainThread;
CSYNC *_csync_ctx;
csync_log_callback _log_callback;
int _log_level;
QElapsedTimer _lastUpdateProgressCallbackCall;
/**
@ -191,12 +189,7 @@ public:
: QObject(parent)
, _csync_ctx(ctx)
{
// We need to forward the log property as csync uses thread local
// and updates run in another thread
_log_callback = csync_get_log_callback();
_log_level = csync_get_log_level();
}
QStringList _selectiveSyncBlackList;
QStringList _selectiveSyncWhiteList;
SyncOptions _syncOptions;

View File

@ -19,13 +19,8 @@
#include <QThread>
#include <qmetaobject.h>
#include "csync.h"
#include "csync_log.h"
namespace OCC {
Q_LOGGING_CATEGORY(lcCsync, "sync.csync", QtInfoMsg)
static void mirallLogCatcher(QtMsgType type, const QMessageLogContext &ctx, const QString &message)
{
auto logger = Logger::instance();
@ -34,26 +29,6 @@ static void mirallLogCatcher(QtMsgType type, const QMessageLogContext &ctx, cons
}
}
static void csyncLogCatcher(int verbosity,
const char *function,
const char *buffer)
{
if (verbosity <= CSYNC_LOG_PRIORITY_FATAL) {
QMessageLogger(0, 0, function, lcCsync().categoryName()).fatal("%s", buffer);
} else if (verbosity <= CSYNC_LOG_PRIORITY_CRIT) {
if (lcCsync().isCriticalEnabled())
QMessageLogger(0, 0, function, lcCsync().categoryName()).critical("%s", buffer);
} else if (verbosity <= CSYNC_LOG_PRIORITY_WARN) {
if (lcCsync().isWarningEnabled())
QMessageLogger(0, 0, function, lcCsync().categoryName()).warning("%s", buffer);
} else if (verbosity <= CSYNC_LOG_PRIORITY_INFO) {
if (lcCsync().isInfoEnabled())
QMessageLogger(0, 0, function, lcCsync().categoryName()).info("%s", buffer);
} else if (verbosity <= CSYNC_LOG_PRIORITY_NOTSET) {
if (lcCsync().isDebugEnabled())
QMessageLogger(0, 0, function, lcCsync().categoryName()).debug("%s", buffer);
}
}
Logger *Logger::instance()
{
@ -74,12 +49,7 @@ Logger::Logger(QObject *parent)
qInstallMessageHandler(mirallLogCatcher);
#else
Q_UNUSED(mirallLogCatcher)
// Always get logging from csync in that case.
csync_set_log_level(11);
#endif
// Setup CSYNC logging to forward to our own logger
csync_set_log_callback(csyncLogCatcher);
}
Logger::~Logger()
@ -112,11 +82,6 @@ void Logger::log(Log log)
msg = log.timeStamp.toString(QLatin1String("MM-dd hh:mm:ss:zzz")) + QLatin1Char(' ');
}
if (log.source == Log::CSync) {
// msg += "csync - ";
} else {
// msg += "ownCloud - ";
}
msg += QString().sprintf("%p ", (void *)QThread::currentThread());
msg += log.message;
// _logs.append(log);
@ -151,7 +116,6 @@ void Logger::doLog(const QString &msg)
void Logger::mirallLog(const QString &message)
{
Log log_;
log_.source = Log::Occ;
log_.timeStamp = QDateTime::currentDateTimeUtc();
log_.message = message;
@ -161,18 +125,12 @@ void Logger::mirallLog(const QString &message)
void Logger::setLogWindowActivated(bool activated)
{
QMutexLocker locker(&_mutex);
csync_set_log_level(11);
_logWindowActivated = activated;
}
void Logger::setLogFile(const QString &name)
{
QMutexLocker locker(&_mutex);
csync_set_log_level(11);
if (_logstream) {
_logstream.reset(0);
_logFile.close();

View File

@ -30,13 +30,7 @@ namespace OCC {
struct Log
{
typedef enum {
Occ,
CSync
} Source;
QDateTime timeStamp;
Source source;
QString message;
};

View File

@ -27,10 +27,6 @@ add_cmocka_test(check_std_c_str std_tests/check_std_c_str.c ${TEST_TARGET_LIBRAR
# csync tests
# This will be rewritten soon anyway.
#add_cmocka_test(check_logger log_tests/check_log.cpp ${TEST_TARGET_LIBRARIES})
add_cmocka_test(check_csync_log csync_tests/check_csync_log.cpp ${TEST_TARGET_LIBRARIES})
add_cmocka_test(check_csync_exclude csync_tests/check_csync_exclude.cpp ${TEST_TARGET_LIBRARIES})
add_cmocka_test(check_csync_util csync_tests/check_csync_util.cpp ${TEST_TARGET_LIBRARIES})
add_cmocka_test(check_csync_misc csync_tests/check_csync_misc.cpp ${TEST_TARGET_LIBRARIES})

View File

@ -1,140 +0,0 @@
/*
* libcsync -- a library to sync a directory with another
*
* Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <sys/types.h>
#include <sys/stat.h>
#include "csync.h"
#include "csync_log.cpp"
#include "c_private.h"
#include "std/c_utf8.h"
#include "torture.h"
static int setup(void **state) {
int rc;
rc = system("mkdir -p /tmp/check_csync1");
assert_int_equal(rc, 0);
*state = NULL;
return 0;
}
static int teardown(void **state) {
int rc;
rc = system("rm -rf /tmp/check_csync");
assert_int_equal(rc, 0);
rc = system("rm -rf /tmp/check_csync1");
assert_int_equal(rc, 0);
*state = NULL;
return 0;
}
static void check_log_callback(int verbosity,
const char *function,
const char *buffer)
{
int rc;
assert_true(verbosity >= 0);
assert_non_null(function);
assert_false(function[0] == '\0');
assert_non_null(buffer);
assert_false(buffer[0] == '\0');
rc = system("touch /tmp/check_csync1/cb_called");
assert_int_equal(rc, 0);
}
static void check_set_log_level(void **state)
{
int rc;
(void) state;
rc = csync_set_log_level(-5);
assert_int_equal(rc, -1);
rc = csync_set_log_level(5);
assert_int_equal(rc, 0);
rc = csync_get_log_level();
assert_int_equal(rc, 5);
}
static void check_set_auth_callback(void **state)
{
csync_log_callback log_fn;
int rc;
(void) state;
rc = csync_set_log_callback(NULL);
assert_int_equal(rc, -1);
rc = csync_set_log_callback(check_log_callback);
assert_int_equal(rc, 0);
log_fn = csync_get_log_callback();
assert_non_null(log_fn);
assert_true(log_fn == &check_log_callback);
}
static void check_logging(void **state)
{
int rc;
csync_stat_t sb;
mbchar_t *path;
path = c_utf8_path_to_locale("/tmp/check_csync1/cb_called");
(void) state; /* unused */
assert_non_null(path);
rc = csync_set_log_level(1);
assert_int_equal(rc, 0);
rc = csync_set_log_callback(check_log_callback);
assert_int_equal(rc, 0);
csync_log(1, __func__, "rc = %d", rc);
rc = _tstat(path, &sb);
c_free_locale_string(path);
assert_int_equal(rc, 0);
}
int torture_run_tests(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(check_set_log_level),
cmocka_unit_test(check_set_auth_callback),
cmocka_unit_test_setup_teardown(check_logging, setup, teardown),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@ -1,98 +0,0 @@
/*
* libcsync -- a library to sync a directory with another
*
* Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include "support.h"
#include "config_csync.h"
#include "csync_log.h"
static void setup(void) {
csync_log_init();
}
static void teardown(void) {
csync_log_fini();
}
START_TEST (log_create)
{
fail_unless((csync_log_init() == 0), NULL);
fail_unless((csync_log_fini() == 0), NULL);
}
END_TEST
START_TEST (log_load)
{
char buf[256];
snprintf(buf, (size_t) 256 - 1, "%s/%s", SOURCEDIR, "config/ocsync_log.conf");
fail_unless(csync_log_load(buf) == 0);
}
END_TEST
START_TEST (log_prio)
{
CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL, "log %s", "test");
CSYNC_LOG(CSYNC_LOG_PRIORITY_FATAL, "log %s", "test");
CSYNC_LOG(CSYNC_LOG_PRIORITY_ALERT, "log %s", "test");
CSYNC_LOG(CSYNC_LOG_PRIORITY_CRIT, "log %s", "test");
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "log %s", "test");
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "log %s", "test");
CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "log %s", "test");
CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "log %s", "test");
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "log %s", "test");
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "log %s", "test");
CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTSET, "log %s", "test");
CSYNC_LOG(CSYNC_LOG_PRIORITY_UNKNOWN, "log %s", "test");
}
END_TEST
START_TEST (log_null)
{
char *z = NULL;
CSYNC_LOG(CSYNC_LOG_PRIORITY_UNKNOWN, "log %s", z);
}
END_TEST
static Suite *log_suite(void) {
Suite *s = suite_create("Logger");
create_case(s, "log_create", log_create);
create_case_fixture(s, "log_load", log_load, setup, teardown);
create_case_fixture(s, "log_prio", log_prio, setup, teardown);
create_case_fixture(s, "log_null", log_null, setup, teardown);
return s;
}
int main(void) {
int nf;
Suite *s = log_suite();
SRunner *sr;
sr = srunner_create(s);
srunner_run_all(sr, CK_VERBOSE);
nf = srunner_ntests_failed(sr);
srunner_free(sr);
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}