1
0

log-viewer.cpp 4.0 KB

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