Commit 27a176d1610a548e096bf83d4e55e995c61743f4
1 parent
844ad9e9
Exists in
master
Iniciada novas classes para redirecionamento das mensagens de QDebug/qWarning/et…
…c para um arquivo log.
Showing
2 changed files
with
81 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,59 @@ | @@ -0,0 +1,59 @@ | ||
1 | +#include "logfile.h" | ||
2 | +#include <QMetaType> | ||
3 | + | ||
4 | + | ||
5 | +LogFile::LogFile(QObject *parent) : | ||
6 | + QObject(parent) | ||
7 | +{ | ||
8 | + qRegisterMetaType<QtMsgType>("QtMsgType"); | ||
9 | + qWarning() << "Sem caminho de arquivo passado como parâmetro."; | ||
10 | + qWarning() << "Utilizando arquivo-padrão logfile.txt."; | ||
11 | + | ||
12 | + logFile = new QFile("./logfile.txt"); | ||
13 | + | ||
14 | + bool fileOpened = logFile->open(QFile::ReadWrite); | ||
15 | + if(!fileOpened) { | ||
16 | + qWarning() << "Não foi possível abrir arquivo de log."; | ||
17 | + return; | ||
18 | + } | ||
19 | +} | ||
20 | + | ||
21 | +LogFile::LogFile(const QString &location, QObject *parent) : | ||
22 | + QObject(parent) | ||
23 | +{ | ||
24 | + qRegisterMetaType<QtMsgType>("QtMsgType"); | ||
25 | + | ||
26 | + logFile = new QFile(location); | ||
27 | + | ||
28 | + bool fileOpened = logFile->open(QFile::ReadWrite); | ||
29 | + if(!fileOpened) { | ||
30 | + qWarning() << "Não foi possível abrir arquivo de log."; | ||
31 | + return; | ||
32 | + } | ||
33 | +} | ||
34 | + | ||
35 | +LogFile::~LogFile() | ||
36 | +{ | ||
37 | + logFile->close(); | ||
38 | + delete logFile; | ||
39 | +} | ||
40 | + | ||
41 | + | ||
42 | +void LogFile::outputMessage(QtMsgType type, const QString &msg) | ||
43 | +{ | ||
44 | + | ||
45 | + | ||
46 | + if (type == QtDebugMsg) { | ||
47 | + QByteArray localMsg = QString("Debug: ").append(msg).append("\n").toLocal8Bit(); | ||
48 | + logFile->write(localMsg); | ||
49 | + } else if(type == QtWarningMsg) { | ||
50 | + QByteArray localMsg = QString("Warning: ").append(msg).append("\n").toLocal8Bit(); | ||
51 | + logFile->write(localMsg); | ||
52 | + } else if(type == QtCriticalMsg) { | ||
53 | + QByteArray localMsg = QString("Critical: ").append(msg).append("\n").toLocal8Bit(); | ||
54 | + logFile->write(localMsg); | ||
55 | + } else if(type == QtFatalMsg) { | ||
56 | + QByteArray localMsg = QString("Fatal: ").append(msg).append("\n").toLocal8Bit(); | ||
57 | + logFile->write(localMsg); | ||
58 | + } | ||
59 | +} |
@@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
1 | +#ifndef LOGBROWSER_H | ||
2 | +#define LOGBROWSER_H | ||
3 | + | ||
4 | +#include <QDebug> | ||
5 | +#include <QFile> | ||
6 | +#include <QObject> | ||
7 | + | ||
8 | +class LogFile : public QObject | ||
9 | +{ | ||
10 | + Q_OBJECT | ||
11 | +public: | ||
12 | + explicit LogFile(QObject *parent = 0); | ||
13 | + explicit LogFile(const QString &fileLocation, QObject *parent = 0); | ||
14 | + ~LogFile(); | ||
15 | + void outputMessage( QtMsgType type, const QString &msg ); | ||
16 | + | ||
17 | +private: | ||
18 | + QFile *logFile; | ||
19 | + | ||
20 | +}; | ||
21 | + | ||
22 | +#endif // LOGBROWSER_H |