From bca54d1885f108865a984620dad38020abee138f Mon Sep 17 00:00:00 2001 From: Dario Date: Thu, 14 Nov 2019 11:15:45 +0100 Subject: [PATCH] update error logging and reading config.. both fro default system pathes --- .gitmodules | 3 + CMakeLists.txt | 1 + conanfile.txt | 2 +- dependencies/spirit-po | 1 + src/cpp/Gradido_LoginServer.cpp | 89 +++++++++++------- src/cpp/Gradido_LoginServer.h | 2 + src/cpp/ServerConfig.cpp | 2 + src/cpp/ServerConfig.h | 1 - src/cpp/SingletonManager/ErrorManager.cpp | 6 +- src/cpp/SingletonManager/ErrorManager.h | 3 + src/cpp/SingletonManager/LanguageManager.cpp | 92 +++++++++++++++++++ src/cpp/SingletonManager/LanguageManager.h | 95 ++++++++++++++++++++ src/cpp/gettext.h | 2 +- src/cpp/lib/ErrorList.cpp | 4 + src/cpp/lib/ErrorList.h | 3 + src/cpp/main.cpp | 2 + 16 files changed, 273 insertions(+), 35 deletions(-) create mode 160000 dependencies/spirit-po create mode 100644 src/cpp/SingletonManager/LanguageManager.cpp create mode 100644 src/cpp/SingletonManager/LanguageManager.h diff --git a/.gitmodules b/.gitmodules index 1e5245bf4..91643fa19 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "dependencies/iroha-ed25519"] path = dependencies/iroha-ed25519 url = ssh://git@***REMOVED***/~/Forks/iroha-ed25519.git +[submodule "dependencies/spirit-po"] + path = dependencies/spirit-po + url = https://github.com/cbeck88/spirit-po.git diff --git a/CMakeLists.txt b/CMakeLists.txt index df5d97bb2..b34f7e2f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ include_directories( "dependencies/iroha-ed25519/include" "dependencies/mariadb-connector-c/include" "dependencies/mariadb-connector-c/build/include" + "dependencies/spirit-po/include" #"dependencies/mariadb-connector-c/build/include" #"dependencies/mariadb-connector-c/include" #"import/mariadb/include" diff --git a/conanfile.txt b/conanfile.txt index 9378c4a10..cd2066f9d 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -2,7 +2,7 @@ Poco/1.9.4@pocoproject/stable libsodium/1.0.18@bincrafters/stable protobuf/3.9.1@bincrafters/stable - +boost/1.71.0@conan/stable [generators] cmake diff --git a/dependencies/spirit-po b/dependencies/spirit-po new file mode 160000 index 000000000..336789e62 --- /dev/null +++ b/dependencies/spirit-po @@ -0,0 +1 @@ +Subproject commit 336789e62363357d87894983e6cbdd6da6c838ea diff --git a/src/cpp/Gradido_LoginServer.cpp b/src/cpp/Gradido_LoginServer.cpp index acc88fc9f..fc497c47b 100644 --- a/src/cpp/Gradido_LoginServer.cpp +++ b/src/cpp/Gradido_LoginServer.cpp @@ -15,6 +15,7 @@ #include "Poco/Net/SSLManager.h" #include "Poco/Environment.h" #include "Poco/Logger.h" +#include "Poco/Path.h" #include "Poco/AsyncChannel.h" #include "Poco/SimpleFileChannel.h" #include "Poco/ConsoleChannel.h" @@ -72,6 +73,22 @@ void Gradido_LoginServer::displayHelp() helpFormatter.format(std::cout); } +void Gradido_LoginServer::createConsoleFileAsyncLogger(std::string name, std::string filePath) +{ + Poco::AutoPtr logConsoleChannel(new Poco::ConsoleChannel); + Poco::AutoPtr logFileChannel(new Poco::SimpleFileChannel(filePath)); + logFileChannel->setProperty("rotation", "500 K"); + Poco::AutoPtr logSplitter(new Poco::SplitterChannel); + logSplitter->addChannel(logConsoleChannel); + logSplitter->addChannel(logFileChannel); + + Poco::AutoPtr logAsyncChannel(new Poco::AsyncChannel(logSplitter)); + + Poco::Logger& log = Poco::Logger::get(name); + log.setChannel(logAsyncChannel); + log.setLevel("information"); +} + int Gradido_LoginServer::main(const std::vector& args) { Profiler usedTime; @@ -81,16 +98,53 @@ int Gradido_LoginServer::main(const std::vector& args) } else { + // ********** logging ************************************ + std::string log_Path = "/var/log/grd_login/"; +#ifdef _WIN32 || _WIN64 + log_Path = "./"; +#endif + + // init speed logger + Poco::AutoPtr speedLogFileChannel(new Poco::SimpleFileChannel(log_Path + "speedLog.txt")); + /* + The optional log file rotation mode: + never: no rotation (default) + : rotate if file size exceeds bytes + K: rotate if file size exceeds Kilobytes + M: rotate if file size exceeds Megabytes + */ + speedLogFileChannel->setProperty("rotation", "500 K"); + Poco::AutoPtr speedLogAsyncChannel(new Poco::AsyncChannel(speedLogFileChannel)); + + Poco::Logger& speedLogger = Poco::Logger::get("SpeedLog"); + speedLogger.setChannel(speedLogAsyncChannel); + speedLogger.setLevel("information"); + + // logging for request handling + createConsoleFileAsyncLogger("requestLog", log_Path + "requestLog.txt"); + + // error logging + createConsoleFileAsyncLogger("errorLog", log_Path + "errorLog.txt"); + Poco::Logger& errorLog = Poco::Logger::get("errorLog"); + + // *************** load from config ******************************************** + + std::string cfg_Path = Poco::Path::config() + "grd_login/"; + loadConfiguration(cfg_Path + "grd_login.properties"); + unsigned short port = (unsigned short)config().getInt("HTTPServer.port", 9980); unsigned short json_port = (unsigned short)config().getInt("JSONServer.port", 1201); + // load word lists if (!ServerConfig::loadMnemonicWordLists()) { - printf("[Gradido_LoginServer::%s] error loading mnemonic Word List\n", __FUNCTION__); + //printf("[Gradido_LoginServer::%s] error loading mnemonic Word List\n", __FUNCTION__); + errorLog.error("[Gradido_LoginServer::main] error loading mnemonic Word List"); return Application::EXIT_CONFIG; } if (!ServerConfig::initServerCrypto(config())) { - printf("[Gradido_LoginServer::%s] error init server crypto\n", __FUNCTION__); + //printf("[Gradido_LoginServer::%s] error init server crypto\n", __FUNCTION__); + errorLog.error("[Gradido_LoginServer::main] error init server crypto"); return Application::EXIT_CONFIG; } @@ -103,22 +157,6 @@ int Gradido_LoginServer::main(const std::vector& args) // start cpu scheduler uint8_t worker_count = Poco::Environment::processorCount() * 2; - // init speed logger - Poco::AutoPtr speedLogFileChannel(new Poco::SimpleFileChannel("speedLog.txt")); - /* - The optional log file rotation mode: - never: no rotation (default) - : rotate if file size exceeds bytes - K: rotate if file size exceeds Kilobytes - M: rotate if file size exceeds Megabytes - */ - speedLogFileChannel->setProperty("rotation", "500 K"); - Poco::AutoPtr speedLogAsyncChannel(new Poco::AsyncChannel(speedLogFileChannel)); - - Poco::Logger& speedLogger = Poco::Logger::get("SpeedLog"); - speedLogger.setChannel(speedLogAsyncChannel); - speedLogger.setLevel("information"); - ServerConfig::g_CPUScheduler = new UniLib::controller::CPUSheduler(worker_count, "Default Worker"); ServerConfig::g_CryptoCPUScheduler = new UniLib::controller::CPUSheduler(2, "Crypto Worker"); @@ -139,23 +177,12 @@ int Gradido_LoginServer::main(const std::vector& args) Poco::Net::initializeSSL(); if(!ServerConfig::initSSLClientContext()) { - printf("[Gradido_LoginServer::%s] error init server SSL Client\n", __FUNCTION__); + //printf("[Gradido_LoginServer::%s] error init server SSL Client\n", __FUNCTION__); + errorLog.error("[Gradido_LoginServer::main] error init server SSL Client\n"); return Application::EXIT_CONFIG; } - // logging for request handling - Poco::AutoPtr requestLogConsoleChannel(new Poco::ConsoleChannel); - Poco::AutoPtr requestLogFileChannel(new Poco::SimpleFileChannel("requestLog.txt")); - requestLogFileChannel->setProperty("rotation", "500 K"); - Poco::AutoPtr requestLogSplitter(new Poco::SplitterChannel); - requestLogSplitter->addChannel(requestLogConsoleChannel); - requestLogSplitter->addChannel(requestLogFileChannel); - - Poco::AutoPtr requestLogAsyncChannel(new Poco::AsyncChannel(requestLogSplitter)); - Poco::Logger& requestLog = Poco::Logger::get("requestLog"); - requestLog.setChannel(requestLogAsyncChannel); - requestLog.setLevel("information"); // HTTP Interface Server // set-up a server socket diff --git a/src/cpp/Gradido_LoginServer.h b/src/cpp/Gradido_LoginServer.h index c2451aad7..e96e80f8b 100644 --- a/src/cpp/Gradido_LoginServer.h +++ b/src/cpp/Gradido_LoginServer.h @@ -32,6 +32,8 @@ protected: int main(const std::vector& args); + void createConsoleFileAsyncLogger(std::string name, std::string filePath); + private: bool _helpRequested; }; diff --git a/src/cpp/ServerConfig.cpp b/src/cpp/ServerConfig.cpp index 8018b531a..e8264ab61 100644 --- a/src/cpp/ServerConfig.cpp +++ b/src/cpp/ServerConfig.cpp @@ -42,6 +42,8 @@ namespace ServerConfig { std::string g_php_serverHost; Poco::Mutex g_TimeMutex; + + bool loadMnemonicWordLists() { for (int i = 0; i < MNEMONIC_MAX; i++) { diff --git a/src/cpp/ServerConfig.h b/src/cpp/ServerConfig.h index cdf4dd6c3..611af97ed 100644 --- a/src/cpp/ServerConfig.h +++ b/src/cpp/ServerConfig.h @@ -41,7 +41,6 @@ namespace ServerConfig { extern std::string g_php_serverHost; extern Poco::Mutex g_TimeMutex; - bool loadMnemonicWordLists(); bool initServerCrypto(const Poco::Util::LayeredConfiguration& cfg); bool initEMailAccount(const Poco::Util::LayeredConfiguration& cfg); diff --git a/src/cpp/SingletonManager/ErrorManager.cpp b/src/cpp/SingletonManager/ErrorManager.cpp index aa31b4181..d5a0b54af 100644 --- a/src/cpp/SingletonManager/ErrorManager.cpp +++ b/src/cpp/SingletonManager/ErrorManager.cpp @@ -14,6 +14,7 @@ ErrorManager* ErrorManager::getInstance() } ErrorManager::ErrorManager() + : mLogging(Poco::Logger::get("errorLog")) { } @@ -36,7 +37,10 @@ void ErrorManager::addError(Error* error) auto it = mErrorsMap.find(id); std::list* list = nullptr; - printf("[ErrorManager::addError] error with function: %s, %s\n", error->getFunctionName(), error->getMessage()); + //printf("[ErrorManager::addError] error with function: %s, %s\n", error->getFunctionName(), error->getMessage()); + mLogging.error("[ErrorManager::addError] error with function: %s, %s", + std::string(error->getFunctionName()), std::string(error->getMessage()) + ); if (it == mErrorsMap.end()) { list = new std::list; diff --git a/src/cpp/SingletonManager/ErrorManager.h b/src/cpp/SingletonManager/ErrorManager.h index 5d2cc1ed8..530fe6be9 100644 --- a/src/cpp/SingletonManager/ErrorManager.h +++ b/src/cpp/SingletonManager/ErrorManager.h @@ -20,6 +20,7 @@ #include "../tasks/CPUTask.h" #include "Poco/Mutex.h" +#include "Poco/Logger.h" #include "Poco/Net/MailMessage.h" @@ -45,6 +46,8 @@ protected: std::map*> mErrorsMap; // how many errors should be stored + // poco logging + Poco::Logger& mLogging; }; diff --git a/src/cpp/SingletonManager/LanguageManager.cpp b/src/cpp/SingletonManager/LanguageManager.cpp new file mode 100644 index 000000000..1d67c48c9 --- /dev/null +++ b/src/cpp/SingletonManager/LanguageManager.cpp @@ -0,0 +1,92 @@ +#include "LanguageManager.h" + +LanguageCatalog::LanguageCatalog(Languages lang) + : mReferenceCount(1), mCatalog(nullptr), mThisLanguage(lang) +{ + // TODO: Catalog init code +} + +LanguageCatalog::~LanguageCatalog() +{ + if (mCatalog) { + delete mCatalog; + mCatalog = nullptr; + } +} + +void LanguageCatalog::release() +{ + lock(); + mReferenceCount--; + bool canReturnPointer = false; + if (mReferenceCount <= 0) { + canReturnPointer = true; + } + unlock(); + + if (canReturnPointer) { + //return pointer + LanguageManager::getInstance()->returnCatalog(this); + } +} + + +// ****************************************************** + +LanguageManager* LanguageManager::getInstance() +{ + static LanguageManager only; + return &only; +} + +LanguageManager::LanguageManager() + : mLogging(Poco::Logger::get("errorLog")) +{ + +} + +LanguageManager::~LanguageManager() +{ + lock(); + for (int lang = 0; lang < LANG_COUNT; lang++) { + for (auto it = mFreeCatalogs[lang].begin(); it != mFreeCatalogs[lang].end(); it++) { + delete *it; + } + mFreeCatalogs[lang].clear(); + } + unlock(); +} + +void LanguageManager::returnCatalog(LanguageCatalog* catalog) +{ + if (!catalog) return; + if (catalog->getLanguage() >= LANG_COUNT) { + //printf("[LanguageManager::returnCatalog] invalid language: %d\n", catalog->getLanguage()); + mLogging.error("[LanguageManager::returnCatalog] invalid language: %d", catalog->getLanguage()); + delete catalog; + return; + } + lock(); + mFreeCatalogs[catalog->getLanguage()].push_back(catalog); + unlock(); +} + +Poco::AutoPtr LanguageManager::getFreeCatalog(Languages lang) +{ + + if (lang >= LANG_COUNT) { + //printf("[LanguageManager::getFreeCatalog] invalid language: %d\n", lang); + mLogging.error("[LanguageManager::getFreeCatalog] invalid language: %d", lang); + return nullptr; + } + lock(); + if (mFreeCatalogs[lang].size() > 0) { + auto result = mFreeCatalogs[lang].back(); + result->duplicate(); + mFreeCatalogs[lang].pop_back(); + unlock(); + return result; + } + Poco::AutoPtr result = new LanguageCatalog(lang); + return result; +} \ No newline at end of file diff --git a/src/cpp/SingletonManager/LanguageManager.h b/src/cpp/SingletonManager/LanguageManager.h new file mode 100644 index 000000000..882e91de8 --- /dev/null +++ b/src/cpp/SingletonManager/LanguageManager.h @@ -0,0 +1,95 @@ +/*! +* +* \author: einhornimmond +* +* \date: 13.11.19 +* +* \brief: manage language translations with help of spirit_po +*/ + +#ifndef GRADIDO_LOGIN_SERVER_SINGLETON_MANAGER_LANGUAGE_MANAGER_H +#define GRADIDO_LOGIN_SERVER_SINGLETON_MANAGER_LANGUAGE_MANAGER_H + +#include "Poco/AutoPtr.h" +#include "Poco/Logger.h" + +#include "../lib/MultithreadContainer.h" +#include +#include + + +enum Languages { + LANG_DE, + LANG_EN, + LANG_COUNT +}; + +class LanguageCatalog : protected UniLib::lib::MultithreadContainer +{ + +public: + LanguageCatalog(Languages lang); + ~LanguageCatalog(); + + // for poco auto ptr + void duplicate() { lock(); mReferenceCount++; unlock(); }; + void release(); + + // catalog overload api + + inline const char * gettext(const char * msgid) { return mCatalog->gettext(msgid); } + inline const char * ngettext(const char * msgid, const char * msgid_plural, spirit_po::uint plural) { + return mCatalog->ngettext(msgid, msgid_plural, plural); + } + inline const char * pgettext(const char * msgctxt, const char * msgid) { + return mCatalog->pgettext(msgctxt, msgid); + } + inline const char * npgettext(const char * msgctxt, const char * msgid, const char * msgid_plural, spirit_po::uint plural) { + return mCatalog->npgettext(msgctxt, msgid, msgid_plural, plural); + } + + inline std::string gettext_str(const std::string & msgid) { return mCatalog->gettext_str(msgid); } + inline std::string ngettext_str(const std::string & msgid, const std::string & msgid_plural, spirit_po::uint plural) { + return mCatalog->ngettext_str(msgid, msgid_plural, plural); + } + inline std::string pgettext_str(const std::string & msgctxt, const std::string & msgid) { + return mCatalog->pgettext_str(msgctxt, msgid); + } + inline std::string npgettext_str(const std::string & msgctxt, const std::string & msgid, const std::string & msgid_plural, spirit_po::uint plural) { + return mCatalog->npgettext_str(msgctxt, msgid, msgid_plural, plural); + } + + inline Languages getLanguage() { return mThisLanguage; } + +protected: + int mReferenceCount; + spirit_po::default_catalog* mCatalog; + Languages mThisLanguage; +}; + +class LanguageManager : protected UniLib::lib::MultithreadContainer +{ + friend class LanguageCatalog; +public: + ~LanguageManager(); + + static LanguageManager* getInstance(); + + Poco::AutoPtr getFreeCatalog(Languages lang); + + +protected: + LanguageManager(); + + void returnCatalog(LanguageCatalog* catalog); + + std::list mFreeCatalogs[LANG_COUNT]; + + // poco logging + Poco::Logger& mLogging; + +}; + + + +#endif //GRADIDO_LOGIN_SERVER_SINGLETON_MANAGER_LANGUAGE_MANAGER_H \ No newline at end of file diff --git a/src/cpp/gettext.h b/src/cpp/gettext.h index c1e3587c4..a9055b1a5 100644 --- a/src/cpp/gettext.h +++ b/src/cpp/gettext.h @@ -7,7 +7,7 @@ extern "C" { #endif -static inline char *gettext (const char *__msgid) +static inline const char *gettext (const char *__msgid) { return __msgid; } diff --git a/src/cpp/lib/ErrorList.cpp b/src/cpp/lib/ErrorList.cpp index d9e14bf20..61c59ec50 100644 --- a/src/cpp/lib/ErrorList.cpp +++ b/src/cpp/lib/ErrorList.cpp @@ -38,6 +38,7 @@ int SendErrorMessage::run() ErrorList::ErrorList() + : mLogging(Poco::Logger::get("errorLog")) { } @@ -52,6 +53,9 @@ ErrorList::~ErrorList() void ErrorList::addError(Error* error) { + mLogging.error("[ErrorList::addError] error with function: %s, %s", + std::string(error->getFunctionName()), std::string(error->getMessage()) + ); mErrorStack.push(error); } diff --git a/src/cpp/lib/ErrorList.h b/src/cpp/lib/ErrorList.h index fbfe9a465..231d9f509 100644 --- a/src/cpp/lib/ErrorList.h +++ b/src/cpp/lib/ErrorList.h @@ -17,6 +17,7 @@ #include "Poco/Net/SecureSMTPClientSession.h" #include "Poco/Net/StringPartSource.h" +#include "Poco/Logger.h" class ErrorList : public IErrorCollection { @@ -47,6 +48,8 @@ public: protected: std::stack mErrorStack; + // poco logging + Poco::Logger& mLogging; }; class SendErrorMessage : public UniLib::controller::CPUTask diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 8423a20c4..fa8c5c2ce 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -16,6 +16,8 @@ int main(int argc, char** argv) } printf("User size: %d Bytes, Session size: %d Bytes\n", sizeof(User), sizeof(Session)); + + Gradido_LoginServer app; return app.run(argc, argv); }