log-viewer.cpp 3.1 KB

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