1 /***
2 
3     Olive - Non-Linear Video Editor
4     Copyright (C) 2019  Olive Team
5 
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 
19 ***/
20 
21 #include "debug.h"
22 
23 #include <QFile>
24 #include <QDateTime>
25 #include <QStandardPaths>
26 #include <QDir>
27 #include <QMutex>
28 
29 #include "dialogs/debugdialog.h"
30 
31 QString debug_info;
32 QMutex debug_mutex;
33 QFile debug_file;
34 QTextStream debug_stream;
35 
open_debug_file()36 void open_debug_file() {
37   QDir debug_dir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
38   debug_dir.mkpath(".");
39   if (debug_dir.exists()) {
40     debug_file.setFileName(debug_dir.path() + "/debug_log");
41     if (debug_file.open(QFile::WriteOnly)) {
42       debug_stream.setDevice(&debug_file);
43     } else {
44       qWarning() << "Couldn't open debug log file, debug log will not be saved";
45     }
46   }
47 }
48 
close_debug_file()49 void close_debug_file()
50 {
51   if (debug_file.isOpen()) {
52     debug_file.close();
53   }
54 }
55 
debug_message_handler(QtMsgType type,const QMessageLogContext & context,const QString & msg)56 void debug_message_handler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
57 {
58   debug_mutex.lock();
59   const QByteArray localMsg = msg.toLocal8Bit();
60   const QDateTime now = QDateTime::currentDateTime();
61   const QByteArray timeRepr(now.toString(Qt::ISODate).toLocal8Bit());
62   QString msgTag;
63   QString fontColor;
64   switch (type) {
65   case QtDebugMsg:
66     msgTag = "DEBUG";
67     fontColor = "grey";
68     break;
69   case QtInfoMsg:
70     msgTag = "INFO";
71     fontColor = "blue";
72     break;
73   case QtWarningMsg:
74     msgTag = "WARNING";
75     fontColor = "yellow";
76     break;
77   case QtCriticalMsg:
78     msgTag = "ERROR";
79     fontColor = "red";
80     break;
81   case QtFatalMsg:
82     msgTag = "FATAL";
83     fontColor = "red";
84     break;
85   default:
86     fprintf(stderr, "Unknown debug msg type");
87     fflush(stderr);
88     break;
89   }//switch
90 
91   /*fprintf(stderr, "%s [%s] %s (%s:%u, %s)\n", timeRepr.data(), msgTag.toLocal8Bit().constData(), localMsg.data(),
92             context.file, context.line, context.function);*/
93 
94   fprintf(stderr, "%s [%s] %s\n", timeRepr.data(), msgTag.toLocal8Bit().constData(), localMsg.data());
95 
96   if (debug_file.isOpen()) {
97     debug_stream << QString("[%1] %2 (%3:%4, %5)\n")
98                     .arg(msgTag, localMsg, context.file, QString::number(context.line), context.function);
99   }
100   debug_info.append(QString("<font color='%1'><b>[%2]</b> %3 (%4:%5, %6)</font><br>")
101                      .arg(fontColor, msgTag, localMsg, context.file, QString::number(context.line), context.function));
102   fflush(stderr);
103   if (olive::DebugDialog != nullptr && olive::DebugDialog->isVisible()) {
104     QMetaObject::invokeMethod(olive::DebugDialog, "update_log", Qt::QueuedConnection);
105   }
106   debug_mutex.unlock();
107 }
108 
get_debug_str()109 const QString &get_debug_str()
110 {
111   return debug_info;
112 }
113