Skip to content

Override Scopes

Eduard Mishkurov edited this page Apr 7, 2026 · 1 revision

Override Scopes

logme supports several override scopes for one-time and rate-limited logging.

Global scope (per call-site)

Macros such as LogmeI_Once(...), LogmeI_Every(ms, ...), fLogmeI_Once(...), and fLogmeI_Every(ms, ...) use an internal static override.

This means the override is unique for that specific call-site and remains active for the lifetime of the process.

for (int i = 0; i < 10; i++)
{
  LogmeI_Once("Only once globally");
  LogmeI_Every(1000, "At most once per second globally");
}

Object scope (per instance)

Inside a class derived from Logme::OverrideGenerator, use:

  • LOGME_ONCE4THIS
  • LOGME_EVERY4THIS(ms)
class Worker : public Logme::OverrideGenerator
{
public:
  void Process()
  {
    LogmeI(LOGME_ONCE4THIS, "Printed once per Worker instance");
    LogmeI(LOGME_EVERY4THIS(1000), "At most once per second per Worker instance");
  }
};

The key is based on __func__ and __LINE__, so each macro call-site inside the object has its own override state.

Call scope (per function invocation)

To keep override state local to the current function invocation, declare LOGME_CALL_SCOPE in that scope and then use:

  • LOGME_ONCE4CALL
  • LOGME_EVERY4CALL(ms)
void ProcessBatch()
{
  LOGME_CALL_SCOPE;

  for (int i = 0; i < 10; i++)
  {
    LogmeI(LOGME_ONCE4CALL, "Printed once for this ProcessBatch() call");
    LogmeI(LOGME_EVERY4CALL(500), "At most once per 500 ms for this ProcessBatch() call");
  }
}

If ProcessBatch() is called again, a new call-scope generator is created and the same messages may be printed again according to their rules.

Summary

Scope One-time Rate-limited Lifetime
Global LogmeX_Once(...) / fLogmeX_Once(...) LogmeX_Every(ms, ...) / fLogmeX_Every(ms, ...) Entire process, per call-site
Object LOGME_ONCE4THIS LOGME_EVERY4THIS(ms) Per object instance
Call LOGME_ONCE4CALL LOGME_EVERY4CALL(ms) Per function invocation / local scope

Notes

  • LOGME_ONCE4THIS and LOGME_EVERY4THIS(ms) require inheritance from Logme::OverrideGenerator.
  • LOGME_ONCE4CALL and LOGME_EVERY4CALL(ms) require LOGME_CALL_SCOPE to be declared in the current scope.
  • The object-scope and call-scope generators also use per-call-site keys based on __func__ and __LINE__.

Clone this wiki locally