ScopedTimer.h
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #ifndef ICL_CORE_LOGGING_SCOPED_TIMER_H_INCLUDED
00024 #define ICL_CORE_LOGGING_SCOPED_TIMER_H_INCLUDED
00025 
00026 #include <boost/preprocessor/cat.hpp>
00027 
00028 #include "icl_core/TimeStamp.h"
00029 #include "icl_core/TimeSpan.h"
00030 #include "icl_core_logging/LogLevel.h"
00031 #include "icl_core_logging/LogStream.h"
00032 #include "icl_core_logging/ThreadStream.h"
00033 #include "icl_core_logging/LoggingMacros_SLOGGING.h"
00034 
00035 namespace icl_core {
00036 namespace logging {
00037 
00043 template <typename TStreamName>
00044 class ScopedTimer
00045 {
00046 public:
00064   ScopedTimer(const std::string& description = "ScopedTimer",
00065               const LogLevel level = eLL_DEBUG,
00066               const std::string& filename = __FILE__,
00067               const std::size_t line = __LINE__,
00068               const std::string& classname = "",
00069               const std::string& objectname = "")
00070     : m_description(description),
00071       m_level(level),
00072       m_filename(filename),
00073       m_line(line),
00074       m_classname(classname),
00075       m_objectname(objectname),
00076       m_start_time(TimeStamp::now()),
00077       m_active(true)
00078   { }
00079 
00081   void print() const
00082   {
00083     ::icl_core::logging::LogStream& stream = TStreamName::instance();
00084     SLOGGING_LOG_FLCO(stream, m_level, m_filename.c_str(), m_line, m_classname.c_str(), m_objectname.c_str(),
00085                       "" << m_description << ": "
00086                       << (TimeStamp::now() - m_start_time).toNSec() << " ns" << endl);
00087   }
00088 
00090   void print(const std::string& extra_description) const
00091   {
00092     ::icl_core::logging::LogStream& stream = TStreamName::instance();
00093     SLOGGING_LOG_FLCO(stream, m_level, m_filename.c_str(), m_line, m_classname.c_str(), m_objectname.c_str(),
00094                       "" << m_description << " (" << extra_description << "): "
00095                       << (TimeStamp::now() - m_start_time).toNSec() << " ns" << endl);
00096   }
00097 
00101   inline void stop()
00102   {
00103     if (m_active)
00104     {
00105       print();
00106     }
00107     m_active = false;
00108   }
00109 
00113   ~ScopedTimer()
00114   {
00115     if (m_active)
00116     {
00117       print();
00118     }
00119   }
00120 
00121 private:
00122   const std::string m_description;
00123   const LogLevel m_level;
00124   const std::string m_filename;
00125   const std::size_t m_line;
00126   const std::string m_classname;
00127   const std::string m_objectname;
00128   const TimeStamp m_start_time;
00129   bool m_active;
00130 };
00131 
00132 }
00133 }
00134 
00135 #define LOGGING_SCOPED_TIMER_VFLCO(streamname, varname, description, level, filename, line, classname, objectname) \
00136   ::icl_core::logging::ScopedTimer<streamname> varname(description, level, filename, line, classname, objectname)
00137 #define LOGGING_SCOPED_TIMER_VCO(streamname, varname, description, level, classname, objectname) \
00138   LOGGING_SCOPED_TIMER_VFLCO(streamname, varname, description, level, __FILE__, __LINE__, classname, objectname)
00139 #define LOGGING_SCOPED_TIMER_VC(streamname, varname, description, level, classname) \
00140   LOGGING_SCOPED_TIMER_VFLCO(streamname, varname, description, level, __FILE__, __LINE__, classname, "")
00141 #define LOGGING_SCOPED_TIMER_V(streamname, varname, description, level) \
00142   LOGGING_SCOPED_TIMER_VFLCO(streamname, varname, description, level, __FILE__, __LINE__, "", "")
00143 
00144 #define LOGGING_SCOPED_TIMER_FLCO(streamname, description, level, filename, line, classname, objectname) \
00145   LOGGING_SCOPED_TIMER_VFLCO(streamname, BOOST_PP_CAT(scoped_timer_, line), description, level, filename, line, classname, objectname)
00146 #define LOGGING_SCOPED_TIMER_CO(streamname, description, level, classname, objectname) \
00147   LOGGING_SCOPED_TIMER_FLCO(streamname, description, level, __FILE__, __LINE__, classname, objectname)
00148 #define LOGGING_SCOPED_TIMER_C(streamname, description, level, classname) \
00149   LOGGING_SCOPED_TIMER_FLCO(streamname, description, level, __FILE__, __LINE__, classname, "")
00150 #define LOGGING_SCOPED_TIMER(streamname, description, level) \
00151   LOGGING_SCOPED_TIMER_FLCO(streamname, description, level, __FILE__, __LINE__, "", "")
00152 
00153 #define LOGGING_SCOPED_TIMER_ERROR(streamname, description)   LOGGING_SCOPED_TIMER(streamname, description, ::icl_core::logging::eLL_ERROR)
00154 #define LOGGING_SCOPED_TIMER_WARNING(streamname, description) LOGGING_SCOPED_TIMER(streamname, description, ::icl_core::logging::eLL_WARNING)
00155 #define LOGGING_SCOPED_TIMER_INFO(streamname, description)    LOGGING_SCOPED_TIMER(streamname, description, ::icl_core::logging::eLL_INFO)
00156 #ifdef _IC_DEBUG_
00157 # define LOGGING_SCOPED_TIMER_DEBUG(streamname, description)   LOGGING_SCOPED_TIMER(streamname, description, ::icl_core::logging::eLL_DEBUG)
00158 # define LOGGING_SCOPED_TIMER_TRACE(streamname, description)   LOGGING_SCOPED_TIMER(streamname, description, ::icl_core::logging::eLL_TRACE)
00159 #else
00160 # define LOGGING_SCOPED_TIMER_DEBUG(streamname, description) (void)0
00161 # define LOGGING_SCOPED_TIMER_TRACE(streamname, description) (void)0
00162 #endif
00163 
00164 #define LOGGING_SCOPED_TIMER_ERROR_C(streamname, description, classname)   LOGGING_SCOPED_TIMER_C(streamname, description, ::icl_core::logging::eLL_ERROR,   classname)
00165 #define LOGGING_SCOPED_TIMER_WARNING_C(streamname, description, classname) LOGGING_SCOPED_TIMER_C(streamname, description, ::icl_core::logging::eLL_WARNING, classname)
00166 #define LOGGING_SCOPED_TIMER_INFO_C(streamname, description, classname)    LOGGING_SCOPED_TIMER_C(streamname, description, ::icl_core::logging::eLL_INFO,    classname)
00167 #ifdef _IC_DEBUG_
00168 # define LOGGING_SCOPED_TIMER_DEBUG_C(streamname, description, classname)   LOGGING_SCOPED_TIMER_C(streamname, description, ::icl_core::logging::eLL_DEBUG,   classname)
00169 # define LOGGING_SCOPED_TIMER_TRACE_C(streamname, description, classname)   LOGGING_SCOPED_TIMER_C(streamname, description, ::icl_core::logging::eLL_TRACE,   classname)
00170 #else
00171 # define LOGGING_SCOPED_TIMER_DEBUG_C(streamname, description, classname) (void)0
00172 # define LOGGING_SCOPED_TIMER_TRACE_C(streamname, description, classname) (void)0
00173 #endif
00174 
00175 #define LOGGING_SCOPED_TIMER_ERROR_CO(streamname, description, classname, objectname)   LOGGING_SCOPED_TIMER_CO(streamname, description, ::icl_core::logging::eLL_ERROR,   classname, objectname)
00176 #define LOGGING_SCOPED_TIMER_WARNING_CO(streamname, description, classname, objectname) LOGGING_SCOPED_TIMER_CO(streamname, description, ::icl_core::logging::eLL_WARNING, classname, objectname)
00177 #define LOGGING_SCOPED_TIMER_INFO_CO(streamname, description, classname, objectname)    LOGGING_SCOPED_TIMER_CO(streamname, description, ::icl_core::logging::eLL_INFO,    classname, objectname)
00178 #ifdef _IC_DEBUG_
00179 # define LOGGING_SCOPED_TIMER_DEBUG_CO(streamname, description, classname, objectname)   LOGGING_SCOPED_TIMER_CO(streamname, description, ::icl_core::logging::eLL_DEBUG,   classname, objectname)
00180 # define LOGGING_SCOPED_TIMER_TRACE_CO(streamname, description, classname, objectname)   LOGGING_SCOPED_TIMER_CO(streamname, description, ::icl_core::logging::eLL_TRACE,   classname, objectname)
00181 #else
00182 # define LOGGING_SCOPED_TIMER_DEBUG_CO(streamname, description, classname, objectname) (void)0
00183 # define LOGGING_SCOPED_TIMER_TRACE_CO(streamname, description, classname, objectname) (void)0
00184 #endif
00185 
00186 #define LOGGING_SCOPED_TIMER_ERROR_V(streamname, varname, description)   LOGGING_SCOPED_TIMER_V(streamname, varname, description, ::icl_core::logging::eLL_ERROR)
00187 #define LOGGING_SCOPED_TIMER_WARNING_V(streamname, varname, description) LOGGING_SCOPED_TIMER_V(streamname, varname, description, ::icl_core::logging::eLL_WARNING)
00188 #define LOGGING_SCOPED_TIMER_INFO_V(streamname, varname, description)    LOGGING_SCOPED_TIMER_V(streamname, varname, description, ::icl_core::logging::eLL_INFO)
00189 #ifdef _IC_DEBUG_
00190 # define LOGGING_SCOPED_TIMER_DEBUG_V(streamname, varname, description)   LOGGING_SCOPED_TIMER_V(streamname, varname, description, ::icl_core::logging::eLL_DEBUG)
00191 # define LOGGING_SCOPED_TIMER_TRACE_V(streamname, varname, description)   LOGGING_SCOPED_TIMER_V(streamname, varname, description, ::icl_core::logging::eLL_TRACE)
00192 #else
00193 # define LOGGING_SCOPED_TIMER_DEBUG_V(streamname, varname, description) (void)0
00194 # define LOGGING_SCOPED_TIMER_TRACE_V(streamname, varname, description) (void)0
00195 #endif
00196 
00197 #define LOGGING_SCOPED_TIMER_ERROR_VC(streamname, varname, description, classname)   LOGGING_SCOPED_TIMER_VC(streamname, varname, description, ::icl_core::logging::eLL_ERROR,   classname)
00198 #define LOGGING_SCOPED_TIMER_WARNING_VC(streamname, varname, description, classname) LOGGING_SCOPED_TIMER_VC(streamname, varname, description, ::icl_core::logging::eLL_WARNING, classname)
00199 #define LOGGING_SCOPED_TIMER_INFO_VC(streamname, varname, description, classname)    LOGGING_SCOPED_TIMER_VC(streamname, varname, description, ::icl_core::logging::eLL_INFO,    classname)
00200 #ifdef _IC_DEBUG_
00201 # define LOGGING_SCOPED_TIMER_DEBUG_VC(streamname, varname, description, classname)   LOGGING_SCOPED_TIMER_VC(streamname, varname, description, ::icl_core::logging::eLL_DEBUG,   classname)
00202 # define LOGGING_SCOPED_TIMER_TRACE_VC(streamname, varname, description, classname)   LOGGING_SCOPED_TIMER_VC(streamname, varname, description, ::icl_core::logging::eLL_TRACE,   classname)
00203 #else
00204 # define LOGGING_SCOPED_TIMER_DEBUG_VC(streamname, varname, description, classname) (void)0
00205 # define LOGGING_SCOPED_TIMER_TRACE_VC(streamname, varname, description, classname) (void)0
00206 #endif
00207 
00208 #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)
00209 #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)
00210 #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)
00211 #ifdef _IC_DEBUG_
00212 # 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)
00213 # 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)
00214 #else
00215 # define LOGGING_SCOPED_TIMER_DEBUG_VCO(streamname, varname, description, classname, objectname) (void)0
00216 # define LOGGING_SCOPED_TIMER_TRACE_VCO(streamname, varname, description, classname, objectname) (void)0
00217 #endif
00218 
00219 #endif


fzi_icl_core
Author(s):
autogenerated on Thu Jun 6 2019 20:22:24