log-viewer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. QTextDocument *doc = ui->textArea->document();
  55. QTextCursor cursor(doc);
  56. cursor.movePosition(QTextCursor::End);
  57. cursor.beginEditBlock();
  58. while (!in.atEnd()) {
  59. QString line = in.readLine();
  60. cursor.insertText(line);
  61. cursor.insertBlock();
  62. }
  63. cursor.endEditBlock();
  64. file.close();
  65. }
  66. QScrollBar *scroll = ui->textArea->verticalScrollBar();
  67. scroll->setValue(scroll->maximum());
  68. obsLogViewer = this;
  69. }
  70. void OBSLogViewer::AddLine(int type, const QString &str)
  71. {
  72. QString msg = str.toHtmlEscaped();
  73. switch (type) {
  74. case LOG_WARNING:
  75. msg = QString("<font color=\"#c08000\">%1</font>").arg(msg);
  76. break;
  77. case LOG_ERROR:
  78. msg = QString("<font color=\"#c00000\">%1</font>").arg(msg);
  79. break;
  80. default:
  81. msg = QString("<font>%1</font>").arg(msg);
  82. break;
  83. }
  84. QScrollBar *scroll = ui->textArea->verticalScrollBar();
  85. bool bottomScrolled = scroll->value() >= scroll->maximum() - 10;
  86. if (bottomScrolled)
  87. scroll->setValue(scroll->maximum());
  88. QTextDocument *doc = ui->textArea->document();
  89. QTextCursor cursor(doc);
  90. cursor.movePosition(QTextCursor::End);
  91. cursor.beginEditBlock();
  92. cursor.insertHtml(msg);
  93. cursor.insertBlock();
  94. cursor.endEditBlock();
  95. if (bottomScrolled)
  96. scroll->setValue(scroll->maximum());
  97. }
  98. void OBSLogViewer::on_openButton_clicked()
  99. {
  100. char logDir[512];
  101. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  102. return;
  103. const char *log = App()->GetCurrentLog();
  104. std::string path = logDir;
  105. path += "/";
  106. path += log;
  107. QUrl url = QUrl::fromLocalFile(QT_UTF8(path.c_str()));
  108. QDesktopServices::openUrl(url);
  109. }