log-viewer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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)
  14. : QDialog(parent), ui(new Ui::OBSLogViewer)
  15. {
  16. setWindowFlags(windowFlags() & Qt::WindowMaximizeButtonHint &
  17. ~Qt::WindowContextHelpButtonHint);
  18. setAttribute(Qt::WA_DeleteOnClose);
  19. ui->setupUi(this);
  20. bool showLogViewerOnStartup = config_get_bool(
  21. App()->GlobalConfig(), "LogViewer", "ShowLogStartup");
  22. ui->showStartup->setChecked(showLogViewerOnStartup);
  23. const char *geom = config_get_string(App()->GlobalConfig(), "LogViewer",
  24. "geometry");
  25. if (geom != nullptr) {
  26. QByteArray ba = QByteArray::fromBase64(QByteArray(geom));
  27. restoreGeometry(ba);
  28. }
  29. InitLog();
  30. }
  31. OBSLogViewer::~OBSLogViewer()
  32. {
  33. config_set_string(App()->GlobalConfig(), "LogViewer", "geometry",
  34. saveGeometry().toBase64().constData());
  35. }
  36. void OBSLogViewer::on_showStartup_clicked(bool checked)
  37. {
  38. config_set_bool(App()->GlobalConfig(), "LogViewer", "ShowLogStartup",
  39. checked);
  40. }
  41. extern QPointer<OBSLogViewer> obsLogViewer;
  42. void OBSLogViewer::InitLog()
  43. {
  44. char logDir[512];
  45. std::string path;
  46. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs")) {
  47. path += logDir;
  48. path += "/";
  49. path += App()->GetCurrentLog();
  50. }
  51. QFile file(QT_UTF8(path.c_str()));
  52. if (file.open(QIODevice::ReadOnly)) {
  53. QTextStream in(&file);
  54. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  55. in.setCodec("UTF-8");
  56. #endif
  57. QTextDocument *doc = ui->textArea->document();
  58. QTextCursor cursor(doc);
  59. cursor.movePosition(QTextCursor::End);
  60. cursor.beginEditBlock();
  61. while (!in.atEnd()) {
  62. QString line = in.readLine();
  63. cursor.insertText(line);
  64. cursor.insertBlock();
  65. }
  66. cursor.endEditBlock();
  67. file.close();
  68. }
  69. QScrollBar *scroll = ui->textArea->verticalScrollBar();
  70. scroll->setValue(scroll->maximum());
  71. obsLogViewer = this;
  72. }
  73. void OBSLogViewer::AddLine(int type, const QString &str)
  74. {
  75. QString msg = str.toHtmlEscaped();
  76. switch (type) {
  77. case LOG_WARNING:
  78. msg = QString("<font color=\"#c08000\">%1</font>").arg(msg);
  79. break;
  80. case LOG_ERROR:
  81. msg = QString("<font color=\"#c00000\">%1</font>").arg(msg);
  82. break;
  83. default:
  84. msg = QString("<font>%1</font>").arg(msg);
  85. break;
  86. }
  87. QScrollBar *scroll = ui->textArea->verticalScrollBar();
  88. bool bottomScrolled = scroll->value() >= scroll->maximum() - 10;
  89. if (bottomScrolled)
  90. scroll->setValue(scroll->maximum());
  91. QTextDocument *doc = ui->textArea->document();
  92. QTextCursor cursor(doc);
  93. cursor.movePosition(QTextCursor::End);
  94. cursor.beginEditBlock();
  95. cursor.insertHtml(msg);
  96. cursor.insertBlock();
  97. cursor.endEditBlock();
  98. if (bottomScrolled)
  99. scroll->setValue(scroll->maximum());
  100. }
  101. void OBSLogViewer::on_openButton_clicked()
  102. {
  103. char logDir[512];
  104. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  105. return;
  106. const char *log = App()->GetCurrentLog();
  107. std::string path = logDir;
  108. path += "/";
  109. path += log;
  110. QUrl url = QUrl::fromLocalFile(QT_UTF8(path.c_str()));
  111. QDesktopServices::openUrl(url);
  112. }