From 8ee5acb2089a3ecb82cf7c8230843d329a4f3b9b Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Wed, 20 Feb 2002 19:22:52 +0000 Subject: [PATCH] redo Sys_Error so it's more usable throughout quake --- include/QF/sys.h | 5 +++- libs/console/console.c | 2 +- libs/util/sys.c | 64 +++++++++++++++++++++++++++--------------- nq/include/server.h | 2 -- nq/source/host.c | 3 +- nq/source/sv_main.c | 13 --------- nq/source/world.c | 9 +++--- qw/include/server.h | 1 - qw/source/sv_ccmds.c | 2 +- qw/source/sv_ents.c | 4 +-- qw/source/sv_init.c | 5 ++-- qw/source/sv_main.c | 16 ++++------- qw/source/sv_phys.c | 5 ++-- qw/source/sv_pr_cmds.c | 7 +++-- qw/source/sv_progs.c | 3 +- qw/source/sv_send.c | 8 +++--- qw/source/world.c | 9 +++--- 17 files changed, 83 insertions(+), 75 deletions(-) diff --git a/include/QF/sys.h b/include/QF/sys.h index 9083f6a53..b6cd723d1 100644 --- a/include/QF/sys.h +++ b/include/QF/sys.h @@ -29,6 +29,7 @@ #ifndef __sys_h #define __sys_h +#include #include #include "QF/gcc_attr.h" @@ -48,8 +49,10 @@ void Sys_mkdir (const char *path); typedef void (*sys_printf_t) (const char *fmt, va_list args); -void Sys_SetPrintf (sys_printf_t func); +void Sys_SetStdPrintf (sys_printf_t func); +void Sys_SetErrPrintf (sys_printf_t func); +void Sys_Print (FILE *stream, const char *fmt, va_list args); void Sys_Printf (const char *fmt, ...) __attribute__((format(printf,1,2))); void Sys_DPrintf (const char *fmt, ...) __attribute__((format(printf,1,2))); void Sys_Error (const char *error, ...) __attribute__((format(printf,1,2), noreturn)); diff --git a/libs/console/console.c b/libs/console/console.c index 17746370d..7afd89922 100644 --- a/libs/console/console.c +++ b/libs/console/console.c @@ -58,7 +58,7 @@ Con_Init (const char *plugin_name) con_module = PI_LoadPlugin ("console", plugin_name); if (con_module) { con_module->functions->general->p_Init (); - Sys_SetPrintf (con_module->functions->console->pC_Print); + Sys_SetStdPrintf (con_module->functions->console->pC_Print); } else { setvbuf (stdout, 0, _IOLBF, BUFSIZ); } diff --git a/libs/util/sys.c b/libs/util/sys.c index 10669e2ba..d9507232f 100644 --- a/libs/util/sys.c +++ b/libs/util/sys.c @@ -65,6 +65,7 @@ static const char rcsid[] = #include "compat.h" static void Sys_StdPrintf (const char *fmt, va_list args); +static void Sys_ErrPrintf (const char *fmt, va_list args); cvar_t *sys_nostdout; cvar_t *sys_extrasleep; @@ -73,7 +74,8 @@ cvar_t *sys_sleep; int sys_checksum; -static sys_printf_t sys_printf_function = Sys_StdPrintf; +static sys_printf_t sys_std_printf_function = Sys_StdPrintf; +static sys_printf_t sys_err_printf_function = Sys_ErrPrintf; typedef struct shutdown_list_s { struct shutdown_list_s *next; @@ -169,28 +171,50 @@ Sys_FileTime (const char *path) actual implementation of Sys_Printf. */ void -Sys_SetPrintf (sys_printf_t func) +Sys_SetStdPrintf (sys_printf_t func) { - sys_printf_function = func; + sys_std_printf_function = func; +} + +void +Sys_SetErrPrintf (sys_printf_t func) +{ + sys_err_printf_function = func; +} + +void +Sys_Print (FILE *stream, const char *fmt, va_list args) +{ + char msg[MAXPRINTMSG]; + unsigned char *p; + + vsnprintf (msg, sizeof (msg), fmt, args); + +#ifdef WIN32 + if (stream == stderr) + MessageBox (NULL, string, "Error", 0 /* MB_OK */ ); +#endif + + /* translate to ASCII instead of printing [xx] --KB */ + for (p = (unsigned char *) msg; *p; p++) + putc (sys_char_map[*p], stream); + + fflush (stream); } static void Sys_StdPrintf (const char *fmt, va_list args) { - char msg[MAXPRINTMSG]; - - unsigned char *p; - if (sys_nostdout && sys_nostdout->int_val) return; + Sys_Print (stdout, fmt, args); +} - vsnprintf (msg, sizeof (msg), fmt, args); - - /* translate to ASCII instead of printing [xx] --KB */ - for (p = (unsigned char *) msg; *p; p++) - putc (sys_char_map[*p], stdout); - - fflush (stdout); +static void +Sys_ErrPrintf (const char *fmt, va_list args) +{ + fprintf (stderr, "Fatal Error: "); + Sys_Print (stderr, fmt, args); } void @@ -198,7 +222,7 @@ Sys_Printf (const char *fmt, ...) { va_list args; va_start (args, fmt); - sys_printf_function (fmt, args); + sys_std_printf_function (fmt, args); va_end (args); } @@ -210,7 +234,7 @@ Sys_DPrintf (const char *fmt, ...) if (!developer || !developer->int_val) return; va_start (args, fmt); - sys_printf_function (fmt, args); + sys_std_printf_function (fmt, args); va_end (args); } @@ -326,17 +350,11 @@ void Sys_Error (const char *error, ...) { va_list argptr; - char string[1024]; va_start (argptr, error); - vsnprintf (string, sizeof (string), error, argptr); + sys_err_printf_function (error, argptr); va_end (argptr); -#ifdef WIN32 - MessageBox (NULL, string, "Error", 0 /* MB_OK */ ); -#endif - fprintf (stderr, "Fatal error: %s\n", string); - run_shutdown_list (); exit (1); diff --git a/nq/include/server.h b/nq/include/server.h index b2fde2e25..475a94dd6 100644 --- a/nq/include/server.h +++ b/nq/include/server.h @@ -267,8 +267,6 @@ void SV_RunClients (void); void SV_SaveSpawnparms (); void SV_SpawnServer (const char *server); -void SV_Error (const char *error, ...) __attribute__((format(printf,1,2))); - void SV_LoadProgs (void); void SV_Progs_Init (void); void SV_Progs_Init_Cvars (void); diff --git a/nq/source/host.c b/nq/source/host.c index e2916c1d9..d4100f638 100644 --- a/nq/source/host.c +++ b/nq/source/host.c @@ -170,7 +170,6 @@ Host_Error (const char *error, ...) va_start (argptr, error); vsnprintf (string, sizeof (string), error, argptr); va_end (argptr); - Con_Printf ("Host_Error: %s\n", string); if (sv.active) Host_ShutdownServer (false); @@ -178,6 +177,8 @@ Host_Error (const char *error, ...) if (cls.state == ca_dedicated) Sys_Error ("Host_Error: %s\n", string); // dedicated servers exit + Con_Printf ("Host_Error: %s\n", string); + CL_Disconnect (); cls.demonum = -1; diff --git a/nq/source/sv_main.c b/nq/source/sv_main.c index 2e871add4..a09e9a066 100644 --- a/nq/source/sv_main.c +++ b/nq/source/sv_main.c @@ -1040,16 +1040,3 @@ SV_SpawnServer (const char *server) Con_DPrintf ("Server spawned.\n"); } - -void -SV_Error (const char *error, ...) -{ - va_list argptr; - static char string[1024]; - - va_start (argptr, error); - vsnprintf (string, sizeof (string), error, argptr); - va_end (argptr); - - Host_Error ("%s", string); -} diff --git a/nq/source/world.c b/nq/source/world.c index b99ef627f..f152b4828 100644 --- a/nq/source/world.c +++ b/nq/source/world.c @@ -42,6 +42,7 @@ static const char rcsid[] = #include "QF/clip_hull.h" #include "QF/console.h" #include "QF/crc.h" +#include "QF/sys.h" #include "compat.h" #include "server.h" @@ -166,12 +167,12 @@ SV_HullForEntity (edict_t *ent, const vec3_t mins, const vec3_t maxs, } if (SVfloat (ent, solid) == SOLID_BSP) { // explicit hulls in the BSP model if (SVfloat (ent, movetype) != MOVETYPE_PUSH) - SV_Error ("SOLID_BSP without MOVETYPE_PUSH"); + Sys_Error ("SOLID_BSP without MOVETYPE_PUSH"); model = sv.models[(int) SVfloat (ent, modelindex)]; if (!model || model->type != mod_brush) - SV_Error ("SOLID_BSP with a non bsp model"); + Sys_Error ("SOLID_BSP with a non bsp model"); hull = &model->hulls[hull_index]; } @@ -419,7 +420,7 @@ SV_HullPointContents (hull_t *hull, int num, const vec3_t p) while (num >= 0) { if (num < hull->firstclipnode || num > hull->lastclipnode) - SV_Error ("SV_HullPointContents: bad node number"); + Sys_Error ("SV_HullPointContents: bad node number"); node = hull->clipnodes + num; plane = hull->planes + node->planenum; @@ -641,7 +642,7 @@ SV_ClipToLinks (areanode_t *node, moveclip_t * clip) if (touch == clip->passedict) continue; if (SVfloat (touch, solid) == SOLID_TRIGGER) - SV_Error ("Trigger in clipping list"); + Sys_Error ("Trigger in clipping list"); if (clip->type == MOVE_NOMONSTERS && SVfloat (touch, solid) != SOLID_BSP) diff --git a/qw/include/server.h b/qw/include/server.h index bffe0c0cf..e27132bf3 100644 --- a/qw/include/server.h +++ b/qw/include/server.h @@ -422,7 +422,6 @@ extern const char *client_info_filters[]; //=========================================================== // FIXME: declare exported functions in their own relevant .h -void SV_Error (const char *error, ...) __attribute__((format(printf,1,2))); void SV_Init (void); void SV_Progs_Init (void); void SV_Progs_Init_Cvars (void); diff --git a/qw/source/sv_ccmds.c b/qw/source/sv_ccmds.c index cfaa6defe..fc7f54668 100644 --- a/qw/source/sv_ccmds.c +++ b/qw/source/sv_ccmds.c @@ -394,7 +394,7 @@ SV_Map_f (void) SV_Printf ("Can't find %s\n", expanded); // If curlevel == level, something is SCREWED! --KB if (strcaseequal (level, curlevel)) - SV_Error ("map: cannot restart level\n"); + Sys_Error ("map: cannot restart level\n"); else Cbuf_AddText (va ("map %s", curlevel)); return; diff --git a/qw/source/sv_ents.c b/qw/source/sv_ents.c index 33741f4eb..a6dd82520 100644 --- a/qw/source/sv_ents.c +++ b/qw/source/sv_ents.c @@ -234,9 +234,9 @@ SV_WriteDelta (entity_state_t *from, entity_state_t *to, sizebuf_t *msg, // write the message if (!to->number) - SV_Error ("Unset entity number"); + Sys_Error ("Unset entity number"); if (to->number >= 512) - SV_Error ("Entity number >= 512"); + Sys_Error ("Entity number >= 512"); if (!bits && !force) return; // nothing to send! diff --git a/qw/source/sv_init.c b/qw/source/sv_init.c index a52ab1442..428d7aba7 100644 --- a/qw/source/sv_init.c +++ b/qw/source/sv_init.c @@ -41,6 +41,7 @@ static const char rcsid[] = #include "QF/cvar.h" #include "QF/info.h" #include "QF/msg.h" +#include "QF/sys.h" #include "QF/va.h" #include "QF/vfs.h" @@ -70,7 +71,7 @@ SV_ModelIndex (const char *name) if (!strcmp (sv.model_precache[i], name)) return i; if (i == MAX_MODELS || !sv.model_precache[i]) - SV_Error ("SV_ModelIndex: model %s not precached", name); + Sys_Error ("SV_ModelIndex: model %s not precached", name); return i; } @@ -86,7 +87,7 @@ SV_FlushSignon (void) return; if (sv.num_signon_buffers == MAX_SIGNON_BUFFERS - 1) - SV_Error ("sv.num_signon_buffers == MAX_SIGNON_BUFFERS-1"); + Sys_Error ("sv.num_signon_buffers == MAX_SIGNON_BUFFERS-1"); sv.signon_buffer_size[sv.num_signon_buffers - 1] = sv.signon.cursize; sv.signon.data = sv.signon_buffers[sv.num_signon_buffers]; diff --git a/qw/source/sv_main.c b/qw/source/sv_main.c index 07978a2ce..ea71fa26a 100644 --- a/qw/source/sv_main.c +++ b/qw/source/sv_main.c @@ -232,26 +232,21 @@ SV_Shutdown (void) then exits */ void -SV_Error (const char *error, ...) +SV_Error (const char *error, va_list argptr) { - va_list argptr; static char string[1024]; static qboolean inerror = false; if (inerror) - Sys_Error ("SV_Error: recursively entered (%s)", string); + return; inerror = true; - va_start (argptr, error); vsnprintf (string, sizeof (string), error, argptr); - va_end (argptr); - - SV_Printf ("SV_Error: %s\n", string); SV_FinalMessage (va ("server crashed: %s\n", string)); - Sys_Error ("SV_Error: %s\n", string); + Sys_Print (stderr, error, argptr); } /* @@ -2422,7 +2417,8 @@ SV_Init (void) CVAR_ROM, 0, "Plugin used for the console"); PI_RegisterPlugins (server_plugin_list); Con_Init (sv_console_plugin->string); - Sys_SetPrintf (SV_Print); + Sys_SetStdPrintf (SV_Print); + Sys_SetErrPrintf (SV_Error); COM_Filesystem_Init_Cvars (); Game_Init_Cvars (); @@ -2476,5 +2472,5 @@ SV_Init (void) if (sv.state == ss_dead) Cmd_ExecuteString ("map start", src_command); if (sv.state == ss_dead) - SV_Error ("Could not initialize server"); + Sys_Error ("Could not initialize server"); } diff --git a/qw/source/sv_phys.c b/qw/source/sv_phys.c index b39597af2..db44dab16 100644 --- a/qw/source/sv_phys.c +++ b/qw/source/sv_phys.c @@ -32,6 +32,7 @@ static const char rcsid[] = #endif #include "QF/cvar.h" +#include "QF/sys.h" #include "pmove.h" #include "server.h" @@ -274,7 +275,7 @@ SV_FlyMove (edict_t *ent, float time, trace_t *steptrace) break; // moved the entire distance if (!trace.ent) - SV_Error ("SV_FlyMove: !trace.ent"); + Sys_Error ("SV_FlyMove: !trace.ent"); if (trace.plane.normal[2] > 0.7) { blocked |= 1; // floor @@ -870,7 +871,7 @@ SV_RunEntity (edict_t *ent) SV_Physics_Toss (ent); break; default: - SV_Error ("SV_Physics: bad movetype %i", (int) SVfloat (ent, + Sys_Error ("SV_Physics: bad movetype %i", (int) SVfloat (ent, movetype)); } } diff --git a/qw/source/sv_pr_cmds.c b/qw/source/sv_pr_cmds.c index 58b38ad27..c50325041 100644 --- a/qw/source/sv_pr_cmds.c +++ b/qw/source/sv_pr_cmds.c @@ -41,6 +41,7 @@ static const char rcsid[] = #include "QF/cmd.h" #include "QF/cvar.h" #include "QF/msg.h" +#include "QF/sys.h" #include "QF/va.h" #include "compat.h" @@ -76,7 +77,7 @@ PF_error (progs_t *pr) ed = PROG_TO_EDICT (pr, *sv_globals.self); ED_Print (pr, ed); - SV_Error ("Program error"); + Sys_Error ("Program error"); } /* @@ -100,7 +101,7 @@ PF_objerror (progs_t *pr) ED_Print (pr, ed); ED_Free (pr, ed); - SV_Error ("Program error"); + Sys_Error ("Program error"); } /* @@ -999,7 +1000,7 @@ WriteDest (progs_t *pr) return &sv.datagram; case MSG_ONE: - SV_Error ("Shouldn't be at MSG_ONE"); + Sys_Error ("Shouldn't be at MSG_ONE"); #if 0 ent = PROG_TO_EDICT (pr, *sv_globals.msg_entity); entnum = NUM_FOR_EDICT (pr, ent); diff --git a/qw/source/sv_progs.c b/qw/source/sv_progs.c index e0cc55f8f..d410c63df 100644 --- a/qw/source/sv_progs.c +++ b/qw/source/sv_progs.c @@ -40,6 +40,7 @@ static const char rcsid[] = #include "QF/cmd.h" #include "QF/cvar.h" +#include "QF/sys.h" #include "compat.h" #include "server.h" @@ -178,7 +179,7 @@ SV_LoadProgs (void) PR_LoadProgs (&sv_pr_state, sv_progs->string, MAX_EDICTS, 0); if (!sv_pr_state.progs) - SV_Error ("SV_LoadProgs: couldn't load %s", sv_progs->string); + Sys_Error ("SV_LoadProgs: couldn't load %s", sv_progs->string); // progs engine needs these globals anyway sv_globals.self = sv_pr_state.globals.self; sv_globals.time = sv_pr_state.globals.time; diff --git a/qw/source/sv_send.c b/qw/source/sv_send.c index a913bb10d..556b8e85d 100644 --- a/qw/source/sv_send.c +++ b/qw/source/sv_send.c @@ -363,7 +363,7 @@ SV_Multicast (const vec3_t origin, int to) default: mask = NULL; - SV_Error ("SV_Multicast: bad to:%i", to); + Sys_Error ("SV_Multicast: bad to:%i", to); } // send the data to all relevent clients @@ -425,13 +425,13 @@ SV_StartSound (edict_t *entity, int channel, const char *sample, int volume, vec3_t origin; if (volume < 0 || volume > 255) - SV_Error ("SV_StartSound: volume = %i", volume); + Sys_Error ("SV_StartSound: volume = %i", volume); if (attenuation < 0 || attenuation > 4) - SV_Error ("SV_StartSound: attenuation = %f", attenuation); + Sys_Error ("SV_StartSound: attenuation = %f", attenuation); if (channel < 0 || channel > 15) - SV_Error ("SV_StartSound: channel = %i", channel); + Sys_Error ("SV_StartSound: channel = %i", channel); // find precache number for sound for (sound_num = 1; sound_num < MAX_SOUNDS diff --git a/qw/source/world.c b/qw/source/world.c index b99ef627f..f152b4828 100644 --- a/qw/source/world.c +++ b/qw/source/world.c @@ -42,6 +42,7 @@ static const char rcsid[] = #include "QF/clip_hull.h" #include "QF/console.h" #include "QF/crc.h" +#include "QF/sys.h" #include "compat.h" #include "server.h" @@ -166,12 +167,12 @@ SV_HullForEntity (edict_t *ent, const vec3_t mins, const vec3_t maxs, } if (SVfloat (ent, solid) == SOLID_BSP) { // explicit hulls in the BSP model if (SVfloat (ent, movetype) != MOVETYPE_PUSH) - SV_Error ("SOLID_BSP without MOVETYPE_PUSH"); + Sys_Error ("SOLID_BSP without MOVETYPE_PUSH"); model = sv.models[(int) SVfloat (ent, modelindex)]; if (!model || model->type != mod_brush) - SV_Error ("SOLID_BSP with a non bsp model"); + Sys_Error ("SOLID_BSP with a non bsp model"); hull = &model->hulls[hull_index]; } @@ -419,7 +420,7 @@ SV_HullPointContents (hull_t *hull, int num, const vec3_t p) while (num >= 0) { if (num < hull->firstclipnode || num > hull->lastclipnode) - SV_Error ("SV_HullPointContents: bad node number"); + Sys_Error ("SV_HullPointContents: bad node number"); node = hull->clipnodes + num; plane = hull->planes + node->planenum; @@ -641,7 +642,7 @@ SV_ClipToLinks (areanode_t *node, moveclip_t * clip) if (touch == clip->passedict) continue; if (SVfloat (touch, solid) == SOLID_TRIGGER) - SV_Error ("Trigger in clipping list"); + Sys_Error ("Trigger in clipping list"); if (clip->type == MOVE_NOMONSTERS && SVfloat (touch, solid) != SOLID_BSP)