2020-05-15 05:34:54 -07:00
|
|
|
#include <QFile>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <QScrollBar>
|
|
|
|
#include <QFont>
|
|
|
|
#include <QFontDatabase>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QCheckBox>
|
|
|
|
#include <QLayout>
|
2020-08-14 22:20:38 -07:00
|
|
|
#include <QDesktopServices>
|
2020-05-15 05:34:54 -07:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "log-viewer.hpp"
|
|
|
|
#include "qt-wrappers.hpp"
|
|
|
|
|
2022-02-27 01:42:18 -08:00
|
|
|
OBSLogViewer::OBSLogViewer(QWidget *parent)
|
|
|
|
: QDialog(parent), ui(new Ui::OBSLogViewer)
|
2020-05-15 05:34:54 -07:00
|
|
|
{
|
2020-08-24 21:30:21 -07:00
|
|
|
setWindowFlags(windowFlags() & Qt::WindowMaximizeButtonHint &
|
|
|
|
~Qt::WindowContextHelpButtonHint);
|
2021-05-09 18:17:32 -07:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2020-05-15 05:34:54 -07:00
|
|
|
|
2022-02-27 01:42:18 -08:00
|
|
|
ui->setupUi(this);
|
2020-05-15 05:34:54 -07:00
|
|
|
|
|
|
|
const QFont fixedFont =
|
|
|
|
QFontDatabase::systemFont(QFontDatabase::FixedFont);
|
|
|
|
|
2022-02-27 01:42:18 -08:00
|
|
|
ui->textArea->setFont(fixedFont);
|
2022-02-26 23:56:19 -08:00
|
|
|
// Fix display of tabs & multiple spaces
|
2022-02-27 01:42:18 -08:00
|
|
|
ui->textArea->document()->setDefaultStyleSheet(
|
2022-02-26 23:56:19 -08:00
|
|
|
"font { white-space: pre; }");
|
2020-05-15 05:34:54 -07:00
|
|
|
|
2020-09-15 20:29:40 -07:00
|
|
|
bool showLogViewerOnStartup = config_get_bool(
|
|
|
|
App()->GlobalConfig(), "LogViewer", "ShowLogStartup");
|
|
|
|
|
2022-02-27 01:42:18 -08:00
|
|
|
ui->showStartup->setChecked(showLogViewerOnStartup);
|
2020-05-15 05:34:54 -07:00
|
|
|
|
|
|
|
const char *geom = config_get_string(App()->GlobalConfig(), "LogViewer",
|
|
|
|
"geometry");
|
|
|
|
|
|
|
|
if (geom != nullptr) {
|
|
|
|
QByteArray ba = QByteArray::fromBase64(QByteArray(geom));
|
|
|
|
restoreGeometry(ba);
|
|
|
|
}
|
|
|
|
|
|
|
|
InitLog();
|
|
|
|
}
|
|
|
|
|
|
|
|
OBSLogViewer::~OBSLogViewer()
|
|
|
|
{
|
|
|
|
config_set_string(App()->GlobalConfig(), "LogViewer", "geometry",
|
|
|
|
saveGeometry().toBase64().constData());
|
|
|
|
}
|
|
|
|
|
2022-02-27 01:42:18 -08:00
|
|
|
void OBSLogViewer::on_showStartup_clicked(bool checked)
|
2020-05-15 05:34:54 -07:00
|
|
|
{
|
|
|
|
config_set_bool(App()->GlobalConfig(), "LogViewer", "ShowLogStartup",
|
|
|
|
checked);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern QPointer<OBSLogViewer> obsLogViewer;
|
|
|
|
|
|
|
|
void OBSLogViewer::InitLog()
|
|
|
|
{
|
|
|
|
char logDir[512];
|
|
|
|
std::string path;
|
|
|
|
|
|
|
|
if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs")) {
|
|
|
|
path += logDir;
|
|
|
|
path += "/";
|
|
|
|
path += App()->GetCurrentLog();
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(QT_UTF8(path.c_str()));
|
|
|
|
|
|
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
|
|
QTextStream in(&file);
|
2021-03-09 06:11:19 -08:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
2020-09-27 02:05:53 -07:00
|
|
|
in.setCodec("UTF-8");
|
2021-03-04 21:30:03 -08:00
|
|
|
#endif
|
2020-05-15 05:34:54 -07:00
|
|
|
|
2022-02-27 01:42:18 -08:00
|
|
|
QTextDocument *doc = ui->textArea->document();
|
2022-02-21 03:23:52 -08:00
|
|
|
QTextCursor cursor(doc);
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
cursor.beginEditBlock();
|
2020-05-15 05:34:54 -07:00
|
|
|
while (!in.atEnd()) {
|
|
|
|
QString line = in.readLine();
|
2022-02-26 23:56:19 -08:00
|
|
|
cursor.insertText(line);
|
2022-02-21 03:23:52 -08:00
|
|
|
cursor.insertBlock();
|
2020-05-15 05:34:54 -07:00
|
|
|
}
|
2022-02-21 03:23:52 -08:00
|
|
|
cursor.endEditBlock();
|
2020-05-15 05:34:54 -07:00
|
|
|
|
|
|
|
file.close();
|
|
|
|
}
|
2022-02-27 01:42:18 -08:00
|
|
|
QScrollBar *scroll = ui->textArea->verticalScrollBar();
|
2022-02-21 03:23:52 -08:00
|
|
|
scroll->setValue(scroll->maximum());
|
2020-05-15 05:34:54 -07:00
|
|
|
|
|
|
|
obsLogViewer = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OBSLogViewer::AddLine(int type, const QString &str)
|
|
|
|
{
|
|
|
|
QString msg = str.toHtmlEscaped();
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case LOG_WARNING:
|
2022-02-21 03:23:52 -08:00
|
|
|
msg = QString("<font color=\"#c08000\">%1</font>").arg(msg);
|
2020-05-15 05:34:54 -07:00
|
|
|
break;
|
|
|
|
case LOG_ERROR:
|
2022-02-21 03:23:52 -08:00
|
|
|
msg = QString("<font color=\"#c00000\">%1</font>").arg(msg);
|
2020-05-15 05:34:54 -07:00
|
|
|
break;
|
2022-02-26 23:56:19 -08:00
|
|
|
default:
|
|
|
|
msg = QString("<font>%1</font>").arg(msg);
|
|
|
|
break;
|
2020-05-15 05:34:54 -07:00
|
|
|
}
|
|
|
|
|
2022-02-27 01:42:18 -08:00
|
|
|
QScrollBar *scroll = ui->textArea->verticalScrollBar();
|
2020-05-15 05:34:54 -07:00
|
|
|
bool bottomScrolled = scroll->value() >= scroll->maximum() - 10;
|
|
|
|
|
|
|
|
if (bottomScrolled)
|
|
|
|
scroll->setValue(scroll->maximum());
|
|
|
|
|
2022-02-27 01:42:18 -08:00
|
|
|
QTextDocument *doc = ui->textArea->document();
|
2022-02-21 03:23:52 -08:00
|
|
|
QTextCursor cursor(doc);
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
cursor.beginEditBlock();
|
|
|
|
cursor.insertHtml(msg);
|
|
|
|
cursor.insertBlock();
|
|
|
|
cursor.endEditBlock();
|
2020-05-15 05:34:54 -07:00
|
|
|
|
|
|
|
if (bottomScrolled)
|
|
|
|
scroll->setValue(scroll->maximum());
|
|
|
|
}
|
|
|
|
|
2022-02-27 01:42:18 -08:00
|
|
|
void OBSLogViewer::on_openButton_clicked()
|
2020-08-14 22:20:38 -07:00
|
|
|
{
|
|
|
|
char logDir[512];
|
|
|
|
if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char *log = App()->GetCurrentLog();
|
|
|
|
|
|
|
|
std::string path = logDir;
|
|
|
|
path += "/";
|
|
|
|
path += log;
|
|
|
|
|
|
|
|
QUrl url = QUrl::fromLocalFile(QT_UTF8(path.c_str()));
|
|
|
|
QDesktopServices::openUrl(url);
|
|
|
|
}
|