Skip to content

Commit

Permalink
Add: log output
Browse files Browse the repository at this point in the history
Signed-off-by: datyuesh <[email protected]>
  • Loading branch information
datyuesh committed Apr 14, 2021
1 parent b6b37b6 commit 0381c02
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/plugins/ukui-clock/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,68 @@
#include <X11/Xlib.h>
#include "xatom-helper.h"

/*!
* \brief myMessageOutput
* 日志打印输出
*/
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
// 加锁
static QMutex mutex;
mutex.lock();

QByteArray localMsg = msg.toLocal8Bit();

QString strMsg("");
switch (type) {
case QtDebugMsg:
strMsg = QString("Debug ");
break;
case QtWarningMsg:
strMsg = QString("Warning ");
break;
case QtCriticalMsg:
strMsg = QString("Critical ");
break;
case QtFatalMsg:
strMsg = QString("Fatal ");
break;
case QtInfoMsg:
strMsg = QString("Info ");
break;
}

// 设置输出信息格式
QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
QString strMessage = QString("[DateTime]: %1 [Message]: %2 [Line]: %3 [Function]: %4")
.arg(strDateTime).arg(localMsg.constData()).arg(context.line).arg(
context.function);

QString dirStr = QStandardPaths::writableLocation(QStandardPaths::HomeLocation)
+ "/.config/kylin-clock/";
QDir dir;
if (!dir.exists(dirStr)) {
dir.mkpath(dirStr);
}
// 输出信息至文件中(读写、追加形式)
QString url_filepath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation)
+ "/.config/kylin-clock/output.log";
QFile file(url_filepath);

file.open(QIODevice::ReadWrite | QIODevice::Append);
QTextStream stream(&file);
stream << strMsg << strMessage << "\r\n";
file.flush();
file.close();

// 解锁
mutex.unlock();
}

int main(int argc, char *argv[])
{
// 自定义消息处理
qInstallMessageHandler(myMessageOutput);

QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
Expand Down

0 comments on commit 0381c02

Please sign in to comment.