SergiusTheBest / plog

Portable, simple and extensible C++ logging library
MIT License
2.21k stars 391 forks source link

How to chain multiple shared lib's log, and identify log content using log header or instance id? #288

Open blazewater-gmail opened 5 months ago

blazewater-gmail commented 5 months ago

hi, I have multiple shared lib, and I want to chained their logs to main application; in main app, I use multiple instance id and custom formater(log instance id string as header's part) to identify multiple modules in log text, but how can I identify log from diffent shared lib? thx.

SergiusTheBest commented 5 months ago

Hello! It's a good question.

One of the solutions I think about is to introduce a macro, for example PLOG_PREFIX, and set it to the library name. It will be automatically prepended to a log message. For example:

#define PLOG_PREFIX "[lib:fat32]"

...
PLOGD << "Hello!";

And the resulting string will be:

2024-05-10 19:36:51.840 DEBUG [30020] [main@37] [lib:fat32] Hello!

Will this work for you?

blazewater-gmail commented 5 months ago

Hello! It's a good question.

One of the solutions I think about is to introduce a macro, for example PLOG_PREFIX, and set it to the library name. It will be automatically prepended to a log message. For example:

#define PLOG_PREFIX "[lib:fat32]"

...
PLOGD << "Hello!";

And the resulting string will be:

2024-05-10 19:36:51.840 DEBUG [30020] [main@37] [lib:fat32] Hello!

Will this work for you?

thanks for reply. actually, now I define Log Facility in main app, for example:

 enum Facility {
    F1 = 1,
   F2 = 2,
   F3 = 3
};

and init these Facility Instances;

plog::init<Facility::F#>()...

then I define Log Facility for lib1 in Lib1.cpp, for example:

 enum {
    F1 = 1
};
init Facility in lib1.cpp

```c++
plog::init();
plog::init<F1>()...

chained lib1's log and main app's log, I can get corresponding log as Lib1.

but this is so weired.

so, I think your solution is better, but as logging analysis, suggest that add default prefix for main app.

Thanks again.