Commit a4fda9e2 authored by Karel Slaný's avatar Karel Slaný

Pulled log device code from the desktop app.

parent 0036289a
......@@ -102,6 +102,10 @@ SOURCES += \
src/datovka_shared/isds/message_interface.cpp \
src/datovka_shared/isds/type_conversion.cpp \
src/datovka_shared/localisation/localisation.cpp \
src/datovka_shared/log/global.cpp \
src/datovka_shared/log/log.cpp \
src/datovka_shared/log/log_c.cpp \
src/datovka_shared/log/log_device.cpp \
src/datovka_shared/records_management/conversion.cpp \
src/datovka_shared/records_management/io/records_management_connection.cpp \
src/datovka_shared/records_management/json/entry_error.cpp \
......@@ -204,6 +208,11 @@ HEADERS += \
src/datovka_shared/isds/type_conversion.h \
src/datovka_shared/isds/types.h \
src/datovka_shared/localisation/localisation.h \
src/datovka_shared/log/global.h \
src/datovka_shared/log/log_c.h \
src/datovka_shared/log/log_common.h \
src/datovka_shared/log/log_device.h \
src/datovka_shared/log/log.h \
src/datovka_shared/records_management/conversion.h \
src/datovka_shared/records_management/io/records_management_connection.h \
src/datovka_shared/records_management/json/entry_error.h \
......
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#include <QtCore> /* Q_NULLPTR */
#include "src/datovka_shared/log/global.h"
class LogDevice *GlobInstcs::logPtr = Q_NULLPTR;
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#pragma once
/*
* The log facility is a global instance with code shared between the desktop
* and mobile application. Therefore the global log instance must be located
* in a separate compilation unit.
*/
/*
* Forward class declaration.
* These classes must be declared before the following namespace.
*/
class LogDevice;
/*!
* @brief The namespace holds pointers to all globally accessible structures.
*/
namespace GlobInstcs {
extern
class LogDevice *logPtr; /*!< Log device. */
}
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#include "src/datovka_shared/log/log.h"
void globalLogOutput(enum QtMsgType type, const QMessageLogContext &context,
const QString &msg)
{
if (GlobInstcs::logPtr != Q_NULLPTR) {
GlobInstcs::logPtr->logQtMessage(type, context, msg);
}
}
void qDebugCall(const char *fmt, ...)
{
std::va_list argp;
va_start(argp, fmt);
qDebugCallV(fmt, argp);
va_end(argp);
}
void qDebugCallV(const char *fmt, std::va_list ap)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
qDebug("%s", QString::vasprintf(fmt, ap).toUtf8().constData());
#else /* < Qt-5.5 */
QString outStr;
outStr.vsprintf(fmt, ap);
qDebug("%s", outStr.toUtf8().constData());
#endif /* >= Qt-5.5 */
}
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#ifndef _LOG_H_
#define _LOG_H_
#include <cstdarg>
#include <QString>
#include "src/datovka_shared/log/global.h" /* GlobInstcs::logPtr */
#include "src/datovka_shared/log/log_common.h"
#include "src/datovka_shared/log/log_device.h"
/*!
* @brief Message handler.
*
* This function if installed by qInstallMessageHandler() is used to log all
* messages generated by the Qt library.
*
* @param[in] type Type of message sent to message handler.
* @param[in] context Additional information about a log message.
* @param[in] msg Message to be handled.
*/
void globalLogOutput(enum QtMsgType type, const QMessageLogContext &context,
const QString &msg);
/*!
* @brief Debugging using Qt-defined output.
*
* @param[in] fmt Format string.
*/
void qDebugCall(const char *fmt, ...);
/*!
* @brief Debugging using Qt-defined output.
*
* @param[in] fmt Format string.
* @param[in,out] ap Variable arguments list.
*/
void qDebugCallV(const char *fmt, std::va_list ap);
/*!
* @brief Generates location debug information.
*/
#ifdef DEBUG
# define debugSlotCall() \
do { \
/* qDebugCall("<SLOT> %s() '%s'", __func__, __FILE__); */ \
logDebugLv1NL("<SLOT> %s() '%s'", __func__, __FILE__); \
} while(0)
#else
/* Forces the semicolon after the macro. */
# define debugSlotCall() do {} while(0)
#endif
/*!
* @brief Generates location debug information.
*/
#ifdef DEBUG
# define debugFuncCall() \
do { \
/* qDebugCall("<FUNC> %s() '%s'", __func__, __FILE__); */ \
logDebugLv2NL("<FUNC> %s() '%s'", __func__, __FILE__); \
} while(0)
#else
/* Forces the semicolon after the macro. */
# define debugFuncCall() do {} while(0)
#endif
/*!
* @brief Logging macro used for internal purposes.
*
* @param[in] logVerbThresh Logging verbosity threshold.
* @param[in] logSrc Logging source.
* @param[in] logUrg Logging urgency.
* @param[in] format Format of the messages, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define _internalLogNL(logVerbThresh, logSrc, logUrg, format, ...) \
if (GlobInstcs::logPtr->logVerbosity() > (logVerbThresh)) { \
GlobInstcs::logPtr->log((logSrc), (logUrg), \
format " (%s:%d, %s())\n", \
__VA_ARGS__, __FILE__, __LINE__, __func__); \
} else { \
GlobInstcs::logPtr->log((logSrc), (logUrg), format "\n", \
__VA_ARGS__); \
}
/*!
* @brief A macro which logs a function name followed with given debugging
* information. The data are logged only when the global verbosity variable
* exceeds the given threshold. Automatic newline is added.
*
* @param[in] verbThresh Verbosity threshold.
* @param[in] format Format of the messages, follows printf syntax.
* @param[in] ... Variadic arguments.
*
* @note This macro works only if the DEBUG macro is defined. If no DEBUG macro
* is defined then it won't generate any code.
*/
#if DEBUG
#define logDebugNL(verbThresh, format, ...) \
do { \
if (GlobInstcs::logPtr->debugVerbosity() > (verbThresh)) { \
_internalLogNL(0, LOGSRC_DFLT, LOG_DEBUG, \
format, __VA_ARGS__); \
} \
} while (0)
#else /* !DEBUG */
#define logDebugNL(verbThresh, format, ...) \
(void) 0
#endif /* DEBUG */
/*!
* @brief Logs the debugging information even if the threshold was not set.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logDebugLv0NL(format, ...) \
logDebugNL(-1, format, __VA_ARGS__)
/*!
* @brief Logs the debugging information only if the verbosity exceeds 0.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logDebugLv1NL(format, ...) \
logDebugNL(0, format, __VA_ARGS__)
/*!
* @brief Logs the debugging information only if the verbosity exceeds 1.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logDebugLv2NL(format, ...) \
logDebugNL(1, format, __VA_ARGS__)
/*!
* @brief Logs the debugging information only if the verbosity exceeds 2.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logDebugLv3NL(format, ...) \
logDebugNL(2, format, __VA_ARGS__)
/*!
* @brief Logs multi-line debugging message.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logDebugMl(verbThresh, format, ...) \
do { \
if (GlobInstcs::logPtr->debugVerbosity() > verbThresh) { \
GlobInstcs::logPtr->logMl(LOGSRC_DFLT, LOG_DEBUG, \
format, __VA_ARGS__); \
} \
} while (0)
/*!
* @brief Logs the debugging information even if the threshold was not set.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logDebugMlLv0(format, ...) \
logDebugMl(-1, format, __VA_ARGS__)
/*!
* @brief Logs the debugging information only if the verbosity exceeds 0.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logDebugMlLv1(format, ...) \
logDebugMl(0, format, __VA_ARGS__)
/*!
* @brief Logs the debugging information only if the verbosity exceeds 1.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logDebugMlLv2(format, ...) \
logDebugMl(1, format, __VA_ARGS__)
/*!
* @brief Logs the debugging information only if the verbosity exceeds 2.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logDebugMlLv3(format, ...) \
logDebugMl(2, format, __VA_ARGS__)
/*!
* @brief Logs information message.
*
* @param[in] format Format of the message.
* @param[in] ... Varidic arguments.
*/
#define logInfo(format, ...) \
do { \
GlobInstcs::logPtr->log(LOGSRC_DFLT, LOG_INFO, format, \
__VA_ARGS__); \
} while (0)
/*!
* @brief Logs info message. Automatic newline is added.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logInfoNL(format, ...) \
_internalLogNL(0, LOGSRC_DFLT, LOG_INFO, format, __VA_ARGS__)
/*!
* @brief Logs multi-line information message.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logInfoMl(format, ...) \
do { \
GlobInstcs::logPtr->logMl(LOGSRC_DFLT, LOG_INFO, format, \
__VA_ARGS__); \
} while (0)
/*!
* @brief Logs warning message.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logWarning(format, ...) \
do { \
GlobInstcs::logPtr->log(LOGSRC_DFLT, LOG_WARNING, format, \
__VA_ARGS__); \
} while (0)
/*!
* @brief Logs warning message. Automatic newline is added.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logWarningNL(format, ...) \
_internalLogNL(0, LOGSRC_DFLT, LOG_WARNING, format, __VA_ARGS__)
/*!
* @brief Logs multi-line warning message.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logWarningMl(format, ...) \
do { \
GlobInstcs::logPtr->logMl(LOGSRC_DFLT, LOG_WARNING, format, \
__VA_ARGS__); \
} while (0)
/*!
* @brief Logs error message.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logError(format, ...) \
do { \
GlobInstcs::logPtr->log(LOGSRC_DFLT, LOG_ERR, format, \
__VA_ARGS__); \
} while (0)
/*!
* @brief Logs error message. Automatic newline is added.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logErrorNL(format, ...) \
_internalLogNL(0, LOGSRC_DFLT, LOG_ERR, format, __VA_ARGS__)
/*!
* @brief Logs multi-line error message.
*
* @param[in] format Format of the message, follows printf syntax.
* @param[in] ... Variadic arguments.
*/
#define logErrorMl(format, ...) \
do { \
GlobInstcs::logPtr->logMl(LOGSRC_DFLT, LOG_ERR, format, \
__VA_ARGS__); \
} while (0)
#endif /* _LOG_H_ */
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#include <cstdarg>
#include <cstdint>
#include "src/datovka_shared/log/global.h"
#include "src/datovka_shared/log/log.h"
#include "src/datovka_shared/log/log_c.h"
void q_debug_call(const char *fmt, ...)
{
std::va_list argp;
va_start(argp, fmt);
qDebugCallV(fmt, argp);
va_end(argp);
}
int glob_log_verbosity(void)
{
return GlobInstcs::logPtr->logVerbosity();
}
int glob_debug_verbosity(void)
{
return GlobInstcs::logPtr->debugVerbosity();
}
int glob_log(enum LogSource source, enum LogLevel level, const char *fmt, ...)
{
std::va_list argp;
va_start(argp, fmt);
GlobInstcs::logPtr->logVlog(source, level, fmt, argp);
va_end(argp);
return 0;
}
int glob_log_ml(enum LogSource source, enum LogLevel level, const char *fmt,
...)
{
std::va_list argp;
va_start(argp, fmt);
GlobInstcs::logPtr->logVlogMl(source, level, fmt, argp);
va_end(argp);
return 0;
}
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#ifndef _LOG_C_H_
#define _LOG_C_H_
#include <stdint.h>
#include "src/datovka_shared/log/log_common.h"
/*!
* @brief Generates location debug information.
*/
#ifdef DEBUG
# define debug_func_call() \
do { \
/* q_debug_call("<FUNC> %s() '%s'", __func__, __FILE__); */ \
log_debug_lv2_nl("<FUNC> %s() '%s'", __func__, __FILE__); \
} while(0)
#else
/* Forces the semicolon after the macro. */
# define debug_func_call() do {} while(0)
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*!
* @brief Debugging using Qt-defined output.
*
* @param[in] fmt Format string.
*/
void q_debug_call(const char *fmt, ...);
/*!
* @brief Get logging verbosity.
*
* @return Logging verbosity.
*/
int glob_log_verbosity(void);
/*!
* @brief Get debug verbosity.
*
* @return Debug verbosity.
*/
int glob_debug_verbosity(void);
/*!
* @brief Log message.
*
* @param[in] source Source identifier.
* @param[in] level Message urgency level.
* @param[in] fmt Format of the log message -- follows printf(3) format.
* @return -1 if error, 0 else.
*/
int glob_log(enum LogSource source, enum LogLevel level, const char *fmt, ...);
/*!
* @brief Log multi-line message.
*
* Every new line is merged with the same prefix.
*
* @param[in] source Source identifier.
* @param[in] level Message urgency level.
* @param[in] fmt Format of the log message -- follows printf(3) format.
* @return -1 if error, 0 else.
*/
int glob_log_ml(enum LogSource source, enum LogLevel level, const char *fmt,
...);
#ifdef __cplusplus
} /* extern "C" */
#endif
/*!
* @brief A macro which logs a function name followed with given debugging
* information. The data are logged only when the global verbosity variable
* exceeds the given threshold.
*
* @param[in] verb_thresh Verbosity threshold.
* @param[in] format Format of the messages, follows printf syntax.
* @param[in] ... Variadic arguments.
*
* @note This macro works only if the DEBUG macro is defined. If no DEBUG macro
* is defined then it won't generate any code.
*/
#if DEBUG
#define log_debug_nl(verb_thresh, format, ...) \
if (glob_debug_verbosity() > verb_thresh) { \
if (glob_log_verbosity() > 0) { \