-
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.
$ TypicalUsageOutOfBox
2026-06-03 16:33:15:398 main(): application started with default logme configuration
2026-06-03 16:33:15:398 ProcessRequest(): request 1001 accepted
2026-06-03 16:33:15:398 W ProcessRequest(): request 1001 took longer than expected
2026-06-03 16:33:15:398 E Error: main(): example error record
$ TypicalUsageCodeConfiguration
2026-06-03 16:33:15:508 main(): application started with code configuration
2026-06-03 16:33:15:508 ProcessRequest(): request 1001 accepted
2026-06-03 16:33:15:508 D ProcessRequest(): request 1001 loaded cached profile
2026-06-03 16:33:15:508 W ProcessRequest(): request 1001 took longer than expected
2026-06-03 16:33:15:508 E Error: main(): example error record
$ TypicalUsageConfigFile
2026-06-03 16:33:15:603 main(): application started with embedded configuration
2026-06-03 16:33:15:603 ProcessRequest(): request 1001 accepted
2026-06-03 16:33:15:603 W ProcessRequest(): request 1001 took longer than expected
2026-06-03 16:33:15:603 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 const char* TYPICAL_USAGE_CONFIGURATION = R"json(
{
"channels": [
{
"name": "",
"level": "debug",
"backends": [
{
"type": "ConsoleBackend",
"async": false
},
{
"type": "FileBackend",
"append": true,
"max-size": "1Mb",
"file": "typical-usage-config.log"
}
]
}
]
}
)json";
static bool LoadLogmeConfiguration()
{
std::string error;
if (Logme::Instance->LoadConfiguration(TYPICAL_USAGE_CONFIGURATION, std::string(), &error))
{
return true;
}
LogmeE("cannot load logme configuration: %s", 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()
{
if (!LoadLogmeConfiguration())
{
return 1;
}
LogmeI("application started with embedded configuration");
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: TracePoints | Next: WindowsEventLogBackend
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