1 /**************************************************************************
2  **                                                                      **
3  ** Copyright (C) 2018 Lukas Spies                                       **
4  ** Contact: http://photoqt.org                                          **
5  **                                                                      **
6  ** This file is part of PhotoQt.                                        **
7  **                                                                      **
8  ** PhotoQt is free software: you can redistribute it and/or modify      **
9  ** it under the terms of the GNU General Public License as published by **
10  ** the Free Software Foundation, either version 2 of the License, or    **
11  ** (at your option) any later version.                                  **
12  **                                                                      **
13  ** PhotoQt is distributed in the hope that it will be useful,           **
14  ** but WITHOUT ANY WARRANTY; without even the implied warranty of       **
15  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        **
16  ** GNU General Public License for more details.                         **
17  **                                                                      **
18  ** You should have received a copy of the GNU General Public License    **
19  ** along with PhotoQt. If not, see <http://www.gnu.org/licenses/>.      **
20  **                                                                      **
21  **************************************************************************/
22 
23 #ifndef LOGGER_H
24 #define LOGGER_H
25 
26 #include <iostream>
27 #include <sstream>
28 #include <QDateTime>
29 #include <QDir>
30 #include <QTextStream>
31 #include "configfiles.h"
32 
33 class Logger {
34 
35 public:
Logger()36     Logger() {
37 #ifdef PHOTOQTDEBUG
38         logFile.setFileName(QDir::tempPath() + "/photoqt.log");
39 #endif
40     }
41 
42     template <class T>
43     Logger &operator<<(const T &v) {
44 
45         std::stringstream str;
46         str << v;
47 
48         if(str.str() == "[[[DATE]]]")
49             std::clog << "[" << QDateTime::currentDateTime().toString("dd/MM/yyyy HH:mm:ss:zzz").toStdString() << "] ";
50         else
51             std::clog << v;
52 
53 #ifdef PHOTOQTDEBUG
54         QTextStream out(&logFile);
55         logFile.open(QIODevice::WriteOnly | QIODevice::Append);
56         if(str.str() == "[[[DATE]]]")
57             out << "[" << QDateTime::currentDateTime().toString("dd/MM/yyyy HH:mm:ss:zzz") << "] ";
58         else
59             out << QString::fromStdString(str.str());
60 
61         logFile.close();
62 #endif
63 
64         return *this;
65 
66     }
67 
68     Logger &operator<<(std::ostream&(*f)(std::ostream&)) {
69         std::clog << f;
70         return *this;
71     }
72 
73 private:
74 #ifdef PHOTOQTDEBUG
75     QFile logFile;
76 #endif
77 
78 };
79 
80 #define LOG Logger()
81 const std::string CURDATE = "[[[DATE]]]";
82 const std::string NL = "\n";
83 
84 #endif // LOGGER_H
85