how to capture log data in QTextEdit

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

how to capture log data in QTextEdit

Stefan Seefeld-3

Hello,

I'm trying to capture ("redirect") log data generated by a wrapped C++ library to a QTextEdit. The internet is full of related questions, but I couldn't quite find a working example that matches my needs. I can configure my logger to either write to the console (stdout) or to a file, so what I'm looking for is a mechanism to attach a "listener" to the given file descriptor and let it emit a `readyRead` signal whenever data are available. For a reason I don't entirely understand, the `QFile` class does *not* emit that signal, and thus is unfortunately unsuitable for this.

So, does PyQt5 offer any classes that are equipped to listen to data being written to a file descriptor such that I could read them asynchronously ? The only examples I could find involve (tcp or udp) sockets or serial ports, but not simple files...

Many thanks,

Stefan
-- 

      ...ich hab' noch einen Koffer in Berlin...
Reply | Threaded
Open this post in threaded view
|

Re: how to capture log data in QTextEdit

Florian Bruhin
Hey Stefan,

On Sun, Feb 21, 2021 at 11:41:58AM -0500, Stefan Seefeld wrote:
> I'm trying to capture ("redirect") log data generated by a wrapped C++
> library to a QTextEdit. The internet is full of related questions, but I
> couldn't quite find a working example that matches my needs. I can configure
> my logger to either write to the console (stdout) or to a file, so what I'm
> looking for is a mechanism to attach a "listener" to the given file
> descriptor and let it emit a `readyRead` signal whenever data are available.
> For a reason I don't entirely understand, the `QFile` class does *not* emit
> that signal, and thus is unfortunately unsuitable for this.

I'm not sure why either, but FWIW that behavior is documented as well:
https://doc.qt.io/qt-5/qfile.html#signals

> So, does PyQt5 offer any classes that are equipped to listen to data being
> written to a file descriptor such that I could read them asynchronously ?
> The only examples I could find involve (tcp or udp) sockets or serial ports,
> but not simple files...

Despite the name, QSocketNotifier might work:
https://doc.qt.io/qt-5/qsocketnotifier.html
("Although the class is called QSocketNotifier, it is normally used for
other types of devices than sockets")

Here's an example using it on a Unix FIFO:
https://github.com/qutebrowser/qutebrowser/blob/v2.0.2/qutebrowser/commands/userscripts.py#L38-L92

I'm not sure if I've ever tried it on an ordinary file, though.


Alternatively, maybe a QFileSystemWatcher would work:
https://doc.qt.io/qt-5/qfilesystemwatcher.html

Here's an example:
https://github.com/qutebrowser/qutebrowser/blob/v2.0.2/qutebrowser/misc/editor.py#L62
https://github.com/qutebrowser/qutebrowser/blob/v2.0.2/qutebrowser/misc/editor.py#L194-L201

Florian

--
            [hidden email] | https://www.qutebrowser.org 
       https://bruhin.software/ | https://github.com/sponsors/The-Compiler/
       GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
             I love long mails! | https://email.is-not-s.ms/

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: how to capture log data in QTextEdit

Stefan Seefeld-3


On 2021-02-22 11:13 a.m., Florian Bruhin wrote:
Hey Stefan,

On Sun, Feb 21, 2021 at 11:41:58AM -0500, Stefan Seefeld wrote:
I'm trying to capture ("redirect") log data generated by a wrapped C++
library to a QTextEdit. The internet is full of related questions, but I
couldn't quite find a working example that matches my needs. I can configure
my logger to either write to the console (stdout) or to a file, so what I'm
looking for is a mechanism to attach a "listener" to the given file
descriptor and let it emit a `readyRead` signal whenever data are available.
For a reason I don't entirely understand, the `QFile` class does *not* emit
that signal, and thus is unfortunately unsuitable for this.
I'm not sure why either, but FWIW that behavior is documented as well:
https://doc.qt.io/qt-5/qfile.html#signals

Indeed, I found that as well.


      
So, does PyQt5 offer any classes that are equipped to listen to data being
written to a file descriptor such that I could read them asynchronously ?
The only examples I could find involve (tcp or udp) sockets or serial ports,
but not simple files...
Despite the name, QSocketNotifier might work:
https://doc.qt.io/qt-5/qsocketnotifier.html
("Although the class is called QSocketNotifier, it is normally used for
other types of devices than sockets")

Thanks ! Yes, I found that solution as well, and was able to apply that to my case to make things work.

It would be nice to rename (or at least provide an alias to) the QSocketNotifier, to clarify its usefulness with arbitrary file descriptors.

Thank you for following up !

Best,


Stefan
-- 

      ...ich hab' noch einen Koffer in Berlin...
Reply | Threaded
Open this post in threaded view
|

Re: how to capture log data in QTextEdit

Giuseppe Corbelli
On 2/22/21 5:18 PM, Stefan Seefeld wrote:

>>> I'm trying to capture ("redirect") log data generated by a wrapped C++
>>> library to a QTextEdit. The internet is full of related questions, but I
>>> couldn't quite find a working example that matches my needs. I can configure
>>> my logger to either write to the console (stdout) or to a file, so what I'm
>>> looking for is a mechanism to attach a "listener" to the given file
>>> descriptor and let it emit a `readyRead` signal whenever data are available.
>>> For a reason I don't entirely understand, the `QFile` class does *not* emit
>>> that signal, and thus is unfortunately unsuitable for this.
>> I'm not sure why either, but FWIW that behavior is documented as well:
>> https://doc.qt.io/qt-5/qfile.html#signals
>
> Indeed, I found that as well.
>
>>> So, does PyQt5 offer any classes that are equipped to listen to data being
>>> written to a file descriptor such that I could read them asynchronously ?
>>> The only examples I could find involve (tcp or udp) sockets or serial ports,
>>> but not simple files...
>> Despite the name, QSocketNotifier might work:
>> https://doc.qt.io/qt-5/qsocketnotifier.html
>> ("Although the class is called QSocketNotifier, it is normally used for
>> other types of devices than sockets")
>
> Thanks ! Yes, I found that solution as well, and was able to apply that
> to my case to make things work.

I don't think that it will work on windows. Maybe not a concern...

--
Giuseppe Corbelli