15#include <condition_variable>
29#define OAK_LOG(level, ...) oak::log2(level, __FILE__, __LINE__, __VA_ARGS__)
30#define OAK_DEBUG(...) OAK_LOG(oak::Level::Debug, __VA_ARGS__)
31#define OAK_INFO(...) OAK_LOG(oak::Level::Info, __VA_ARGS__)
32#define OAK_WARN(...) OAK_LOG(oak::Level::Warn, __VA_ARGS__)
33#define OAK_ERROR(...) OAK_LOG(oak::Level::Error, __VA_ARGS__)
35#define OAK_LOG2(logger, level, ...) logger->log2(level, __FILE__, __LINE__, __VA_ARGS__)
36#define OAK_DEBUG2(logger, ...) OAK_LOG2(logger, oak::Level::Debug, __VA_ARGS__)
37#define OAK_INFO2(logger, ...) OAK_LOG2(logger, oak::Level::Info, __VA_ARGS__)
38#define OAK_WARN2(logger, ...) OAK_LOG2(logger, oak::Level::Warn, __VA_ARGS__)
39#define OAK_ERROR2(logger, ...) OAK_LOG2(logger, oak::Level::Error, __VA_ARGS__)
82 virtual void write(
const std::string& str) = 0;
92 bool should_stop =
false;
93 std::queue<std::string> logs;
95 std::mutex logs_mutex;
98 std::condition_variable cv;
109 FileWriter(
const std::filesystem::path &path);
111 void write(
const std::string& str)
override;
128 void write(
const std::string& str)
override;
141 const std::string&
log)>;
148 template<
typename... Args>
149 inline void debug(
const char *fmt, Args &&...args);
150 template<
typename... Args>
151 inline void debug2(
const char* file,
int line,
152 const char *fmt, Args &&...args);
154 template<
typename... Args>
155 inline void info(
const char *fmt, Args &&...args);
156 template<
typename... Args>
157 inline void info2(
const char* file,
int line,
158 const char *fmt, Args &&...args);
160 template<
typename... Args>
161 inline void warn(
const char *fmt, Args &&...args);
162 template<
typename... Args>
163 inline void warn2(
const char* file,
int line,
164 const char *fmt, Args &&...args);
166 template<
typename... Args>
167 inline void error(
const char *fmt, Args &&...args);
168 template<
typename... Args>
169 inline void error2(
const char* file,
int line,
170 const char *fmt, Args &&...args);
172 template<
typename... Args>
173 void log(
enum Level l,
const char *fmt, Args &&...args);
174 template<
typename... Args>
176 const char *fmt, Args &&...args);
178 template<
typename W,
typename ...Args>
188 template<
typename ...F>
190 template<
typename ...F>
195 std::expected<int, std::string>
203 void enable_event(
unsigned int id,
const std::string& name);
206 template<
typename... Args>
207 void event2(
const char* file,
int line,
unsigned int id,
208 const char *fmt, Args &&...args);
209 template<
typename... Args>
210 void event(
unsigned int id,
const char *fmt, Args &&...args);
214 static std::string colorize(
enum Level level,
const std::string &str);
215 static std::string json_formatter(
enum Level level,
int flags,
216 const char* file,
int line,
217 const std::string&
log);
221 std::mutex logger_mutex;
222 std::unordered_map<unsigned int, std::string> events;
224 static const Formatter default_formatter;
226 std::vector<std::shared_ptr<Writer>> writers;
235template<
typename... Args>
236static inline void log2(
enum oak::Level level,
const char* file,
int line,
const char* fmt, Args &&...args);
238template<
typename... Args>
239static inline void log(
enum oak::Level level,
const char* fmt, Args &&...args);
241template<
typename W,
typename ...Args>
250template<
typename ...F>
252template<
typename ...F>
254static inline std::expected<int, std::string>
258 const std::string& name);
266template <>
struct std::formatter<
oak::Level>
268 constexpr auto parse(format_parse_context &ctx)
273 template <
typename FormatContext>
279 return format_to(ctx.out(),
"error");
281 return format_to(ctx.out(),
"warn");
283 return format_to(ctx.out(),
"info");
285 return format_to(ctx.out(),
"debug");
287 return format_to(ctx.out(),
"unknown");
292template <>
struct std::formatter<
oak::Flags>
294 constexpr auto parse(format_parse_context &ctx)
299 template <
typename FormatContext>
305 return format_to(ctx.out(),
"none");
307 return format_to(ctx.out(),
"level");
309 return format_to(ctx.out(),
"date");
311 return format_to(ctx.out(),
"time");
313 return format_to(ctx.out(),
"pid");
315 return format_to(ctx.out(),
"tid");
317 return format_to(ctx.out(),
"unknown");
std::string get_name() override
static const std::string name
void write(const std::string &str) override
void enable_event(unsigned int id, const std::string &name)
std::expected< int, std::string > load_config_file(const std::filesystem::path &file)
void event(unsigned int id, const char *fmt, Args &&...args)
void debug(const char *fmt, Args &&...args)
std::function< std::string(enum Level, int flags, const char *file, int line, const std::string &log)> Formatter
void error(const char *fmt, Args &&...args)
void info2(const char *file, int line, const char *fmt, Args &&...args)
void event2(const char *file, int line, unsigned int id, const char *fmt, Args &&...args)
bool remove_writer(const std::string &name)
void warn(const char *fmt, Args &&...args)
void add_flags(Flags flag, F &&... flags)
void debug2(const char *file, int line, const char *fmt, Args &&...args)
void error2(const char *file, int line, const char *fmt, Args &&...args)
void set_formatter(Formatter formatter)
void add_flags(Flags flag)
void info(const char *fmt, Args &&...args)
void disable_event(unsigned int id)
unsigned int get_flags() const
void set_level(enum Level level)
void log2(enum Level level, const char *file, int line, const char *fmt, Args &&...args)
void add_writer(Args &&...init_args)
void log(enum Level l, const char *fmt, Args &&...args)
enum Level get_level() const
void set_flags(F &&... flags)
void warn2(const char *file, int line, const char *fmt, Args &&...args)
static const std::string name
void write(const std::string &str) override
std::string get_name() override
virtual void write(const std::string &str)=0
virtual std::string get_name()=0
void submit(const std::string &log)
std::string level_to_string(enum Level level)
static std::expected< int, std::string > load_config_file(const std::filesystem::path &file)
static void add_writer(Args &&...init_args)
static void log2(enum oak::Level level, const char *file, int line, const char *fmt, Args &&...args)
static void disable_event(unsigned int id)
static void log(enum oak::Level level, const char *fmt, Args &&...args)
static void add_flags(F &&...flags)
static unsigned int get_flags()
static void enable_event(unsigned int id, const std::string &name)
static void set_flags(F &&...flags)
static enum Level get_level()
static bool remove_writer(const std::string &name)
static void set_level(enum Level level)