OLS/Source/ols/Public/OLSLog.h

124 lines
5.9 KiB
C
Raw Permalink Normal View History

2025-01-16 18:09:31 +00:00
// © 2024 Long Ly. All rights reserved. Any unauthorized use, reproduction, or distribution of this trademark is strictly prohibited and may result in legal action.
#pragma once
#include "CoreMinimal.h"
#include "Engine/SCS_Node.h"
/*
* Helper macro where you can pass a C++ variable or function identifier and it will be converted to UE4's TEXT("")
* e.g: GET_IDENTIFIER_TEXT(myVariable);
*
* This allows us to reference the identifier instead of "hardcoding" it in between quotes.
*
* Advantages:
* 1) Renaming the variable or function name will also update the value contained inside GET_IDENTIFIER_TEXT();
* 2) Finding references of that variable or function will also work and find it in all areas where "GET_IDENTIFIER_TEXT" is referencing that variable.
*/
#define GET_IDENTIFIER_TEXT(identifier) TEXT(#identifier)
/*
* Helper Macro which returns the text 'Server', 'Client', 'ListenServer' or 'Standalone' depending on the World that's passed in
* (Based off ENetMode, which is a raw struct, so we can't use UENUM_TO_STRING here)
*/
#define GET_NETMODE_STRING(world) (world == nullptr) ? TEXT("NULLWORLD") : (world->GetNetMode() == ENetMode::NM_Client) ? TEXT("CLIENT") : (world->GetNetMode() == ENetMode::NM_DedicatedServer) ? TEXT("SERVER") : (world->GetNetMode() == ENetMode::NM_ListenServer) ? TEXT("LISTENSERVER") : (world->GetNetMode() == ENetMode::NM_Standalone) ? TEXT("STANDALONE") : TEXT("NM_MAX")
/*
* Helper Macro which returns the string from GetName on the UObject it's passed
* (Returns NULL if the pointer is null)
*/
#define GET_UOBJECT_NAME(obj) obj ? *obj->GetName() : TEXT("NULL")
/*
* Helper Macro which returns the string from GetNameSafe on the UObject it's passed
* (Returns NULL if the pointer is null)
*/
#define GET_UOBJECT_NAME_SAFE(obj) obj ? *obj->GetNameSafe() : TEXT("NULL")
2025-01-16 18:09:31 +00:00
/*
* Helper Macro which returns the string from GetFullName on the UObject it's passed
* (Returns NULL if the pointer is null)
*/
#define GET_UOBJECT_FULL_NAME(obj) obj ? *obj->GetFullName() : TEXT("NULL")
/*
* Helper Macro which returns the string from GetPathName on the UObject it's passed
* (Returns NULL if the pointer is null)
*/
#define GET_UOBJECT_PATH_NAME(obj) obj ? *obj->GetPathName() : TEXT("NULL")
/*
* Helper Macro which returns the string from GetTagName on the FGameplayTag it's passed
*/
#define GET_TAG_NAME(gameplayTag) *gameplayTag.GetTagName().ToString()
/*
* Helper Macro which changes a bool into a string of TEXT
*/
#define BOOL_TO_STRING(boolToRead) boolToRead ? TEXT("TRUE") : TEXT("FALSE")
/*
* Helper Macro which gets the display string of an enum
*/
#define UENUM_TO_STRING(enumClass, enumValue) *StaticEnum<enumClass>()->GetDisplayValueAsText(enumValue).ToString()
/*
* Same as UE_LOG but prefixes the log with [NETMODE][FunctionName].
* This version doesn't accept Variadic Arguments. Use TOWERS_LOG if you want to pass printf-style parameters
*/
#define OLS_LOG_SIMPLE(categoryName, verbosity, format) UE_LOG(categoryName, verbosity, TEXT("[%s][%s]: ") format, GET_NETMODE_STRING(GetWorld()), ANSI_TO_TCHAR(__func__));
/*
* Same as UE_LOG but prefixes the log with [NETMODE][FunctionName].
* This version doesn't accept Variadic Arguments. Use TOWERS_LOG if you want to pass printf-style parameters
*/
#define OLS_LOG_SIMPLE_NO_WORLD(categoryName, verbosity, format) UE_LOG(categoryName, verbosity, TEXT("[NO_WORLD][%s]: ") format, ANSI_TO_TCHAR(__func__));
/*
* Same as UE_LOG but prefixes the log with [NETMODE][FunctionName].
* This version doesn't accept Variadic Arguments. Use TOWERS_LOG if you want to pass printf-style parameters
*/
#define OLS_LOG_SIMPLE_WORLD_CTX(categoryName, verbosity, worldContextObject, format) UE_LOG(categoryName, verbosity, TEXT("[%s][%s]: ") format, GET_NETMODE_STRING(worldContextObject), ANSI_TO_TCHAR(__func__));
/*
* Same as UE_LOG but prefixes the log with [NETMODE][FunctionName].
*/
#define OLS_LOG(categoryName, verbosity, format, ...) UE_LOG(categoryName, verbosity, TEXT("[%s][%s]: ") format, GET_NETMODE_STRING(GetWorld()), ANSI_TO_TCHAR(__func__), ##__VA_ARGS__);
/*
* Same as UE_LOG but prefixes the log with [NETMODE][FunctionName].
* This version can be used when called from a function which doesn't have GetWorld().
* In that case, you can pass a worldContextObject (any UObject should do).
*/
#define OLS_LOG_WORLD_CTX(categoryName, verbosity, worldContextObject, format, ...) UE_LOG(categoryName, verbosity, TEXT("[%s][%s]: ") format, GET_NETMODE_STRING(worldContextObject), ANSI_TO_TCHAR(__func__), ##__VA_ARGS__);
/*
* Same as UE_LOG but prefixes the log with [NO_WORLD][FunctionName].
* This version should only be used if you can't use either of the following alternatives:
* OLS_LOG
* OLS_LOG_WORLD_CTX
*/
#define OLS_LOG_NO_WORLD(categoryName, verbosity, format, ...) UE_LOG(categoryName, verbosity, TEXT("[NO_WORLD][%s]: ") format, ANSI_TO_TCHAR(__func__), ##__VA_ARGS__);
/*
* Helper macro that will log the name of the function.
* NOTE: Use TOWERS_LOG_FUNCTION_WORLD_CTX instead if GetWorld() is not an available and you have access to a worldContextObject.
*/
#define OLS_LOG_FUNC(categoryName, verbosity) UE_LOG(categoryName, verbosity, TEXT("[%s][%s]"), GET_NETMODE_STRING(GetWorld()), ANSI_TO_TCHAR(__func__));
/*
* Helper macro that will log the name of the function
* This version can be used when called from a function which doesn't have GetWorld().
* In that case, you can pass a worldContextObject (any UObject should do).
*/
#define OLS_LOG_FUNC_WORLD_CTX(categoryName, verbosity, worldContextObject) UE_LOG(categoryName, verbosity, TEXT("[%s][%s]"), GET_NETMODE_STRING(worldContextObject), ANSI_TO_TCHAR(__func__));
/*
* Helper macro that will log the name of the function
* This version should only be used if you can't use either of the following alternatives:
* OLS_LOG_FUNC
* OLS_LOG_FUNCTION_WORLD_CTX
*/
2025-01-16 20:54:02 +00:00
#define OLS_LOG_FUNC_NO_WORLD(categoryName, verbosity) UE_LOG(categoryName, verbosity, TEXT("[NO_WORLD][%s]"), ANSI_TO_TCHAR(__func__));
OLS_API FString GetClientServerContextString(UObject* contextObject = nullptr);