TraceObject.cc

00001 /*
00002  * $Id: TraceObject.cc,v 1.1 2006/04/03 15:11:21 brook Exp $
00003  */
00004 
00005 /*
00006  * ClearSilver++ Software License.
00007  *
00008  * Copyright (c) 2006 Brook Milligan <brook@nmsu.edu>
00009  * All rights reserved.
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 
00015  * 1. Redistributions of source code must retain the above copyright
00016  *    notice, this list of conditions and the following disclaimer.
00017  * 2. Redistributions in binary form must reproduce the above
00018  *    copyright notice, this list of conditions and the following
00019  *    disclaimer in the documentation and/or other materials provided
00020  *    with the distribution.
00021  * 3. The name of the author may not be used to endorse or promote
00022  *    products derived from this software without specific prior
00023  *    written permission.
00024  * 
00025  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00026  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00027  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00029  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00031  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00033  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036  */
00037 
00038 #include "TraceObject.h"
00039 #include <iostream>
00040 #include <syslog.h>
00041 
00042 using namespace std;
00043 
00044 namespace ClearSilver
00045 {
00046 
00047   static void create (const string&, const string&, const void*);
00048   static void copy (const string&, const void*, const void*);
00049   static void destroy (const string&, const void*);
00050   static void assign (const string&, const void*, const void*);
00051   static void swap (const string&, const void*, const void*);
00052 
00053   bool&
00054   TraceObject::log_messages()
00055   {
00056     static bool flag_ = true;
00057     return flag_;
00058   }
00059 
00060   bool&
00061   TraceObject::stream_messages()
00062   {
00063     static bool flag_ = false;
00064     return flag_;
00065   }
00066 
00067 #if HAVE_OBJECT_TRACING
00068 
00069   // constructors
00070   TraceObject::TraceObject (bool debug, const string& name)
00071     : debug_(debug), name_(name)
00072   {
00073     if (debug_)
00074       create(name_, string(), this);
00075   }
00076 
00077   TraceObject::TraceObject (bool debug, const string& name, const string& args)
00078     : debug_(debug), name_(name)
00079   {
00080     if (debug_)
00081       create(name_, args, this);
00082   }
00083 
00084   TraceObject::TraceObject (const TraceObject& m)
00085     : debug_(m.debug_), name_(m.name_)
00086   {
00087     if (debug_)
00088       copy(name_, this, &m);
00089   }
00090 
00091   TraceObject::~TraceObject () throw()
00092   {
00093     if (debug_)
00094       destroy (name_, this);
00095   }
00096 
00097   TraceObject::TraceObject&
00098   TraceObject::operator = (const TraceObject& m)
00099   {
00100     if (debug_)
00101       assign (name_, this, &m);
00102     string name (name_);
00103     debug_ = m.debug_;
00104     name_.swap (name);
00105     return *this;
00106   }
00107 
00108   void
00109   TraceObject::swap (TraceObject& m) throw()
00110   {
00111     if (debug_)
00112       ClearSilver::swap (name_, this, &m);
00113     std::swap (debug_, m.debug_);
00114     std::swap (name_, m.name_);
00115   }
00116 
00117 
00118   void
00119   create (const string& name, const string& args, const void* p)
00120   {
00121     if (ClearSilver::TraceObject::log_messages())
00122       syslog (LOG_DEBUG, "%s::%s(%s): this=0x%x",
00123               name.c_str(), name.c_str(), args.c_str(), p);
00124     if (ClearSilver::TraceObject::stream_messages())
00125       cerr
00126         << name << "::" << name << "(" << args << "): "
00127         << "this=" << hex << p << dec << endl;
00128   }  
00129 
00130   void
00131   copy (const string& name, const void* dest, const void* src)
00132   {
00133     if (ClearSilver::TraceObject::log_messages())
00134       syslog (LOG_DEBUG, "%s::%s(const %s&): this=0x%x, %s=0x%x",
00135               name.c_str(), name.c_str(), name.c_str(),
00136               dest, name.c_str(), src);
00137     if (ClearSilver::TraceObject::stream_messages())
00138       cerr
00139         << name << "::" << name << "(const " << name << "&): "
00140         << "this=" << hex << dest << ", " << name << "=" << src << dec << endl;
00141   }  
00142 
00143   void
00144   destroy (const string& name, const void* p)
00145   {
00146     if (ClearSilver::TraceObject::log_messages())
00147       syslog (LOG_DEBUG, "%s::~%s(): this=0x%x",
00148               name.c_str(), name.c_str(), p);
00149     if (ClearSilver::TraceObject::stream_messages())
00150       cerr
00151         << name << "::~" << name << "(): "
00152         << "this=" << hex << p << dec << endl;
00153   }  
00154 
00155   void
00156   assign (const string& name, const void* dest, const void* src)
00157   {
00158     if (ClearSilver::TraceObject::log_messages())
00159       syslog (LOG_DEBUG, "%s::%s& %s::operator = (const %s&): "
00160               "this=0x%x, %s=0x%x",
00161               name.c_str(), name.c_str(), name.c_str(), name.c_str(),
00162               dest, name.c_str(), src);
00163     if (ClearSilver::TraceObject::stream_messages())
00164       cerr
00165         << name << "::" << name << "& "
00166         << name << "::operator = (const " << name << "&): "
00167         << "this=" << hex << dest << ", " << name << "=" << src << dec << endl;
00168   }  
00169 
00170   void
00171   swap (const string& name, const void* dest, const void* src)
00172   {
00173     if (ClearSilver::TraceObject::log_messages())
00174       syslog (LOG_DEBUG, "%s::swap(%s&): this=0x%x, %s=0x%x",
00175               name.c_str(), name.c_str(), dest, name.c_str(), src);
00176     if (ClearSilver::TraceObject::stream_messages())
00177       cerr
00178         << name << "::swap(" << name << "&): "
00179         << "this=" << hex << dest << ", " << name << "=" << src << dec << endl;
00180   }  
00181 
00182 #endif // HAVE_OBJECT_TRACING
00183 
00184 } // namespace ClearSilver

Generated on Tue May 16 14:50:52 2006 for ClearSilver C++ Library by  doxygen 1.4.5