Sayonara Player
Logger.h
1 /* Logger.h */
2 
3 /* Copyright (C) 2011-2017 Lucio Carreras
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef LOGGER_H
22 #define LOGGER_H
23 
24 #include <typeinfo>
25 #include <type_traits>
26 #include <string>
27 #include <QObject>
28 #include <QDateTime>
29 #include <QString>
30 
31 
36 class QString;
37 class QStringList;
38 class QByteArray;
39 class QPoint;
40 class QChar;
41 class QObject;
42 class LogListener;
43 
44 
45 enum class Log : unsigned char
46 {
47  Warning,
48  Error,
49  Info,
50  Debug,
51  Develop
52 };
53 
54 struct LogEntry
55 {
56  QDateTime dt;
57  Log type;
58  QString class_name;
59  QString message;
60 };
61 
62 
67 class Logger
68 {
69 
70 private:
71  struct Private;
72  Private* m=nullptr;
73 
74 public:
75  explicit Logger(Log type, const QString& class_name);
76 
77  ~Logger();
78 
79  static void register_log_listener(LogListener* log_listener);
80 
81  Logger& operator << (const QString& msg);
82  Logger& operator << (const QChar& c);
83  Logger& operator << (const QStringList& lst);
84  Logger& operator << (const QByteArray& arr);
85  Logger& operator << (const QPoint& point);
86  Logger& operator << (const char* str);
87  Logger& operator << (const std::string& str);
88  Logger& operator << (const Log& log_type);
89 
90  template<typename T>
91  typename std::enable_if< std::is_floating_point<T>::value, Logger&>::type
92  operator << (const T& val){
93 
94  (*this) << std::to_string(val);
95 
96  return *this;
97  }
98 
99  template<typename T>
100  typename std::enable_if< std::is_integral<T>::value, Logger&>::type
101  operator << (const T& val){
102 
103  (*this) << std::to_string(val);
104 
105  return *this;
106  }
107 
108  template<typename T, template <typename ELEM> class CONT>
109  Logger& operator << (const CONT<T> list){
110  for(const T& item : list){
111  (*this) << std::to_string(item) << ", ";
112  }
113 
114  return *this;
115  }
116 };
117 
118 Logger sp_log(Log type);
119 Logger sp_log(Log type, const char* data);
120 
121 template<typename T>
122 typename std::enable_if< std::is_class<T>::value, Logger>::type
123 sp_log(Log type, const T*)
124 {
125  return sp_log(type, typeid(T).name());
126 }
127 
128 Q_DECLARE_METATYPE(Log)
129 
130 #endif // LOGGER_H
The Logger class.
Definition: Logger.h:67
Definition: LogListener.h:32
Definition: Logger.h:54