log-viewer.cpp 4.1 KB

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