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/current/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:     13th November 2006
00008  *
00009  * Home:        http://recls.org/
00010  *
00011  * Copyright (c) 2003-2006, 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   4
00030 # define RECLS_VER_H_RECLS_DEBUG_EDIT       32
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  * Namespace
00077  */
00078 
00079 #if !defined(RECLS_NO_NAMESPACE)
00080 namespace recls
00081 {
00082 #endif /* !RECLS_NO_NAMESPACE */
00083 
00084 #if RECLS_DEBUG_LEVEL >= 2
00085 # if defined(RECLS_PLATFORM_IS_UNIX)
00086 typedef stlsoft_ns_qual(errno_scope)        error_scope_t;
00087 #  if defined(WIN32) && \
00088       defined(EMULATE_UNIX_ON_WIN32)
00089 #  else /* ? WIN32 */
00090 /* Really UNIX */
00091 inline void OutputDebugStringA(char const *s)
00092 {
00093     static_cast<void>(fprintf(stderr, s));
00094 }
00095 #  endif /* ? WIN32 */
00096 #  if defined(_RECLS_MT)
00097 unixstl_ns_using(tss_index)
00098 #  endif /* _RECLS_MT */
00099 # elif defined(RECLS_PLATFORM_IS_WIN32)
00100 typedef winstl_ns_qual(last_error_scope)    error_scope_t;
00101 #  if defined(_RECLS_MT)
00102 winstl_ns_using(tss_index)
00103 #  endif /* _RECLS_MT */
00104 # else /* ? platform */
00105 #  error Unrecognised platform
00106 # endif /* platform */
00107 #endif /* RECLS_DEBUG_LEVEL >= 2 */
00108 
00109 /* /////////////////////////////////////////////////////////////////////////////
00110  * debug_printf
00111  */
00112 
00113 #if RECLS_DEBUG_LEVEL >= 1
00114 inline void debug_printf(char const *fmt, ...)
00115 {
00116     va_list args;
00117     char    _sz[2048];
00118 
00119     va_start(args, fmt);
00120 
00121 # if defined(RECLS_PLATFORM_IS_UNIX) && \
00122      (   !defined(WIN32) || \
00123          !defined(EMULATE_UNIX_ON_WIN32))
00124     vsnprintf(_sz, RECLS_NUM_ELEMENTS(_sz), fmt, args);
00125 # else /* ? OS */
00126     _vsnprintf(_sz, RECLS_NUM_ELEMENTS(_sz), fmt, args);
00127 # endif /* OS */
00128 # if defined(RECLS_PLATFORM_IS_UNIX)
00129     syslog(LOG_DEBUG, _sz);
00130 # elif defined(RECLS_PLATFORM_IS_WIN32)
00131     OutputDebugStringA(_sz);
00132 # endif /* platform */
00133 
00134     va_end(args);
00135 }
00136 
00137 #else /* ? RECLS_DEBUG_LEVEL */
00138 
00139 # ifdef __cplusplus
00140 inline 
00141 # else /* ? __cplusplus */
00142 static 
00143 # endif /* __cplusplus */
00144        void debug_printf_(char const *fmt, ...)
00145 {
00146     static_cast<void>(fmt);
00147 }
00148 
00149 # define debug_printf                   (0) ? _RECLS_STATIC_CAST(void, 0) : debug_printf_
00150 
00151 #endif /* RECLS_DEBUG_LEVEL */
00152 
00153 #if RECLS_DEBUG_LEVEL >= 2
00154 class function_scope
00155 {
00156 public:
00157     function_scope(char const *fn)
00158     {
00159         error_scope_t    error_scope;
00160 
00161         ::strncpy(m_fn, fn, RECLS_NUM_ELEMENTS(m_fn) - 1);
00162         debug_printf("%*s>> %s()\n", static_cast<int>(post_inc_()), "", m_fn);
00163     }
00164     ~function_scope()
00165     {
00166         error_scope_t    error_scope;
00167 
00168         debug_printf("%*s<< %s()\n", static_cast<int>(pre_dec_()), "", m_fn);
00169     }
00170 
00171 private:
00172     typedef ::stlsoft::sint32_t int32_t;
00173 
00174     union U
00175     {
00176     public:
00177         int32_t i;
00178         void    *pv;
00179     };
00180 
00181 # if defined(_RECLS_MT)
00182 
00183 #  if defined(RECLS_COMPILER_IS_INTEL)
00184 #   pragma warning(push)
00185 #   pragma warning(disable : 171)
00186 #  endif /* RECLS_COMPILER_IS_INTEL */
00187 
00188     static int32_t tls_get_value_()
00189     {
00190         U u;
00191 
00192         u.pv = get_index_().get_value();
00193 
00194         return u.i;
00195     }
00196     static void tls_set_value_(int32_t i)
00197     {
00198         U u;
00199 
00200         u.i = i;
00201 
00202         get_index_().set_value(u.pv);
00203     }
00204 
00205 #  if defined(RECLS_COMPILER_IS_INTEL)
00206 #   pragma warning(pop)
00207 #  endif /* RECLS_COMPILER_IS_INTEL */
00208 
00209 # endif /* _RECLS_MT */
00210 
00211     static int32_t  post_inc_()
00212     {
00213 # if defined(_RECLS_MT)
00214         int32_t i = (int32_t)(tls_get_value_());
00215         int32_t r = i++;
00216 
00217         tls_set_value_(i);
00218 
00219         return r;
00220 # else /* ? _RECLS_MT */
00221         return get_index_()++;
00222 # endif /* _RECLS_MT */
00223     }
00224     static int32_t  pre_dec_()
00225     {
00226 # if defined(_RECLS_MT)
00227         int32_t i = (int32_t)(tls_get_value_());
00228         int32_t r = --i;
00229 
00230         tls_set_value_(i);
00231 
00232         return r;
00233 # else /* ? _RECLS_MT */
00234         return --get_index_();
00235 # endif /* _RECLS_MT */
00236     }
00237 
00238 private:
00239     char                m_fn[1024];
00240 # if defined(_RECLS_MT)
00241     static tss_index &get_index_()
00242     {
00243         static tss_index    s_index;
00244 
00245         return s_index;
00246     }
00247 # else /* ? _RECLS_MT */
00248     static int32_t &get_index_()
00249     {
00250         static int32_t  s_count;
00251 
00252         return s_count;
00253     }
00254 # endif /* _RECLS_MT */
00255 };
00256 
00257 # define function_scope_trace(f)        function_scope  _scope_ ## __LINE__(f)
00258 
00259 #else /* ? RECLS_DEBUG_LEVEL */
00260 
00261 # define function_scope_trace(f)        do { ; } while(0)
00262 
00263 #endif /* RECLS_DEBUG_LEVEL */
00264 
00265 /* /////////////////////////////////////////////////////////////////////////////
00266  * Namespace
00267  */
00268 
00269 #if !defined(RECLS_NO_NAMESPACE)
00270 } /* namespace recls */
00271 #endif /* !RECLS_NO_NAMESPACE */
00272 
00273 /* ////////////////////////////////////////////////////////////////////////// */
00274 
00275 #endif /* !RECLS_INCL_H_RECLS_DEBUG */
00276 
00277 /* ////////////////////////////////////////////////////////////////////////// */

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