-
Notifications
You must be signed in to change notification settings - Fork 3
C API
logme provides a native C API for pure C translation units and mixed C/C++ projects. The public entry point is the same as for C++:
#include <Logme/Logme.h>When this header is included from C code, logme exposes the C API and C logging macros. The C layer is intentionally smaller than the C++ API, but it supports the normal production path: channels, basic backends, levels, printf-style messages, subsystem tags, per-message overrides, configuration loading, and shutdown.
#include <Logme/Logme.h>
int main(void)
{
LogmeCreateChannel(
"app"
, LEVEL_DEBUG
);
LogmeAddConsoleBackend("app", 0);
LogmeI("hello from C");
LogmeW_Ch("app", "warning from channel");
LogmeE_Sid("CORE", "error from subsystem");
LogmeShutdown();
return 0;
}The repository also contains a dedicated examples/CApi example. Example wiki pages are generated from the corresponding examples/<name>/README.md files by tools/sync_examples.py.
The C API uses the same level enum values as the C++ API:
LEVEL_DEBUG
LEVEL_INFO
LEVEL_WARN
LEVEL_ERROR
LEVEL_CRITICALThe C macros use printf-style formatting:
LogmeD("debug value: %d", value);
LogmeI("information message");
LogmeW("warning message");
LogmeE("error message");
LogmeC("critical message");Unlike the C++ macros, the C macros do not provide stream-style output. They always write a formatted message.
C does not have the same overload mechanism as C++, so channel and subsystem variants use explicit macro names:
LogmeI_Ch("network", "message for channel");
LogmeI_Sid("HTTP", "message for subsystem");
LogmeI_ChSid("network", "HTTP", "message for channel and subsystem");Conditional variants are also available:
LogmeI_If(condition, "message only when condition is true");int LogmeCreateChannel(
const char* channel
, LogmeLevel level
);
void LogmeDeleteChannel(const char* channel);void LogmeSetChannelLevel(
const char* channel
, LogmeLevel level
);
void LogmeSetChannelEnabled(
const char* channel
, int enabled
);void LogmeSetChannelFlags(
const char* channel
, LogmeOutputFlags flags
);LogmeOutputFlags is the C-visible form of the same OutputFlags bit-field set used by the C++ API.
int LogmeAddConsoleBackend(
const char* channel
, int async
);int LogmeAddDebugBackend(const char* channel);int LogmeAddFileBackend(
const char* channel
, const char* fileName
, int append
, size_t maxSize
, int dailyRotation
, int maxParts
);void LogmeRemoveChannelBackends(const char* channel);
void LogmeFlushChannel(const char* channel);The macros are the normal way to write log messages from C, but the lower-level functions are also exposed:
void LogmeWrite(
LogmeLevel level
, const char* channel
, const char* subsystem
, const char* function
, const char* file
, int line
, const char* format
, ...
);
void LogmeWriteV(
LogmeLevel level
, const char* channel
, const char* subsystem
, const char* function
, const char* file
, int line
, const char* format
, va_list args
);The C API exposes a C-compatible override structure:
typedef struct LogmeCOverride
{
LogmeOutputFlags Add;
LogmeOutputFlags Remove;
int MaxRepetitions;
int Repetitions;
uint64_t MaxFrequency;
uint64_t LastTime;
const LogmeCShortenerPair* Shortener;
} LogmeCOverride;Initialize it before use:
LogmeCOverride once;
LogmeInitOverride(
&once
, 1
, 0
);
LogmeI_Ovr(&once, "this message is printed once");
LogmeI_Ovr(&once, "this message is suppressed");For rate-limited logging, keep the same LogmeCOverride object alive between calls because the repetition and timing state is stored inside it:
static LogmeCOverride everySecond;
static int initialized = 0;
if (!initialized)
{
LogmeInitOverride(
&everySecond
, -1
, 1000
);
initialized = 1;
}
LogmeI_Ovr(&everySecond, "printed at most once per second");Override macro variants follow the same naming style:
LogmeI_Ovr(&overrideData, "message");
LogmeI_ChOvr("app", &overrideData, "message");
LogmeI_SidOvr("CORE", &overrideData, "message");
LogmeI_ChSidOvr("app", "CORE", &overrideData, "message");C code can load the same configuration files as C++ code:
char error[512];
if (!LogmeLoadConfigurationFile(
"logme.json"
, NULL
, error
, sizeof(error)
))
{
LogmeE("configuration error: %s", error);
}The raw configuration text variant is also available:
int LogmeLoadConfiguration(
const char* configData
, const char* section
, char* errorBuffer
, size_t errorBufferSize
);Call LogmeShutdown() when the application wants to flush and stop logging explicitly:
LogmeShutdown();The C API intentionally does not expose the full C++ surface. The following features remain C++-only:
- stream-style logging with
operator << -
fLogme...macros based onstd::format - C++ overload-based macro dispatch
- C++ RAII helpers
- direct construction of C++ backend objects
Use the explicit C macro families (_Ch, _Sid, _Ovr) and the C wrapper functions instead.
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