qInstallMessageHandler successful, but sub thread can't see it

684 views Asked by At

I have an app written with Qt5. There is a parent process which spins off an array of child threads - works fine. When I was prototyping it I installed my own message handler (with qInstallMessageHandler) to customize logging, but I put the call to install the message handler in each thread that was spun off (effectively the threads are little clones of each other, so every thread created had the call to install the message handler). Oddly enough that worked. Even though qInstallMessageHandler should only be called once for an application (only one can have the ring of power), calling it multiple times evidently worked because they were all for the same handler. All the threads sent their qDebug (qWarning, etc) messages to my handler and so did the parent.

Now that I'm finalizing the prototype, I wanted to clean things up so I moved the call for qInstallMessageHandler into the parent (which seems cleaner), but now only the parent uses the message handler but the child threads seem to be oblivious that it is installed. I can't see why this doesn't work. The QMessageHandler is application scope. The actual message handler code is outside of any class (as it's always been).

Can anyone offer any insight as to why the threads can't see my handler?

1

There are 1 answers

0
Wolf On

So here's the deal. My parent process is a daemon (QtService) which it turns out was trying to install a message handler of its own (because I defined debugging for it). When I called the message handler from within my thread the timing was such that I stole the handler from qservice, but when I moved it into the daemon proper, qservice stole the handler from me. In any case, I was just stepping on my own toes as usual. Whoever calls qInstallMessageHandler last wins.