Introduce logging in shared library dictionaries

This commit is contained in:
Alexander Sapin 2018-06-05 22:17:16 +03:00
parent ead9493741
commit 4e3dc0d8a2
4 changed files with 109 additions and 1 deletions

View File

@ -124,12 +124,18 @@ LibraryDictionarySource::LibraryDictionarySource(const DictionaryStructure & dic
description.init(sample_block);
library = std::make_shared<SharedLibrary>(path);
settings = std::make_shared<CStringsHolder>(getLibSettings(config, config_prefix + lib_config_settings));
if (auto libNew = library->tryGet<decltype(lib_data) (*)(decltype(&settings->strings))>("ClickHouseDictionary_v2_libNew"))
if (auto libNew = library->tryGet<decltype(lib_data) (*)(decltype(&settings->strings), ClickHouseLibrary::CLogger*)>("ClickHouseDictionary_v2_libNew")) {
clogger = std::make_shared<ClickHouseLibrary::CLogger>();
ClickHouseLibrary::initDictLogger(clogger.get());
lib_data = libNew(&settings->strings, clogger.get());
} else if (auto libNew = library->tryGet<decltype(lib_data) (*)(decltype(&settings->strings))>("ClickHouseDictionary_v2_libNew")) {
lib_data = libNew(&settings->strings);
}
}
LibraryDictionarySource::LibraryDictionarySource(const LibraryDictionarySource & other)
: log(&Logger::get("LibraryDictionarySource"))
, clogger{other.clogger}
, dict_struct{other.dict_struct}
, config_prefix{other.config_prefix}
, path{other.path}
@ -139,8 +145,11 @@ LibraryDictionarySource::LibraryDictionarySource(const LibraryDictionarySource &
, description{other.description}
, settings{other.settings}
{
if (auto libClone = library->tryGet<decltype(lib_data) (*)(decltype(other.lib_data))>("ClickHouseDictionary_v2_libClone"))
lib_data = libClone(other.lib_data);
else if (auto libNew = library->tryGet<decltype(lib_data) (*)(decltype(&settings->strings), ClickHouseLibrary::CLogger*)>("ClickHouseDictionary_v2_libNew"))
lib_data = libNew(&settings->strings, clogger.get());
else if (auto libNew = library->tryGet<decltype(lib_data) (*)(decltype(&settings->strings))>("ClickHouseDictionary_v2_libNew"))
lib_data = libNew(&settings->strings);
}

View File

@ -3,6 +3,7 @@
#include <Dictionaries/DictionaryStructure.h>
#include <Dictionaries/ExternalResultDescription.h>
#include <Dictionaries/IDictionarySource.h>
#include <Dictionaries/LibraryDictionarySourceExternal.h>
#include <Common/SharedLibrary.h>
#include <common/LocalDateTime.h>
@ -65,6 +66,7 @@ public:
private:
Poco::Logger * log;
std::shared_ptr<ClickHouseLibrary::CLogger> clogger;
LocalDateTime getLastModification() const;

View File

@ -0,0 +1,82 @@
#include <Dictionaries/LibraryDictionarySourceExternal.h>
#include <common/logger_useful.h>
namespace
{
const char DICT_LOGGER_NAME[] = "LibraryDictionarySourceExternal";
void trace(ClickHouseLibrary::CString msg)
{
auto & logger = Logger::get(DICT_LOGGER_NAME);
if (logger.trace()) {
logger.trace(msg);
}
}
void debug(ClickHouseLibrary::CString msg)
{
auto & logger = Logger::get(DICT_LOGGER_NAME);
if (logger.debug()) {
logger.debug(msg);
}
}
void information(ClickHouseLibrary::CString msg)
{
auto & logger = Logger::get(DICT_LOGGER_NAME);
if (logger.information()) {
logger.information(msg);
}
}
void notice(ClickHouseLibrary::CString msg)
{
auto & logger = Logger::get(DICT_LOGGER_NAME);
if (logger.notice()) {
logger.notice(msg);
}
}
void warning(ClickHouseLibrary::CString msg)
{
auto & logger = Logger::get(DICT_LOGGER_NAME);
if (logger.warning()) {
logger.warning(msg);
}
}
void error(ClickHouseLibrary::CString msg)
{
auto & logger = Logger::get(DICT_LOGGER_NAME);
if (logger.error()) {
logger.error(msg);
}
}
void critical(ClickHouseLibrary::CString msg)
{
auto & logger = Logger::get(DICT_LOGGER_NAME);
if (logger.critical()) {
logger.critical(msg);
}
}
void fatal(ClickHouseLibrary::CString msg)
{
auto & logger = Logger::get(DICT_LOGGER_NAME);
if (logger.fatal()) {
logger.fatal(msg);
}
}
}
void ClickHouseLibrary::initDictLogger(CLogger * logger) {
logger->trace = trace;
logger->debug = debug;
logger->information = information;
logger->notice = notice;
logger->warning = warning;
logger->error = error;
logger->critical = critical;
logger->fatal = fatal;
}

View File

@ -47,4 +47,19 @@ struct Table
uint64_t error_code = 0; // 0 = ok; !0 = error, with message in error_string
const char * error_string = nullptr;
};
struct CLogger
{
void (*trace)(CString msg);
void (*debug)(CString msg);
void (*information)(CString msg);
void (*notice)(CString msg);
void (*warning)(CString msg);
void (*error)(CString msg);
void (*critical)(CString msg);
void (*fatal)(CString msg);
};
void initDictLogger(CLogger * logger);
}