Skip to content

Examples CApi

Eduard Mishkurov edited this page May 13, 2026 · 3 revisions

CApi example

CApi

This example shows how to use logme from a pure C translation unit.

What it demonstrates

  • Including the common public entry point Logme/Logme.h from C code
  • Creating a channel from C
  • Attaching console and file backends from C
  • Using printf-style C logging macros
  • Logging to the default channel, a named channel, and a subsystem
  • Using a C override object for per-call output changes and rate/repetition limits
  • Flushing and shutting down logme from C

Notes

  • The C API intentionally supports a smaller feature set than the C++ API.
  • C logging macros are printf-style only. The C++ stream-style syntax is not available in C.
  • Channel and subsystem routing use explicit C macro variants such as LogmeI_Ch, LogmeE_Sid, and LogmeW_ChSid.
  • LogmeCOverride is mutable because repetition and frequency limits keep state between calls. Keep it alive for as long as the call site needs that state.

Console output

2026-05-13 10:54:20:321 D main(): C debug message: 1
2026-05-13 10:54:20:321   main(): C info message: text
2026-05-13 10:54:20:321 W main(): C warning in a named channel
2026-05-13 10:54:20:321 E Error: main(): C error with subsystem
2026-05-13 10:54:20:321 C Critical: main(): C critical message
2026-05-13 10:54:20:321   main(): conditional C message
2026-05-13 10:54:20:321   this C override message is printed once

Source code

Sources

Repository folder: examples/CApi

CApi.c

Open in repository

#include <Logme/LogmeC.h>

int main(void)
{
  LogmeSetChannelLevel(NULL, LOGME_C_ENUM_VALUE(LEVEL_DEBUG));

  LogmeCreateChannel("c-channel", LOGME_C_ENUM_VALUE(LEVEL_DEBUG));
  LogmeRemoveChannelBackends("c-channel");
  LogmeAddConsoleBackend("c-channel", 0);
  LogmeAddFileBackend("c-channel", "c-api-example.log", 1, 1024 * 1024, 0, 3);

  LogmeD("C debug message: %d", 1);
  LogmeI("C info message: %s", "text");
  LogmeW_Ch("c-channel", "C warning in a named channel");
  LogmeE_Sid("CAPI", "C error with subsystem");
  LogmeC_ChSid("c-channel", "CAPI", "C critical message");

  LogmeI_If(1, "conditional C message");

  static LogmeCOverride onceOverride = LOGME_C_OVERRIDE_INIT;
  onceOverride.MaxRepetitions = 1;
  onceOverride.Remove.Value = 0;
  onceOverride.Remove.Method = 1;
  LogmeI_Ovr(&onceOverride, "this C override message is printed once");
  LogmeI_Ovr(&onceOverride, "this C override message is suppressed");

  LogmeFlushChannel("c-channel");
  LogmeShutdown();
  return 0;
}

Previous: Basic | Next: Channels

Clone this wiki locally