Line data Source code
1 : #pragma once
2 :
3 : #include "WorkQueueLoopThread.h"
4 : #include <chrono>
5 : #include "utils_export.h"
6 : #include <fstream>
7 : #include <string>
8 : #include "EnumMap.h"
9 :
10 : namespace Utils
11 : {
12 :
13 : #ifndef loginfo
14 : # define loginfo(s) Utils::Logger::instance()->info(s, __FILE__, __LINE__)
15 : #endif // loginfo
16 : #ifndef logerror
17 : # define logerror(s) Utils::Logger::instance()->error(s, __FILE__, __LINE__)
18 : #endif // logerror
19 : #ifndef logwarn
20 : # define logwarn(s) Utils::Logger::instance()->warn(s, __FILE__, __LINE__)
21 : #endif // logwarn
22 : #ifndef logdebug
23 : # define logdebug(s) Utils::Logger::instance()->debug(s, __FILE__, __LINE__)
24 : #endif // logdebug
25 : #ifndef logexception
26 : # define logexception(e) Utils::Logger::instance()->exception(e, __FILE__, __LINE__)
27 : #endif // logexception
28 : #ifndef logexception_msg
29 : # define logexception_msg(e, m) Utils::Logger::instance()->exception(e, m, __FILE__, __LINE__)
30 : #endif // logexception_msg
31 :
32 : class UTILS_EXPORT Logger final// : public WorkQueueLoopThread<struct LogMessage>
33 : {
34 : public:
35 : enum class Level
36 : {
37 : None,
38 : Debug,
39 : Info,
40 : Warn,
41 : Error,
42 : };
43 :
44 : struct Message
45 : {
46 : std::string message;
47 : Level level;
48 : std::chrono::system_clock::time_point timeStamp;
49 : std::string file;
50 : int line;
51 :
52 : Message(const std::string& message, Level level)
53 : : message(message), level(level), timeStamp(std::chrono::system_clock::now()), file(""), line(-1)
54 : {
55 : }
56 :
57 429440 : Message(const std::string& message, Level level, const std::string& file, int line)
58 429440 : : message(message), level(level), timeStamp(std::chrono::system_clock::now()), file(file), line(line)
59 : {
60 429440 : }
61 : };
62 :
63 : enum OutputTypes
64 : {
65 : StdOut = 1,
66 : StdErr = 2,
67 : File = 4
68 : };
69 :
70 : Logger();
71 : ~Logger();
72 :
73 : static Logger* instance();
74 :
75 : Level logLevel() const;
76 : void logLevel(Level level);
77 :
78 : void outputTypes(int outputTypes);
79 : int outputTypes() const;
80 :
81 : std::string filename() const;
82 :
83 : void start();
84 : void stop();
85 :
86 : void log(Level level, const std::string& message, const std::string& file = "", int line = -1);
87 : void error(const std::string& message, const std::string& file = "", int line = -1);
88 : void warn(const std::string& message, const std::string& file = "", int line = -1);
89 : void info(const std::string& message, const std::string& file = "", int line = -1);
90 : void info(const std::stringstream& message, const std::string& file = "", int line = -1);
91 : void debug(const std::string& message, const std::string& file = "", int line = -1);
92 :
93 : void exception(const std::exception& e, const std::string& file = "", int line = -1);
94 : void exception(const std::exception& e, const std::string& message, const std::string& file = "", int line = -1);
95 : //void exception(const std::string& message, const std::string& file = "", int line = -1);
96 :
97 : template<class T>
98 : Logger& operator<<(const T& output);
99 :
100 : private:
101 : Level m_level;
102 : std::string m_logFilename;
103 :
104 : int m_outputTypes;
105 : std::ofstream m_logFileStream;
106 :
107 : //bool m_enableInternalLogging = false;
108 :
109 : WorkQueueLoopThread<struct Message> m_logMessageLoop;
110 :
111 : //bool processWorkItem(struct LogMessage& logMessage) override;
112 : bool logMessage(const struct Message& logMessage);
113 :
114 : static std::string formatLogMessage(const struct Message& logMessage);
115 : //static std::string logLevelToString(Level level);
116 :
117 : static Logger* _instance;
118 :
119 : inline static constexpr const char DEFAULT_LOG_FILENAME[] = "log.txt";
120 : //inline static constexpr const char* LogLevelStrings[] = ;
121 :
122 : static const inline EnumMap<Level> logLevelMap{
123 : { "NONE", "DEBUG", "INFO", "WARN", "ERROR" }
124 : };
125 : };
126 :
127 : template<class T>
128 : Logger& Logger::operator<<(const T& output)
129 : {
130 : log(Level::Info, output);
131 : return *this;
132 : }
133 : }
|