10#include "Misc/Paths.h"
19 ULogFile* LogFile = NewObject<ULogFile>(ULogFile::StaticClass());
23 LogFile->
Create(FileNameWithoutExtension, Settings);
45 FString Folder = FString(
"/Logs/");
46 FString LogDirPath = FPaths::Combine(*ProjectPath, *Folder);
48 if (!std::filesystem::exists(TCHAR_TO_UTF8(*LogDirPath)))
50 if (!std::filesystem::create_directory(TCHAR_TO_UTF8(*LogDirPath)))
61 LogFilePath = FPaths::Combine(*Settings.
FilePath, *FileNameWithoutExtension) + TEXT(
".txt");
65 LogFilePath = FPaths::Combine(*LogDirPath, *FileNameWithoutExtension) + TEXT(
".txt");
85 FString UniqueFileName = FileNameWithoutExtension + TEXT(
"_") + FString::FromInt(suffix++);
86 FString UniqueLogFilePath = FPaths::Combine(*LogDirPath, *UniqueFileName) + TEXT(
".txt");
88 while (std::filesystem::exists(TCHAR_TO_UTF8(*UniqueLogFilePath)))
90 UniqueFileName = FileNameWithoutExtension + TEXT(
"_") + FString::FromInt(suffix++) + TEXT(
".txt");
91 UniqueLogFilePath = FPaths::Combine(*LogDirPath, *UniqueFileName);
94 LogFilePath = UniqueLogFilePath;
103 std::ofstream newFile(TCHAR_TO_UTF8(*LogFilePath), std::ios::app);
104 if (newFile.is_open())
110 std::ifstream file(TCHAR_TO_UTF8(*LogFilePath));
121 if (
FilePath.IsEmpty() || !IsValid(
this))
135 FString MessageToAdd = Text;
138 std::time_t now = std::time(
nullptr);
139 std::tm tm_time = *std::localtime(&now);
141 std::strftime(timestamp,
sizeof(timestamp),
"[%H:%M:%S] ", &tm_time);
142 MessageToAdd = FString(timestamp) + Text;
163 if (std::filesystem::exists(TCHAR_TO_UTF8(*
FilePath)))
165 std::ofstream clearFile(TCHAR_TO_UTF8(*
FilePath), std::ofstream::out | std::ofstream::trunc);
176 ConditionalBeginDestroy();
194 std::string filePathString(TCHAR_TO_UTF8(*
FilePath));
218 if (
FilePath.IsEmpty() || !IsValid(
this))
225 std::string filePathString(TCHAR_TO_UTF8(*
FilePath));
236 std::time_t now = std::time(
nullptr);
237 std::tm tm_time = *std::localtime(&now);
239 std::strftime(timestamp,
sizeof(timestamp),
"[%H:%M:%S] ", &tm_time);
241 LogFileStream << timestamp << TCHAR_TO_UTF8(*Text) << std::endl;
static FString GetDataFolder()
static void OpenFileExplorer(FString Path)
void WriteQueuedMessages()
FLogFileSettings CurrentSettings
void Write(const FString &Text)
std::ofstream LogFileStream
void WriteToFile(const FString &Text, bool UseTimestamp)
static ULogFile * CreateLogFile(const FString &FileNameWithoutExtension, FLogFileSettings Settings=FLogFileSettings())
void Create(const FString &FileNameWithoutExtension, FLogFileSettings Settings)
TArray< FString > MessageQueue
FFileWriteOptions FileWriteOptions
FFileCreationOptions FileCreationOptions