-
Notifications
You must be signed in to change notification settings - Fork 2
Override Scopes
logme supports several override scopes for one-time and rate-limited logging.
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");
}Inside a class derived from Logme::OverrideGenerator, use:
LOGME_ONCE4THISLOGME_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.
To keep override state local to the current function invocation, declare LOGME_CALL_SCOPE in that scope and then use:
LOGME_ONCE4CALLLOGME_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.
| 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 |
-
LOGME_ONCE4THISandLOGME_EVERY4THIS(ms)require inheritance fromLogme::OverrideGenerator. -
LOGME_ONCE4CALLandLOGME_EVERY4CALL(ms)requireLOGME_CALL_SCOPEto be declared in the current scope. - The object-scope and call-scope generators also use per-call-site keys based on
__func__and__LINE__.
logme — flexible runtime logging system
Home · Getting Started · Architecture · Output · Backends · Configuration
GitHub: https://github.com/efmsoft/logme