C/C++ User's Journal STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - where the Standard Template Library meets the Active Template Library COMSTL - where the Standard Template Library meets the Component Object Model
Synesis Software InetSTL - where the Standard Template Library meets the Internet UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

H:/freelibs/recls/1.8/src/recls_debug.h

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:        recls_debug.h
00003  *
00004  * Purpose:     Debug tracing.
00005  *
00006  * Created:     30th September 2003
00007  * Updated:     27th September 2007
00008  *
00009  * Home:        http://recls.org/
00010  *
00011  * Copyright (c) 2003-2007, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without 
00015  * modification, are permitted in accordance with the license and warranty
00016  * information described in recls.h (included in this distribution, or available
00017  * from http://recls.org/)
00018  *
00019  * ////////////////////////////////////////////////////////////////////////// */
00020 
00021 
00022 #ifndef RECLS_INCL_H_RECLS_DEBUG
00023 #define RECLS_INCL_H_RECLS_DEBUG
00024 
00025 /* File version */
00026 #ifndef RECLS_DOCUMENTATION_SKIP_SECTION
00027 # define RECLS_VER_H_RECLS_DEBUG_MAJOR      3
00028 # define RECLS_VER_H_RECLS_DEBUG_MINOR      1
00029 # define RECLS_VER_H_RECLS_DEBUG_REVISION   5
00030 # define RECLS_VER_H_RECLS_DEBUG_EDIT       33
00031 #endif /* !RECLS_DOCUMENTATION_SKIP_SECTION */
00032 
00036 /* /////////////////////////////////////////////////////////////////////////////
00037  * Includes
00038  */
00039 
00040 /* recls Header Files */
00041 #include <recls/recls.h>
00042 #include "recls_impl.h"
00043 
00044 #ifndef RECLS_DEBUG_LEVEL
00045 # error RECLS_DEBUG_LEVEL must be defined
00046 #endif /* !RECLS_DEBUG_LEVEL */
00047 
00048 /* STLSoft / platform-specific Header Files */
00049 #if RECLS_DEBUG_LEVEL > 0
00050 # if defined(RECLS_PLATFORM_IS_UNIX)
00051 #  include <stlsoft/error/errno_scope.hpp>
00052 #  if defined(_RECLS_MT)
00053 #   include <unixstl/synch/tss_index.hpp>
00054 #  endif /* _RECLS_MT */
00055 # elif defined(RECLS_PLATFORM_IS_WIN32)
00056 #  include <winstl/error/last_error_scope.hpp>
00057 #   if defined(_RECLS_MT)
00058 #    include <winstl/synch/tss_index.hpp>
00059 #   endif /* _RECLS_MT */
00060 # else /* ? platform */
00061 #  error Unrecognised platform
00062 # endif /* platform */
00063 # include <stdarg.h>
00064 # if defined(RECLS_PLATFORM_IS_UNIX)
00065 #  include <syslog.h>
00066 # endif /* platform */
00067 #endif /* RECLS_DEBUG_LEVEL > 0 */
00068 
00069 /* Standard C Header Files */
00070 #include <stdio.h>
00071 #if RECLS_DEBUG_LEVEL > 0
00072 # include <string.h>
00073 #endif /* RECLS_DEBUG_LEVEL > 0 */
00074 
00075 /* /////////////////////////////////////////////////////////////////////////
00076  * Compiler compatibility
00077  */
00078 
00079 #if defined(STLSOFT_COMPILER_IS_DMC) || \
00080     (   defined(STLSOFT_COMPILER_IS_COMO) && \
00081         defined(_MSC_VER) && \
00082         defined(WIN32)) || \
00083     (   defined(STLSOFT_COMPILER_IS_INTEL) && \
00084         defined(WIN32)) || \
00085     defined(STLSOFT_COMPILER_IS_MSVC)
00086 # define snprintf           _snprintf
00087 # define vsnprintf          _vsnprintf
00088 #endif /* compiler */
00089 
00090 /* /////////////////////////////////////////////////////////////////////////////
00091  * Namespace
00092  */
00093 
00094 #if !defined(RECLS_NO_NAMESPACE)
00095 namespace recls
00096 {
00097 #endif /* !RECLS_NO_NAMESPACE */
00098 
00099 #if RECLS_DEBUG_LEVEL >= 2
00100 # if defined(RECLS_PLATFORM_IS_UNIX)
00101 typedef stlsoft_ns_qual(errno_scope)        error_scope_t;
00102 #  if defined(_RECLS_MT)
00103 unixstl_ns_using(tss_index)
00104 #  endif /* _RECLS_MT */
00105 # elif defined(RECLS_PLATFORM_IS_WIN32)
00106 typedef winstl_ns_qual(last_error_scope)    error_scope_t;
00107 #  if defined(_RECLS_MT)
00108 winstl_ns_using(tss_index)
00109 #  endif /* _RECLS_MT */
00110 # else /* ? platform */
00111 #  error Unrecognised platform
00112 # endif /* platform */
00113 #endif /* RECLS_DEBUG_LEVEL >= 2 */
00114 
00115 /* /////////////////////////////////////////////////////////////////////////////
00116  * debug_printf
00117  */
00118 
00119 #if RECLS_DEBUG_LEVEL >= 1
00120 inline void debug_printf(char const *fmt, ...)
00121 {
00122     va_list args;
00123     char    _sz[2048];
00124 
00125     va_start(args, fmt);
00126 
00127     vsnprintf(_sz, RECLS_NUM_ELEMENTS(_sz), fmt, args);
00128 
00129 # if defined(RECLS_PLATFORM_IS_UNIX)
00130     syslog(LOG_DEBUG, _sz);
00131 # elif defined(RECLS_PLATFORM_IS_WIN32)
00132     OutputDebugStringA(_sz);
00133 # endif /* platform */
00134 
00135     va_end(args);
00136 }
00137 
00138 #else /* ? RECLS_DEBUG_LEVEL */
00139 
00140 # ifdef __cplusplus
00141 inline 
00142 # else /* ? __cplusplus */
00143 static 
00144 # endif /* __cplusplus */
00145        void debug_printf_(char const *fmt, ...)
00146 {
00147     static_cast<void>(fmt);
00148 }
00149 
00150 # define debug_printf                   (0) ? _RECLS_STATIC_CAST(void, 0) : debug_printf_
00151 
00152 #endif /* RECLS_DEBUG_LEVEL */
00153 
00154 #if RECLS_DEBUG_LEVEL >= 2
00155 class function_scope
00156 {
00157 public:
00158     function_scope(char const *fn)
00159     {
00160         error_scope_t    error_scope;
00161 
00162         ::strncpy(m_fn, fn, RECLS_NUM_ELEMENTS(m_fn) - 1);
00163         debug_printf("%*s>> %s()\n", static_cast<int>(post_inc_()), "", m_fn);
00164     }
00165     ~function_scope()
00166     {
00167         error_scope_t    error_scope;
00168 
00169         debug_printf("%*s<< %s()\n", static_cast<int>(pre_dec_()), "", m_fn);
00170     }
00171 
00172 private:
00173     typedef ::stlsoft::sint32_t int32_t;
00174 
00175     union U
00176     {
00177     public:
00178         int32_t i;
00179         void    *pv;
00180     };
00181 
00182 # if defined(_RECLS_MT)
00183 
00184 #  if defined(RECLS_COMPILER_IS_INTEL)
00185 #   pragma warning(push)
00186 #   pragma warning(disable : 171)
00187 #  endif /* RECLS_COMPILER_IS_INTEL */
00188 
00189     static int32_t tls_get_value_()
00190     {
00191         U u;
00192 
00193         u.pv = get_index_().get_value();
00194 
00195         return u.i;
00196     }
00197     static void tls_set_value_(int32_t i)
00198     {
00199         U u;
00200 
00201         u.i = i;
00202 
00203         get_index_().set_value(u.pv);
00204     }
00205 
00206 #  if defined(RECLS_COMPILER_IS_INTEL)
00207 #   pragma warning(pop)
00208 #  endif /* RECLS_COMPILER_IS_INTEL */
00209 
00210 # endif /* _RECLS_MT */
00211 
00212     static int32_t  post_inc_()
00213     {
00214 # if defined(_RECLS_MT)
00215         int32_t i = (int32_t)(tls_get_value_());
00216         int32_t r = i++;
00217 
00218         tls_set_value_(i);
00219 
00220         return r;
00221 # else /* ? _RECLS_MT */
00222         return get_index_()++;
00223 # endif /* _RECLS_MT */
00224     }
00225     static int32_t  pre_dec_()
00226     {
00227 # if defined(_RECLS_MT)
00228         int32_t i = (int32_t)(tls_get_value_());
00229         int32_t r = --i;
00230 
00231         tls_set_value_(i);
00232 
00233         return r;
00234 # else /* ? _RECLS_MT */
00235         return --get_index_();
00236 # endif /* _RECLS_MT */
00237     }
00238 
00239 private:
00240     char                m_fn[1024];
00241 # if defined(_RECLS_MT)
00242     static tss_index &get_index_()
00243     {
00244         static tss_index    s_index;
00245 
00246         return s_index;
00247     }
00248 # else /* ? _RECLS_MT */
00249     static int32_t &get_index_()
00250     {
00251         static int32_t  s_count;
00252 
00253         return s_count;
00254     }
00255 # endif /* _RECLS_MT */
00256 };
00257 
00258 # define function_scope_trace(f)        function_scope  _scope_ ## __LINE__(f)
00259 
00260 #else /* ? RECLS_DEBUG_LEVEL */
00261 
00262 # define function_scope_trace(f)        do { ; } while(0)
00263 
00264 #endif /* RECLS_DEBUG_LEVEL */
00265 
00266 /* /////////////////////////////////////////////////////////////////////////////
00267  * Namespace
00268  */
00269 
00270 #if !defined(RECLS_NO_NAMESPACE)
00271 } /* namespace recls */
00272 #endif /* !RECLS_NO_NAMESPACE */
00273 
00274 /* ////////////////////////////////////////////////////////////////////////// */
00275 
00276 #endif /* !RECLS_INCL_H_RECLS_DEBUG */
00277 
00278 /* ////////////////////////////////////////////////////////////////////////// */

recls Library documentation © Synesis Software Pty Ltd, 2001-2006