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 
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 
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 
115  template<typename T>
117  {
118  currentMessage << message;
119  return *this;
120  }
121 
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)
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);
159  MessageTypeT getSeverity();
160 
167  LogSenderPtr setFile(const std::string& file);
168 
175  LogSenderPtr setLine(int line);
176 
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 
202  static MessageTypeT StringToLevel(const std::string& typeStr);
203 
210  static void SetGlobalMinimumLoggingLevel(MessageTypeT level);
211  static MessageTypeT GetGlobalMinimumLoggingLevel();
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:
236  void log(MessageTypeT severity, std::string message);
237  private:
238  void initConsole();
239 
243  void resetLocation();
244 
245  std::stringstream currentMessage;
246 
247  struct Impl;
248  std::unique_ptr<Impl> impl;
249  };
250 
252 
259  template<>
260  ARMARXCORE_IMPORT_EXPORT LogSender& LogSender::operator<< <MessageTypeT>(const MessageTypeT& severity);
261 
268  template<>
269  ARMARXCORE_IMPORT_EXPORT LogSender& LogSender::operator<< <LogTag>(const LogTag& tag);
270 
277  template<>
278  ARMARXCORE_IMPORT_EXPORT LogSender& LogSender::operator<< <LogSender::manipulator>(const manipulator& manipulator);
279 
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
timestamp
std::string timestamp()
Definition: CartographerAdapter.cpp:85
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:455
armarx::setProxy
void setProxy(ManagedIceObject *object, T &proxy, std::string const &name)
Definition: SemanticRelationViewerWidgetController.cpp:148
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