PDA

View Full Version : How to define string of QLoggingCategory during runtime?



antimatter
31st May 2016, 14:03
Hello,

I've read the docs, searched the forum and the web, but I am still confused or it is not possible.

I've been working on a little private application, that has multiple worker threads. These workers process something on a specific physical drive, e.g. Worker A on drive "E:" and Worker B on drive "F:". Only a single worker can work with a specific drive. During processing, these workers produce some logging output which should preferrably not go into a single log file. I'd like to have a separate log file, so that everything related to drive "E:" is logged in its own log file.

Here, QLoggingCategory seems to be the right way. I could use the string parameter of Q_LOGGING_CATEGORY as the drive name. But as it seems, by doing


Q_DECLARE_LOGGING_CATEGORY(foo)
Q_LOGGING_CATEGORY(foo, "bar")

"bar" cannot be defined during runtime. Only after startup the application knows for sure, what drives are available on the machine. Especially, when there are many removable drives in use. As it is now, the syntax forces me to define the string before I compile the source.

Is there a way, how I can use the logging system of Qt (qInfo, qDebug, etc.) with category strings set during runtime? What kind of alternative can I use, if it is not feasable?
I can think of embedding special tags in the log message itself and react on them in the message handler, but that already feels like an evil hack while typing it. ;-)

Andreas

anda_skoa
31st May 2016, 14:23
Q_LOGGIN_CATEGORY is just a macro.

If you look at how it is defined, you'll see that it simply passes the second argument to the QLoggingCategory constructor.

The function around is just a technique to safely create a globally accessible object.

Cheers,
_