Skip to content

Instantly share code, notes, and snippets.

@RobQuistNL
Created February 11, 2024 22:49
Show Gist options
  • Save RobQuistNL/d23bd3f05bb9489817d8b73d83e9a87c to your computer and use it in GitHub Desktop.
Save RobQuistNL/d23bd3f05bb9489817d8b73d83e9a87c to your computer and use it in GitHub Desktop.
Some nice extended (UE_LOG and Multiplayer) macro's to use in C++ in Unreal Engine (5.3) for Multiplayer games
/// Since I like to log a bit, and making it clear where a UE_LOG comes from is
/// usually a bit of a pain, I'm now using these logs and macro's in my code.
/// This should help make logs a bit clearer and easier to write;
///
/// Example usage: MY_LOG("Input vector %s", *Value.ToString());
/// Example output:
/// Sun Feb 11 23:20:27 CET 2024 Display MY_GAME [Client] AMyPlayerController::Move: Input vector X=1.000 Y=0.000
///
/// Based off of https://unrealcommunity.wiki/log-macro-with-net-mode-and-color-q3vj8r7c
// Copyleft DukeSoft 2024
#pragma once
#include "CoreMinimal.h"
DECLARE_LOG_CATEGORY_EXTERN(MY_GAME, Display, All);
#define IS_SERVER (\
(GEngine == nullptr || GetWorld() == nullptr) ? true \
: (GEngine->GetNetMode(GetWorld()) == NM_Client) ? false \
: (GEngine->GetNetMode(GetWorld()) == NM_ListenServer) ? true \
: (GEngine->GetNetMode(GetWorld()) == NM_DedicatedServer) ? true : true \
)
#define NETMODE_WORLD_TEXT (\
(GEngine == nullptr || GetWorld() == nullptr) ? TEXT("[Unknown]\t") \
: (GEngine->GetNetMode(GetWorld()) == NM_Client) ? TEXT("[Client]\t") \
: (GEngine->GetNetMode(GetWorld()) == NM_ListenServer) ? TEXT("[LServer]\t") \
: (GEngine->GetNetMode(GetWorld()) == NM_DedicatedServer) ? TEXT("[DServer]\t") : TEXT("[Standalone]\t") \
)
#if _MSC_VER
#define FUNC_NAME TEXT(__FUNCTION__)
#else // FIXME - GCC?
#define FUNC_NAME TEXT(__func__)
#endif
/**
* @param Level The verbosity level
* @param Format Format string literal in the style of printf.
*/
#define _MY_LOG_INTERNAL(Level, Format, ...) \
UE_LOG(MY_GAME, Level, TEXT("%s%s: " Format), NETMODE_WORLD_TEXT, FUNC_NAME, ##__VA_ARGS__);
/**
* @param Format String format in printf style
*/
#define MY_LOG(Format, ...) _MY_LOG_INTERNAL(Display, Format, ##__VA_ARGS__)
/**
* @param Format String format in printf style
*/
#define MY_WARN(Format, ...) _MY_LOG_INTERNAL(Warning, Format, ##__VA_ARGS__)
/**
* @param Format String format in printf style
*/
#define MY_ERROR(Format, ...) _MY_LOG_INTERNAL(Error, Format, ##__VA_ARGS__)
/**
* @param Format String format in printf style
*/
#define MY_FATAL(Format, ...) _MY_LOG_INTERNAL(Fatal, Format, ##__VA_ARGS__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment