2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
|
|
|
Copyright (C) 2005-2007 Warzone Resurrection Project
|
|
|
|
|
|
|
|
Warzone 2100 is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Warzone 2100 is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2006-09-25 09:46:28 -07:00
|
|
|
/*! \file debug.h
|
|
|
|
* \brief Debugging functions
|
|
|
|
*/
|
|
|
|
|
2008-02-01 13:26:37 -08:00
|
|
|
#ifndef __INCLUDED_LIB_FRAMEWORK_DEBUG_H__
|
|
|
|
#define __INCLUDED_LIB_FRAMEWORK_DEBUG_H__
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/* Check the header files have been included from frame.h if they
|
|
|
|
* are used outside of the framework library.
|
|
|
|
*/
|
|
|
|
#if !defined(_frame_h) && !defined(FRAME_LIB_INCLUDE)
|
2007-08-21 05:30:00 -07:00
|
|
|
# error Framework header files MUST be included from Frame.h ONLY.
|
2007-06-28 10:47:08 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <assert.h>
|
2008-05-11 04:51:23 -07:00
|
|
|
#include "lib/script/interp.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-02-01 13:26:37 -08:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
/****************************************************************************************
|
|
|
|
*
|
|
|
|
* Basic debugging macro's
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-11-18 05:19:56 -08:00
|
|
|
#define MAX_EVENT_NAME_LEN 100
|
2007-12-29 14:34:41 -08:00
|
|
|
|
|
|
|
/** Stores name of the last function or event called by scripts. */
|
2006-11-18 05:19:56 -08:00
|
|
|
extern char last_called_script_event[MAX_EVENT_NAME_LEN];
|
2006-11-16 06:30:29 -08:00
|
|
|
|
2007-12-29 14:34:41 -08:00
|
|
|
/**
|
2007-06-28 10:47:08 -07:00
|
|
|
*
|
|
|
|
* Rewritten version of assert that allows a printf format text string to be passed
|
|
|
|
* to ASSERT along with the condition.
|
|
|
|
*
|
2006-08-23 05:58:48 -07:00
|
|
|
* Arguments: ASSERT( condition, "Format string with variables: %d, %d", var1, var2 );
|
2007-06-28 10:47:08 -07:00
|
|
|
*/
|
2006-09-03 13:30:46 -07:00
|
|
|
#define ASSERT( expr, ... ) \
|
2008-05-11 04:51:23 -07:00
|
|
|
( (expr) ? (void)0 : scrOutputCallTrace() ); \
|
2008-04-19 07:52:35 -07:00
|
|
|
( (expr) ? (void)0 : (void)_debug( LOG_ERROR, __FUNCTION__, __VA_ARGS__ ) ); \
|
|
|
|
( (expr) ? (void)0 : (void)_debug( LOG_ERROR, __FUNCTION__, "Assert in Warzone: %s:%d (%s), last script event: '%s'", \
|
|
|
|
__FILE__, __LINE__, (#expr), last_called_script_event ) ); \
|
2006-09-03 13:30:46 -07:00
|
|
|
assert( expr );
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-05-02 08:50:30 -07:00
|
|
|
|
2007-12-29 14:34:41 -08:00
|
|
|
/**
|
2007-05-02 08:50:30 -07:00
|
|
|
* Compile time assert
|
2007-11-03 14:42:55 -07:00
|
|
|
* Not to be used in global context!
|
2007-05-02 08:50:30 -07:00
|
|
|
* \param expr Expression to evaluate
|
|
|
|
*/
|
2007-05-20 07:24:54 -07:00
|
|
|
#define STATIC_ASSERT( expr ) \
|
2007-11-03 14:42:55 -07:00
|
|
|
do { enum { assert_static__ = 1/(expr) }; } while(0)
|
2007-05-02 08:50:30 -07:00
|
|
|
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
/***
|
|
|
|
***
|
|
|
|
*** New debug logging output interface below. Heavily inspired
|
|
|
|
*** by similar code in Freeciv. Parts ripped directly.
|
|
|
|
***
|
|
|
|
***/
|
|
|
|
|
2007-12-29 14:34:41 -08:00
|
|
|
/** Debug enums. Must match code_part_names in debug.c */
|
2006-08-21 14:27:12 -07:00
|
|
|
typedef enum {
|
2007-06-28 10:47:08 -07:00
|
|
|
LOG_ALL, /* special: sets all to on */
|
|
|
|
LOG_MAIN,
|
2006-02-18 10:54:37 -08:00
|
|
|
LOG_SOUND,
|
|
|
|
LOG_VIDEO,
|
2007-06-28 10:47:08 -07:00
|
|
|
LOG_WZ,
|
|
|
|
LOG_3D,
|
|
|
|
LOG_TEXTURE,
|
2006-02-18 10:54:37 -08:00
|
|
|
LOG_NET,
|
2006-08-05 06:16:12 -07:00
|
|
|
LOG_MEMORY,
|
2007-12-29 14:34:41 -08:00
|
|
|
LOG_WARNING, /**< special; on in debug mode */
|
|
|
|
LOG_ERROR, /**< special; on by default */
|
|
|
|
LOG_NEVER, /**< if too verbose for anything but dedicated debugging... */
|
2006-08-12 03:02:59 -07:00
|
|
|
LOG_SCRIPT,
|
2006-12-01 14:22:29 -08:00
|
|
|
LOG_MOVEMENT,
|
|
|
|
LOG_ATTACK,
|
2007-02-24 08:07:44 -08:00
|
|
|
LOG_FOG,
|
2007-08-02 13:23:09 -07:00
|
|
|
LOG_SENSOR,
|
2007-08-03 09:24:17 -07:00
|
|
|
LOG_GUI,
|
2007-10-23 11:18:48 -07:00
|
|
|
LOG_MAP,
|
2008-04-19 07:52:35 -07:00
|
|
|
LOG_SAVE,
|
|
|
|
LOG_SYNC,
|
2008-01-02 09:08:29 -08:00
|
|
|
LOG_DEATH,
|
2008-01-11 14:45:04 -08:00
|
|
|
LOG_GATEWAY,
|
2008-04-19 14:41:18 -07:00
|
|
|
LOG_MSG,
|
2007-12-29 14:34:41 -08:00
|
|
|
LOG_LAST /**< _must_ be last! */
|
2006-08-21 14:27:12 -07:00
|
|
|
} code_part;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-07-01 04:54:09 -07:00
|
|
|
extern BOOL enabled_debug[LOG_LAST];
|
|
|
|
|
2006-08-21 14:27:12 -07:00
|
|
|
typedef void (*debug_callback_fn)(void**, const char*);
|
|
|
|
typedef void (*debug_callback_init)(void**);
|
|
|
|
typedef void (*debug_callback_exit)(void**);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-08-21 14:27:12 -07:00
|
|
|
typedef struct _debug_callback {
|
|
|
|
struct _debug_callback * next;
|
|
|
|
debug_callback_fn callback; /// Function which does the output
|
|
|
|
debug_callback_init init; /// Setup function
|
|
|
|
debug_callback_exit exit; /// Cleaning function
|
|
|
|
void * data; /// Used to pass data to the above functions. Eg a filename or handle.
|
|
|
|
} debug_callback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call once to initialize the debug logging system.
|
|
|
|
*
|
|
|
|
* Doesn't register any callbacks!
|
|
|
|
*/
|
|
|
|
void debug_init( void );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shutdown the debug system and remove all output callbacks
|
|
|
|
*/
|
|
|
|
void debug_exit( void );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a callback to be called on every call to debug()
|
|
|
|
*
|
|
|
|
* \param callback Function which does the output
|
|
|
|
* \param init Initializer function which does all setup for the callback (optional, may be NULL)
|
|
|
|
* \param exit Cleanup function called when unregistering the callback (optional, may be NULL)
|
|
|
|
* \param data Data to be passed to all three functions (optional, may be NULL)
|
|
|
|
*/
|
|
|
|
void debug_register_callback( debug_callback_fn callback, debug_callback_init init, debug_callback_exit exit, void * data );
|
|
|
|
|
2006-12-02 05:06:56 -08:00
|
|
|
void debug_callback_file(void **data, const char *outputBuffer);
|
|
|
|
void debug_callback_file_init(void **data);
|
|
|
|
void debug_callback_file_exit(void **data);
|
|
|
|
|
|
|
|
void debug_callback_stderr(void **data, const char *outputBuffer);
|
|
|
|
|
2008-04-13 15:54:58 -07:00
|
|
|
#if defined WIN32 && defined DEBUG
|
|
|
|
void debug_callback_win32debug(void** data, const char* outputBuffer);
|
|
|
|
#endif
|
|
|
|
|
2006-08-21 14:27:12 -07:00
|
|
|
/**
|
|
|
|
* Toggle debug output for part associated with str
|
|
|
|
*
|
|
|
|
* \param str Codepart in textformat
|
|
|
|
*/
|
2007-06-28 10:47:08 -07:00
|
|
|
BOOL debug_enable_switch(const char *str);
|
2006-08-21 14:27:12 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Output printf style format str with additional arguments.
|
|
|
|
*
|
|
|
|
* Only outputs if debugging of part was formerly enabled with debug_enable_switch.
|
|
|
|
*
|
|
|
|
* \param part Code part to associate with this message
|
|
|
|
* \param str printf style formatstring
|
|
|
|
*/
|
2008-04-19 07:52:35 -07:00
|
|
|
#define debug(part, ...) do { if (enabled_debug[part]) _debug(part, __FUNCTION__, __VA_ARGS__); } while(0)
|
|
|
|
void _debug( code_part part, const char *function, const char *str, ...)
|
|
|
|
WZ_DECL_FORMAT(printf, 3, 4);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-01-11 14:45:04 -08:00
|
|
|
/** Global to keep track of which game object to trace. */
|
|
|
|
extern UDWORD traceID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output printf style format str for debugging a specific game object whose debug part
|
|
|
|
* has been enabled.
|
|
|
|
* @see debug
|
|
|
|
*/
|
2008-04-19 07:52:35 -07:00
|
|
|
#define objTrace(part, id, ...) do { if (enabled_debug[part] && id == traceID) _debug(part, __FUNCTION__, __VA_ARGS__); } while(0)
|
2008-01-11 14:45:04 -08:00
|
|
|
static inline void objTraceEnable(UDWORD id) { traceID = id; }
|
|
|
|
static inline void objTraceDisable(void) { traceID = 0; }
|
|
|
|
|
2008-02-01 13:26:37 -08:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
#endif
|
2008-02-01 13:26:37 -08:00
|
|
|
|
2008-02-14 14:07:53 -08:00
|
|
|
/** Dump last two debug log calls into given file descriptor. For exception handler. */
|
2008-04-05 16:37:46 -07:00
|
|
|
#if defined(WZ_OS_WIN)
|
|
|
|
extern void dumpLog(HANDLE file);
|
|
|
|
#else
|
|
|
|
extern void dumpLog(int file);
|
|
|
|
#endif
|
2008-02-14 14:07:53 -08:00
|
|
|
|
2008-03-22 10:42:07 -07:00
|
|
|
/** Checks if a particular debub flag was enabled */
|
|
|
|
extern bool debugPartEnabled(code_part codePart);
|
|
|
|
|
2008-02-01 13:26:37 -08:00
|
|
|
#endif // __INCLUDED_LIB_FRAMEWORK_DEBUG_H__
|