OBSLogViewer.cpp 3.0 KB

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