lundi 1 mars 2021

How better organize logger for a class - static pointer field, global variable, singleton field, etc?

I have some class and one field is a static pointer - let's say it's a logger, so I want it to be initialized and be same for all instances pf this class.

class A
{
private:
static Logger* logger;
};

And this logger will be initialized from the dll, so it's initialization is a function, I can't just write new or make_unique or whatever. My question is how better to organize this and where to initialize this logger? Should I add Logger * initLogger() as a static method to the class and call it in the constructor like

A::A() {
    if (!logger)
    logger = initLogger();
}

Or maybe I better make this logger static variable in the cpp file, but then again I have the question when to call it's initialization function. Or write new class for a logger that will be singleton? I think it's pretty standard situation so there should be some best approaches?

Aucun commentaire:

Enregistrer un commentaire