plog icon indicating copy to clipboard operation
plog copied to clipboard

Multiple loggers with dynamic instance Ids

Open bogoss2012 opened this issue 4 years ago • 11 comments

Hi,

I am trying to init a logger with a dynamic instance Id but it looks like it does not work.

int instanceId = 1;
plog::Logger<instanceId>& test = plog::init<loggerId>(plog::verbose, &consoleAppender);

Seems that instanceId must be a constant.

Is there a workaround to have a dynamic number of loggers?

Many thanks :)

bogoss2012 avatar Jan 31 '21 20:01 bogoss2012

Hi!

No, by design they are compile-time constants. What is your use-case so you need a dynamic instance ids?

SergiusTheBest avatar Jan 31 '21 21:01 SergiusTheBest

Thank you very much for your answer.

I want to run a certain number of different scenarios (for which I don't know at compiler time how many they will be). For each scenario, I want to log what happens in its own corresponding file.

Like Scenario 1 -> log in file1.txt Scenario 2 -> log in file2.txt etc

bogoss2012 avatar Jan 31 '21 23:01 bogoss2012

If you run them sequentially then you can change the output file name on the beginning of each scenario.

SergiusTheBest avatar Feb 01 '21 09:02 SergiusTheBest

Unfortunately I want to run them in parallel. Thinking of changing the output file name each time I want to log, and adding a mutex on top of that to be sure to write in the right file.

I am not a fan of this approach and it might affect the performances if I log too much. But I may give it a try.

bogoss2012 avatar Feb 01 '21 10:02 bogoss2012

Probably there can be a mix of static and dynamic instance ids. I'll think about possible ways to implement it.

SergiusTheBest avatar Feb 03 '21 14:02 SergiusTheBest

Nice! I tried my idea but it was a bad one. It started creating appenders each time I wanted to log.

bogoss2012 avatar Feb 03 '21 20:02 bogoss2012

@bogoss2012 Dynamic logger instances is ready. Take a look at the sample.

SergiusTheBest avatar Feb 07 '21 22:02 SergiusTheBest

Thanks a lot!

Sample works fine. I am having issues to use dynamic instances branch in my own project... trying to figure out how to fix them.

bogoss2012 avatar Feb 09 '21 19:02 bogoss2012

I have two questions:

  • Do you need a LoggerContext class in your sample?
  • Why do you not use the init function to init the logger?

Many thanks

bogoss2012 avatar Feb 10 '21 21:02 bogoss2012

LoggerContext holds all objects required by the logger. At least you need one appender and one logger. It's up to you how to store them. LoggerContext is just one of the possible solutions.

Init function initializes a logger with the static instance id. So it can't be used.

SergiusTheBest avatar Feb 11 '21 08:02 SergiusTheBest

Ok that's clear. So as long as the instance I pass has a get() function that ends up on my logger, it works.

I solved my issue, which was totally on my side, the object manipulating the logger was badly managed.

It works great! I am gonna use that branch now untill it is merged and released. Thanks a lot!

bogoss2012 avatar Feb 11 '21 21:02 bogoss2012