UE4打印大全

介绍

都是自描述性的且项目无关的, 可以直接放心使用.

. . .

使用例子

覆盖了以下几种场合 :

  • Example usage: A_LOG();

  • Example usage: A_LOG_1( “Action!” );

  • Example usage: A_LOG_2(“Action!”, “Cut!”);

  • Example usage: A_LOG_N(“Action!”, 88.f);

  • Example usage: A_LOG_M(“Action! %f, %d”, 88.f, 88);

示例代码

PrintHelper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#define ACTION_SHOW_DEBUG_SCREEN_MSG false
#define ACTION_SHOW_DEBUG_OUTPUT_LOG false
#define ACTION_SHOW_DEBUG_OUTPUT_LOG_EXTRA false


//Current Class Name + Function Name where this is called!
#define STR_CUR_CLASS_FUNC (FString(__FUNCTION__))

//Current Class where this is called!
#define STR_CUR_CLASS (FString(__FUNCTION__).Left(FString(__FUNCTION__).Find(TEXT(":"))) )

//Current Function Name where this is called!
#define STR_CUR_FUNC (FString(__FUNCTION__).Right(FString(__FUNCTION__).Len() - FString(__FUNCTION__).Find(TEXT("::")) - 2 ))

//Current Line Number in the code where this is called!
#define STR_CUR_LINE (FString::FromInt(__LINE__))

//Current Class and Line Number where this is called!
#define STR_CUR_CLASS_LINE (STR_CUR_CLASS + "(" + STR_CUR_LINE + ")")

//Current Class and Line Number where this is called!
#define STR_CUR_CLASS_FUNC_LINE (STR_CUR_CLASS_FUNC + "(" + STR_CUR_LINE + ")")

//Current Function Signature where this is called!
#define STR_CUR_FUNCSIG (FString(__FUNCSIG__))


////////////// Screen Message
// Gives you the Class name and exact line number where you print a message to yourself!


#define A_MSG(TimeToDisplay ) if (ACTION_SHOW_DEBUG_SCREEN_MSG) (GEngine->AddOnScreenDebugMessage(-1, (float)TimeToDisplay, FColor::Red, *(STR_CUR_CLASS_FUNC_LINE )) )

#define A_MSG_1(TimeToDisplay, StringParam1) if (ACTION_SHOW_DEBUG_SCREEN_MSG) (GEngine->AddOnScreenDebugMessage(-1, (float)TimeToDisplay, FColor::Red, *(STR_CUR_CLASS_FUNC_LINE + " : " + StringParam1)) )

#define A_MSG_2(TimeToDisplay, StringParam1, StringParam2) if (ACTION_SHOW_DEBUG_SCREEN_MSG) (GEngine->AddOnScreenDebugMessage(-1, (float)TimeToDisplay, FColor::Red, *(STR_CUR_CLASS_FUNC_LINE + " : " + StringParam1 + " " + StringParam2)) )

//#define A_SCREENMSG_F(StringParam1, NumericalParam2) if (ACTION_SHOW_DEBUG_SCREEN_MSG) (GEngine->AddOnScreenDebugMessage(-1, (float)TimeToDisplay, FColor::Red, *(STR_CUR_CLASS_FUNC_LINE + " : " + StringParam1 + " " + FString::SanitizeFloat(NumericalParam2))) )
#define A_MSG_N(TimeToDisplay, StringParam1, NumericalParam2) if (ACTION_SHOW_DEBUG_SCREEN_MSG) (GEngine->AddOnScreenDebugMessage(-1, (float)TimeToDisplay, FColor::Red, FString::Printf( TEXT("%s : %s %f"), *STR_CUR_CLASS_FUNC_LINE, *FString(StringParam1), float(NumericalParam2) ) ) )

#define A_MSG_M(TimeToDisplay, FormatString, ...) if (ACTION_SHOW_DEBUG_SCREEN_MSG) (GEngine->AddOnScreenDebugMessage(-1, (float)TimeToDisplay, FColor::Red, FString::Printf( TEXT("%s : %s"), *STR_CUR_CLASS_FUNC_LINE, *FString::Printf(TEXT(FormatString), ##__VA_ARGS__ ) ) ) )



///////// UE LOG!

// Example usage: A_LOG();
#define A_LOG() if (ACTION_SHOW_DEBUG_OUTPUT_LOG) UE_LOG(LogTemp, Warning, TEXT("%s"), *STR_CUR_CLASS_FUNC_LINE )

// Example usage: A_LOG_1( "Action!" );
#define A_LOG_1(StringParam1) if (ACTION_SHOW_DEBUG_OUTPUT_LOG) UE_LOG(LogTemp, Warning, TEXT("%s : %s"), *STR_CUR_CLASS_FUNC_LINE, *FString(StringParam1))

// Example usage: A_LOG_2("Action!", "Cut!");
#define A_LOG_2(StringParam1, StringParam2) if (ACTION_SHOW_DEBUG_OUTPUT_LOG) UE_LOG(LogTemp, Warning, TEXT("%s : %s %s"), *STR_CUR_CLASS_FUNC_LINE, *FString(StringParam1), *FString(StringParam2))

// Example usage: A_LOG_N("Action!", 88.f);
#define A_LOG_N(StringParam1, NumericalParam2) if (ACTION_SHOW_DEBUG_OUTPUT_LOG) UE_LOG(LogTemp, Warning, TEXT("%s : %s %f"), *STR_CUR_CLASS_FUNC_LINE, *FString(StringParam1), float(NumericalParam2) )

//
#define A_LOG_M(FormatString, ...) if (ACTION_SHOW_DEBUG_OUTPUT_LOG) UE_LOG(LogTemp, Warning, TEXT("%s : %s"), *STR_CUR_CLASS_FUNC_LINE, *FString::Printf(TEXT(FormatString), ##__VA_ARGS__ ) )

class PrintHelper
{
public:

static void ScreenMsg( const FString& Msg );
static void ScreenMsg( const FString& Msg, const FString& Msg2 );
static void ScreenMsg( const FString& Msg, const float FloatValue );
};
PrintHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Fill out your copyright notice in the Description page of Project Settings.


#include "PrintHelper.h"


//ScreenMsg
void PrintHelper::ScreenMsg( const FString& Msg )
{
if (!ACTION_SHOW_DEBUG_SCREEN_MSG) return;
GEngine->AddOnScreenDebugMessage( -1, 55.f, FColor::Red, *Msg );
}

void PrintHelper::ScreenMsg( const FString& Msg, const FString& Msg2 )
{
if (!ACTION_SHOW_DEBUG_SCREEN_MSG) return;
GEngine->AddOnScreenDebugMessage( -1, 55.f, FColor::Red, FString::Printf( TEXT( "%s %s" ), *Msg, *Msg2 ) );
}

void PrintHelper::ScreenMsg( const FString& Msg, const float Value )
{
if (!ACTION_SHOW_DEBUG_SCREEN_MSG) return;
GEngine->AddOnScreenDebugMessage( -1, 55.f, FColor::Red, FString::Printf( TEXT( "%s %f" ), *Msg, Value ) );
}