虛幻引擎:4.22
VS:2017
將自定義的Log日誌,存放到項目工程或者打包程序的Saved/Logs目錄下
1、創建一個C++空類,我命名爲GunTestLog
2、GunTestLog.h文件
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
void UE_GUNTEST_LOG_S(FString FileName, FString Value);
static FORCEINLINE void UE_GUNTEST_LOG(FString FileName, const TCHAR* Format, ...)
{
TCHAR TempStr[1024];
va_list Ptr;
va_start(Ptr, Format);
FCString::GetVarArgs(TempStr, ARRAY_COUNT(TempStr), ARRAY_COUNT(TempStr) - 1, Format, Ptr);
va_end(Ptr);
UE_GUNTEST_LOG_S(FileName, FString(TempStr));
}
3、GunTestLog.cpp文件
// Fill out your copyright notice in the Description page of Project Settings.
#include "GunTestLog.h"
#include "Paths.h"
#include "FileHelper.h"
#include "PlatformFileManager.h"
void UE_GUNTEST_LOG_S(FString _FileName, FString _Value)
{
static bool mIsNewLog = true;
FString mSaveDirectory = FPaths::ProjectSavedDir();
mSaveDirectory.Append("Logs/GunTest/");
FString mTmpFileName = _FileName;
mTmpFileName.Append(".log.txt");
IPlatformFile& mPlatformFile = FPlatformFileManager::Get().GetPlatformFile();
if (mPlatformFile.DirectoryExists(*mSaveDirectory))
{
mPlatformFile.CreateDirectoryTree(*mSaveDirectory);
}
//獲取Log日誌的絕對路徑.
FString mAbsoluteFilePath = mSaveDirectory + "/" + mTmpFileName;
IFileManager* mFileManager = &IFileManager::Get();
uint32 WriteFlags = (uint32)(EFileWrite::FILEWRITE_Append);
//如果Log文件存在,修改名字另存.
if (mPlatformFile.FileExists(*mAbsoluteFilePath) && mIsNewLog)
{
mIsNewLog = false;
FString AbsoluteDestinationPath = mSaveDirectory + "/" + FDateTime::Now().ToString() + "-" + mTmpFileName;
mPlatformFile.MoveFile(*AbsoluteDestinationPath, *mAbsoluteFilePath);
mPlatformFile.DeleteFile(*mAbsoluteFilePath);
}
else
{
FString TextToSave = FDateTime::Now().ToString();
TextToSave.Append("-");
TextToSave.Append(_Value);
TextToSave.Append("\r\n");
FFileHelper::SaveStringToFile(TextToSave, *mAbsoluteFilePath, FFileHelper::EEncodingOptions::AutoDetect, mFileManager, WriteFlags);
}
}
4、在需要保存日誌的.cpp文件中調用
5、第4步我是爲了測試按鍵信息,然後將按鍵日誌保存到Saved/Logs/GunTest/文件目錄下