Botcraft 1.21.4
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
48namespace Botcraft
49{
50 enum class LogLevel
51 {
52 Trace,
53 Debug,
54 Info,
55 Warning,
56 Error,
57 Fatal,
58 None,
60 };
61 std::ostream& operator<<(std::ostream& os, const LogLevel v);
62
63 class Logger
64 {
65 private:
66 Logger();
67 public:
68 static constexpr std::array<std::string_view, static_cast<size_t>(LogLevel::NUM_LOG_LEVEL)> level_strings =
69 {
70 "[ TRACE ]",
71 "[ DEBUG ]",
72 "[ INFO ]",
73 "[WARNING]",
74 "[ ERROR ]",
75 "[ FATAL ]",
76 "[ ]"
77 };
78 Logger(const Logger&) = delete;
79 Logger& operator=(const Logger&) = delete;
80 Logger(Logger&&) = delete;
81 Logger& operator=(Logger&&) = delete;
82 ~Logger();
83
84 static Logger& GetInstance();
85 void Log(const std::string& s);
86 void SetFilename(const std::string& s);
87 void SetLogLevel(const LogLevel l);
88 LogLevel GetLogLevel() const;
89 void SetLogFunc(const std::function<void(const std::string&)>& f);
90 std::stringstream GetDate() const;
91
92 /// @brief Register the current thread in the map. It will be automatically removed on thread exit.
93 /// @param name Thread name
94 void RegisterThread(const std::string& name);
95
96 /// @brief Register a thread in the map. It will *NOT* be automatically removed on thread exit.
97 /// @param id Thread id
98 /// @param name Thread name
99 void RegisterThread(const std::thread::id id, const std::string& name);
100
101 /// @brief Get the name of a given thread
102 /// @param id Thread id
103 /// @return The name of the thread, "" if not in map
104 std::string GetThreadName(const std::thread::id id);
105
106 /// @brief Remove a thread from the map
107 /// @param id Thread id
108 void UnregisterThread(const std::thread::id id);
109
110 private:
111 std::mutex mutex;
112 std::string filename;
113 std::atomic<LogLevel> log_level;
114 std::function<void(const std::string&)> log_func;
115
116 std::chrono::steady_clock::time_point last_time_logged;
117 std::string file_buffer;
118
119 std::mutex thread_mutex;
120 std::unordered_map<std::thread::id, std::string> thread_names;
121 };
122}
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:113
void RegisterThread(const std::string &name)
Register the current thread in the map.
Definition Logger.cpp:104
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:116
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:117
void SetLogLevel(const LogLevel l)
Definition Logger.cpp:68
std::unordered_map< std::thread::id, std::string > thread_names
Definition Logger.hpp:120
Logger & operator=(Logger &&)=delete
Logger(const Logger &)=delete
std::mutex thread_mutex
Definition Logger.hpp:119
std::string filename
Definition Logger.hpp:112
std::function< void(const std::string &)> log_func
Definition Logger.hpp:114
Logger & operator=(const Logger &)=delete
static constexpr std::array< std::string_view, static_cast< size_t >(LogLevel::NUM_LOG_LEVEL)> level_strings
Definition Logger.hpp:68
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:111
std::ostream & operator<<(std::ostream &os, const EntityAttribute::Type v)