!-------------------------------------------------------------------------------
! debuglib.h - a library to allow for easier printing of debugging statements
! Copyright (C) 1999 by John Cater
! Send comments or suggestions to:
[email protected]
!-------------------------------------------------------------------------------
#Ifndef debuglib_h;
Constant debuglib_h;
Message "[Including <debuglib>]";
System_file;
!! debuglib.h provides short-cut functions for printing debugging messages.
!! The functions are Print_Crit, Print_Warn, and Print_Trace, which
!! respectively are intended for critical errors, warning messages, and
!! stepping through the program.
!! Each of these functions take the same parameters. The first can be either
!! an object, a class, or a string. If it is one of the first two, then the
!! name of the object or class will be printed. A string will be printed as
!! is. This will be followed by the phrase 'critical', 'warning', or 'trace'
!! to distinguish the type of error. Finally, the second argument (which
!! should be a string) will be printed.
!! Also included in this library is the read_char function. This will cause
!! the program to pause while waiting for a key to be pressed. This has the
!! useful side effect of forcing the output buffer to be emptied. Under
!! zcode, there are no arguments to this function. Under glulx, the only
!! argument is the window to read the character from.
#Ifdef debuglib_h; ! remove "Constant declared but not used" warnings
#Endif;
#Ifdef TARGET_GLULX;
#Include "infglk.h";
#Endif;
! Debugging levels
Constant NO_DEBUGGING 0;
Constant DEBUG_CRITICAL 1;
Constant DEBUG_WARNING 2;
Constant DEBUG_TRACE 3;
Global debugging_level = DEBUG_CRITICAL;
! Global variables
Global debug_init = false;
#Ifdef TARGET_GLULX;
Array event-->4;
Global debug_win = GLK_NULL;
Global debug_str = GLK_NULL;
Global old_str = GLK_NULL;
#Endif;
! Functions
[ check_debug_init
;
if (debug_init == true) rtrue;
return debug_initialize ();
];
#Ifdef TARGET_GLULX;
[ debug_initialize
root_win;
#Ifnot;
[ debug_initialize;
#Endif;
#Ifdef TARGET_GLULX;
root_win = glk_window_get_root ();
if (root_win == GLK_NULL)
{
debug_win = glk_window_open (0, 0, 0, wintype_TextBuffer, 0);
} else
{
debug_win = glk_window_open (root_win, winmethod_Below |
winmethod_Proportional, 20, wintype_TextBuffer, 0);
}
debug_str = glk_window_get_stream (debug_win);
#Endif;
debug_init = true;
rtrue;
];
#Ifdef TARGET_GLULX;
[ read_char win
ch d;
#Ifnot;
[ read_char
ch;
#Endif;
! read_char is included as it is the best way to force the buffer to be
! emptied
#ifdef TARGET_GLULX;
d = false;
glk_request_char_event (win);
while (~~d)
{
glk_select (event);
switch (event-->0)
{
2: ! evtype_CharInput
if (event-->1 == win)
{
ch = event-->2;
d = true;
}
}
}
#ifnot; ! TARGET_ZCODE;
@read_char 1 ch;
#endif; ! TARGET_
return ch;
];
[ Print_Crit thing str;
if (debugging_level >= DEBUG_CRITICAL)
Print_Level (thing, "error", str);
rtrue;
];
[ Print_Warn thing str;
if (debugging_level >= DEBUG_WARNING)
Print_Level (thing, "warning", str);
rtrue;
];
[ Print_Trace thing str;
if (debugging_level >= DEBUG_TRACE)
Print_Level (thing, "trace", str);
rtrue;
];
[ Print_Level thing level str
old_str;
check_debug_init();
#Ifdef TARGET_GLULX;
old_str = glk_stream_get_current ();
glk_stream_set_current (debug_str);
#Endif;
switch (metaclass (thing))
{
String: print (string) thing, " ";
Object: print (name) thing, " ";
Class: print (name) thing, " ";
default: ; ! nothing
}
print (string) level, ": ";
print (string) str;
new_line;
#Ifdef TARGET_GLULX;
glk_stream_set_current (old_str);
read_char (debug_win);
#Endif;
];
#Endif;