trace_point.h

Go to the documentation of this file.
00001 /* libthrowable - a C++ library to ease error handling and debugging
00002  *
00003  * Copyright (C) Matthias Langer 2006, 2008 <mlangc@gmx.at>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public
00016  * License along with this library; if not, write to the Free
00017  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  */
00019 
00020 #ifndef H_LIBTHROWABLE_TRACER
00021 #define H_LIBTHROWABLE_TRACER
00022 #include "libthrowable/util/util.h"
00023 #include <ostream>
00024 #include <stack>
00025 namespace libthrowable {
00038     class LIBTHROWABLE_API TracePoint
00039     {
00040         public:
00044         TracePoint(const char *filename, int linenumber, const char *function) throw();
00045 
00049         TracePoint(const char *filename, int linenumber, const char *function, const std::string& message) throw();
00050 
00066         template<class OStreamableT> TracePoint(const char *filename, int linenumber, const char *function, 
00067                                                  const OStreamableT& t, const std::string& name) throw()
00068         : _function(filename, function)
00069         { 
00070             std::string output;
00071             genVarOutput(output, linenumber, util::stringify(t), name);
00072             getOstream() << optimizeForLineWidth(output) << std::endl;
00073         }
00074 
00075         ~TracePoint() throw();
00076             
00092         static void setLineWidth(int width) throw();
00093 
00099         static void setOstream(std::ostream& os) throw();
00100 
00116         static void setThreadName(const std::string& name) throw();
00117 
00118         private:
00119         //no copy, no assignement
00120         TracePoint(const TracePoint& other);
00121         TracePoint& operator=(const TracePoint& other);
00122 
00123         struct Function
00124         {
00125             Function(const char *tFile, const char *tName) throw()
00126             : file(tFile), name(tName) {}
00127 
00128             bool operator==(const Function& other) const throw()
00129             { return (name == other.name && file == other.file); }
00130 
00131             bool operator!=(const Function& other) const throw()
00132             { return !(*this == other); }
00133 
00134             const std::string file;
00135             const std::string name;
00136         };
00137 
00138         //static
00139         static std::ostream& getOstream(std::ostream *p_os = NULL) throw();
00140         static std::stack<Function>& getFunctionStack() throw();
00141 
00142         static unsigned& getLevel() throw();
00143         static std::string optimizeForLineWidth(const std::string& str) throw();
00145 
00146         void genBasicOutput(std::string& toWriteIn, int linenumber) const throw();
00147         void genVarOutput(std::string& toWriteIn, int linenumber, const std::string& varStr, const std::string& varName) const throw();
00148 
00149         //pushes _function on the stack and
00150         //  increases _s_level if we have just entered _function and returns true.
00151         //  returns false otherwise
00152         bool fStackPush() const throw();
00153 
00154         //pops from the stack and
00155         //  decreases _s_level if we just left _function
00156         void fStackPop() const throw();
00157 
00158         const Function _function;
00159 
00160     };
00161 }//namespace
00162 #endif

Generated on Wed Jan 9 18:07:17 2008 for libthrowable by  doxygen 1.5.4  Hosted on  SourceForge.net Logo