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 std::string to_string(float f);
18 std::string to_string(double d);
19 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
80 void SetLogFile(std::string const& logFilename = "");
81
82 enum class Level
83 {
84 Debug,
85 Info,
86 Warn,
87 Error,
88 Fatal
89 };
90 void SetLogLevel(Level level);
91 Level GetLogLevel() const;
92
93 virtual void SetLogTime(const SEScalarTime* time);
94
95 virtual bool HasForward() const;
96 virtual void AddForward(LoggerForward* forward);
97 virtual void RemoveForward(LoggerForward* forward);
98 virtual void RemoveForwards();
99
100 virtual void Debug(std::string const& msg);
101 virtual void Debug(std::stringstream &msg);
102 virtual void Debug(std::ostream &msg);
103
104 virtual void Info(std::string const& msg);
105 virtual void Info(std::stringstream &msg);
106 virtual void Info(const std::stringstream &msg);
107 virtual void Info(std::ostream &msg);
108
109 virtual void Warning(std::string const& msg);
110 virtual void Warning(std::stringstream &msg);
111 virtual void Warning(std::ostream &msg);
112
113 virtual void Error(std::string const& msg);
114 virtual void Error(std::stringstream &msg);
115 virtual void Error(std::ostream &msg);
116
117 virtual void Fatal(std::string const& msg);
118 virtual void Fatal(std::stringstream &msg);
119 virtual void Fatal(std::ostream &msg);
120
121protected:
122
123 virtual std::string FormatLogMessage(std::string const& msg);
124
125 std::vector<LoggerForward*> m_Forwards;
127
128private:
130};
131
132class CDM_DECL LogMessages
133{
134public:
136 virtual ~LogMessages() {};
137
138 bool static SerializeToString(const LogMessages& msgs, std::string& output, eSerializationFormat m, Logger* logger);
139 bool static SerializeFromString(const std::string& src, LogMessages& msgs, eSerializationFormat m, Logger* logger);
140
141 void Clear()
142 {
143 debug_msgs.clear();
144 info_msgs.clear();
145 warning_msgs.clear();
146 error_msgs.clear();
147 fatal_msgs.clear();
148 }
149
150 bool IsEmpty()
151 {
152 if (!debug_msgs.empty())
153 return false;
154 if (!info_msgs.empty())
155 return false;
156 if (!warning_msgs.empty())
157 return false;
158 if (!error_msgs.empty())
159 return false;
160 if (!fatal_msgs.empty())
161 return false;
162 return true;
163 }
164
165 std::vector<std::string> debug_msgs;
166 std::vector<std::string> info_msgs;
167 std::vector<std::string> warning_msgs;
168 std::vector<std::string> error_msgs;
169 std::vector<std::string> fatal_msgs;
170};
171
172inline std::ostream& operator<< (std::ostream& out, const LogMessages& a)
173{
174 if (!a.debug_msgs.empty())
175 {
176 out << "Debug Messages: \n";
177 for (auto& m : a.debug_msgs)
178 out << m << "\n";
179 }
180 if (!a.info_msgs.empty())
181 {
182 out << "Info Messages: \n";
183 for (auto& m : a.info_msgs)
184 out << m << "\n";
185 }
186 if (!a.warning_msgs.empty())
187 {
188 out << "Warning Messages: \n";
189 for (auto& m : a.warning_msgs)
190 out << m << "\n";
191 }
192 if (!a.error_msgs.empty())
193 {
194 out << "Error Messages: \n";
195 for (auto& m : a.error_msgs)
196 out << m << "\n";
197 }
198 if (!a.fatal_msgs.empty())
199 {
200 out << "Fatal Messages: \n";
201 for (auto& m : a.fatal_msgs)
202 out << m << "\n";
203 }
204 return out;
205}
Definition: Logger.h:133
std::vector< std::string > fatal_msgs
Definition: Logger.h:169
std::vector< std::string > error_msgs
Definition: Logger.h:168
virtual ~LogMessages()
Definition: Logger.h:136
void Clear()
Definition: Logger.h:141
std::vector< std::string > info_msgs
Definition: Logger.h:166
std::vector< std::string > debug_msgs
Definition: Logger.h:165
LogMessages()
Definition: Logger.h:135
bool IsEmpty()
Definition: Logger.h:150
std::vector< std::string > warning_msgs
Definition: Logger.h:167
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:125
const SEScalarTime * m_time
Definition: Logger.h:126
friend Loggable
Definition: Logger.h:72
log_lib * _log_lib
Definition: Logger.h:129
Level
Definition: Logger.h:83
Definition: SEScalarTime.h:28
Definition: SimpleLogger.cpp:10
std::string PrettyPrint(const std::string &str, ePrettyPrintType ppt)
Definition: Logger.cpp:30
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.