PyQt5接口处理日志数据到文件,特别适合多线程程序。
dvg-pyqt-filelogger的Python项目详细描述
DvG_PyQt_文件记录器
PyQt5接口来处理日志数据到文件,特别适合于 多线程程序。
- Github:https://github.com/Dennis-van-Gils/python-dvg-pyqt-filelogger
- PyPI:https://pypi.org/project/dvg-pyqt-filelogger
安装:
pip install dvg-pyqt-filelogger
美国石油学会
class FileLogger(write_header_function: Callable = None, write_data_function: Callable = None)
Bases: ^{tt2}$
Handles logging data to a file particularly well suited for multithreaded programs where one thread is writing data to the log and the other thread (the main/GUI thread) requests starting and stopping of the log, e.g., by the user pressing a button.
The methods ^{tt3}$, ^{tt4}$ and ^{tt5}$ can be directly called from the main/GUI thread.
In the logging thread you repeatedly need to call ^{tt6}$. This method takes cares of the state machine behind ^{tt7}$ and will perform the appropiate action, such as creating a file on disk, creating the header or writing new data to the log.
- Args:
- 注意:
- write_header_function (^{tt8}$, optional):
Reference to a function that contains your specific code to write a header to the log file. This will get called during ^{tt6}$.
Default: ^{tt10}$
- write_data_function (^{tt8}$, optional):
Reference to a function that contains your specific code to write new data to the log file. This will get called during ^{tt6}$.
Default: ^{tt10}$
Both of the above functions can contain calls to the following class members:
- ^{tt14}$
- ^{tt15}$
- 这个类缺少互斥体,因此从一开始就不是线程安全的。 只要update()在另一个互斥体内被调用,比如 例如,作为数据采集互斥体,它是安全的。
- 注意:
- 根据设计,当发生异常时,此类中的代码将继续运行。 它们被报告到命令行。
信号:
- signal_recording_started (str):
新录制开始时发出。例如,对。, 更新记录按钮的文本。在
- 返回:
- 新创建的日志文件的文件路径为str。
- 类型:
- PyQt5.QtCore.pyqtSignal()
- signal_recording_stopped (pathlib.Path):
在录制停止时发出。有助于,例如,更新 记录按钮的文本。在
- 返回:
- 新创建的日志文件的文件路径为pathlib.Path()。 例如,您可以使用它自动导航到登录 或要求用户输入“保存到”目标。
- 类型:
- PyQt5.QtCore.pyqtSignal()
用法示例:
^{pr2}$
方法
set_write_header_function(write_header_function: Callable)
在set_write_data_function(write_data_function: Callable)
在- record(state: bool = True)
可以从任何线程调用。在
- start_recording()
可以从任何线程调用。在
- stop_recording()
可以从任何线程调用。在
- update(filepath: str = "", mode: str = "a")
必须重复调用此方法,大概是在 需要记录日志的线程,例如,数据生成线程。 此方法负责FileLogger和后面的状态机 将执行适当的操作,例如在磁盘上创建文件, 正在创建标头或将新数据写入日志。在
- 参数:
- 文件路径(str):
必须创建或打开的日志文件的位置 用于写入访问。在
默认值:"{yyMMdd_HHmmss}.txt",表示当前日期和时间。在
- 模式(str,可选): 打开日志文件的模式,请参见open() 更多细节。最常见的选择:
- ^{tt34}$: Open for writing, truncating the file first.
- ^{tt35}$: Open for writing, appending to the end of the file if it exists.
默认值:a
- write(data: AnyStr) -> bool 将二进制或ASCII数据写入当前打开的日志文件。通过 设计此方法中发生的任何异常都不会终止 执行,但它将向命令行报告错误并继续 改为打开。在
如果成功,则返回True,否则返回False。在
- flush()
一旦 可能。不要重复调用,因为这会导致开销。在
close()
在is_recording() -> bool
在- elapsed() -> float
返回自开始录制以来的时间(以秒为单位)(float)。在
- pretty_elapsed() -> str
以“h:mm:ss”(str)的形式返回录制开始后的时间。在
变更日志
- 关于PyPI的第一次发布
- 项目
标签: