Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1#pragma once
3#ifdef _WIN32
4#undef ERROR
5#endif
6#include <string>
7#include <iostream>
8#include <sstream>
9#include <mutex>
10#include <functional>
11#include <utility>
12#include <vector>
13
14namespace orbit::utils {
15
16enum class LogLevel {
17 DEBUG = 0,
18 INFO = 1,
19 WARN = 2,
20 ERROR = 3
21};
22
25enum class LogFormat { Text, Json };
26
27using LogFields = std::vector<std::pair<std::string, std::string>>;
28
29class Logger {
30public:
31 static void init(const std::string& level_str);
32 static void log(LogLevel level, const char* file, int line, const std::string& msg);
35 static void log_fields(LogLevel level, const char* file, int line, const std::string& msg,
36 const LogFields& fields);
37
40 static void set_format(const std::string& format);
41 static LogFormat format();
42
45 using Sink = std::function<void(const std::string& line)>;
46 static void set_sink(Sink sink);
47
49private:
50 static std::mutex log_mutex;
51 static std::string level_to_string(LogLevel level, bool color);
52};
53
54} // namespace utils
55
56// The macros expand where they are used, and <windows.h> (pulled in by curl,
57// winsock, ...) defines ERROR as a macro. They therefore refer to these
58// constants, which are declared here where ERROR has been #undef'd. The
59// stream variable has an unusual name so it cannot shadow a local variable
60// (MSVC treats that warning as an error).
62inline constexpr LogLevel kDebug = LogLevel::DEBUG;
63inline constexpr LogLevel kInfo = LogLevel::INFO;
64inline constexpr LogLevel kWarn = LogLevel::WARN;
65inline constexpr LogLevel kError = LogLevel::ERROR;
66} // namespace utils::log_levels
67
68#define ORBIT_LOG_AT(level, msg) do { if (::orbit::utils::Logger::current_level <= (level)) { std::ostringstream orbit_log_stream_; orbit_log_stream_ << msg; ::orbit::utils::Logger::log((level), __FILE__, __LINE__, orbit_log_stream_.str()); } } while(0)
69#define LOG_DEBUG(msg) ORBIT_LOG_AT(::orbit::utils::log_levels::kDebug, msg)
70#define LOG_INFO(msg) ORBIT_LOG_AT(::orbit::utils::log_levels::kInfo, msg)
71#define LOG_WARN(msg) ORBIT_LOG_AT(::orbit::utils::log_levels::kWarn, msg)
72#define LOG_ERROR(msg) ORBIT_LOG_AT(::orbit::utils::log_levels::kError, msg)
Definition Logger.hpp:29
std::function< void(const std::string &line)> Sink
Definition Logger.hpp:45
static std::string level_to_string(LogLevel level, bool color)
static void log(LogLevel level, const char *file, int line, const std::string &msg)
static void set_sink(Sink sink)
static void set_format(const std::string &format)
Parses "text" or "json" (case-insensitive); anything else keeps the current format.
static LogLevel current_level
Definition Logger.hpp:48
static void init(const std::string &level_str)
static void set_format(LogFormat format)
static LogFormat format()
static void log_fields(LogLevel level, const char *file, int line, const std::string &msg, const LogFields &fields)
static std::mutex log_mutex
Definition Logger.hpp:50
Definition Logger.hpp:61
constexpr LogLevel kDebug
Definition Logger.hpp:62
constexpr LogLevel kInfo
Definition Logger.hpp:63
constexpr LogLevel kWarn
Definition Logger.hpp:64
constexpr LogLevel kError
Definition Logger.hpp:65
Definition legacy_namespaces.hpp:26
LogFormat
Definition Logger.hpp:25
LogLevel
Definition Logger.hpp:16
std::vector< std::pair< std::string, std::string > > LogFields
Definition Logger.hpp:27