src/foundation/ZDebug.h

00001 /*  @(#) $Id: ZDebug.h,v 1.5 2006/04/26 22:31:27 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2000 Andrew Green and Learning in Motion, Inc.
00005 http://www.zoolib.org
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 ------------------------------------------------------------ */
00024 
00025 #ifndef __ZDebug__
00026 #define __ZDebug__ 1
00027 #include "zconfig.h"
00028 
00029 enum ZDebug_Action { eDebug_ActionContinue, eDebug_ActionStop };
00030 
00031 typedef void (*ZDebug_HandleInitial_t)(int inLevel, ZDebug_Action inAction, const char* inFilename,
00032         int inLine, const char* inAssertionMessage, const char* inUserMessage);
00033 
00034 typedef void (*ZDebug_HandleActual_t)(int inLevel, ZDebug_Action inAction, const char* inMessage);
00035 
00036 extern ZDebug_HandleInitial_t sDebug_HandleInitial;
00037 extern ZDebug_HandleActual_t sDebug_HandleActual;
00038 
00039 void ZDebug_DisplayMessageSimple(const char* message, ...);
00040 
00041 const char* ZDebug_FormatMessage(const char* iMessage, ...);
00042 
00043 inline const char* ZDebug_FormatMessage() { return nil; }
00044 
00045 // The actual macros that get used in source code. The funkiness of these macros (should)
00046 // generate minimal inline code, so that an assertion that's active at level 2 will
00047 // generate *no code* at level 1 or 0. This does rely on the compiler optimizing away
00048 // statements/expressions with no effect.
00049 #define ZDebugPrintf(a, b) ((a)<=ZCONFIG_Debug ? ZDebug_DisplayMessageSimple b : ((void)0))
00050 
00051 #define ZDebugLogf(a, b) \
00052         ((a) <= ZCONFIG_Debug && sDebug_HandleInitial ? \
00053         sDebug_HandleInitial(a, eDebug_ActionContinue, __FILE__, __LINE__, 0, ZDebug_FormatMessage b) \
00054         : ((void)0))
00055 
00056 #define ZDebugStopf(a, b) \
00057         ((a) <= ZCONFIG_Debug && sDebug_HandleInitial ? \
00058         sDebug_HandleInitial(a, eDebug_ActionStop, __FILE__, __LINE__, 0, ZDebug_FormatMessage b) \
00059         : ((void)0))
00060 
00061 #define ZDebugLog(a) \
00062         ((a) <= ZCONFIG_Debug && sDebug_HandleInitial ? \
00063         sDebug_HandleInitial(a, eDebug_ActionContinue, __FILE__, __LINE__, 0, 0) : ((void)0))
00064 
00065 #define ZDebugStop(a) \
00066         ((a)<=ZCONFIG_Debug && sDebug_HandleInitial ? \
00067         sDebug_HandleInitial(a, eDebug_ActionStop, __FILE__, __LINE__, 0, 0): ((void)0))
00068 
00069 #define ZAssertLogf(a, b, c) \
00070         ((a) <= ZCONFIG_Debug && sDebug_HandleInitial && !(b) ? \
00071         sDebug_HandleInitial(a, eDebug_ActionContinue, __FILE__, __LINE__, #b, \
00072         ZDebug_FormatMessage c), false : true)
00073 
00074 #define ZAssertStopf(a, b, c) \
00075         ((a) <= ZCONFIG_Debug && sDebug_HandleInitial && !(b) ? \
00076         sDebug_HandleInitial(a, eDebug_ActionStop, __FILE__, __LINE__, #b, \
00077         ZDebug_FormatMessage c), false : true)
00078 
00079 #define ZAssertLog(a, b) \
00080         ((a) <= ZCONFIG_Debug && sDebug_HandleInitial && !(b) ? \
00081         sDebug_HandleInitial(a, eDebug_ActionContinue, __FILE__, __LINE__, #b, nil), false : true)
00082 
00083 #define ZAssertStop(a, b) \
00084         ((a)<=ZCONFIG_Debug && sDebug_HandleInitial && !(b) ? \
00085         sDebug_HandleInitial(a, eDebug_ActionStop, __FILE__, __LINE__, #b, nil), false : true)
00086 
00087 
00088 // ZAssertCompile can be used to enforce a constraint at compile time, (for example that a
00089 // struct obeys necessary alignment rules). It either drops out completely or generates an
00090 // error, depending on whether the expression evaulates true or false.
00091 #define ZAssertCompile(a) extern int sCompileTimeAssertionViolated[(a) ? 1 : 0]
00092 
00093 // I'd like to formalize ZUnimplemented a little more sometime. Perhaps it should
00094 // throw an exception in production code.
00095 #define ZUnimplemented() ZDebugStopf(0, ("Unimplemented routine"))
00096 
00097 // There are still quite a lot of places where plain old ZAssert is used.
00098 #define ZAssert(a) ZAssertStop(1, a)
00099 
00100 // =================================================================================================
00101 // These defines could go into a private header, but I'd rather not add yet
00102 // another file. Perhaps later.
00103 
00104 #define ZDebug_Message_AssertionAndUser "Assertion failed: %s. %s, %s:%d"
00105 #define ZDebug_Message_AssertionOnly "Assertion failed: %s, %s:%d"
00106 #define ZDebug_Message_UserOnly "%s, %s:%d"
00107 #define ZDebug_Message_None "%s:%d"
00108 
00109 #endif // __ZDebug__

Generated on Thu Jul 26 11:21:51 2007 for ZooLib by  doxygen 1.4.7