log-viewer.cpp 3.4 KB

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