TraceSource.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/common/TraceSource.h#20 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-2009 John V. Sichi
00008 //
00009 // This program is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by the Free
00011 // Software Foundation; either version 2 of the License, or (at your option)
00012 // any later version approved by The Eigenbase Project.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 #ifndef Fennel_TraceSource_Included
00025 #define Fennel_TraceSource_Included
00026 
00027 #include <sstream>
00028 #include "fennel/common/TraceTarget.h"
00029 
00030 FENNEL_BEGIN_NAMESPACE
00031 
00036 class FENNEL_COMMON_EXPORT TraceSource
00037 {
00038     SharedTraceTarget pTraceTarget;
00039 
00040     std::string name;
00041 
00042     TraceLevel minimumLevel;
00043 
00044 protected:
00048     explicit TraceSource();
00049 
00059     explicit TraceSource(
00060         SharedTraceTarget pTraceTarget,
00061         std::string name);
00062 
00063 public:
00064     virtual ~TraceSource();
00065 
00073     virtual void initTraceSource(
00074         SharedTraceTarget pTraceTarget, std::string name);
00075 
00083     void trace(TraceLevel level,std::string message) const;
00084 
00088     bool isTracing() const
00089     {
00090         return pTraceTarget.get() ? true : false;
00091     }
00092 
00100     bool isTracingLevel(TraceLevel level) const
00101     {
00102         return level >= minimumLevel;
00103     }
00104 
00108     TraceTarget &getTraceTarget() const
00109     {
00110         assert(isTracing());
00111         return *(pTraceTarget.get());
00112     }
00113 
00117     SharedTraceTarget getSharedTraceTarget() const
00118     {
00119         return pTraceTarget;
00120     }
00121 
00127     std::string getTraceSourceName() const
00128     {
00129         return name;
00130     }
00131 
00136     void setTraceSourceName(std::string const& n)
00137     {
00138         name = n;
00139     }
00140 
00141     TraceLevel getMinimumTraceLevel() const
00142     {
00143         return minimumLevel;
00144     }
00145 
00146     void disableTracing();
00147 };
00148 
00155 #define FENNEL_TRACE(level,msg) FENNEL_DELEGATE_TRACE(level,this,msg)
00156 #define FENNEL_DELEGATE_TRACE(level,tracer,msg) \
00157 do { \
00158     if ((tracer)->isTracingLevel(level)) { \
00159         std::ostringstream oss; \
00160         oss << msg; \
00161         (tracer)->trace(level,oss.str()); \
00162     } \
00163 } while (false)
00164 
00165 // REVIEW jvs 18-Mar-2005:  Why can't we just pass getCurrentThreadId()
00166 // to operator <<?
00167 
00168 // return a string id for the current thread
00169 inline char *get_tid(char *tidstr, int cb)
00170 {
00171     snprintf(tidstr, cb, "%d", getCurrentThreadId());
00172     return tidstr;
00173 }
00174 
00179 #define FENNEL_TRACE_THREAD(level, expr) \
00180 { \
00181     char tidstr[32]; \
00182     FENNEL_TRACE(\
00183         level, \
00184         "[thread " << fennel::get_tid(tidstr,sizeof(tidstr)) << "] " << expr); \
00185 }
00186 
00187 FENNEL_END_NAMESPACE
00188 
00189 #endif
00190 
00191 // End TraceSource.h

Generated on Mon Jun 22 04:00:18 2009 for Fennel by  doxygen 1.5.1