Skip to content

Commit

Permalink
Add a UserData param to the LogHander function
Browse files Browse the repository at this point in the history
Like most other APIs do it
  • Loading branch information
Ghabry committed Dec 5, 2023
1 parent 9ecdb0d commit 5f07138
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/lcf/log_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define LCF_OUTPUT_H

#include "string_view.h"
#include "enum_tags.h"

namespace lcf {
namespace LogHandler {
Expand All @@ -22,15 +23,24 @@ enum class Level {
Highest
};

using LogHandlerFn = void (*)(Level level, StringView message);
static constexpr auto kLevelTags = lcf::makeEnumTags<Level>(
"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.
Expand Down
15 changes: 9 additions & 6 deletions src/log_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: ";
Expand All @@ -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;
}
}

Expand All @@ -60,7 +63,7 @@ namespace {
return {};
}

return std::string(buf, static_cast<unsigned int>(result) < sizeof(buf) ? result : sizeof(buf));
return {buf, static_cast<unsigned int>(result) < sizeof(buf) ? result : sizeof(buf)};
}
}

Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 5f07138

Please sign in to comment.