emilk / loguru

A lightweight C++ logging library
The Unlicense
1.79k stars 260 forks source link

Stream function for skipping stack lines? #96

Closed Qix- closed 5 years ago

Qix- commented 5 years ago

It'd be great if I could do something like the following:

LOG_S(INFO) << loguru::skip_stack(1) << "some text";

The use-case here is that I often write perror()-like functions that aren't useful where LOG_S is being used but are instead important where the wrapper was called.

emilk commented 5 years ago

You can accomplish this by making you own macro:

void my_perror(const char* file, unsigned line)
{
    ...
    loguru::log(loguru::Verbosity_INFO, file, line, "some text");
}

#define MY_PERROR my_perror(__FILE__, __LINE__)
Qix- commented 5 years ago

Sometimes a macro isn't possible, though. Being able to use a stream modifier would be really convenient.

emilk commented 5 years ago

I agree it would be nice. The problem is that would require getting the file/line from the stacktrace which is slow, unreliable and not always available.

Qix- commented 5 years ago

Not getting the file, but instead just knocking one frame off the call to backtrack (since it gives you a list of void pointers).

emilk commented 5 years ago

loguru::stacktrace takes a skip argument that does just this.

Qix- commented 5 years ago

Ah I see, you're not getting the backtrace for each logging invocation. Not sure why I had that in my head.

Thanks :)