From 80f24f026adf00f4b846334c7abf5d93e3d8da3f Mon Sep 17 00:00:00 2001 From: peters <peters@irmb.tu-bs.de> Date: Tue, 3 Aug 2021 14:19:21 +0200 Subject: [PATCH] Moved logger to independent library --- CMakeLists.txt | 28 ++++++------ apps/cpu/LaminarTubeFlow/ltf.cpp | 4 +- src/basics/CMakeLists.txt | 2 +- src/cpu/VirtualFluids.h | 2 +- .../CoProcessors/WriteBlocksCoProcessor.cpp | 2 +- .../WriteBoundaryConditionsCoProcessor.cpp | 2 +- src/logger/CMakeLists.txt | 2 + src/{basics => }/logger/Logger.cpp | 45 +++++++++++++------ src/{basics => }/logger/Logger.h | 34 +++++++++++--- 9 files changed, 84 insertions(+), 37 deletions(-) create mode 100644 src/logger/CMakeLists.txt rename src/{basics => }/logger/Logger.cpp (68%) rename src/{basics => }/logger/Logger.h (70%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 25f8c1bb6..c65e87e08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -139,6 +139,19 @@ endif() ################################################################################# # COMMON LIBRARIES ################################################################################# +include(FetchContent) + +set(spdlog_version "v1.9.1") +set(spdlog_url "https://github.com/gabime/spdlog") +message(STATUS "Fetching spdlog: ${spdlog_version}") +FetchContent_Declare( + spdlog + GIT_REPOSITORY ${spdlog_url} + GIT_TAG ${spdlog_version} +) + +FetchContent_MakeAvailable(spdlog) + if(BUILD_VF_UNIT_TESTS) add_subdirectory(${VF_THIRD_DIR}/googletest) include(GoogleTest) @@ -151,25 +164,12 @@ endif() find_package(MPI REQUIRED) - +add_subdirectory(src/logger) add_subdirectory(src/basics) #add_subdirectory(src/mpi) #add_subdirectory(src/cuda) add_subdirectory(src/lbm) -include(FetchContent) - -set(spdlog_version "v1.9.1") -set(spdlog_url "https://github.com/gabime/spdlog") -message(STATUS "Fetching spdlog: ${spdlog_version}") -FetchContent_Declare( - spdlog - GIT_REPOSITORY ${spdlog_url} - GIT_TAG ${spdlog_version} -) - -FetchContent_MakeAvailable(spdlog) - ################################################################################# # VIRTUAL FLUIDS CPU / GPU diff --git a/apps/cpu/LaminarTubeFlow/ltf.cpp b/apps/cpu/LaminarTubeFlow/ltf.cpp index d3414618e..3cffab61c 100644 --- a/apps/cpu/LaminarTubeFlow/ltf.cpp +++ b/apps/cpu/LaminarTubeFlow/ltf.cpp @@ -49,6 +49,8 @@ void run(string configname) stringstream logFilename; logFilename << pathname + "/logfile" + UbSystem::toString(UbSystem::getTimeStamp()) + ".txt"; UbLog::output_policy::setStream(logFilename.str()); + + vf::logging::Logger::changeLogPath(pathname); } } @@ -347,7 +349,7 @@ void run(string configname) int main(int argc, char *argv[]) { try { - vf::basics::logging::initalizeLogger(); + vf::logging::Logger::initalizeLogger(); VF_LOG_INFO("Starting VirtualFluids..."); diff --git a/src/basics/CMakeLists.txt b/src/basics/CMakeLists.txt index 545ecdb8a..7f871424b 100644 --- a/src/basics/CMakeLists.txt +++ b/src/basics/CMakeLists.txt @@ -1,7 +1,7 @@ include(Core/buildInfo.cmake) -vf_add_library(PUBLIC_LINK spdlog MPI::MPI_CXX EXCLUDE buildInfo.in.cpp) +vf_add_library(PUBLIC_LINK logger MPI::MPI_CXX EXCLUDE buildInfo.in.cpp) vf_get_library_name (library_name) target_include_directories(${library_name} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Core) diff --git a/src/cpu/VirtualFluids.h b/src/cpu/VirtualFluids.h index c8e1c6bb4..f52a4634f 100644 --- a/src/cpu/VirtualFluids.h +++ b/src/cpu/VirtualFluids.h @@ -43,7 +43,7 @@ #include <basics/PointerDefinitions.h> #include <basics/config/ConfigurationFile.h> -#include <basics/logger/Logger.h> +#include <logger/Logger.h> #include <basics/container/CbArray2D.h> #include <basics/container/CbArray3D.h> diff --git a/src/cpu/VirtualFluidsCore/CoProcessors/WriteBlocksCoProcessor.cpp b/src/cpu/VirtualFluidsCore/CoProcessors/WriteBlocksCoProcessor.cpp index fc3e182e2..ed624de5b 100644 --- a/src/cpu/VirtualFluidsCore/CoProcessors/WriteBlocksCoProcessor.cpp +++ b/src/cpu/VirtualFluidsCore/CoProcessors/WriteBlocksCoProcessor.cpp @@ -33,7 +33,7 @@ #include "WriteBlocksCoProcessor.h" #include "basics/writer/WbWriterVtkXmlASCII.h" -#include <basics/logger/Logger.h> +#include <logger/Logger.h> #include "Block3D.h" #include "Communicator.h" diff --git a/src/cpu/VirtualFluidsCore/CoProcessors/WriteBoundaryConditionsCoProcessor.cpp b/src/cpu/VirtualFluidsCore/CoProcessors/WriteBoundaryConditionsCoProcessor.cpp index 18b388523..02f7bb4a2 100644 --- a/src/cpu/VirtualFluidsCore/CoProcessors/WriteBoundaryConditionsCoProcessor.cpp +++ b/src/cpu/VirtualFluidsCore/CoProcessors/WriteBoundaryConditionsCoProcessor.cpp @@ -37,7 +37,7 @@ #include <string> #include <vector> -#include <basics/logger/Logger.h> +#include <logger/Logger.h> #include "BCArray3D.h" #include "Block3D.h" diff --git a/src/logger/CMakeLists.txt b/src/logger/CMakeLists.txt new file mode 100644 index 000000000..4c6c52942 --- /dev/null +++ b/src/logger/CMakeLists.txt @@ -0,0 +1,2 @@ + +vf_add_library(NAME logger PUBLIC_LINK spdlog) diff --git a/src/basics/logger/Logger.cpp b/src/logger/Logger.cpp similarity index 68% rename from src/basics/logger/Logger.cpp rename to src/logger/Logger.cpp index 25f6f3dc8..708e359c8 100644 --- a/src/basics/logger/Logger.cpp +++ b/src/logger/Logger.cpp @@ -5,31 +5,50 @@ #include <spdlog/sinks/daily_file_sink.h> #include <spdlog/sinks/basic_file_sink.h> -namespace vf::basics::logging +namespace vf::logging { - void initalizeLogger() + + std::string Logger::logPath = {"logs/"}; + + void Logger::initalizeLogger() + { + updateDefaultLogger(); + + // setting default log level to trace + // levels: trace < debug < info < warn < error < critical + spdlog::set_level(spdlog::level::trace); + + // setting the log pattern + // formatting is documented here: https://github.com/gabime/spdlog/wiki/3.-Custom-formatting + spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v"); + + // according to the flush policy https://github.com/gabime/spdlog/wiki/7.-Flush-policy + spdlog::flush_on(spdlog::level::info); + } + + + void Logger::changeLogPath(const std::string& path) { - // Initalizing the spdlog logger https://github.com/gabime/spdlog + logPath = path; + + updateDefaultLogger(); + } + + void Logger::updateDefaultLogger() + { // initialize stdout sink with colored output auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>(); // initialize daily file sink // files will be written into "logs" folder relative to pwd. A new files is created at 0:00 o'clock. - auto daily_file_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>("logs/daily.txt", 0, 0); + auto daily_file_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>(logPath + "daily.txt", 0, 0); // initialize last run file sink - auto last_run_file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/last_run.txt", true); + auto last_run_file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logPath + "last_run.txt", true); // creating default logger with console and file sink spdlog::set_default_logger(std::make_shared<spdlog::logger>("default", spdlog::sinks_init_list({console_sink, daily_file_sink, last_run_file_sink}))); - - // setting default log level to trace - // levels: trace < debug < info < warn < error < critical - spdlog::set_level(spdlog::level::trace); - - // setting the log pattern - // formatting is documented here: https://github.com/gabime/spdlog/wiki/3.-Custom-formatting - spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v"); } + } diff --git a/src/basics/logger/Logger.h b/src/logger/Logger.h similarity index 70% rename from src/basics/logger/Logger.h rename to src/logger/Logger.h index 61b345e2d..594decaf5 100644 --- a/src/basics/logger/Logger.h +++ b/src/logger/Logger.h @@ -28,11 +28,22 @@ // //! \author Soeren Peters //======================================================================================= -#ifndef VF_BASICS_LOGGER_H -#define VF_BASICS_LOGGER_H - +#ifndef VF_LOGGER_H +#define VF_LOGGER_H +// VirtualFluids is using the spdlog logger https://github.com/gabime/spdlog #include <spdlog/spdlog.h> +// To initialize spdlog initalizeLogger() must be called. +// spdlog supports 5 log level, which can be changed at runtime e.g.: +// spdlog::set_level(spdlog::level::debug) +// The default log level is set to trace. Supported levels: trace < debug < info < warning < critical +// +// The logging is realized in 3 different log sinks: +// 1. colorded console output +// 2. a daily log file +// 3. a log file from the last run of VirtualFluids +// The default file path is relativ to executed command logs/ +// File path can be changed via changeLogPath() #define VF_LOG_TRACE(...) spdlog::trace(__VA_ARGS__) #define VF_LOG_DEBUG(...) spdlog::debug(__VA_ARGS__) @@ -41,9 +52,22 @@ #define VF_LOG_CRITICAL(...) spdlog::critical(__VA_ARGS__) -namespace vf::basics::logging +namespace vf::logging { - void initalizeLogger(); + class Logger + { + public: + // initalizing the above named logger + static void initalizeLogger(); + + // changing the path of the log files + static void changeLogPath(const std::string& path); + + private: + static void updateDefaultLogger(); + + static std::string logPath; + }; } #endif -- GitLab