gpt4 book ai didi

如果通过 cronjob 执行应用程序,Qt 输出(qDebug qWarning 等)不起作用

转载 作者:行者123 更新时间:2023-12-04 10:17:10 28 4
gpt4 key购买 nike

我为此创建了一个复制样本:

#include <iostream>
#include <QtCore/QLoggingCategory>
#include <QtCore/QDebug>
#include <QtCore/QtCore>
using namespace std;
int main () {
int i;
QLoggingCategory::setFilterRules("*.debug=true\n");
QLoggingCategory LogO(NULL);
if (LogO.isDebugEnabled()) {
cout << "QDebug enabled\n";
} else {
cout << "QDebug disabled!\n";
}
cout << "Start!\n";
qDebug() << "qStart!";
cerr << "print to stderr.\n";
qWarning() << "qWarning";
return 0;
}

构建步骤:

g++ -c -fPIC -I/usr/include/qt5 main.cpp -o main.o
g++ -fPIC main.o -L /usr/lib64 -lQt5Core -o testapp

在交互式 shell 中执行应用程序时,输出重定向按预期工作:

设置:

./testapp > out 2> err

输出:

>>cat out:
QDebug enabled
Start!

>>cat err:
qStart!
print to stderr.
qWarning

但是,如果应用程序作为 cronjob 执行,则它不起作用,缺少 qDebug() 和 qWarning() 的输出:

设置:

* * * * * username /home/username/temp/build/testapp 1> /home/username/temp/log/out 2> /home/username/temp/log/err

输出:

>>cat /home/username/temp/log/out
QDebug enabled
Start!

>>cat home/username/temp/log/err
print to stderr.

环境变量

env 在交互式 shell 中的输出如下:

LS_COLORS=*long string*
SSH_CONNECTION=*censored*
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
HOSTNAME=*censored*
XDG_SESSION_ID=492
USER=username
SELINUX_ROLE_REQUESTED=
PWD=/home/username/temp/build
HOME=/home/username
SSH_CLIENT=*censored*
SELINUX_LEVEL_REQUESTED=
SSH_TTY=/dev/pts/0
MAIL=/var/spool/mail/username
TERM=xterm
SHELL=/bin/bash
SELINUX_USE_CURRENT_RANGE=
SHLVL=1
LOGNAME=username
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
XDG_RUNTIME_DIR=/run/user/1000
PATH=/usr/lib64/ccache:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/username/.local/bin:/home/username/bin
HISTSIZE=1000
LESSOPEN=||/usr/bin/lesspipe.sh %s
_=/usr/bin/env
OLDPWD=/home/username/temp/build/logs

通过 cronjob 调用时 env 的输出如下:

LS_COLORS=*long string*
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
HOSTNAME=*censored*
XDG_SESSION_ID=995
USER=username
PWD=/home/username
HOME=/home/username
MAIL=/var/spool/mail/username
TERM=xterm
SHELL=/bin/bash
SHLVL=1
LOGNAME=username
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
XDG_RUNTIME_DIR=/run/user/1000
PATH=/usr/lib64/ccache:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/username/.local/bin:/home/username/bin
LESSOPEN=||/usr/bin/lesspipe.sh %s
_=/usr/bin/env

最佳答案

问题在于 qt 的行为有所不同,具体取决于它是否认为自己正在(交互​​式?)终端中运行。

Quote :

One pitfall to be aware of: the destination of logging depends on an environment variable. If the variable QT_LOGGING_TO_CONSOLE is set to 1, the message functions will always log to the console. If set to 0, they will not log to the console, and will instead log to syslog, if enabled. When the environment variable is not set, the message functions log to a console if one is present (i.e. if the program is attached to a terminal). Thus, to ensure that the output of our example program goes to syslog, I set the environment variable to 0 within the program.

因此,qDebug、QWarning等从cron执行时的输出不是通过stderr输出,而是直接交给了journald

TL;DR: quickfix:将 QT_LOGGING_TO_CONSOLE=1 添加到 /etc/crontab

.

.

PS:如果您需要使用 QDebug 调试问题,请注意:

  1. 请注意:https://bugzilla.redhat.com/show_bug.cgi?id=1227295
  2. 你可以添加QT_LOGGING_DEBUG=1作为环境变量来使qt 输出在执行过程中记录行为的变化。

关于如果通过 cronjob 执行应用程序,Qt 输出(qDebug qWarning 等)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52027483/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com