Skip to content

Commit

Permalink
Add ability to log 'per session', i.e. create a new file each time yo…
Browse files Browse the repository at this point in the history
…u click play.
  • Loading branch information
mbellgardt committed Dec 12, 2019
1 parent 5efdfae commit 9646e17
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Binaries/
Intermediate/
14 changes: 10 additions & 4 deletions Source/UniversalLogging/Private/LogStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

#include "HAL/PlatformFilemanager.h"

LogStreamImpl::LogStreamImpl(const FString filepath, const FString filename)
LogStreamImpl::LogStreamImpl(const FString filepath, const FString filename, const bool per_session)
: _filepath(filepath)
, _filename(filename)
, _per_session(per_session)
, _is_open(false)
, _is_valid(false)
, _file_handle(nullptr)
{
Open();
if (_is_open)
Expand Down Expand Up @@ -38,8 +40,12 @@ bool LogStreamImpl::GetIsValid()

void LogStreamImpl::Open()
{
FString file_path = FPaths::ConvertRelativePathToFull(FPaths::ProjectDir() + _filepath) + "/" + _filename;
IPlatformFile& platform_file = FPlatformFileManager::Get().GetPlatformFile();
FString file_path = FPaths::ConvertRelativePathToFull(FPaths::ProjectDir() + _filepath);
if(_per_session)
file_path += "/" + UniLog.GetSessionIdentifier();
platform_file.CreateDirectoryTree(*file_path);
file_path += "/" + _filename;
_file_handle = platform_file.OpenWrite(*file_path);
if (!_file_handle)
{
Expand All @@ -53,8 +59,8 @@ void LogStreamImpl::Open()

void LogStreamImpl::Close()
{
if (_file_handle)
delete _file_handle;
delete _file_handle;
_file_handle = nullptr;
_is_open = false;
}

Expand Down
3 changes: 2 additions & 1 deletion Source/UniversalLogging/Private/LogStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class LogStreamImpl : public ILogStream
{
public:
LogStreamImpl(const FString filepath= "Saved/Logs", const FString filename = "UniversalLogging.log");
LogStreamImpl(const FString filepath= "Saved/Logs", const FString filename = "UniversalLogging.log", const bool per_session = false);
virtual ~LogStreamImpl();

FString GetFilepath() override;
Expand All @@ -19,6 +19,7 @@ class LogStreamImpl : public ILogStream
private:
const FString _filepath;
const FString _filename;
bool _per_session;

bool _is_open;
bool _is_valid;
Expand Down
40 changes: 34 additions & 6 deletions Source/UniversalLogging/Private/UniversalLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ void UniversalLoggingImpl::StartupModule()

on_pre_world_finish_destroy_delegate_.BindRaw(this, &UniversalLoggingImpl::OnSessionEnd);
FWorldDelegates::OnPreWorldFinishDestroy.Add(on_pre_world_finish_destroy_delegate_);

_session_id = "";
}

void UniversalLoggingImpl::ShutdownModule()
Expand All @@ -21,14 +23,26 @@ void UniversalLoggingImpl::ShutdownModule()

void UniversalLoggingImpl::OnSessionStart(UWorld* world, const UWorld::InitializationValues)
{
if (world->IsGameWorld())
Log("OnSessionStart", "Test");
if (!world->IsGameWorld())
return;
if (world->IsPlayInEditor())
ResetSessionId("PlayInEditor");
else if (world->IsPlayInPreview())
ResetSessionId("PlayInPreview");
else
ResetSessionId("Play");
}

void UniversalLoggingImpl::OnSessionEnd(UWorld* world)
{
if (world->IsGameWorld())
Log("OnSessionEnd", "Test");
if (!world->IsGameWorld())
return;
ResetSessionId("Stopped");

for (auto& elem : _streams)
{
elem.Value->Close();
}
}

ILogStream* UniversalLoggingImpl::NewLogStream(const FString streamname)
Expand All @@ -37,9 +51,9 @@ ILogStream* UniversalLoggingImpl::NewLogStream(const FString streamname)
return _streams[streamname].Get();
}

ILogStream* UniversalLoggingImpl::NewLogStream(const FString streamname, const FString filepath, const FString filename)
ILogStream* UniversalLoggingImpl::NewLogStream(const FString streamname, const FString filepath, const FString filename, bool per_session /* = false*/)
{
_streams.Add(streamname, TUniquePtr<LogStreamImpl>(new LogStreamImpl(filepath, filename)));
_streams.Add(streamname, TUniquePtr<LogStreamImpl>(new LogStreamImpl(filepath, filename, per_session)));
return _streams[streamname].Get();
}

Expand All @@ -56,6 +70,11 @@ ILogStream * UniversalLoggingImpl::GetDefaultLogStream()
return GetLogStream("");
}

FString UniversalLoggingImpl::GetSessionIdentifier()
{
return _session_id;
}

void UniversalLoggingImpl::Log(const FString text, const FString stream /*= ""*/, bool omit_newline /*= false*/)
{
LogStreamImpl* stream_obj = nullptr;
Expand All @@ -68,4 +87,13 @@ void UniversalLoggingImpl::Log(const FString text, const FString stream /*= ""*/
stream_obj->Write(full_text);
}

void UniversalLoggingImpl::ResetSessionId(FString prefix)
{
FString timestamp = FGenericPlatformTime::StrTimestamp();
timestamp = timestamp.Replace(TEXT("/"), TEXT("-"));
timestamp = timestamp.Replace(TEXT(" "), TEXT("_"));
timestamp = timestamp.Replace(TEXT(":"), TEXT("-"));
_session_id = FString::Printf(TEXT("%s_%s"), *prefix, *timestamp);
}

IMPLEMENT_MODULE(UniversalLoggingImpl, UniversalLogging)
6 changes: 5 additions & 1 deletion Source/UniversalLogging/Private/UniversalLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ class UniversalLoggingImpl : public IUniversalLogging
void OnSessionEnd(UWorld*);

ILogStream* NewLogStream(const FString streamname) override;
ILogStream* NewLogStream(const FString streamname, const FString filepath, const FString filename) override;
ILogStream* NewLogStream(const FString streamname, const FString filepath, const FString filename, bool per_session = false) override;
ILogStream* GetLogStream(const FString streamname) override;
ILogStream* GetDefaultLogStream() override;
FString GetSessionIdentifier() override;
void Log(const FString text, const FString stream = "", bool omit_newline = false) override;

void ResetSessionId(FString prefix);

private:
TMap<FString, TUniquePtr<LogStreamImpl>> _streams;
FString _session_id;
TBaseDelegate<void, UWorld*, const UWorld::InitializationValues> on_post_world_initialization_delegate_;
TBaseDelegate<void, UWorld*> on_pre_world_finish_destroy_delegate_;
};
10 changes: 7 additions & 3 deletions Source/UniversalLogging/Public/IUniversalLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class IUniversalLogging : public IModuleInterface
{
public:
/**
* Singleton-like access to this module's interface. This is just for convenience!
* Beware of calling this during the shutdown phase, though. Your module might have been unloaded already.
* Singleton-like access to this module's interface. This should not be necessary as the intended use is through the UniLog macro
*
* @return Returns singleton instance, loading the module on demand if needed.
*/
Expand Down Expand Up @@ -42,7 +41,7 @@ class IUniversalLogging : public IModuleInterface
*
* @return the newly created log stream object.
*/
virtual ILogStream* NewLogStream(const FString streamname, const FString filepath, const FString filename) = 0;
virtual ILogStream* NewLogStream(const FString streamname, const FString filepath, const FString filename, bool per_session = false) = 0;

/**
* Getter for log streams.
Expand All @@ -58,6 +57,11 @@ class IUniversalLogging : public IModuleInterface
*/
virtual ILogStream* GetDefaultLogStream() = 0;

/**
* Get the current session identifier. It uniquely identifies each time the application starts.
*/
virtual FString GetSessionIdentifier() = 0;

/**
* Logs the given text to the given log stream. If no log stream is specified, the default is used.
*/
Expand Down

0 comments on commit 9646e17

Please sign in to comment.