VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
VoltMod::Log Namespace Reference

Typedefs

using Handler = std::function< void(LogLevel level, std::string_view message)>
 Where log lines end up.
 

Functions

void SetHandler (Handler handler)
 
bool Enabled ()
 
void SetMinimumLevel (LogLevel level)
 
LogLevel MinimumLevel ()
 
bool Wanted (LogLevel level)
 
void Emit (LogLevel level, std::string message)
 
void DeliverPending ()
 
template<typename... Args>
void Info (std::format_string< Args... > fmt, Args &&... args)
 
template<typename... Args>
void Warn (std::format_string< Args... > fmt, Args &&... args)
 
template<typename... Args>
void Error (std::format_string< Args... > fmt, Args &&... args)
 

Variables

static Handler g_handler
 
static std::thread::id g_gameThread {}
 
static std::atomic< LogLevel > g_minimumLevel {LogLevel::Info}
 
static std::mutex g_deferredMutex
 
static std::deque< std::pair< LogLevel, std::string > > g_deferred
 
static std::atomic< bool > g_hasDeferred {false}
 
static constexpr size_t MaxDeferred = 256
 

Typedef Documentation

◆ Handler

using VoltMod::Log::Handler = typedef std::function<void(LogLevel level, std::string_view message)>

Where log lines end up.

Invoked on the game thread only. message borrows the caller's storage for the duration of the call, so a handler that keeps a line must copy it.

Definition at line 30 of file Log.hpp.

Function Documentation

◆ DeliverPending()

void VoltMod::Log::DeliverPending ( )

Replay lines queued from worker threads. Game thread only; Runtime::OnGameFrame calls it.

Definition at line 69 of file Logger.cpp.

References g_deferred, g_deferredMutex, g_handler, g_hasDeferred, and VoltMod::ReadFile().

Referenced by VoltMod::Runtime::OnGameFrame(), and VoltMod::Runtime::~Runtime().

◆ Emit()

void VoltMod::Log::Emit ( LogLevel  level,
std::string  message 
)

Route one formatted line to the handler.

The console handler reaches tier0's ConColorMsg/Msg, which is game-thread-only, but the database and HTTP workers log too. A line raised off the installing thread is queued instead and replayed by DeliverPending, so worker diagnostics still reach the console without a worker ever touching the engine.

Definition at line 47 of file Logger.cpp.

References g_deferred, g_deferredMutex, g_gameThread, g_handler, g_hasDeferred, MaxDeferred, and VoltMod::ReadFile().

Referenced by Error(), Info(), Warn(), and VoltMod::PluginContext::WriteLog().

◆ Enabled()

bool VoltMod::Log::Enabled ( )

True while a handler is installed. The gate the formatting helpers below check.

Definition at line 32 of file Logger.cpp.

References g_handler.

Referenced by Wanted().

◆ Error()

◆ Info()

◆ MinimumLevel()

LogLevel VoltMod::Log::MinimumLevel ( )

The level set by SetMinimumLevel; LogLevel::Info until one is.

Definition at line 42 of file Logger.cpp.

References g_minimumLevel.

Referenced by Wanted().

◆ SetHandler()

void VoltMod::Log::SetHandler ( Handler  handler)

Install the process-wide handler and record the calling thread as the game thread.

Set once per load cycle by the plugin module, before anything else logs. A file-static rather than an injected service because logging must work from code that holds no runtime at all - static initializers, engine trampolines, and worker threads.

Definition at line 26 of file Logger.cpp.

References g_gameThread, g_handler, and VoltMod::ReadFile().

Referenced by VoltMod::Host::Start().

◆ SetMinimumLevel()

void VoltMod::Log::SetMinimumLevel ( LogLevel  level)

Drop lines below level.

The host decides what each plugin prints, and the SDK reads that back once a frame. Set here rather than checked per line so the formatting helpers can skip the whole std::format call.

Definition at line 37 of file Logger.cpp.

References g_minimumLevel, and VoltMod::ReadFile().

◆ Wanted()

bool VoltMod::Log::Wanted ( LogLevel  level)
inline

Whether a line at level would be printed at all.

Definition at line 56 of file Log.hpp.

References Enabled(), MinimumLevel(), and VoltMod::ReadFile().

Referenced by Error(), Info(), and Warn().

◆ Warn()

Variable Documentation

◆ g_deferred

std::deque<std::pair<LogLevel, std::string> > VoltMod::Log::g_deferred
static

Definition at line 17 of file Logger.cpp.

Referenced by DeliverPending(), and Emit().

◆ g_deferredMutex

std::mutex VoltMod::Log::g_deferredMutex
static

Definition at line 16 of file Logger.cpp.

Referenced by DeliverPending(), and Emit().

◆ g_gameThread

std::thread::id VoltMod::Log::g_gameThread {}
static

Definition at line 12 of file Logger.cpp.

Referenced by Emit(), and SetHandler().

◆ g_handler

Handler VoltMod::Log::g_handler
static

Definition at line 11 of file Logger.cpp.

Referenced by DeliverPending(), Emit(), Enabled(), and SetHandler().

◆ g_hasDeferred

std::atomic<bool> VoltMod::Log::g_hasDeferred {false}
static

Read by DeliverPending every game frame, so the common empty case never takes the lock or builds a container. Only ever set true under g_deferredMutex.

Definition at line 20 of file Logger.cpp.

Referenced by DeliverPending(), and Emit().

◆ g_minimumLevel

std::atomic<LogLevel> VoltMod::Log::g_minimumLevel {LogLevel::Info}
static

What the host wants from this module. Read on every log call, written once a frame.

Definition at line 14 of file Logger.cpp.

Referenced by MinimumLevel(), and SetMinimumLevel().

◆ MaxDeferred

constexpr size_t VoltMod::Log::MaxDeferred = 256
staticconstexpr

A worker that logs in a tight failure loop must not grow this without bound; past the cap the oldest lines go, because the first error is rarely the interesting one.

Definition at line 24 of file Logger.cpp.

Referenced by Emit().