SHADE_Y3/SHADE_Managed/src/Utility/Debug.hxx

256 lines
12 KiB
C++

/************************************************************************************//*!
\file Debug.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 30, 2021
\brief Contains the definition of the Debug static class and the declaration of
its functions.
Note: This file is written in C++17/CLI.
Copyright (C) 2021 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
// Standard Library
#include <stdexcept>
#include <string>
/*-------------------------------------------------------------------------------------*/
/* Macro Functions */
/*-------------------------------------------------------------------------------------*/
/// <summary>
/// Macro expansion that is used together with SAFE_NATIVE_CALL_END or
/// SAFE_NATIVE_CALL_END_N to wrap the body of a function with a try and catch that
/// catches native and managed exceptions. This is needed to prevent crashes when calling
/// managed code from native code.
/// </summary>
#define SAFE_NATIVE_CALL_BEGIN try {
/// <summary>
/// Macro expansion that is used together with SAFE_NATIVE_CALL_BEGIN or to wrap the body
/// of a function with a try and catch that catches native and managed exceptions. This
/// is needed to prevent crashes when calling managed code from native code.
/// <br/>
/// Use this instead of SAFE_NATIVE_CALL_END_N if passing in managed types as the owner.
/// </summary>
/// <param name="OWNER">
/// The managed object that owns the function that this macro encapsulates.
/// </param>
#define SAFE_NATIVE_CALL_END(OWNER) \
} \
catch (System::Exception^ e) \
{ \
Debug::LogException(e); \
} \
catch (const std::exception& e) \
{ \
Debug::LogException(e, OWNER); \
} \
catch (...) \
{ \
Debug::LogError("Unsupported native exception.", OWNER); \
} \
/// <summary>
/// Macro expansion that is used together with SAFE_NATIVE_CALL_BEGIN or to wrap the body
/// of a function with a try and catch that catches native and managed exceptions. This
/// is needed to prevent crashes when calling managed code from native code.
/// <br/>
/// Use this instead of SAFE_NATIVE_CALL_END if passing in a native string that specifies
/// the owner.
/// </summary>
/// <param name="OWNER">
/// The managed object that owns the function that this macro encapsulates.
/// </param>
#define SAFE_NATIVE_CALL_END_N(OWNER) \
} \
catch (System::Exception^ e) \
{ \
Debug::LogExceptionNative(e, OWNER); \
} \
catch (const std::exception& e) \
{ \
Debug::LogExceptionNative(e, OWNER); \
} \
catch (...) \
{ \
Debug::LogErrorNative("Unsupported native exception.", OWNER); \
} \
/*-------------------------------------------------------------------------------------*/
/* Type Definitions */
/*-------------------------------------------------------------------------------------*/
namespace SHADE
{
/// <summary>
/// Static class that contains the functions for working with time.
/// </summary>
public ref class Debug abstract sealed
{
public:
/*-----------------------------------------------------------------------------*/
/* Logging Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Logs a message to the output.
/// </summary>
/// <param name="str">The string to output.</param>
static void Log(const std::string& str);
/// <summary>
/// Logs a message to the output.
/// </summary>
/// <param name="str">The string to output.</param>
static void Log(System::String^ str);
/// <summary>
/// Logs a message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="owner">
/// Object that sent the message to label the message.
/// The name of the object will be used.
/// </param>
static void Log(System::String^ str, Object^ owner);
/// <summary>
/// Logs a message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that sent the message to label the message.
/// The name of the object will be used.
/// </param>
static void Log(System::String^ str, System::String^ throwerName);
/// <summary>
/// Logs a message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that sent the message to label the message.
/// The name of the object will be used.
/// </param>
static void Log(System::String^ str, const std::string& throwerName);
/// <summary>
/// Logs a warning message to the output.
/// </summary>
/// <param name="str">The string to output.</param>
static void LogWarning(const std::string& str);
/// <summary>
/// Logs a warning message to the output.
/// </summary>
/// <param name="str">The string to output.</param>
static void LogWarning(System::String^ str);
/// <summary>
/// Logs a warning message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="thrower">
/// Object that threw the warning to label the warning message.
/// The name of the object will be used.
/// </param>
static void LogWarning(System::String^ str, Object^ thrower);
/// <summary>
/// Logs a warning message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that threw the warning to label the warning message.
/// The name of the object will be used.
/// </param>
static void LogWarning(System::String^ str, System::String^ throwerName);
/// <summary>
/// Logs a warning message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that threw the warning to label the warning message.
/// The name of the object will be used.
/// </param>
static void LogWarning(System::String^ str, const std::string& throwerName);
/// <summary>
/// Logs a error message to the output.
/// </summary>
/// <param name="str">The string to output.</param>
static void LogError(const std::string& str);
/// <summary>
/// Logs a error message to the output.
/// </summary>
/// <param name="str">The string to output.</param>
static void LogError(System::String^ str);
/// <summary>
/// Logs a error message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="thrower">
/// Object that threw the error to label the error message.
/// The name of the object will be used.
/// </param>
static void LogError(System::String^ str, Object^ thrower);
/// <summary>
/// Logs a error message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that threw the error to label the error message.
/// The name of the object will be used.
/// </param>
static void LogErrorNative(System::String^ str, const std::string& throwerName);
/// <summary>
/// Logs a error message to the output with a label such that it looks like this:
/// "[Label] Message"
/// </summary>
/// <param name="str">The string to output.</param>
/// <param name="throwerName">
/// Name of the object that threw the error to label the error message.
/// The name of the object will be used.
/// </param>
static void LogError(System::String^ str, System::String^ throwerName);
/// <summary>
/// Logs an exception that is formatted nicely to the output.
/// </summary>
/// <param name="exception">Exception to log.</param>
static void LogException(System::Exception^ exception);
/// <summary>
/// Logs an exception that is formatted nicely to the output.
/// </summary>
/// <param name="exception">Exception to log.</param>
/// <param name="thrower">
/// Object that threw the exception to label the exception message.
/// The name of the object will be used.
/// </param>
static void LogException(System::Exception^ exception, Object^ thrower);
/// <summary>
/// Logs a native exception that is formatted nicely to the output.
/// Equivalent to calling
/// LogException(exception, Convert::ToNative(thrower->GetType()->Name));
/// </summary>
/// <param name="exception">Native exception to log.</param>
/// <param name="thrower">
/// Object that threw the exception to label the exception message.
/// The name of the object will be used.
/// </param>
static void LogException(const std::exception& exception, Object^ thrower);
/// <summary>
/// Logs an exception that is formatted nicely to the output.
/// </summary>
/// <param name="throwerName">Name of the one responsible for the exception.</param>
/// <param name="exception">Exception to log.</param>
static void LogExceptionNative(System::Exception^ exception, const std::string& throwerName);
/// <summary>
/// Logs a native exception that is formatted nicely to the output.
/// </summary>
/// <param name="exception">Native exception to log.</param>
/// <param name="throwerName">Name of the one responsible for the exception.</param>
static void LogExceptionNative(const std::exception& exception, const std::string& throwerName);
};
}