Subversion Repositories Games.Descent

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Portions of this file are copyright Rebirth contributors and licensed as
  3.  * described in COPYING.txt.
  4.  * Portions of this file are copyright Parallax Software and licensed
  5.  * according to the Parallax license below.
  6.  * See COPYING.txt for license details.
  7.  
  8. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  9. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  10. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  11. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  12. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  13. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  14. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  15. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  16. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
  17. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  18. */
  19. /*
  20.  *
  21.  * Error handling/printing/exiting code
  22.  *
  23.  */
  24.  
  25. #include <cstdlib>
  26. #include <stdio.h>
  27. #include <stdarg.h>
  28. #include <string.h>
  29.  
  30. #include "messagebox.h"
  31. #include "pstypes.h"
  32. #include "console.h"
  33. #include "dxxerror.h"
  34.  
  35. namespace dcx {
  36.  
  37. #define MAX_MSG_LEN 2048
  38.  
  39. static void warn_printf(const char *s)
  40. {
  41.         con_puts(CON_URGENT,s);
  42. }
  43.  
  44. #if DXX_USE_EDITOR
  45. static void (*warn_func)(const char *s)=warn_printf;
  46.  
  47. //provides a function to call with warning messages
  48. void set_warn_func(void (*f)(const char *s))
  49. {
  50.         warn_func = f;
  51. }
  52.  
  53. //uninstall warning function - install default printf
  54. void clear_warn_func()
  55. {
  56.         warn_func = warn_printf;
  57. }
  58. #else
  59. constexpr auto warn_func = &warn_printf;
  60. #endif
  61.  
  62. static void print_exit_message(const char *exit_message, size_t len)
  63. {
  64.         con_puts(CON_CRITICAL, exit_message, len);
  65.         msgbox_error(exit_message);
  66. }
  67.  
  68. __noreturn
  69. static void abort_print_exit_message(const char *exit_message, size_t len)
  70. {
  71.         print_exit_message(exit_message, len);
  72.         d_debugbreak();
  73.         std::abort();
  74. }
  75.  
  76. __noreturn
  77. static void graceful_print_exit_message(const char *exit_message, size_t len)
  78. {
  79.         print_exit_message(exit_message, len);
  80.         exit(1);
  81. }
  82.  
  83. void (Error_puts)(const char *filename, const unsigned line, const char *func, const char *str)
  84. {
  85.         char exit_message[MAX_MSG_LEN]; // don't put the new line in for dialog output
  86.         int len = snprintf(exit_message, sizeof(exit_message), "%s:%u: %s: error: %s", filename, line, func, str);
  87.         abort_print_exit_message(exit_message, len);
  88. }
  89.  
  90. void (UserError_puts)(const char *str, std::size_t len)
  91. {
  92.         graceful_print_exit_message(str, len);
  93. }
  94.  
  95. //terminates with error code 1, printing message
  96. void (Error)(const char *filename, const unsigned line, const char *func, const char *fmt,...)
  97. {
  98.         char exit_message[MAX_MSG_LEN]; // don't put the new line in for dialog output
  99.         va_list arglist;
  100.  
  101.         int leader = snprintf(exit_message, sizeof(exit_message), "%s:%u: %s: error: ", filename, line, func);
  102.         va_start(arglist,fmt);
  103.         int len = vsnprintf(exit_message+leader,sizeof(exit_message)-leader,fmt,arglist);
  104.         va_end(arglist);
  105.         abort_print_exit_message(exit_message, len);
  106. }
  107.  
  108. void (UserError)(const char *fmt,...)
  109. {
  110.         char exit_message[MAX_MSG_LEN]; // don't put the new line in for dialog output
  111.         va_list arglist;
  112.         va_start(arglist,fmt);
  113.         int len = vsnprintf(exit_message, sizeof(exit_message), fmt, arglist);
  114.         va_end(arglist);
  115.         graceful_print_exit_message(exit_message, len);
  116. }
  117.  
  118. void Warning_puts(const char *str)
  119. {
  120.         const auto warn = warn_func;
  121. #if DXX_USE_EDITOR
  122.         if (warn == NULL)
  123.                 return;
  124. #endif
  125.         char warn_message[MAX_MSG_LEN];
  126.         snprintf(warn_message, sizeof(warn_message), "Warning: %s", str);
  127.         (*warn)(warn_message);
  128. }
  129.  
  130. //print out warning message to user
  131. void (Warning)(const char *fmt,...)
  132. {
  133.         va_list arglist;
  134.  
  135.         const auto warn = warn_func;
  136. #if DXX_USE_EDITOR
  137.         if (warn == NULL)
  138.                 return;
  139. #endif
  140.  
  141.         char warn_message[MAX_MSG_LEN];
  142.         strcpy(warn_message,"Warning: ");
  143.  
  144.         va_start(arglist,fmt);
  145.         vsnprintf(warn_message+9,sizeof(warn_message)-9,fmt,arglist);
  146.         va_end(arglist);
  147.  
  148.         (*warn)(warn_message);
  149. }
  150.  
  151. }
  152.