diff --git a/CMakeLists.txt b/CMakeLists.txt index 25f8c1bb65dabd69583311a5bb5cc71fedd6d175..c65e87e0871db2633ac84d4b0de960ad6d06b77f 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 d3414618e5326b0dac85ec9a7493b3e92aa97fbc..3cffab61c644e4b0cfab2795e2a8c7698555262a 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 545ecdb8a2b2bfb2257e2bd7119703e766fea754..7f871424b2c6849d2c0f6e8d277b17214fa5cd9c 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 c8e1c6bb482567d9a9ca19f548f6e4112ad078c5..f52a4634f8266a886598558c205560a85bdbc21e 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 fc3e182e296b3b542abc8f0821fc6918a8a32f56..ed624de5bb0e750c9e2f3cfaf301cdc7f66fd2a3 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 18b388523e5cb5dee1c3f5ceeb2b0f0f89eb6011..02f7bb4a28972f946d7d6a3d45487a7906494fea 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 0000000000000000000000000000000000000000..4c6c5294241eb5f3587e126003e7f669592905fa --- /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 25f6f3dc8ad4648a84a7a3120770224151752ddd..708e359c8430380dd57c404ed9b3c41f53dcb714 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 61b345e2d1a4f9529909876ddb50092c694d3130..594decaf5bd85913335e6d1659b6d89cad6d0610 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