Skip to content

Commit

Permalink
feat(Logger): add va_list version of ::log
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichaelis committed Nov 2, 2023
1 parent bba5450 commit d4dad43
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/zenkit/Logger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once
#include "zenkit/Library.hh"

#include <cstdarg>
#include <cstdint>
#include <functional>
#include <string>
Expand Down Expand Up @@ -40,6 +41,7 @@ namespace zenkit {
ZKREM("renamed to ::set_default") ZKAPI static void use_default_logger();

ZK_PRINTF_LIKE(3, 4) ZKAPI static void log(LogLevel lvl, char const* name, char const* fmt, ...);
ZKAPI static void logv(LogLevel lvl, char const* name, char const* fmt, va_list ap);
ZKAPI static void set(LogLevel lvl, std::function<void(LogLevel, char const*, char const*)> const& cb);
ZKAPI static void set_default(LogLevel lvl);

Expand Down
10 changes: 6 additions & 4 deletions src/Logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ namespace zenkit {
}

void Logger::log(LogLevel lvl, char const* name, char const* fmt, ...) {
if (!_s_callback) return;
if (lvl > _s_level) return;

va_list ap;
va_start(ap, fmt);
vsnprintf(zk_global_logger_buffer, sizeof zk_global_logger_buffer - 1, fmt, ap);
logv(lvl, name, fmt, ap);
va_end(ap);
}

void Logger::logv(LogLevel lvl, char const* name, char const* fmt, va_list ap) {
if (!_s_callback) return;
if (lvl > _s_level) return;
vsnprintf(zk_global_logger_buffer, sizeof zk_global_logger_buffer - 1, fmt, ap);
_s_callback(lvl, name, zk_global_logger_buffer);
}

Expand Down

0 comments on commit d4dad43

Please sign in to comment.