Botcraft 1.21.10
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <atomic>
5#include <chrono>
6#include <functional>
7#include <mutex>
8#include <sstream>
9#include <string>
10#include <string_view>
11#include <thread>
12#include <unordered_map>
13
14constexpr const char* file_name(const char* path)
15{
16 const char* file = path;
17 while (*path)
18 {
19 if (*path == '\\' || *path == '/')
20 {
21 file = path + 1;
22 }
23 path += 1;
24 }
25 return file;
26}
27
28#define LOG(osstream, level) do { \
29 Botcraft::Logger& logger = Botcraft::Logger::GetInstance(); \
30 if (level < logger.GetLogLevel()) \
31 break; \
32 std::ostringstream logger_ostringstream; \
33 logger_ostringstream << logger.GetDate().rdbuf() \
34 << ' ' << Botcraft::Logger::level_strings[static_cast<size_t>(level)] \
35 << " [" << logger.GetThreadName(std::this_thread::get_id()) \
36 << "(" << std::this_thread::get_id() << ")] " \
37 << file_name(__FILE__) << '(' << __LINE__ << "): " << osstream << '\n'; \
38 logger.Log(logger_ostringstream.str()); \
39} while(0)
40
41#define LOG_TRACE(osstream) LOG(osstream, Botcraft::LogLevel::Trace)
42#define LOG_DEBUG(osstream) LOG(osstream, Botcraft::LogLevel::Debug)
43#define LOG_INFO(osstream) LOG(osstream, Botcraft::LogLevel::Info)
44#define LOG_WARNING(osstream) LOG(osstream, Botcraft::LogLevel::Warning)
45#define LOG_ERROR(osstream) LOG(osstream, Botcraft::LogLevel::Error)
46#define LOG_FATAL(osstream) LOG(osstream, Botcraft::LogLevel::Fatal)
47#define LOG_ALWAYS(osstream) LOG(osstream, Botcraft::LogLevel::NUM_LOG_LEVEL)
48
49namespace Botcraft
50{
51 enum class LogLevel
52 {
53 Trace,
54 Debug,
55 Info,
56 Warning,
57 Error,
58 Fatal,
59 None,
61 };
62 std::ostream& operator<<(std::ostream& os, const LogLevel v);
63
64 class Logger
65 {
66 private:
67 Logger();
68 public:
69 static constexpr std::array<std::string_view, static_cast<size_t>(LogLevel::NUM_LOG_LEVEL) + 1> level_strings =
70 {
71 "[ TRACE ]",
72 "[ DEBUG ]",
73 "[ INFO ]",
74 "[WARNING]",
75 "[ ERROR ]",
76 "[ FATAL ]",
77 "[ ]",
78 "[ALWAYS ]"
79 };
80 Logger(const Logger&) = delete;
81 Logger& operator=(const Logger&) = delete;
82 Logger(Logger&&) = delete;
83 Logger& operator=(Logger&&) = delete;
84 ~Logger();
85
86 static Logger& GetInstance();
87 void Log(const std::string& s);
88 void SetFilename(const std::string& s);
89 void SetLogLevel(const LogLevel l);
90 LogLevel GetLogLevel() const;
91 void SetLogFunc(const std::function<void(const std::string&)>& f);
92 std::stringstream GetDate() const;
93
94 /// @brief Register the current thread in the map. It will be automatically removed on thread exit.
95 /// @param name Thread name
96 void RegisterThread(const std::string& name);
97
98 /// @brief Register a thread in the map. It will *NOT* be automatically removed on thread exit.
99 /// @param id Thread id
100 /// @param name Thread name
101 void RegisterThread(const std::thread::id id, const std::string& name);
102
103 /// @brief Get the name of a given thread
104 /// @param id Thread id
105 /// @return The name of the thread, "" if not in map
106 std::string GetThreadName(const std::thread::id id);
107
108 /// @brief Remove a thread from the map
109 /// @param id Thread id
110 void UnregisterThread(const std::thread::id id);
111
112 private:
113 std::mutex mutex;
114 std::string filename;
115 std::atomic<LogLevel> log_level;
116 std::function<void(const std::string&)> log_func;
117
118 std::chrono::steady_clock::time_point last_time_logged;
119 std::string file_buffer;
120
121 std::mutex thread_mutex;
122 std::unordered_map<std::thread::id, std::string> thread_names;
123 };
124}
constexpr const char * file_name(const char *path)
Definition Logger.hpp:14
void UnregisterThread(const std::thread::id id)
Remove a thread from the map.
Definition Logger.cpp:130
std::atomic< LogLevel > log_level
Definition Logger.hpp:115
void RegisterThread(const std::string &name)
Register the current thread in the map.
Definition Logger.cpp:104
static constexpr std::array< std::string_view, static_cast< size_t >(LogLevel::NUM_LOG_LEVEL)+1 > level_strings
Definition Logger.hpp:69
LogLevel GetLogLevel() const
Definition Logger.cpp:73
std::stringstream GetDate() const
Definition Logger.cpp:84
void Log(const std::string &s)
Definition Logger.cpp:42
static Logger & GetInstance()
Definition Logger.cpp:36
std::chrono::steady_clock::time_point last_time_logged
Definition Logger.hpp:118
Logger(Logger &&)=delete
std::string GetThreadName(const std::thread::id id)
Get the name of a given thread.
Definition Logger.cpp:124
std::string file_buffer
Definition Logger.hpp:119
void SetLogLevel(const LogLevel l)
Definition Logger.cpp:68
std::unordered_map< std::thread::id, std::string > thread_names
Definition Logger.hpp:122
Logger & operator=(Logger &&)=delete
Logger(const Logger &)=delete
std::mutex thread_mutex
Definition Logger.hpp:121
std::string filename
Definition Logger.hpp:114
std::function< void(const std::string &)> log_func
Definition Logger.hpp:116
Logger & operator=(const Logger &)=delete
void SetFilename(const std::string &s)
Definition Logger.cpp:62
void SetLogFunc(const std::function< void(const std::string &)> &f)
Definition Logger.cpp:78
std::mutex mutex
Definition Logger.hpp:113
std::ostream & operator<<(std::ostream &os, const EntityAttribute::Type v)