Log Issue (Again)

Hello,

So I’m trying to get UE_Log to work in MyProjectCharacter.cpp, and I’m not having much luck.

Here is what I have:

MyProjectCharacter.cpp:

// Header data above this line...
DEFINE_LOG_CATEGORY(LogMyCharacter);

// Constructor and all that jazz goes here [...]

void AMyProjectCharacter::PostBeginPlay()
{
	Super::PostBeginPlay();
	UE_LOG(LogMyCharacter, Log, TEXT("Chickens"));
}

After registering a log and attempting to output via UE_Log, the compiler spits this out:

Any idea why? (I’ve also tried “DEFINE_LOG_CATEGORY(LogMyCharacter)” without the semicolon – the result is the same.

Thing about shootergame is that it explicitly defines log categories outside of the character file. So I don’t know for certain if I’m allowed to make any log statements within the character file, if this is intended behavior, a bug, or just plain user error.

Could use some help on this one…

Two commonly used ways of setting up logs.

  1. Accessible for all files in project:
  • let compiler know about log category in header file included everywhere (Public/MyProject.h) with: DECLARE_LOG_CATEGORY_EXTERN(LogSomething, All, All)
  • add log category code in module source (only file in project with #include "YourProject.generated.inl", by default it’s Private/MyProject.cpp) with: DEFINE_LOG_CATEGORY(LogSomething)
  1. Accessible in single file only (useful for quick debugging):
  • add log category code in file you want to use it (in your case: Private/MyProjectCharacter.cpp) with:
    DEFINE_LOG_CATEGORY_STATIC(LogSomething, All, All)

Verbosity All means, that every UE_LOG call with this category will be shown in output.

Going off of this answer and the response to Fabrice’s other question, everything worked perfectly. Thanks!

DEFINE_LOG_CATEGORY(LogMyCharacter); should be in MyProjectModule.cpp, take a look at ShooterGameModule.cpp.

you then need to add DECLARE_LOG_CATEGORY_EXTERN(LogMyCharacter, Log, All); to your GameInfo.h, take a look at ShooterGame.h

after that you can use the log function like you are doing, except it becomes universal (you can use it from any class)

Hey Oscar,

I gave your solution a go, and I ran into some nasty business with the compiler.

To the devs: How did I do it, you ask?

Version: 1711197

Specs: Win7, 16GB, GTX580, UAC: on

Description: Compiler refuses to compile basic logging functionality.

Repro Steps:

1. Create a new third person project with C++ in rocket. In my case, my new project’s name is MyProject2. Open up VS Express 2012, and open the new MyProject2 solution.

2. Went into MyProject2.h and modified it to show the following:

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#ifndef __MYPROJECT2_H__
#define __MYPROJECT2_H__

#include "Engine.h"
#include "EngineAIClasses.h"
#include "MyProject2Classes.h"

DECLARE_LOG_CATEGORY_EXTERN(LogMyCharacter, Log, All) 

#endif

3. Then I created a custom file called MyProject2Module.cpp and saved it in C:\Users\Markus\Documents\Rocket Projects\MyProject2\Source\MyProject2\Private\, since that’s where all the other CPP files relevant to this game appeared to be saved. In the MyProject2Module.cpp file, transcribed almost verbatim from shootergamemodule.cpp:

#include "MyProject2.h"

#include "MyProject2.generated.inl"

IMPLEMENT_PRIMARY_GAME_MODULE(FDefaultGameModuleImpl, MyProject2, "MyProject2" );

DEFINE_LOG_CATEGORY(LogMyCharacter)

Presto chango, your compiler will now start talking Greek to you when you select the build option from the menu.

I’m also requesting to the devs that new C++ projects come with basic log functionality built in as part of the solution. I mean should a simple log statement be this complex to generate?

Generated projects already have module file - Private/MyProject.cpp. Move log category code there and remove newly added source file.

when speaking to me he said there was no module file, hence why he created one

It’s b/c the naming conventions used in shootergame are very weird. There was explicitly a file named ShooterGameModule.cpp where the log was defined, and so it was assumed that this was done as an optimization for the current architecture. So based on this assumption, I implemented one as well for a brand new project. Hopefully removing that and moving the data to the MyProject.cpp file will fix this. I’ll report back.

What should ‘LogSomething’ be in this instance?