UE4 定义一个全局工程可以使用的日志类名

发布时间 2023-05-06 02:46:20作者: 索智源

使用宏封装了一个文本输出窗口打印

NewTypes.h

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 
 7 // 定义一个全局工程可以使用的日志类名, 通常在头文件中声明,并在源文件中与 DEFINE_LOG_CATEGORY 配对。所有包含头文件的文件都可以访问。
 8 // @param CategoryName,类别名
 9 // @param DefaultVerbosity,样式类型
10 // @param CompileTimeVerbosity,覆盖范围
11 DECLARE_LOG_CATEGORY_EXTERN(SZYLog, Log, All);
12 
13 
14 #ifndef BREAK_IF
15 #define BREAK_IF(x) if(x) break
16 #endif // !BREAK_IF
17 
18 #ifndef BREAK_IF_WARNING
19 #define BREAK_IF_WARNING(x, Format, ...) if (x){UE_LOG(SZYLog, Warning, Format, ##__VA_ARGS__); break;}
20 #endif // !BREAK_IF_WARRING
21 
22 #ifndef BREAK_IF_ERROR
23 #define BREAK_IF_ERROR(x, Format, ...) if (x){UE_LOG(SZYLog, Error, Format, ##__VA_ARGS__); break;}
24 #endif // !BREAK_IF_ERROR

NewTypes.cpp

1 // Fill out your copyright notice in the Description page of Project Settings.
2 
3 
4 #include "NewTypes.h"
5 
6 
7 DEFINE_LOG_CATEGORY(SZYLog)            // 定义日志类别的宏,通常与头文件中的 DECLARE_LOG_CATEGORY_EXTERN 配对。 @param类别名,类别定义

 

调用:

BREAK_IF_WARNING(条件为真, 输出内容)

如果不加 do while 会报错 非法 brank

1     do 
2     {
3         MyScene = CreateDefaultSubobject<USceneComponent>(FName("MyRoot"));
4         BREAK_IF_WARNING(!SetRootComponent(MyScene), TEXT("Set root component error!"))
5         BREAK_IF_WARNING(RootComponent == nullptr, TEXT("Root is nullptr!!!"));
6 
7     } while (false);

 

 

封装了一个屏幕打印

 1 namespace DemoHelper
 2 {
 3 
 4     FORCEINLINE void Debug(FString Message, float Duration = 3.f)
 5     {
 6         if (GEngine) 
 7         {
 8             GEngine->AddOnScreenDebugMessage(-1, Duration, FColor::Yellow, Message);
 9         }
10     }
11 }

调用:DemoHelper::Debug(FString("TEST!!!"));