r5sdk/r5dev/tier0/dbg.cpp
Kawe Mazidjatari f120354e96 Initial port to CMake
* All libraries have been isolated from each other, and build into separate artifacts.
* Project has been restructured to support isolating libraries.
* CCrashHandler now calls a callback on crash (setup from core/dllmain.cpp, this can be setup in any way for any project. This callback is getting called when the apllication crashes. Useful for flushing buffers before closing handles to logging files for example).
* Tier0 'CoreMsgV' function now calls a callback sink, which could be set by the user (currently setup to the SDK's internal logger in core/dllmain.cpp).

TODO:
* Add a batch file to autogenerate all projects.
* Add support for dedicated server.
* Add support for client dll.

Bugs:
* Game crashes on the title screen after the UI script compiler has finished (root cause unknown).
* Curl error messages are getting logged twice for the dedicated server due to the removal of all "DEDICATED" preprocessor directives to support isolating projects. This has to be fixed properly!
2023-05-10 00:05:38 +02:00

198 lines
5.7 KiB
C++

//==== Copyright (c) 1996-2005, Valve Corporation, All rights reserved. =====//
//
// Purpose:
//
// $NoKeywords: $
//
//===========================================================================//
#include "tier0_pch.h"
#include "tier0/dbg.h"
#include "tier0/platform.h"
#ifndef NETCONSOLE
#include "tier0/threadtools.h"
#include "tier0/commandline.h"
#if defined( _X360 )
#include "xbox/xbox_console.h"
#endif
#endif // !NETCONSOLE
CoreMsgVCallbackSink_t g_CoreMsgVCallback = nullptr;
//-----------------------------------------------------------------------------
// True if -hushasserts was passed on command line.
//-----------------------------------------------------------------------------
bool HushAsserts()
{
#if defined (DBGFLAG_ASSERT) && !defined (NETCONSOLE)
static bool s_bHushAsserts = !!CommandLine()->FindParm("-hushasserts");
return s_bHushAsserts;
#else
return true;
#endif
}
//-----------------------------------------------------------------------------
// Templates to assist in validating pointers:
//-----------------------------------------------------------------------------
/*PLATFORM_INTERFACE*/ void _AssertValidReadPtr(void* ptr, int count/* = 1*/)
{
#if defined( _WIN32 ) && !defined( _X360 )
Assert(!IsBadReadPtr(ptr, count));
#else
Assert(!count || ptr);
#endif
#ifdef NDEBUG
NOTE_UNUSED(ptr);
NOTE_UNUSED(count);
#endif // NDEBUG
}
/*PLATFORM_INTERFACE*/ void _AssertValidWritePtr(void* ptr, int count/* = 1*/)
{
#if defined( _WIN32 ) && !defined( _X360 )
Assert(!IsBadWritePtr(ptr, count));
#else
Assert(!count || ptr);
#endif
#ifdef NDEBUG
NOTE_UNUSED(ptr);
NOTE_UNUSED(count);
#endif // NDEBUG
}
/*PLATFORM_INTERFACE*/ void _AssertValidReadWritePtr(void* ptr, int count/* = 1*/)
{
#if defined( _WIN32 ) && !defined( _X360 )
Assert(!(IsBadWritePtr(ptr, count) || IsBadReadPtr(ptr, count)));
#else
Assert(!count || ptr);
#endif
#ifdef NDEBUG
NOTE_UNUSED(ptr);
NOTE_UNUSED(count);
#endif // NDEBUG
}
/*PLATFORM_INTERFACE*/ void _AssertValidStringPtr(const TCHAR* ptr, int maxchar/* = 0xFFFFFF */)
{
#if defined( _WIN32 ) && !defined( _X360 )
#ifdef TCHAR_IS_CHAR
Assert(!IsBadStringPtr(ptr, maxchar));
#else
Assert(!IsBadStringPtrW(ptr, maxchar));
#endif
#else
Assert(ptr);
#endif
#ifdef NDEBUG
NOTE_UNUSED(ptr);
NOTE_UNUSED(maxchar);
#endif // NDEBUG
}
/*PLATFORM_INTERFACE*/ void AssertValidWStringPtr(const wchar_t* ptr, int maxchar/* = 0xFFFFFF */)
{
#if defined( _WIN32 ) && !defined( _X360 )
Assert(!IsBadStringPtrW(ptr, maxchar));
#else
Assert(ptr);
#endif
#ifdef NDEBUG
NOTE_UNUSED(ptr);
NOTE_UNUSED(maxchar);
#endif // NDEBUG
}
//-----------------------------------------------------------------------------
// Purpose: Show logs to all console interfaces (va_list version)
// Input : logType -
// logLevel -
// context -
// *pszLogger -
// *pszFormat -
// args -
// exitCode -
// *pszUptimeOverride -
//-----------------------------------------------------------------------------
void CoreMsgV(LogType_t logType, LogLevel_t logLevel, eDLL_T context,
const char* pszLogger, const char* pszFormat, va_list args,
const UINT exitCode /*= NO_ERROR*/, const char* pszUptimeOverride /*= nullptr*/)
{
// Must be initialized before calling this function!
Assert(g_CoreMsgVCallback != nullptr);
g_CoreMsgVCallback(logType, logLevel, context, pszLogger, pszFormat, args, exitCode, pszUptimeOverride);
}
//-----------------------------------------------------------------------------
// Purpose: Show logs to all console interfaces
// Input : logType -
// logLevel -
// context -
// exitCode -
// *pszLogger -
// *pszFormat -
// ... -
//-----------------------------------------------------------------------------
void CoreMsg(LogType_t logType, LogLevel_t logLevel, eDLL_T context,
const UINT exitCode, const char* pszLogger, const char* pszFormat, ...)
{
va_list args;
va_start(args, pszFormat);
CoreMsgV(logType, logLevel, context, pszLogger, pszFormat, args, exitCode);
va_end(args);
}
//-----------------------------------------------------------------------------
// Purpose: Prints general debugging messages
// Input : context -
// *fmt - ... -
//-----------------------------------------------------------------------------
void DevMsg(eDLL_T context, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
CoreMsgV(LogType_t::LOG_INFO, LogLevel_t::LEVEL_NOTIFY, context, "sdk", fmt, args);
va_end(args);
}
//-----------------------------------------------------------------------------
// Purpose: Prints logs from remote console
// Input : context -
// *fmt - ... -
//-----------------------------------------------------------------------------
void NetMsg(LogType_t logType, eDLL_T context, const char* uptime, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
CoreMsgV(logType, LogLevel_t::LEVEL_NOTIFY, context, "netconsole", fmt, args, NO_ERROR, uptime);
va_end(args);
}
//-----------------------------------------------------------------------------
// Purpose: Print engine and SDK warnings
// Input : context -
// *fmt - ... -
//-----------------------------------------------------------------------------
void Warning(eDLL_T context, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
CoreMsgV(LogType_t::LOG_WARNING, LogLevel_t::LEVEL_NOTIFY, context, "sdk(warning)", fmt, args);
va_end(args);
}
//-----------------------------------------------------------------------------
// Purpose: Print engine and SDK errors
// Input : context -
// code -
// *fmt - ... -
//-----------------------------------------------------------------------------
void Error(eDLL_T context, const UINT code, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
CoreMsgV(LogType_t::LOG_ERROR, LogLevel_t::LEVEL_NOTIFY, context, "sdk(error)", fmt, args, code);
va_end(args);
}