log-viewer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <QFile>
  2. #include <QTextStream>
  3. #include <QScrollBar>
  4. #include <QFont>
  5. #include <QFontDatabase>
  6. #include <QPushButton>
  7. #include <QCheckBox>
  8. #include <QLayout>
  9. #include <QDesktopServices>
  10. #include <string>
  11. #include "log-viewer.hpp"
  12. #include "qt-wrappers.hpp"
  13. OBSLogViewer::OBSLogViewer(QWidget *parent) : QDialog(parent)
  14. {
  15. setWindowFlags(windowFlags() & Qt::WindowMaximizeButtonHint &
  16. ~Qt::WindowContextHelpButtonHint);
  17. setAttribute(Qt::WA_DeleteOnClose);
  18. QVBoxLayout *layout = new QVBoxLayout();
  19. layout->setContentsMargins(0, 0, 0, 0);
  20. const QFont fixedFont =
  21. QFontDatabase::systemFont(QFontDatabase::FixedFont);
  22. textArea = new QPlainTextEdit();
  23. textArea->setReadOnly(true);
  24. textArea->setFont(fixedFont);
  25. QHBoxLayout *buttonLayout = new QHBoxLayout();
  26. QPushButton *clearButton = new QPushButton(QTStr("Clear"));
  27. connect(clearButton, &QPushButton::clicked, this,
  28. &OBSLogViewer::ClearText);
  29. QPushButton *openButton = new QPushButton(QTStr("OpenFile"));
  30. connect(openButton, &QPushButton::clicked, this,
  31. &OBSLogViewer::OpenFile);
  32. QPushButton *closeButton = new QPushButton(QTStr("Close"));
  33. connect(closeButton, &QPushButton::clicked, this, &QDialog::close);
  34. bool showLogViewerOnStartup = config_get_bool(
  35. App()->GlobalConfig(), "LogViewer", "ShowLogStartup");
  36. QCheckBox *showStartup = new QCheckBox(QTStr("ShowOnStartup"));
  37. showStartup->setChecked(showLogViewerOnStartup);
  38. connect(showStartup, SIGNAL(toggled(bool)), this,
  39. SLOT(ToggleShowStartup(bool)));
  40. buttonLayout->addSpacing(10);
  41. buttonLayout->addWidget(showStartup);
  42. buttonLayout->addStretch();
  43. buttonLayout->addWidget(openButton);
  44. buttonLayout->addWidget(clearButton);
  45. buttonLayout->addWidget(closeButton);
  46. buttonLayout->addSpacing(10);
  47. buttonLayout->setContentsMargins(0, 0, 0, 4);
  48. layout->addWidget(textArea);
  49. layout->addLayout(buttonLayout);
  50. setLayout(layout);
  51. setWindowTitle(QTStr("LogViewer"));
  52. resize(800, 300);
  53. const char *geom = config_get_string(App()->GlobalConfig(), "LogViewer",
  54. "geometry");
  55. if (geom != nullptr) {
  56. QByteArray ba = QByteArray::fromBase64(QByteArray(geom));
  57. restoreGeometry(ba);
  58. }
  59. InitLog();
  60. }
  61. OBSLogViewer::~OBSLogViewer()
  62. {
  63. config_set_string(App()->GlobalConfig(), "LogViewer", "geometry",
  64. saveGeometry().toBase64().constData());
  65. }
  66. void OBSLogViewer::ToggleShowStartup(bool checked)
  67. {
  68. config_set_bool(App()->GlobalConfig(), "LogViewer", "ShowLogStartup",
  69. checked);
  70. }
  71. extern QPointer<OBSLogViewer> obsLogViewer;
  72. void OBSLogViewer::InitLog()
  73. {
  74. char logDir[512];
  75. std::string path;
  76. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs")) {
  77. path += logDir;
  78. path += "/";
  79. path += App()->GetCurrentLog();
  80. }
  81. QFile file(QT_UTF8(path.c_str()));
  82. if (file.open(QIODevice::ReadOnly)) {
  83. QTextStream in(&file);
  84. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  85. in.setCodec("UTF-8");
  86. #endif
  87. QTextDocument *doc = textArea->document();
  88. QTextCursor cursor(doc);
  89. cursor.movePosition(QTextCursor::End);
  90. cursor.beginEditBlock();
  91. while (!in.atEnd()) {
  92. QString line = in.readLine();
  93. cursor.insertHtml(line);
  94. cursor.insertBlock();
  95. }
  96. cursor.endEditBlock();
  97. file.close();
  98. }
  99. QScrollBar *scroll = textArea->verticalScrollBar();
  100. scroll->setValue(scroll->maximum());
  101. obsLogViewer = this;
  102. }
  103. void OBSLogViewer::AddLine(int type, const QString &str)
  104. {
  105. QString msg = str.toHtmlEscaped();
  106. switch (type) {
  107. case LOG_WARNING:
  108. msg = QString("<font color=\"#c08000\">%1</font>").arg(msg);
  109. break;
  110. case LOG_ERROR:
  111. msg = QString("<font color=\"#c00000\">%1</font>").arg(msg);
  112. break;
  113. }
  114. QScrollBar *scroll = textArea->verticalScrollBar();
  115. bool bottomScrolled = scroll->value() >= scroll->maximum() - 10;
  116. if (bottomScrolled)
  117. scroll->setValue(scroll->maximum());
  118. QTextDocument *doc = textArea->document();
  119. QTextCursor cursor(doc);
  120. cursor.movePosition(QTextCursor::End);
  121. cursor.beginEditBlock();
  122. cursor.insertHtml(msg);
  123. cursor.insertBlock();
  124. cursor.endEditBlock();
  125. if (bottomScrolled)
  126. scroll->setValue(scroll->maximum());
  127. }
  128. void OBSLogViewer::ClearText()
  129. {
  130. textArea->clear();
  131. }
  132. void OBSLogViewer::OpenFile()
  133. {
  134. char logDir[512];
  135. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  136. return;
  137. const char *log = App()->GetCurrentLog();
  138. std::string path = logDir;
  139. path += "/";
  140. path += log;
  141. QUrl url = QUrl::fromLocalFile(QT_UTF8(path.c_str()));
  142. QDesktopServices::openUrl(url);
  143. }