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 "LoggingUtil.h"
29 
31 
32 #include <Ice/ProxyHandle.h>
33 #include <IceUtil/Time.h>
34 
35 #include <memory>
36 #include <chrono>
37 
39 {
40  class Log;
41 }
42 
43 namespace armarx
44 {
45  enum class MessageTypeT
46  {
47  UNDEFINED,
48  DEBUG,
49  VERBOSE,
50  INFO,
51  IMPORTANT,
52  WARN,
53  ERROR,
54  FATAL,
56  };
57 
58  typedef ::IceInternal::ProxyHandle< ::IceProxy::armarx::Log> LogPrx;
59 
60 
61  /**
62  \class LogSender
63  \brief Wrapper for the Log IceStorm topic with convenience methods for logging.
64  \ingroup Logging
65  */
66  class ARMARXCORE_IMPORT_EXPORT LogSender : public std::enable_shared_from_this<LogSender>
67  {
68  public:
70  {
71  eReset = -1,
72  eBlack = 0,
73  eRed = 1,
74  eGreen = 2,
75  eYellow = 3,
76  eBlue = 4
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 
87 
88 
89 
90  static LogSenderPtr createLogSender();
91 
92  LogSender();
93  ~LogSender();
94 
95  /**
96  * Constructor taking a LogPrx pointer to the IceStorm topic.
97  *
98  * \param componentName All log messages sent through this instance
99  * will show as having originated from this
100  * component.
101  * \param logProxy Ice proxy to send messages to.
102  */
103  LogSender(const std::string& componentName, LogPrx logProxy);
104 
105  static void setProxy(const std::string& componentName, LogPrx logProxy);
106  static void SetComponentName(const std::string& componentName);
107  static void SetLoggingGroup(const std::string& loggingGroup);
108 
109  /**
110  Appends a variable to the current message stringstream
111 
112  \param message
113  \return This object for further streaming
114  */
115  template<typename T>
117  {
118  currentMessage << message;
119  return *this;
120  }
121 
122  /**
123  * @brief operator << overload for IceUtil::Time
124  * @param timestamp IceUtil::Time
125  * @return
126  */
127  LogSender& operator<<(const IceUtil::Time& timestamp);
128 
129  // TODO: C++20 offers operator<< for std::chrono::duration.
130  // See: https://en.cppreference.com/w/cpp/chrono/duration/operator_ltlt
131  // The operator<<'s for timestamps can then be removed
132 
133  LogSender& operator<<(const std::chrono::minutes& timestamp);
134 
135  LogSender& operator<<(const std::chrono::seconds& timestamp);
136 
137  LogSender& operator<<(const std::chrono::milliseconds& timestamp);
138 
139  LogSender& operator<<(const std::chrono::microseconds& timestamp);
140 
141  LogSender& operator<<(const std::chrono::nanoseconds& timestamp);
142 
143 
144  LogSender& operator<< (const StandardEndLine& 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 
229  protected:
230  /**
231  * Sends a message to the logging component.
232  *
233  * \param severity eVERBOSE / eINFO / eWARN / eERROR
234  * \param message
235  */
236  void log(MessageTypeT severity, std::string message);
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<>
260  ARMARXCORE_IMPORT_EXPORT LogSender& LogSender::operator<< <MessageTypeT>(const MessageTypeT& severity);
261 
262  /**
263  * Changes the current tag for streamed messages
264  *
265  * \param tag
266  * \return This object for further streaming
267  */
268  template<>
269  ARMARXCORE_IMPORT_EXPORT LogSender& LogSender::operator<< <LogTag>(const LogTag& tag);
270 
271  /**
272  * Executes a manipulator like flush on the stream
273  *
274  * \param manipulator
275  * \return This object for further streaming
276  */
277  template<>
278  ARMARXCORE_IMPORT_EXPORT LogSender& LogSender::operator<< <LogSender::manipulator>(const manipulator& manipulator);
279 
280  /**
281  * Changes the current message color
282  *
283  * \param colorCode Color of the Text
284  * \return This object for further streaming
285  */
286  template<>
287  ARMARXCORE_IMPORT_EXPORT LogSender& LogSender::operator<< <LogSender::ConsoleColor>(const LogSender::ConsoleColor& colorCode);
288 
289  template<>
290  ARMARXCORE_IMPORT_EXPORT LogSender& LogSender::operator<< <bool>(const bool& duality);
291 
292  template<>
293  ARMARXCORE_IMPORT_EXPORT LogSender& LogSender::operator<< <SpamFilterDataPtr>(const SpamFilterDataPtr& spamFilterData);
294 
295 
296 
297 
298 }
299 
armarx::MessageTypeT::ERROR
@ ERROR
armarx::MessageTypeT::LogLevelCount
@ LogLevelCount
armarx::MessageTypeT
MessageTypeT
Definition: LogSender.h:45
armarx::MessageTypeT::WARN
@ WARN
armarx::MessageTypeT::FATAL
@ FATAL
armarx::LogSender::CoutType
std::basic_ostream< char, std::char_traits< char > > CoutType
Definition: LogSender.h:81
IceProxy::armarx
Definition: LogSender.h:38
armarx::MessageTypeT::UNDEFINED
@ UNDEFINED
armarx::MessageTypeT::INFO
@ INFO
armarx::LogPrx
::IceInternal::ProxyHandle< ::IceProxy::armarx::Log > LogPrx
Definition: LogSender.h:58
armarx::MessageTypeT::VERBOSE
@ VERBOSE
armarx::LogSender
Wrapper for the Log IceStorm topic with convenience methods for logging.
Definition: LogSender.h:66
armarx::MessageTypeT::IMPORTANT
@ IMPORTANT
message
message(STATUS "Boost-Library-Dir: " "${Boost_LIBRARY_DIRS}") message(STATUS "Boost-LIBRARIES
Definition: CMakeLists.txt:8
armarx::LogSender::operator<<
LogSender & operator<<(const T &message)
Appends a variable to the current message stringstream.
Definition: LogSender.h:116
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
armarx::SpamFilterDataPtr
std::shared_ptr< SpamFilterData > SpamFilterDataPtr
Definition: Logging.h:220
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::GetTypeString
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
Definition: GetTypeString.h:36
LoggingUtil.h
armarx::LogSenderPtr
std::shared_ptr< LogSender > LogSenderPtr
Typedef of std::shared_ptr for convenience.
Definition: Logging.h:217
armarx::LogSender::flush
void flush()
Sends the current message to the log and resets the content.
Definition: LogSender.cpp:462
armarx::setProxy
void setProxy(ManagedIceObject *object, T &proxy, std::string const &name)
Definition: SemanticRelationViewerWidgetController.cpp:148
IceInternal::ProxyHandle< ::IceProxy::armarx::Log >
armarx::operator<<
std::ostream & operator<<(std::ostream &os, const PythonApplicationManager::Paths &paths)
Definition: PythonApplicationManager.cpp:221
armarx::MessageTypeT::DEBUG
@ DEBUG
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
ImportExport.h
armarx::LogSender::manipulator
void(LogSender::* manipulator)()
Definition: LogSender.h:78
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::LogTag
Definition: LoggingUtil.h:66
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::LogSender::ConsoleColor
ConsoleColor
Definition: LogSender.h:69