Logger.h
1/* Distributed under the Apache License, Version 2.0.
2 See accompanying NOTICE file for details.*/
3
4#pragma once
5
6class Logger;
7class SEScalarTime;
8class log_lib; // Encapsulates 3rd party logging library
9#include <sstream>
10#include <mutex>
11
12enum class ePrettyPrintType { Action = 0, Condition };
13
14namespace pulse { namespace cdm
15{
16 // Not happy with how std does this for floats/doubles
17 CDM_DECL std::string to_string(float f);
18 CDM_DECL std::string to_string(double d);
19 CDM_DECL std::string PrettyPrint(const std::string& str, ePrettyPrintType ppt);
20}}
21
22class CDM_DECL Loggable
23{
24public:
25
26 Loggable(Logger* logger = nullptr);
27 Loggable(std::string const& logfile);
28 virtual ~Loggable();
29
30 virtual Logger* GetLogger() const;
31 virtual void SetLogger(Logger& logger);
32
33 virtual void Debug(std::string const& msg) const;
34 virtual void Debug(std::stringstream &msg) const;
35 virtual void Debug(std::ostream &msg) const;
36
37 virtual void Info(std::string const& msg) const;
38 virtual void Info(std::stringstream &msg) const;
39 virtual void Info(const std::stringstream &msg) const;
40 virtual void Info(std::ostream &msg) const;
41
42 virtual void Warning(std::string const& msg) const;
43 virtual void Warning(std::stringstream &msg) const;
44 virtual void Warning(std::ostream &msg) const;
45
46 virtual void Error(std::string const& msg) const;
47 virtual void Error(std::stringstream &msg) const;
48 virtual void Error(std::ostream &msg) const;
49
50 virtual void Fatal(std::string const& msg) const;
51 virtual void Fatal(std::stringstream &msg) const;
52 virtual void Fatal(std::ostream &msg) const;
53
54protected:
57};
58
59class CDM_DECL LoggerForward
60{
61public:
62 virtual ~LoggerForward() = default;
63 virtual void ForwardDebug(std::string const& /*msg*/) {};
64 virtual void ForwardInfo(std::string const& /*msg*/){};
65 virtual void ForwardWarning(std::string const& /*msg*/){};
66 virtual void ForwardError(std::string const& /*msg*/){};
67 virtual void ForwardFatal(std::string const& /*msg*/){};
68};
69
70class CDM_DECL Logger
71{
72 friend Loggable;
73public:
74 Logger(std::string const& logFilename = "");
75 virtual ~Logger();
76
77 void LogToConsole(bool b);
78 bool IsLoggingToConsole();
79 // Put some identifier at the beginning of each message going to std::cout
80 void AddConsolePrefix(const std::string& p);
81
82 void SetLogFile(std::string const& logFilename = "");
83
84 enum class Level
85 {
86 Debug,
87 Info,
88 Warn,
89 Error,
90 Fatal
91 };
92 void SetLogLevel(Level level);
93 Level GetLogLevel() const;
94
95
96 virtual void SetLogTime(const SEScalarTime* time);
97
98 virtual bool HasForward() const;
99 virtual void AddForward(LoggerForward* forward);
100 virtual void RemoveForward(LoggerForward* forward);
101 virtual void RemoveForwards();
102
103 virtual void Debug(std::string const& msg);
104 virtual void Debug(std::stringstream &msg);
105 virtual void Debug(std::ostream &msg);
106
107 virtual void Info(std::string const& msg);
108 virtual void Info(std::stringstream &msg);
109 virtual void Info(const std::stringstream &msg);
110 virtual void Info(std::ostream &msg);
111
112 virtual void Warning(std::string const& msg);
113 virtual void Warning(std::stringstream &msg);
114 virtual void Warning(std::ostream &msg);
115
116 virtual void Error(std::string const& msg);
117 virtual void Error(std::stringstream &msg);
118 virtual void Error(std::ostream &msg);
119
120 virtual void Fatal(std::string const& msg);
121 virtual void Fatal(std::stringstream &msg);
122 virtual void Fatal(std::ostream &msg);
123
124protected:
125
126 virtual std::string FormatLogMessage(std::string const& msg);
127
128 std::vector<LoggerForward*> m_Forwards;
130
131private:
133};
134
135class CDM_DECL LogMessages
136{
137public:
139 virtual ~LogMessages() {};
140
141 bool static SerializeToString(const LogMessages& msgs, std::string& output, eSerializationFormat m, Logger* logger);
142 bool static SerializeFromString(const std::string& src, LogMessages& msgs, eSerializationFormat m, Logger* logger);
143
144 void Clear()
145 {
146 debug_msgs.clear();
147 info_msgs.clear();
148 warning_msgs.clear();
149 error_msgs.clear();
150 fatal_msgs.clear();
151 }
152
153 bool IsEmpty()
154 {
155 if (!debug_msgs.empty())
156 return false;
157 if (!info_msgs.empty())
158 return false;
159 if (!warning_msgs.empty())
160 return false;
161 if (!error_msgs.empty())
162 return false;
163 if (!fatal_msgs.empty())
164 return false;
165 return true;
166 }
167
168 std::vector<std::string> debug_msgs;
169 std::vector<std::string> info_msgs;
170 std::vector<std::string> warning_msgs;
171 std::vector<std::string> error_msgs;
172 std::vector<std::string> fatal_msgs;
173};
174
175inline std::ostream& operator<< (std::ostream& out, const LogMessages& a)
176{
177 if (!a.debug_msgs.empty())
178 {
179 out << "Debug Messages: \n";
180 for (auto& m : a.debug_msgs)
181 out << m << "\n";
182 }
183 if (!a.info_msgs.empty())
184 {
185 out << "Info Messages: \n";
186 for (auto& m : a.info_msgs)
187 out << m << "\n";
188 }
189 if (!a.warning_msgs.empty())
190 {
191 out << "Warning Messages: \n";
192 for (auto& m : a.warning_msgs)
193 out << m << "\n";
194 }
195 if (!a.error_msgs.empty())
196 {
197 out << "Error Messages: \n";
198 for (auto& m : a.error_msgs)
199 out << m << "\n";
200 }
201 if (!a.fatal_msgs.empty())
202 {
203 out << "Fatal Messages: \n";
204 for (auto& m : a.fatal_msgs)
205 out << m << "\n";
206 }
207 return out;
208}
Definition: Logger.h:136
std::vector< std::string > fatal_msgs
Definition: Logger.h:172
std::vector< std::string > error_msgs
Definition: Logger.h:171
virtual ~LogMessages()
Definition: Logger.h:139
void Clear()
Definition: Logger.h:144
std::vector< std::string > info_msgs
Definition: Logger.h:169
std::vector< std::string > debug_msgs
Definition: Logger.h:168
LogMessages()
Definition: Logger.h:138
bool IsEmpty()
Definition: Logger.h:153
std::vector< std::string > warning_msgs
Definition: Logger.h:170
Definition: Logger.h:23
bool myLogger
Definition: Logger.h:55
Logger * m_Logger
Definition: Logger.h:56
Definition: Logger.h:60
virtual ~LoggerForward()=default
virtual void ForwardFatal(std::string const &)
Definition: Logger.h:67
virtual void ForwardDebug(std::string const &)
Definition: Logger.h:63
virtual void ForwardWarning(std::string const &)
Definition: Logger.h:65
virtual void ForwardError(std::string const &)
Definition: Logger.h:66
virtual void ForwardInfo(std::string const &)
Definition: Logger.h:64
Definition: Logger.h:71
std::vector< LoggerForward * > m_Forwards
Definition: Logger.h:128
const SEScalarTime * m_time
Definition: Logger.h:129
friend Loggable
Definition: Logger.h:72
log_lib * _log_lib
Definition: Logger.h:132
Level
Definition: Logger.h:85
Definition: SEScalarTime.h:28
Definition: SimpleLogger.cpp:10
CDM_DECL std::string PrettyPrint(const std::string &str, ePrettyPrintType ppt)
Definition: Logger.cpp:30
CDM_DECL std::string to_string(float f)
Definition: Logger.cpp:22
Definition: Logger.h:14

Distributed under the Apache License, Version 2.0.

See accompanying NOTICE file for details.