-
Notifications
You must be signed in to change notification settings - Fork 3
Examples CallbackBackend
Eduard Mishkurov edited this page Jun 10, 2026
·
2 revisions
This example shows how to route log records to a user callback.
CallbackBackend is useful when an application wants to collect records in memory, forward them to an existing telemetry pipeline, or integrate logme with a custom diagnostics system.
The callback receives the original Logme::Context, the owning channel, and the user data pointer supplied when the backend is created.
The example formats the record with the owning channel flags and prints it from the callback.
callback[1]: 2026-06-10 14:51:39:870 main(): first callback record: 1
callback[2]: 2026-06-10 14:51:39:870 W main(): second callback record
records captured by callback: 2
Repository folder: examples/CallbackBackend
#include <Logme/Logme.h>
#include <Logme/Backend/CallbackBackend.h>
#include <Logme/Channel.h>
#include <cstdio>
#include <memory>
namespace
{
struct CallbackState
{
size_t Count;
};
void StoreRecord(
Logme::Context& context
, const Logme::ChannelPtr& owner
, void* userData
)
{
auto state = static_cast<CallbackState*>(userData);
int nc = 0;
const char* text = context.Apply(owner, owner->GetFlags(), nc);
++state->Count;
std::printf(
"callback[%zu]: %.*s\n"
, state->Count
, nc
, text
);
}
}
int main()
{
Logme::ID id{"callback"};
auto channel = Logme::Instance->CreateChannel(id);
CallbackState state{};
auto backend = std::make_shared<Logme::CallbackBackend>(
channel
, StoreRecord
, &state
);
channel->AddBackend(backend);
LogmeI(channel, "first callback record: %d", 1);
LogmeW(channel) << "second callback record";
std::printf("records captured by callback: %zu\n", state.Count);
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