2006-03-20 16:43:15 +00:00
|
|
|
/*
|
2014-06-01 17:55:23 +00:00
|
|
|
* Portions of this file are copyright Rebirth contributors and licensed as
|
|
|
|
* described in COPYING.txt.
|
|
|
|
* Portions of this file are copyright Parallax Software and licensed
|
|
|
|
* according to the Parallax license below.
|
|
|
|
* See COPYING.txt for license details.
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
|
|
|
|
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
|
|
|
|
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
|
|
|
|
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
|
|
|
|
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
|
|
|
|
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
|
|
|
|
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
|
|
|
|
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
|
2008-01-23 17:25:09 +00:00
|
|
|
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
|
2006-03-20 16:43:15 +00:00
|
|
|
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Error handling/printing/exiting code
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-12-11 02:38:19 +00:00
|
|
|
#include <cstdlib>
|
2006-03-20 16:43:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-01-23 17:25:09 +00:00
|
|
|
#include "pstypes.h"
|
|
|
|
#include "console.h"
|
2012-07-07 18:35:06 +00:00
|
|
|
#include "dxxerror.h"
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2015-03-22 18:49:21 +00:00
|
|
|
#define MAX_MSG_LEN 2048
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2013-06-23 21:18:48 +00:00
|
|
|
static void (*ErrorPrintFunc)(const char *);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
//takes string in register, calls printf with string on stack
|
2014-12-13 04:10:58 +00:00
|
|
|
static void warn_printf(const char *s)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2013-12-07 01:01:27 +00:00
|
|
|
con_puts(CON_URGENT,s);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
2013-06-30 20:59:51 +00:00
|
|
|
static void (*warn_func)(const char *s)=warn_printf;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
//provides a function to call with warning messages
|
2013-06-08 22:24:17 +00:00
|
|
|
void set_warn_func(void (*f)(const char *s))
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
warn_func = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
//uninstall warning function - install default printf
|
2013-06-08 23:12:44 +00:00
|
|
|
void clear_warn_func()
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
warn_func = warn_printf;
|
|
|
|
}
|
|
|
|
|
2015-06-16 03:15:40 +00:00
|
|
|
static void print_exit_message(const char *exit_message, size_t len)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2008-01-23 17:25:09 +00:00
|
|
|
if (ErrorPrintFunc)
|
|
|
|
{
|
|
|
|
(*ErrorPrintFunc)(exit_message);
|
|
|
|
}
|
2013-12-19 22:55:34 +00:00
|
|
|
con_puts(CON_CRITICAL, exit_message, len);
|
2015-06-16 03:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__noreturn
|
|
|
|
static void abort_print_exit_message(const char *exit_message, size_t len)
|
|
|
|
{
|
|
|
|
print_exit_message(exit_message, len);
|
2014-12-11 02:38:19 +00:00
|
|
|
d_debugbreak();
|
|
|
|
std::abort();
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 03:15:40 +00:00
|
|
|
__noreturn
|
|
|
|
static void graceful_print_exit_message(const char *exit_message, size_t len)
|
|
|
|
{
|
|
|
|
print_exit_message(exit_message, len);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-12-08 23:37:40 +00:00
|
|
|
void (Error_puts)(const char *func, const unsigned line, const char *str)
|
|
|
|
{
|
|
|
|
char exit_message[MAX_MSG_LEN]; // don't put the new line in for dialog output
|
2013-12-19 22:55:34 +00:00
|
|
|
int len = snprintf(exit_message, sizeof(exit_message), "%s:%u: error: %s", func, line, str);
|
2015-06-16 03:15:40 +00:00
|
|
|
abort_print_exit_message(exit_message, len);
|
2013-12-08 23:37:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-09 02:46:11 +00:00
|
|
|
void (UserError_puts)(const char *str, std::size_t len)
|
|
|
|
{
|
|
|
|
graceful_print_exit_message(str, len);
|
|
|
|
}
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
//terminates with error code 1, printing message
|
2012-07-07 18:37:55 +00:00
|
|
|
void (Error)(const char *func, const unsigned line, const char *fmt,...)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2012-07-07 18:37:55 +00:00
|
|
|
char exit_message[MAX_MSG_LEN]; // don't put the new line in for dialog output
|
2006-03-20 16:43:15 +00:00
|
|
|
va_list arglist;
|
|
|
|
|
2012-07-07 18:37:55 +00:00
|
|
|
int leader = snprintf(exit_message, sizeof(exit_message), "%s:%u: error: ", func, line);
|
2006-03-20 16:43:15 +00:00
|
|
|
va_start(arglist,fmt);
|
2013-12-19 22:55:34 +00:00
|
|
|
int len = vsnprintf(exit_message+leader,sizeof(exit_message)-leader,fmt,arglist);
|
2006-03-20 16:43:15 +00:00
|
|
|
va_end(arglist);
|
2015-06-16 03:15:40 +00:00
|
|
|
abort_print_exit_message(exit_message, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void (UserError)(const char *fmt,...)
|
|
|
|
{
|
|
|
|
char exit_message[MAX_MSG_LEN]; // don't put the new line in for dialog output
|
|
|
|
va_list arglist;
|
|
|
|
va_start(arglist,fmt);
|
|
|
|
int len = vsnprintf(exit_message, sizeof(exit_message), fmt, arglist);
|
|
|
|
va_end(arglist);
|
|
|
|
graceful_print_exit_message(exit_message, len);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 23:37:40 +00:00
|
|
|
void Warning_puts(const char *str)
|
|
|
|
{
|
|
|
|
if (warn_func == NULL)
|
|
|
|
return;
|
|
|
|
char warn_message[MAX_MSG_LEN];
|
|
|
|
snprintf(warn_message, sizeof(warn_message), "Warning: %s", str);
|
|
|
|
(*warn_func)(warn_message);
|
|
|
|
}
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
//print out warning message to user
|
2013-12-08 23:37:40 +00:00
|
|
|
void (Warning)(const char *fmt,...)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
va_list arglist;
|
|
|
|
|
|
|
|
if (warn_func == NULL)
|
|
|
|
return;
|
|
|
|
|
2013-12-08 23:37:40 +00:00
|
|
|
char warn_message[MAX_MSG_LEN];
|
2006-03-20 16:43:15 +00:00
|
|
|
strcpy(warn_message,"Warning: ");
|
|
|
|
|
|
|
|
va_start(arglist,fmt);
|
2012-06-30 05:05:33 +00:00
|
|
|
vsnprintf(warn_message+9,sizeof(warn_message)-9,fmt,arglist);
|
2006-03-20 16:43:15 +00:00
|
|
|
va_end(arglist);
|
|
|
|
|
|
|
|
(*warn_func)(warn_message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//initialize error handling system, and set default message. returns 0=ok
|
2013-06-23 21:18:48 +00:00
|
|
|
int error_init(void (*func)(const char *))
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2008-01-23 17:25:09 +00:00
|
|
|
ErrorPrintFunc = func; // Set Error Print Functions
|
2006-03-20 16:43:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|