-
Notifications
You must be signed in to change notification settings - Fork 3
Examples TypicalUsage
This directory shows the three common ways to use logme in an application. The examples are intentionally separate because these scenarios should not be mixed in one startup path.
OutOfBox.cpp uses logme without explicit configuration. It writes to the default channel and is suitable for small tools, quick diagnostics, and first integration tests.
LogmeI("application started with default logme configuration");
LogmeW("request %d took longer than expected", requestId);Run:
./TypicalUsageOutOfBoxCodeConfiguration.cpp configures logging from C++ code. It creates an application channel, attaches console and file backends, sets the filter level, and then logs through that channel.
Use this style when the logging topology is part of the application logic or when the application does not use JSON configuration.
Run:
./TypicalUsageCodeConfigurationConfigFile.cpp loads logging setup from typical-usage.json or from a path passed on the command line. It does not create channels or backends in code after loading the file.
Use this style for production applications where channels, backends, levels, and runtime behavior should be adjustable without recompiling the application.
Run:
./TypicalUsageConfigFile typical-usage.jsonLoading a JSON configuration file requires logme to be built with JsonCpp support.
cmake -S . -B build -DLOGME_BUILD_EXAMPLES=ON
cmake --build buildThe binaries are placed in the examples output directory used by the main build.
2026-05-14 11:47:01:830 main(): application started with default logme configuration
2026-05-14 11:47:01:830 ProcessRequest(): request 1001 accepted
2026-05-14 11:47:01:830 W ProcessRequest(): request 1001 took longer than expected
2026-05-14 11:47:01:830 E Error: main(): example error record
Repository folder: examples/TypicalUsage
Files:
#include <memory>
#include <Logme/Backend/ConsoleBackend.h>
#include <Logme/Backend/FileBackend.h>
#include <Logme/Logme.h>
static Logme::ChannelPtr CreateApplicationChannel()
{
Logme::ID id{"app"};
auto ch = Logme::Instance->CreateChannel(id, Logme::OutputFlags(), Logme::LEVEL_DEBUG);
auto console = std::make_shared<Logme::ConsoleBackend>(ch);
console->SetAsync(true);
ch->AddBackend(console);
auto file = std::make_shared<Logme::FileBackend>(ch);
file->SetAppend(true);
file->SetMaxSize(1024ULL * 1024);
if (file->CreateLog("typical-usage.log") == false)
{
LogmeE("cannot create log file 'typical-usage.log'");
exit(1);
}
ch->AddBackend(file);
return ch;
}
static void ProcessRequest(
const Logme::ChannelPtr& ch
, int requestId
)
{
Logme::SID network = Logme::SID::Build("network");
Logme::SID storage = Logme::SID::Build("storage");
LogmeI(ch, network, "request %d accepted", requestId);
LogmeD(ch, storage, "request %d loaded cached profile", requestId);
LogmeW(ch, network, "request %d took longer than expected", requestId);
}
int main()
{
auto app = CreateApplicationChannel();
LogmeI(app, "application started with code configuration");
ProcessRequest(app, 1001);
LogmeE(app, "example error record");
return 0;
}#include <string>
#include <Logme/Logme.h>
static bool LoadLogmeConfiguration(const std::string& path)
{
std::string error;
if (Logme::Instance->LoadConfigurationFile(path, std::string(), &error))
{
return true;
}
LogmeE("cannot load logme configuration '%s': %s", path.c_str(), error.c_str());
return false;
}
static void ProcessRequest(int requestId)
{
LogmeI("request %d accepted", requestId);
LogmeW("request %d took longer than expected", requestId);
}
int main(int argc, char* argv[])
{
const char* configPath = argc > 1
? argv[1]
: "typical-usage.json";
if (!LoadLogmeConfiguration(configPath))
{
return 1;
}
LogmeI("application started with configuration file '%s'", configPath);
ProcessRequest(1001);
LogmeE("example error record");
return 0;
}#include <Logme/Logme.h>
static void ProcessRequest(int requestId)
{
LogmeI("request %d accepted", requestId);
LogmeW("request %d took longer than expected", requestId);
}
int main()
{
LogmeI("application started with default logme configuration");
ProcessRequest(1001);
LogmeE("example error record");
return 0;
}Previous: ThreadContext
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