-
Notifications
You must be signed in to change notification settings - Fork 3
Examples Precheck
This example shows how Logme can skip unnecessary work before building a log record.
- Early check for regular
LogmeI(...)calls when the first argument is aChannelPtr -
LogmeI_Do(...)for preparation code that should run only when the record will really be emitted - The same idea for
std::formatstyle logging throughfLogmeI_Do(...)when it is enabled - The practical difference between an inactive channel and an active visible channel
In a logging library, the expensive part is often not the final write. It is the work done before the logger decides whether the message should be emitted at all:
- evaluating function calls used as log arguments
- building temporary strings and other helper objects
- running custom preparation code just to feed the logging macro
When you pass a ChannelPtr as the first argument, LogmeI(...) can check the channel state before evaluating the remaining arguments.
When you need even more control, LogmeI_Do(...) lets you put the preparation code directly into the macro. That code runs only if the logger has already decided that the message will be written.
- Plain
LogmeI("...", ...)cannot do this early channel-based check because there is no explicitChannelPtrin the first argument. -
LogmeI_Do(...)can work with eitherChannelPtrorLogme::ID. - The example prints counters so the behavior is visible immediately when you run it.
2026-06-10 14:51:46:239 main(): Precheck example
2026-06-10 14:51:46:239 main(): ExpensiveText() was evaluated
2026-06-10 14:51:46:239 main(): ExpensiveText() calls after both messages: 1
2026-06-10 14:51:46:239 main(): PrepareValue() calls after inactive LogmeI_Do: 0
2026-06-10 14:51:46:239 main(): active prepared=42
2026-06-10 14:51:46:239 main(): PrepareValue() calls after active LogmeI_Do: 1
2026-06-10 14:51:46:239 main(): std::format prepared=42
Repository folder: examples/Precheck
#include <Logme/Logme.h>
static Logme::ChannelPtr EnsureVisibleChannel(const Logme::ID& id)
{
auto ch = Logme::Instance->CreateChannel(id);
ch->AddLink(::CH);
return ch;
}
static int ExpensiveTextCalls;
static int PrepareValueCalls;
static std::string ExpensiveText()
{
ExpensiveTextCalls++;
return "ExpensiveText() was evaluated";
}
static int PrepareValue()
{
PrepareValueCalls++;
return 42;
}
int main()
{
auto active = EnsureVisibleChannel(Logme::ID{"precheck_active_example"});
auto inactive = Logme::Instance->CreateChannel(Logme::ID{"precheck_inactive_example"});
inactive->SetEnabled(false);
LogmeI(active) << "Precheck example";
// Passing ChannelPtr as the first argument lets Logme check the channel state
// before evaluating the rest of the logging arguments.
LogmeI(inactive, "%s", ExpensiveText().c_str());
LogmeI(active, "%s", ExpensiveText().c_str());
LogmeI("ExpensiveText() calls after both messages: %d", ExpensiveTextCalls);
int prepared = 0;
// The *_Do macro is meant for cases where some preparation code would be
// wasteful unless the log record is really going to be emitted.
LogmeI_Do(inactive->GetID(), prepared = PrepareValue(), "inactive prepared=%d", prepared);
LogmeI("PrepareValue() calls after inactive LogmeI_Do: %d", PrepareValueCalls);
LogmeI_Do(active->GetID(), prepared = PrepareValue(), "active prepared=%d", prepared);
LogmeI("PrepareValue() calls after active LogmeI_Do: %d", PrepareValueCalls);
#ifndef LOGME_DISABLE_STD_FORMAT
prepared = 0;
fLogmeI_Do(active->GetID(), prepared = PrepareValue(), "std::format prepared={}", prepared);
#endif
return 0;
}logme — flexible runtime logging system
Home · Getting Started · Architecture · Output · Backends · Configuration
GitHub: https://github.com/efmsoft/logme
- Home
- Getting Started
- Why logme?
- Core Concepts
- Logging Macros
- Fatal Handling
- Crash Logging
- glog Compatibility
- C API
- Choosing Logging Macros
- Function tracing
- Trace Points
- Override Scopes
- Advanced Features
- Collapse Logging
- Feature Map
- Overview
- Console Backend
- Debugger Backend
- File Backend
- File Rotation & Retention
- Buffer Backend
- Ring Buffer Backend
- SharedFile Backend
- Callback Backend
- Windows Event Log Backend
- Custom Backends
- Runtime Control
- Configuration
- Configuration JSON
- Control Server
- Environment Control
- Control Policies
- Trace Points
- Message Filtering