Skip to content

Examples OnceEvery

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

OnceEvery example

OnceEvery

This example shows once-only and periodic logging helpers.

What it demonstrates

  • LogmeI_Once(...)
  • LogmeI_Every(...)
  • The same helpers in std::format style when available

Notes

  • ..._Once prints only the first matching call.
  • ..._Every limits output by time interval.

Console output

2026-05-03 10:33:57:847   OnceGlobal(): LogmeI_Once() [first call]: i=0
2026-05-03 10:33:57:847   OnceScope(): LogmeI(LOGME_ONCE4CALL) [first call]: i=0
2026-05-03 10:33:57:847   OnceScope(): LogmeI(LOGME_ONCE4CALL) [second call]: i=0
2026-05-03 10:33:57:847   EveryGlobal(): LogmeI_Every(300ms) [first]: i=0
2026-05-03 10:33:58:171   EveryGlobal(): LogmeI_Every(300ms) [first]: i=3
2026-05-03 10:33:58:498   EveryGlobal(): LogmeI_Every(300ms) [first]: i=6
2026-05-03 10:33:58:823   EveryGlobal(): LogmeI_Every(300ms) [first]: i=9
2026-05-03 10:33:59:151   EveryGlobal(): LogmeI_Every(300ms) [second]: i=12
2026-05-03 10:33:59:478   EveryGlobal(): LogmeI_Every(300ms) [second]: i=15
2026-05-03 10:33:59:803   EveryGlobal(): LogmeI_Every(300ms) [second]: i=18

Source code

Sources

Repository folder: examples/OnceEvery

OnceEvery.cpp

Open in repository

#include <Logme/Logme.h>
#include <chrono>
#include <thread>

static void OnceGlobal(bool first)
{
  for (int i = 0; i < 5; i++)
    LogmeI_Once("LogmeI_Once() [%s call]: i=%d", first ? "first" : "second", i);
}

static void OnceScope(bool first)
{
  LOGME_CALL_SCOPE;

  for (int i = 0; i < 5; i++)
    LogmeI(LOGME_ONCE4CALL, "LogmeI(LOGME_ONCE4CALL) [%s call]: i=%d", first ? "first" : "second", i);
}

static void EveryGlobal(bool first, int i, int j)
{
  for (; i < j; i++)
  {
    LogmeI_Every(300, "LogmeI_Every(300ms) [%s]: i=%d", first ? "first" : "second", i);
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
  }
}

int main()
{
  OnceGlobal(true);
  OnceGlobal(false);

  OnceScope(true);
  OnceScope(false);

  EveryGlobal(true, 0, 10);
  EveryGlobal(false, 10, 20);

  return 0;
}

Previous: Multithread | Next: OneTime

Clone this wiki locally