The following page provides a nice simple solution for file based logging in Qt for debugging without using a larger logging framework like the many that are suggested in other SO questions.
I’m writing a library and would like to instantiate a logger that the classes in the library can use (mostly for debugging purposes). There is no int main() function since it’s a library. So would the best approach be to add the instantiation into a file like logger.h and have any classes include logger.h if it would like to do qDebug() << PREFIX << "Bla" as the link above suggests?
I pretty much agree with OrcunC but I’d recommend making that
ofstreama little more accessible and capable of handling the Qt value types.Here’s my recommended process:
For example:
And once the global logger is initialized, you could reference it as a global:
But you might want some filtering:
And then you’d likely create an enum that represented the LogLevel and do something like this:
As you’d be dealing with globals, carefully consider memory management issues.