OGS
Logging.cpp
Go to the documentation of this file.
1
12#include "Logging.h"
13
14#include <spdlog/common.h>
15#include <spdlog/sinks/stdout_color_sinks.h>
16#include <spdlog/spdlog.h>
17
18#include <exception>
19#include <iostream>
20#include <map>
21
22#ifdef USE_PETSC
23#include <mpi.h>
24#include <petscsys.h>
25#endif
26
27#include "Error.h"
28
29namespace BaseLib
30{
31#ifdef USE_PETSC
32std::shared_ptr<spdlog::logger> console = spdlog::stdout_color_mt("ogs");
33#else // USE_PETSC
34std::shared_ptr<spdlog::logger> console = spdlog::stdout_color_st("ogs");
35#endif // USE_PETSC
36
37void setConsoleLogLevel(std::string const& level_string)
38{
39 using namespace spdlog::level;
40 std::map<std::string, level_enum> string_to_log_level = {
41 {"none", off}, {"critical", critical}, {"error", err}, {"warn", warn},
42 {"info", info}, {"debug", debug}, {"all", trace}};
43
44 auto const level = string_to_log_level.find(level_string);
45 if (level == string_to_log_level.end())
46 {
47 ERR("'{:s}' is not a valid log level!", level_string);
48 OGS_FATAL("Wrong log level string.");
49 }
50 console->set_level(level->second);
51 spdlog::set_default_logger(console);
52}
53
54void initOGSLogger(std::string const& log_level)
55{
57 spdlog::set_pattern("%^%l:%$ %v");
58 spdlog::set_error_handler(
59 [](const std::string& msg)
60 {
61 std::cerr << "spdlog error: " << msg << std::endl;
62 OGS_FATAL("spdlog logger error occurred.");
63 });
64}
65} // namespace BaseLib
#define OGS_FATAL(...)
Definition Error.h:26
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:54
void setConsoleLogLevel(std::string const &level_string)
Definition Logging.cpp:37
std::shared_ptr< spdlog::logger > console
Definition Logging.cpp:32