netgen/libsrc/core/logging.hpp

113 lines
3.1 KiB
C++
Raw Normal View History

2018-12-28 17:43:15 +05:00
#ifndef NETGEN_CORE_LOGGING_HPP
#define NETGEN_CORE_LOGGING_HPP
2019-01-16 18:33:48 +05:00
#include <iostream>
2018-12-28 17:43:15 +05:00
#include <memory>
#include <string>
#include <vector>
2019-01-16 18:33:48 +05:00
#include "exception.hpp"
2018-12-28 17:43:15 +05:00
#include "ngcore_api.hpp"
2019-01-16 18:33:48 +05:00
#include "utils.hpp"
2018-12-28 17:43:15 +05:00
2019-01-16 18:33:48 +05:00
#ifndef NETGEN_DEBUG_LOG
#define NETGEN_DEBUG_LOG(logger, ...)
#endif // NETGEN_DEBUG_LOG
2018-12-28 17:43:15 +05:00
namespace spdlog
{
2019-01-16 18:33:48 +05:00
class logger;
} // namespace spdlog
namespace ngcore
{
2019-05-28 16:51:53 +05:00
NGCORE_API extern std::ostream* testout; // NOLINT
2018-12-28 17:43:15 +05:00
namespace level
{
enum level_enum
2019-01-16 18:33:48 +05:00
{
trace = 0,
debug = 1,
info = 2,
warn = 3,
err = 4,
critical = 5,
off = 6
};
2019-01-16 19:10:53 +05:00
} // namespace level
2019-01-16 18:33:48 +05:00
class Logger
2019-01-02 22:38:03 +05:00
{
static NGCORE_API level::level_enum global_level;
2019-01-02 22:38:03 +05:00
public:
static void SetGlobalLoggingLevel( level::level_enum level ) { global_level = level; }
2019-01-16 18:33:48 +05:00
std::shared_ptr<spdlog::logger> logger;
2019-01-11 14:34:07 +05:00
2019-01-16 19:10:53 +05:00
Logger(std::shared_ptr<spdlog::logger> l) : logger(std::move(l)) {}
2019-01-16 18:33:48 +05:00
void NGCORE_API log( level::level_enum level, std::string && s);
2019-01-02 22:38:03 +05:00
template<typename T>
2019-01-16 18:33:48 +05:00
std::string replace(std::string s, const T & t)
{
auto p0 = s.find_first_of('{');
auto p1 = s.find_first_of('}', p0);
if(p0==std::string::npos || p1==std::string::npos)
throw Exception("invalid format string");
s.replace(p0, p1-p0+1, ToString(t));
return s;
}
std::string log_helper(std::string s)
{
return s;
}
template<typename T>
std::string log_helper(std::string s, const T &t)
{
return replace(s,t);
}
2019-01-02 22:38:03 +05:00
template<typename T, typename ... Args>
2019-01-16 18:33:48 +05:00
std::string log_helper( std::string s, const T &t, Args ... args)
2019-01-02 22:38:03 +05:00
{
2019-01-16 18:33:48 +05:00
return log_helper(replace(s,t), args...);
2019-01-02 22:38:03 +05:00
}
template<typename ... Args>
2019-01-16 18:33:48 +05:00
void log( level::level_enum level, const char* str, Args ... args)
2019-01-02 22:38:03 +05:00
{
2019-01-16 18:33:48 +05:00
log(level, log_helper(std::string(str), args...));
2019-01-02 22:38:03 +05:00
}
template<typename ... Args>
2019-01-16 18:33:48 +05:00
void trace( const char* str, Args ... args) { log(level::level_enum::trace, str, args...); }
2019-01-02 22:38:03 +05:00
template<typename ... Args>
2019-01-16 18:33:48 +05:00
void debug( const char* str, Args ... args) { log(level::level_enum::debug, str, args...); }
2019-01-02 22:38:03 +05:00
template<typename ... Args>
2019-01-16 18:33:48 +05:00
void info( const char* str, Args ... args) { log(level::level_enum::info, str, args...); }
2019-01-02 22:38:03 +05:00
template<typename ... Args>
2019-01-16 18:33:48 +05:00
void warn( const char* str, Args ... args) { log(level::level_enum::warn, str, args...); }
2019-01-02 22:38:03 +05:00
template<typename ... Args>
2019-01-16 18:33:48 +05:00
void error( const char* str, Args ... args) { log(level::level_enum::err, str, args...); }
2019-01-02 22:38:03 +05:00
template<typename ... Args>
2019-01-16 18:33:48 +05:00
void critical( const char* str, Args ... args) { log(level::level_enum::critical, str, args...); }
2019-01-02 22:38:03 +05:00
};
2018-12-28 17:43:15 +05:00
2019-01-16 18:33:48 +05:00
NGCORE_API std::shared_ptr<Logger> GetLogger(const std::string& name);
NGCORE_API void SetLoggingLevel(level::level_enum level, const std::string& name);
NGCORE_API void AddFileSink(const std::string& filename, level::level_enum level, const std::string& logger);
NGCORE_API void AddConsoleSink(level::level_enum level, const std::string& logger);
2018-12-28 17:43:15 +05:00
NGCORE_API void ClearLoggingSinks(const std::string& logger);
2019-01-16 18:33:48 +05:00
NGCORE_API void FlushOnLoggingLevel(level::level_enum level, const std::string& logger);
2018-12-28 17:43:15 +05:00
} // namespace ngcore
#endif // NETGEN_CORE_LOGGING_HPP