diff --git a/src/lcf/log_handler.h b/src/lcf/log_handler.h index c6eb69ac..26a2165d 100644 --- a/src/lcf/log_handler.h +++ b/src/lcf/log_handler.h @@ -11,6 +11,7 @@ #define LCF_OUTPUT_H #include "string_view.h" +#include "enum_tags.h" namespace lcf { namespace LogHandler { @@ -22,15 +23,24 @@ enum class Level { Highest }; -using LogHandlerFn = void (*)(Level level, StringView message); +static constexpr auto kLevelTags = lcf::makeEnumTags( + "Debug", + "Warning", + "Error", + "Highest" +); + +using UserData = void*; +using LogHandlerFn = void (*)(Level level, StringView message, UserData userdata); /** * Sets the output handler for all lcf logging. * The default handler prints to standard error. * * @param fn New output handler. nullptr for default handler. + * @param userdata Passed to the log handler function. Is not touched by liblcf. */ -void SetHandler(LogHandlerFn fn); +void SetHandler(LogHandlerFn fn, UserData userdata = nullptr); /** * Only report issues that have at least this log level. diff --git a/src/log_handler.cpp b/src/log_handler.cpp index 33272c8c..623aad8d 100644 --- a/src/log_handler.cpp +++ b/src/log_handler.cpp @@ -16,7 +16,7 @@ namespace lcf { namespace LogHandler { namespace { - void DefaultHandler(lcf::LogHandler::Level level, StringView message) { + void DefaultHandler(LogHandler::Level level, StringView message, UserData) { switch (level) { case Level::Debug: std::cerr << "Debug: "; @@ -35,13 +35,16 @@ namespace { Level level = Level::Debug; LogHandlerFn output_fn = DefaultHandler; + UserData output_userdata = nullptr; } -void SetHandler(LogHandlerFn fn) { +void SetHandler(LogHandlerFn fn, UserData userdata) { if (!fn) { output_fn = DefaultHandler; + output_userdata = nullptr; } else { output_fn = fn; + output_userdata = userdata; } } @@ -60,7 +63,7 @@ namespace { return {}; } - return std::string(buf, static_cast(result) < sizeof(buf) ? result : sizeof(buf)); + return {buf, static_cast(result) < sizeof(buf) ? result : sizeof(buf)}; } } @@ -69,7 +72,7 @@ void Debug(const char* fmt, ...) { va_list args; va_start(args, fmt); auto msg = format_string(fmt, args); - LogHandler::output_fn(LogHandler::Level::Debug, msg); + LogHandler::output_fn(LogHandler::Level::Debug, msg, LogHandler::output_userdata); va_end(args); } } @@ -79,7 +82,7 @@ void Warning(const char* fmt, ...) { va_list args; va_start(args, fmt); auto msg = format_string(fmt, args); - LogHandler::output_fn(LogHandler::Level::Warning, msg); + LogHandler::output_fn(LogHandler::Level::Warning, msg, LogHandler::output_userdata); va_end(args); } } @@ -89,7 +92,7 @@ void Error(const char* fmt, ...) { va_list args; va_start(args, fmt); auto msg = format_string(fmt, args); - LogHandler::output_fn(LogHandler::Level::Error, msg); + LogHandler::output_fn(LogHandler::Level::Error, msg, LogHandler::output_userdata); va_end(args); } }