게임개발/Unreal C++

[Unreal]#20_Log

Hardii2 2022. 11. 2. 10:31

 

[Unreal]#20_Log

Unreal 개발 중 "Debugging"에 대해 알아보겠습니다.

커스텀 로그 클래스를 생성하고 활용하는 방법에 대해 살펴보겠습니다.

 

 


 

로그

1. ELogVerbosity

namespace ELogVerbosity
{
    enum Type
    {
        NoLogging        = 0,
        Fatal,
        Error,
        Warning,
        Display,
        Log,
        Verbose,
        VeryVerbose,
        All              = VeryVerbose,
        NumVerbosity,
        VerbosityMask    = 0xf,
        SetColor         = 0x40,
        BreakOnLog       = 0x80,
    }
}
  • 단순히, 로깅 메세지의 컬러를 변경합니다.
  • "Fatal" 타입만 예외적으로, 메세지가 출력되면 Crash가 발생합니다!

 

2. Custom Log Category

DEFINE_LOG_CATEGORY_STATIC(GameProject, Display, All)
  • LogTemp 대신, 다른 로그 카테고리를 활용하기위해 위 문장이 필요합니다!

 

3. 메크로 

출저: https://www.kernelpanic.kr/50

  • 해당 메크로들은 디버깅 로그시 유용하게 활용할 수 있겠죠!

 

커스텀 로그 클래스

1. /Utilities/CustomLog.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"

#define PrintLine(){ CLog::Log(__FUNCTION__, __LINE__);}

/**
 * 
 */
class DARKSOUL_API CustomLog
{

public:
	static void Print(int32 InValue, int32 InKey = -1, float Duration = 10.0f, FColor InColor = FColor::Blue);
	static void Print(float InValue, int32 InKey = -1, float Duration = 10.0f, FColor InColor = FColor::Blue);
	static void Print(const FString& InValue, int32 InKey = -1, float Duration = 10.0f, FColor InColor = FColor::Blue);
	static void Print(const FVector& InValue, int32 InKey = -1, float Duration = 10.0f, FColor InColor = FColor::Blue);
	static void Print(const FRotator& InValue, int32 InKey = -1, float Duration = 10.0f, FColor InColor = FColor::Blue);

	static void Log(int32 InValue);
	static void Log(float InValue);
	static void Log(const FString& InValue);
	static void Log(const FVector& InValue);
	static void Log(const FRotator& InValue);
	static void Log(const UObject* InObject);
	static void Log(const FString& InFuncName, int32 InLineNumber);
};

 

2. /Utilities/CustomLog.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "CustomLog.h"
#include "Engine.h"

DEFINE_LOG_CATEGORY_STATIC(GameProject, Display, All)

void CustomLog::Print(int32 InValue, int32 InKey, float Duration, FColor InColor)
{
	GEngine->AddOnScreenDebugMessage(InKey, Duration, InColor, FString::FromInt(InValue));
}

void CustomLog::Print(float InValue, int32 InKey, float Duration, FColor InColor)
{
	GEngine->AddOnScreenDebugMessage(InKey, Duration, InColor, FString::SanitizeFloat(InValue));
}

void CustomLog::Print(const FString& InValue, int32 InKey, float Duration, FColor InColor)
{
	GEngine->AddOnScreenDebugMessage(InKey, Duration, InColor, InValue);
}

void CustomLog::Print(const FVector& InValue, int32 InKey, float Duration, FColor InColor)
{
	GEngine->AddOnScreenDebugMessage(InKey, Duration, InColor, InValue.ToString());
}

void CustomLog::Print(const FRotator& InValue, int32 InKey, float Duration, FColor InColor)
{
	GEngine->AddOnScreenDebugMessage(InKey, Duration, InColor, InValue.ToString());
}

void CustomLog::Log(int32 InValue)
{
	UE_LOG(GameProject, Display, L"%d", InValue);
}

void CustomLog::Log(float InValue)
{
	UE_LOG(GameProject, Display, L"%f", InValue);
}

void CustomLog::Log(const FString& InValue)
{
	UE_LOG(GameProject, Display, L"%s", *InValue);
}

void CustomLog::Log(const FVector& InValue)
{
	UE_LOG(GameProject, Display, L"%s", *InValue.ToString());
}

void CustomLog::Log(const FRotator& InValue)
{
	UE_LOG(GameProject, Display, L"%s", *InValue.ToString());
}

void CustomLog::Log(const UObject* InObject)
{
	FString str;
	if (!!InObject)
		str.Append(InObject->GetName());

	str.Append(!!InObject ? " Not Null" : "Null");

	UE_LOG(GameProject, Display, L"%s", *str);
}

void CustomLog::Log(const FString& InFuncName, int32 InLineNumber)
{
	FString str;
	str.Append(InFuncName);
	str.Append(", ");
	str.Append(FString::FromInt(InLineNumber));

	UE_LOG(GameProject, Display, L"%s", *str);
}