⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 report_error.cpp

📁 通用网络游戏开发框架
💻 CPP
字号:
// ============================================================================
// Error reporting functions
//
// (c) 2003 Ken Reed
//
// This is free software. You can redistribute it and/or modify it under the
// terms of the GNU General Public License version 2 as published by the Free
// Software Foundation.
// ============================================================================

#include "stdafx.h"
#include "report_error.h"

#include <errno.h>
#include <stdio.h>
#include <stdarg.h>

namespace {
   char error_text [256];
   char additional_text [132];
}


// ============================================================================
// report_error
//
// This routine can be called to report a system error (when the error code can
// be retrieved by GetLastError. It pops up a dialog box with the error
// information in it plus any additional information specified.
// ============================================================================

void report_error (const char * format_string, ...)
{
   va_list arguments;                        // Variable length arguments
   va_start(arguments, format_string);
   vsprintf(additional_text, format_string, arguments);
   va_end(arguments);

   char * message_buffer;
 
   FormatMessage(
      FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
      NULL,
      GetLastError(),
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
      reinterpret_cast<LPTSTR>(&message_buffer),
      0,
      NULL);

   sprintf(error_text, "%s\n%s", additional_text, message_buffer);

   MessageBox(0, error_text, error_title, MB_ICONSTOP| MB_SETFOREGROUND);

   LocalFree(message_buffer);
}

// ============================================================================
// report_errno
//
// This is similar to the above routine except that the extra error information
// is obtained from errno.
// ============================================================================

void report_errno (const char * format_string, ...)
{
   va_list arguments;                        // Variable length arguments
   int error (errno);                        // Save errno before it's changed

   va_start(arguments, format_string);
   vsprintf(additional_text, format_string, arguments);
   va_end(arguments);

   sprintf(error_text, "%s\n%s", additional_text, strerror(error));

   MessageBox(0, error_text, error_title, MB_ICONSTOP | MB_SETFOREGROUND);
}


// ============================================================================
// report_text
//
// This is similar to the above routine except that there is only the error
// text to report.
// ============================================================================

void report_text (const char * format_string, ...)
{
   va_list arguments;                        // Variable length arguments
   va_start(arguments, format_string);
   vsprintf(error_text, format_string, arguments);
   va_end(arguments);

   MessageBox(0, error_text, error_title, MB_ICONSTOP | MB_SETFOREGROUND);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -