ScopedTimer.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #ifndef ICL_CORE_LOGGING_SCOPED_TIMER_H_INCLUDED
24 #define ICL_CORE_LOGGING_SCOPED_TIMER_H_INCLUDED
25 
26 #include <boost/preprocessor/cat.hpp>
27 
28 #include "icl_core/TimeStamp.h"
29 #include "icl_core/TimeSpan.h"
34 
35 namespace icl_core {
36 namespace logging {
37 
43 template <typename TStreamName>
45 {
46 public:
64  ScopedTimer(const std::string& description = "ScopedTimer",
65  const LogLevel level = eLL_DEBUG,
66  const std::string& filename = __FILE__,
67  const std::size_t line = __LINE__,
68  const std::string& classname = "",
69  const std::string& objectname = "")
70  : m_description(description),
71  m_level(level),
72  m_filename(filename),
73  m_line(line),
74  m_classname(classname),
75  m_objectname(objectname),
76  m_start_time(TimeStamp::now()),
77  m_active(true)
78  { }
79 
81  void print() const
82  {
83  ::icl_core::logging::LogStream& stream = TStreamName::instance();
84  SLOGGING_LOG_FLCO(stream, m_level, m_filename.c_str(), m_line, m_classname.c_str(), m_objectname.c_str(),
85  "" << m_description << ": "
86  << (TimeStamp::now() - m_start_time).toNSec() << " ns" << endl);
87  }
88 
90  void print(const std::string& extra_description) const
91  {
92  ::icl_core::logging::LogStream& stream = TStreamName::instance();
93  SLOGGING_LOG_FLCO(stream, m_level, m_filename.c_str(), m_line, m_classname.c_str(), m_objectname.c_str(),
94  "" << m_description << " (" << extra_description << "): "
95  << (TimeStamp::now() - m_start_time).toNSec() << " ns" << endl);
96  }
97 
101  inline void stop()
102  {
103  if (m_active)
104  {
105  print();
106  }
107  m_active = false;
108  }
109 
114  {
115  if (m_active)
116  {
117  print();
118  }
119  }
120 
121 private:
122  const std::string m_description;
124  const std::string m_filename;
125  const std::size_t m_line;
126  const std::string m_classname;
127  const std::string m_objectname;
129  bool m_active;
130 };
131 
132 }
133 }
134 
135 #define LOGGING_SCOPED_TIMER_VFLCO(streamname, varname, description, level, filename, line, classname, objectname) \
136  ::icl_core::logging::ScopedTimer<streamname> varname(description, level, filename, line, classname, objectname)
137 #define LOGGING_SCOPED_TIMER_VCO(streamname, varname, description, level, classname, objectname) \
138  LOGGING_SCOPED_TIMER_VFLCO(streamname, varname, description, level, __FILE__, __LINE__, classname, objectname)
139 #define LOGGING_SCOPED_TIMER_VC(streamname, varname, description, level, classname) \
140  LOGGING_SCOPED_TIMER_VFLCO(streamname, varname, description, level, __FILE__, __LINE__, classname, "")
141 #define LOGGING_SCOPED_TIMER_V(streamname, varname, description, level) \
142  LOGGING_SCOPED_TIMER_VFLCO(streamname, varname, description, level, __FILE__, __LINE__, "", "")
143 
144 #define LOGGING_SCOPED_TIMER_FLCO(streamname, description, level, filename, line, classname, objectname) \
145  LOGGING_SCOPED_TIMER_VFLCO(streamname, BOOST_PP_CAT(scoped_timer_, line), description, level, filename, line, classname, objectname)
146 #define LOGGING_SCOPED_TIMER_CO(streamname, description, level, classname, objectname) \
147  LOGGING_SCOPED_TIMER_FLCO(streamname, description, level, __FILE__, __LINE__, classname, objectname)
148 #define LOGGING_SCOPED_TIMER_C(streamname, description, level, classname) \
149  LOGGING_SCOPED_TIMER_FLCO(streamname, description, level, __FILE__, __LINE__, classname, "")
150 #define LOGGING_SCOPED_TIMER(streamname, description, level) \
151  LOGGING_SCOPED_TIMER_FLCO(streamname, description, level, __FILE__, __LINE__, "", "")
152 
153 #define LOGGING_SCOPED_TIMER_ERROR(streamname, description) LOGGING_SCOPED_TIMER(streamname, description, ::icl_core::logging::eLL_ERROR)
154 #define LOGGING_SCOPED_TIMER_WARNING(streamname, description) LOGGING_SCOPED_TIMER(streamname, description, ::icl_core::logging::eLL_WARNING)
155 #define LOGGING_SCOPED_TIMER_INFO(streamname, description) LOGGING_SCOPED_TIMER(streamname, description, ::icl_core::logging::eLL_INFO)
156 #ifdef _IC_DEBUG_
157 # define LOGGING_SCOPED_TIMER_DEBUG(streamname, description) LOGGING_SCOPED_TIMER(streamname, description, ::icl_core::logging::eLL_DEBUG)
158 # define LOGGING_SCOPED_TIMER_TRACE(streamname, description) LOGGING_SCOPED_TIMER(streamname, description, ::icl_core::logging::eLL_TRACE)
159 #else
160 # define LOGGING_SCOPED_TIMER_DEBUG(streamname, description) (void)0
161 # define LOGGING_SCOPED_TIMER_TRACE(streamname, description) (void)0
162 #endif
163 
164 #define LOGGING_SCOPED_TIMER_ERROR_C(streamname, description, classname) LOGGING_SCOPED_TIMER_C(streamname, description, ::icl_core::logging::eLL_ERROR, classname)
165 #define LOGGING_SCOPED_TIMER_WARNING_C(streamname, description, classname) LOGGING_SCOPED_TIMER_C(streamname, description, ::icl_core::logging::eLL_WARNING, classname)
166 #define LOGGING_SCOPED_TIMER_INFO_C(streamname, description, classname) LOGGING_SCOPED_TIMER_C(streamname, description, ::icl_core::logging::eLL_INFO, classname)
167 #ifdef _IC_DEBUG_
168 # define LOGGING_SCOPED_TIMER_DEBUG_C(streamname, description, classname) LOGGING_SCOPED_TIMER_C(streamname, description, ::icl_core::logging::eLL_DEBUG, classname)
169 # define LOGGING_SCOPED_TIMER_TRACE_C(streamname, description, classname) LOGGING_SCOPED_TIMER_C(streamname, description, ::icl_core::logging::eLL_TRACE, classname)
170 #else
171 # define LOGGING_SCOPED_TIMER_DEBUG_C(streamname, description, classname) (void)0
172 # define LOGGING_SCOPED_TIMER_TRACE_C(streamname, description, classname) (void)0
173 #endif
174 
175 #define LOGGING_SCOPED_TIMER_ERROR_CO(streamname, description, classname, objectname) LOGGING_SCOPED_TIMER_CO(streamname, description, ::icl_core::logging::eLL_ERROR, classname, objectname)
176 #define LOGGING_SCOPED_TIMER_WARNING_CO(streamname, description, classname, objectname) LOGGING_SCOPED_TIMER_CO(streamname, description, ::icl_core::logging::eLL_WARNING, classname, objectname)
177 #define LOGGING_SCOPED_TIMER_INFO_CO(streamname, description, classname, objectname) LOGGING_SCOPED_TIMER_CO(streamname, description, ::icl_core::logging::eLL_INFO, classname, objectname)
178 #ifdef _IC_DEBUG_
179 # define LOGGING_SCOPED_TIMER_DEBUG_CO(streamname, description, classname, objectname) LOGGING_SCOPED_TIMER_CO(streamname, description, ::icl_core::logging::eLL_DEBUG, classname, objectname)
180 # define LOGGING_SCOPED_TIMER_TRACE_CO(streamname, description, classname, objectname) LOGGING_SCOPED_TIMER_CO(streamname, description, ::icl_core::logging::eLL_TRACE, classname, objectname)
181 #else
182 # define LOGGING_SCOPED_TIMER_DEBUG_CO(streamname, description, classname, objectname) (void)0
183 # define LOGGING_SCOPED_TIMER_TRACE_CO(streamname, description, classname, objectname) (void)0
184 #endif
185 
186 #define LOGGING_SCOPED_TIMER_ERROR_V(streamname, varname, description) LOGGING_SCOPED_TIMER_V(streamname, varname, description, ::icl_core::logging::eLL_ERROR)
187 #define LOGGING_SCOPED_TIMER_WARNING_V(streamname, varname, description) LOGGING_SCOPED_TIMER_V(streamname, varname, description, ::icl_core::logging::eLL_WARNING)
188 #define LOGGING_SCOPED_TIMER_INFO_V(streamname, varname, description) LOGGING_SCOPED_TIMER_V(streamname, varname, description, ::icl_core::logging::eLL_INFO)
189 #ifdef _IC_DEBUG_
190 # define LOGGING_SCOPED_TIMER_DEBUG_V(streamname, varname, description) LOGGING_SCOPED_TIMER_V(streamname, varname, description, ::icl_core::logging::eLL_DEBUG)
191 # define LOGGING_SCOPED_TIMER_TRACE_V(streamname, varname, description) LOGGING_SCOPED_TIMER_V(streamname, varname, description, ::icl_core::logging::eLL_TRACE)
192 #else
193 # define LOGGING_SCOPED_TIMER_DEBUG_V(streamname, varname, description) (void)0
194 # define LOGGING_SCOPED_TIMER_TRACE_V(streamname, varname, description) (void)0
195 #endif
196 
197 #define LOGGING_SCOPED_TIMER_ERROR_VC(streamname, varname, description, classname) LOGGING_SCOPED_TIMER_VC(streamname, varname, description, ::icl_core::logging::eLL_ERROR, classname)
198 #define LOGGING_SCOPED_TIMER_WARNING_VC(streamname, varname, description, classname) LOGGING_SCOPED_TIMER_VC(streamname, varname, description, ::icl_core::logging::eLL_WARNING, classname)
199 #define LOGGING_SCOPED_TIMER_INFO_VC(streamname, varname, description, classname) LOGGING_SCOPED_TIMER_VC(streamname, varname, description, ::icl_core::logging::eLL_INFO, classname)
200 #ifdef _IC_DEBUG_
201 # define LOGGING_SCOPED_TIMER_DEBUG_VC(streamname, varname, description, classname) LOGGING_SCOPED_TIMER_VC(streamname, varname, description, ::icl_core::logging::eLL_DEBUG, classname)
202 # define LOGGING_SCOPED_TIMER_TRACE_VC(streamname, varname, description, classname) LOGGING_SCOPED_TIMER_VC(streamname, varname, description, ::icl_core::logging::eLL_TRACE, classname)
203 #else
204 # define LOGGING_SCOPED_TIMER_DEBUG_VC(streamname, varname, description, classname) (void)0
205 # define LOGGING_SCOPED_TIMER_TRACE_VC(streamname, varname, description, classname) (void)0
206 #endif
207 
208 #define LOGGING_SCOPED_TIMER_ERROR_VCO(streamname, varname, description, classname, objectname) LOGGING_SCOPED_TIMER_VCO(streamname, varname, description, ::icl_core::logging::eLL_ERROR, classname, objectname)
209 #define LOGGING_SCOPED_TIMER_WARNING_VCO(streamname, varname, description, classname, objectname) LOGGING_SCOPED_TIMER_VCO(streamname, varname, description, ::icl_core::logging::eLL_WARNING, classname, objectname)
210 #define LOGGING_SCOPED_TIMER_INFO_VCO(streamname, varname, description, classname, objectname) LOGGING_SCOPED_TIMER_VCO(streamname, varname, description, ::icl_core::logging::eLL_INFO, classname, objectname)
211 #ifdef _IC_DEBUG_
212 # define LOGGING_SCOPED_TIMER_DEBUG_VCO(streamname, varname, description, classname, objectname) LOGGING_SCOPED_TIMER_VCO(streamname, varname, description, ::icl_core::logging::eLL_DEBUG, classname, objectname)
213 # define LOGGING_SCOPED_TIMER_TRACE_VCO(streamname, varname, description, classname, objectname) LOGGING_SCOPED_TIMER_VCO(streamname, varname, description, ::icl_core::logging::eLL_TRACE, classname, objectname)
214 #else
215 # define LOGGING_SCOPED_TIMER_DEBUG_VCO(streamname, varname, description, classname, objectname) (void)0
216 # define LOGGING_SCOPED_TIMER_TRACE_VCO(streamname, varname, description, classname, objectname) (void)0
217 #endif
218 
219 #endif
Represents absolute times.
Definition: TimeStamp.h:61
void print(const std::string &extra_description) const
Outputs the time passed since construction.
Definition: ScopedTimer.h:90
ScopedTimer(const std::string &description="ScopedTimer", const LogLevel level=eLL_DEBUG, const std::string &filename=__FILE__, const std::size_t line=__LINE__, const std::string &classname="", const std::string &objectname="")
Definition: ScopedTimer.h:64
static TimeStamp now()
Definition: TimeStamp.cpp:111
Contains icl_logging::ThreadStream.
Defines SLOGGING logging macros.
const std::string m_objectname
Definition: ScopedTimer.h:127
Implements a thread-safe logging framework.
Definition: LogStream.h:54
ThreadStream & endl(ThreadStream &stream)
Definition: ThreadStream.h:249
Contains icl_logging::LogLevel.
const std::string m_filename
Definition: ScopedTimer.h:124
const std::string m_description
Definition: ScopedTimer.h:122
Contains icl_logging::LogStream.
Contains TimeStamp.
void print() const
Outputs the time passed since construction.
Definition: ScopedTimer.h:81
#define SLOGGING_LOG_FLCO(stream, level, filename, line, classname, objectname, arg)
const std::string m_classname
Definition: ScopedTimer.h:126


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58