ViaCognita
(ViaCognita)
December 2, 2023, 9:41am
1
Hi!
I’m using Unreal 5.2.1 with C++ in a project called LineTraceProject.
I want to add custom logging, so I have added this to LineTraceProject.h
:
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
DECLARE_LOG_CATEGORY_EXTERN(LogCustom, Log, All);
And this to LineTraceProject.cpp
:
// Copyright Epic Games, Inc. All Rights Reserved.
#include "LineTraceProject.h"
#include "Modules/ModuleManager.h"
IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, LineTraceProject, "LineTraceProject" );
DEFINE_LOG_CATEGORY(LogCustom);
But, when I use it to the LineTraceProjectCharacter.cpp
file:
UE_LOG(LogCustom, Log, TEXT("Tracing line: %s to %s"), *TraceStart.ToCompactString(), *TraceEnd.ToCompactString());
I get the following compiler error:
LineTraceProjectCharacter.cpp(126): error C2065: ‘LogCustom’: identifier not declared
Do I need to add #include "LineTraceProject.h"
in LineTraceProjectCharacter.cpp
?
I thought that it’s not necessary add the LineTraceProject.h
file on each file that use the custom log.
3dRaven
(3dRaven)
December 2, 2023, 11:16am
2
At the top of your header add
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "LineTraceProjectCharacter.generated.h"
DECLARE_LOG_CATEGORY_EXTERN(LogCustom, Log, All);
at the cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "LineTraceProjectCharacter.h"
// at top of code
DEFINE_LOG_CATEGORY(LogCustom);
then
UE_LOG(LogCustom, Log, TEXT("Tracing line: %s to %s"), *TraceStart.ToCompactString(), *TraceEnd.ToCompactString());
should compile
ViaCognita
(ViaCognita)
December 2, 2023, 2:26pm
3
Thanks, but I want to use globally. This is why I have defined it in LineTraceProject.h
.
3dRaven
(3dRaven)
December 2, 2023, 2:38pm
4
Do what the engine does:
define a custom class to hold your globals (Unreal has CoreGlobals h and cpp)
In your custom globals cpp file add your DEFINE_LOG_CATEGORY(LogCustom); and include it in your header where you want to use it.
Maybe this could be done in a subsystem? I’ll give it a look over.
==================================
Edit: found the perfect middle-ground:
Create 2 files
the global class header file
the global class header file
I called my files MyGlobal.h & MyGlobal.cpp
inside of the header put:
#pragma once
#include "CoreMinimal.h"
DECLARE_LOG_CATEGORY_EXTERN(LogCustom, Log, All);
inside of the CPP
#include "MyGlobal.h" // your header name here
DEFINE_LOG_CATEGORY(LogCustom);
Then inside of your main file in this case
LineTraceProject.h because the project name is LineTraceProject
#pragma once
#include "CoreMinimal.h"
#include "MyGlobal.h" // <== your global files header
Now you can use the custom log globally within the project
2 Likes