LogSender.h
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5*
6* ArmarX is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License version 2 as
8* published by the Free Software Foundation.
9*
10* ArmarX is distributed in the hope that it will be useful, but
11* WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17*
18* @package ArmarXCore::core
19* @author Nils Adermann (naderman at naderman dot de)
20* @author Mirko Waechter (waechter at kit dot edu)
21* @date 2010
22* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
23* GNU General Public License
24*/
25
26#pragma once
27
28#include <chrono>
29#include <memory>
30
31#include <Ice/ProxyHandle.h>
32#include <IceUtil/Time.h>
33
35
36#include "LoggingUtil.h"
37
39{
40 class Log;
41}
42
43namespace armarx
44{
57
58 typedef ::IceInternal::ProxyHandle<::IceProxy::armarx::Log> LogPrx;
59
60 /**
61 \class LogSender
62 \brief Wrapper for the Log IceStorm topic with convenience methods for logging.
63 \ingroup Logging
64 */
65 class ARMARXCORE_IMPORT_EXPORT LogSender : public std::enable_shared_from_this<LogSender>
66 {
67 public:
69 {
70 eReset = -1,
71 eBlack = 0,
72 eRed = 1,
73 eGreen = 2,
76 };
77
78 typedef void (LogSender::*manipulator)();
79
80 // this is the type of std::cout
81 using CoutType = std::basic_ostream<char, std::char_traits<char>>;
82
83 // this is the function signature of std::endl
84 typedef CoutType& (*StandardEndLine)(CoutType&);
85
86
88
89 LogSender();
90 ~LogSender();
91
92 /**
93 * Constructor taking a LogPrx pointer to the IceStorm topic.
94 *
95 * \param componentName All log messages sent through this instance
96 * will show as having originated from this
97 * component.
98 * \param logProxy Ice proxy to send messages to.
99 */
100 LogSender(const std::string& componentName, LogPrx logProxy);
101
102 static void setProxy(const std::string& componentName, LogPrx logProxy);
103 static void SetComponentName(const std::string& componentName);
104 static void SetLoggingGroup(const std::string& loggingGroup);
105
106 /**
107 Appends a variable to the current message stringstream
108
109 \param message
110 \return This object for further streaming
111 */
112 template <typename T>
114 operator<<(const T& message)
115 {
116 currentMessage << message;
117 return *this;
118 }
119
120 /**
121 * @brief operator << overload for IceUtil::Time
122 * @param timestamp IceUtil::Time
123 * @return
124 */
125 LogSender& operator<<(const IceUtil::Time& timestamp);
126
127 // TODO: C++20 offers operator<< for std::chrono::duration.
128 // See: https://en.cppreference.com/w/cpp/chrono/duration/operator_ltlt
129 // The operator<<'s for timestamps can then be removed
130
131 LogSender& operator<<(const std::chrono::minutes& timestamp);
132
133 LogSender& operator<<(const std::chrono::seconds& timestamp);
134
135 LogSender& operator<<(const std::chrono::milliseconds& timestamp);
136
137 LogSender& operator<<(const std::chrono::microseconds& timestamp);
138
139 LogSender& operator<<(const std::chrono::nanoseconds& timestamp);
140
141
143 const StandardEndLine&
144 manipulator); // overloading for std::endl, but cannot get it to work with template specialization (Mirko 2012)
145 /**
146 Sends the current message to the log and resets the content.
147 */
148 void flush();
149
150 static std::string GetColorCodeString(MessageTypeT verbosityLevel);
151 static std::string GetColorCodeString(ConsoleColor colorCode);
152
153 static std::string CreateBackTrace(int linesToSkip = 1);
154 /**
155 Retrieves the current message severity
156
157 \return Current message severity
158 */
159 MessageTypeT getSeverity();
160
161 /**
162 Set the source code filename associated with this message.
163
164 \param filename The file this message was sent from
165 \return a pointer to this object
166 */
167 LogSenderPtr setFile(const std::string& file);
168
169 /**
170 Set the source code line associated with this message.
171
172 \param line The line this message was sent from
173 \return a pointer to this object
174 */
175 LogSenderPtr setLine(int line);
176
177 /**
178 Set the function name associated with this message.
179
180 \param line The function this message was sent from
181 \return a pointer to this object
182 */
183 LogSenderPtr setFunction(const std::string& function);
184 LogSenderPtr setLocalMinimumLoggingLevel(MessageTypeT level);
185 LogSenderPtr setBacktrace(bool printBackTrace);
186 LogSenderPtr setThreadId(Ice::Int tid);
187
188 LogSenderPtr setTag(const LogTag& tag);
189
190 // LogSenderPtr setSpamFilter(SpamFilterMapPtr spamFilter);
191
192 static std::string levelToString(MessageTypeT type);
193
194 /**
195 * \brief stringToLevel converts a string into a LoggingLevel,
196 * if possible.
197 *
198 * <b>Case-Insensitive</b>
199 * \param typeStr string that is to be converted
200 * \return The LoggingLevel or Undefined if conversion not possible.
201 */
202 static MessageTypeT StringToLevel(const std::string& typeStr);
203
204 /**
205 * \brief With setGlobalMinimumLoggingLevel the minimum verbosity-level of
206 * log-messages can be set for the whole application.<br/>
207 * the flag 'minimumLoggingLevel' overrides this setting.
208 * \param level The minimum logging level
209 */
210 static void SetGlobalMinimumLoggingLevel(MessageTypeT level);
211 static MessageTypeT GetGlobalMinimumLoggingLevel();
212 /**
213 * \brief setLoggingActivated() is used to activate or disable
214 * the logging facilities in the whole application
215 * \param activated
216 */
217 static void SetLoggingActivated(bool activated = true, bool showMessage = true);
218 static void SetSendLoggingActivated(bool activated = true);
219
220 static void SetColoredConsoleActivated(bool activated = true);
221 static long getThreadId();
222 static long getProcessId();
223 template <typename T>
224 static std::string GetTypeString();
225 static std::string CropFunctionName(const std::string& originalFunctionName);
226
227
228 protected:
229 /**
230 * Sends a message to the logging component.
231 *
232 * \param severity eVERBOSE / eINFO / eWARN / eERROR
233 * \param message
234 */
235 void log(MessageTypeT severity, std::string message);
236
237 private:
238 void initConsole();
239
240 /**
241 Resets information about associated file, line and function.
242 */
243 void resetLocation();
244
245 std::stringstream currentMessage;
246
247 struct Impl;
248 std::unique_ptr<Impl> impl;
249 };
250
252
253 /**
254 * Changes the current message severity for streamed messages
255 *
256 * \param severity
257 * \return This object for further streaming
258 */
259 template <>
261 LogSender::operator<< <MessageTypeT>(const MessageTypeT& severity);
262
263 /**
264 * Changes the current tag for streamed messages
265 *
266 * \param tag
267 * \return This object for further streaming
268 */
269 template <>
270 ARMARXCORE_IMPORT_EXPORT LogSender& LogSender::operator<< <LogTag>(const LogTag& tag);
271
272 /**
273 * Executes a manipulator like flush on the stream
274 *
275 * \param manipulator
276 * \return This object for further streaming
277 */
278 template <>
280 LogSender::operator<< <LogSender::manipulator>(const manipulator& manipulator);
281
282 /**
283 * Changes the current message color
284 *
285 * \param colorCode Color of the Text
286 * \return This object for further streaming
287 */
288 template <>
290 LogSender::operator<< <LogSender::ConsoleColor>(const LogSender::ConsoleColor& colorCode);
291
292 template <>
293 ARMARXCORE_IMPORT_EXPORT LogSender& LogSender::operator<< <bool>(const bool& duality);
294
295 template <>
297 LogSender::operator<< <SpamFilterDataPtr>(const SpamFilterDataPtr& spamFilterData);
298
299
300} // namespace armarx
std::ostream & operator<<(std::ostream &strm, const AbstractInterface &a)
std::string timestamp()
#define ARMARXCORE_IMPORT_EXPORT
Wrapper for the Log IceStorm topic with convenience methods for logging.
Definition LogSender.h:66
static std::string GetTypeString()
static std::string CropFunctionName(const std::string &originalFunctionName)
static void SetLoggingGroup(const std::string &loggingGroup)
static LogSenderPtr createLogSender()
Definition LogSender.cpp:82
std::basic_ostream< char, std::char_traits< char > > CoutType
Definition LogSender.h:81
void(LogSender::* manipulator)()
Definition LogSender.h:78
void log(MessageTypeT severity, std::string message)
Sends a message to the logging component.
static void SetComponentName(const std::string &componentName)
void flush()
Sends the current message to the log and resets the content.
static void setProxy(const std::string &componentName, LogPrx logProxy)
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::Log > LogPrx
Definition LogSender.h:58
std::shared_ptr< SpamFilterData > SpamFilterDataPtr
Definition Logging.h:227
const LogSender::manipulator flush
Definition LogSender.h:251
std::shared_ptr< LogSender > LogSenderPtr
Typedef of std::shared_ptr for convenience.
Definition Logging.h:224
MessageTypeT
Definition LogSender.h:46