diff --git a/BaseLib/Logging.cpp b/BaseLib/Logging.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebc3829b86e94c54ee7e0ecef6e59586ac9de2da --- /dev/null +++ b/BaseLib/Logging.cpp @@ -0,0 +1,37 @@ +/** + * \file + * + * \copyright + * Copyright (c) 2012-2020, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#include "Logging.h" +#include <spdlog/sinks/stdout_color_sinks.h> +#include <map> + +#include "Error.h" +namespace BaseLib +{ +std::shared_ptr<spdlog::logger> console = spdlog::stdout_color_st("ogs"); + +void setConsoleLogLevel(std::string const& level_string) +{ + using namespace spdlog::level; + std::map<std::string, level_enum> string_to_log_level = { + {"none", off}, {"critical", critical}, {"error", err}, {"warn", warn}, + {"info", info}, {"debug", debug}, {"all", trace}}; + + auto const level = string_to_log_level.find(level_string); + if (level == string_to_log_level.end()) + { + ERR("'{:s}' is not a valid log level!", level_string); + OGS_FATAL("Wrong log level string."); + } + console->set_level(level->second); + spdlog::set_default_logger(console); +} +} // namespace BaseLib diff --git a/BaseLib/Logging.h b/BaseLib/Logging.h new file mode 100644 index 0000000000000000000000000000000000000000..4aaa631e09831eda75a8b2b78404c719b133d1ef --- /dev/null +++ b/BaseLib/Logging.h @@ -0,0 +1,47 @@ +/** + * \file + * + * \copyright + * Copyright (c) 2012-2020, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#pragma once + +#include <spdlog/spdlog.h> + +namespace BaseLib +{ +extern std::shared_ptr<spdlog::logger> console; +void setConsoleLogLevel(std::string const& level_string); + +} // namespace BaseLib + +template <typename... Args> +void DBUG(char const* fmt, Args const&... args) +{ + BaseLib::console->debug(fmt, args...); +} +template <typename... Args> +void INFO(char const* fmt, Args const&... args) +{ + BaseLib::console->info(fmt, args...); +} +template <typename... Args> +void WARN(char const* fmt, Args const&... args) +{ + BaseLib::console->warn(fmt, args...); +} +template <typename... Args> +void ERR(char const* fmt, Args const&... args) +{ + BaseLib::console->error(fmt, args...); +} +template <typename... Args> +void CRITICAL(char const* fmt, Args const&... args) +{ + BaseLib::console->critical(fmt, args...); +}