Agrarsense
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
ULogFile Class Reference

#include <LogFile.h>

Inheritance diagram for ULogFile:
Inheritance graph
[legend]
Collaboration diagram for ULogFile:
Collaboration graph
[legend]

Public Member Functions

 ULogFile ()
 
 ~ULogFile ()
 
void Create (const FString &FileNameWithoutExtension, FLogFileSettings Settings)
 
void Write (const FString &Text)
 
void Close ()
 
void Clear ()
 
void Destroy ()
 
void OpenFilePath ()
 
FString GetLogFilePath ()
 

Static Public Member Functions

static ULogFileCreateLogFile (const FString &FileNameWithoutExtension, FLogFileSettings Settings=FLogFileSettings())
 

Private Member Functions

void WriteQueuedMessages ()
 
void WriteToFile (const FString &Text, bool UseTimestamp)
 

Private Attributes

TArray< FString > MessageQueue
 
FLogFileSettings CurrentSettings
 
FString FilePath
 
std::ofstream LogFileStream
 

Detailed Description

LogFile is a Blueprintable class that allows .txt file creation and writing into it. The .txt file is created in the ROOT/Data/ directory.

Definition at line 62 of file LogFile.h.

Constructor & Destructor Documentation

◆ ULogFile()

ULogFile::ULogFile ( )

Definition at line 29 of file LogFile.cpp.

30{
31
32}

◆ ~ULogFile()

ULogFile::~ULogFile ( )

Definition at line 34 of file LogFile.cpp.

35{
37 Close();
38}
void WriteQueuedMessages()
Definition: LogFile.cpp:185
void Close()
Definition: LogFile.cpp:153

References Close(), and WriteQueuedMessages().

Member Function Documentation

◆ Clear()

void ULogFile::Clear ( )

Clear the file if it exists.

Definition at line 161 of file LogFile.cpp.

162{
163 if (std::filesystem::exists(TCHAR_TO_UTF8(*FilePath)))
164 {
165 std::ofstream clearFile(TCHAR_TO_UTF8(*FilePath), std::ofstream::out | std::ofstream::trunc);
166 clearFile.close();
167 }
168}
FString FilePath
Definition: LogFile.h:147

References FilePath.

Referenced by Create().

◆ Close()

void ULogFile::Close ( )

Close the file if it's open.

Definition at line 153 of file LogFile.cpp.

154{
155 if (LogFileStream.is_open())
156 {
157 LogFileStream.close();
158 }
159}
std::ofstream LogFileStream
Definition: LogFile.h:149

References LogFileStream.

Referenced by Destroy(), and ~ULogFile().

◆ Create()

void ULogFile::Create ( const FString &  FileNameWithoutExtension,
FLogFileSettings  Settings 
)

Create the log file.

Parameters
FileNameWithoutExtensionThe name of the log file without extension.
SettingsThe settings for creating the log file.

Definition at line 40 of file LogFile.cpp.

41{
42 CurrentSettings = Settings;
43
44 FString ProjectPath = UAgrarsensePaths::GetDataFolder();
45 FString Folder = FString("/Logs/");
46 FString LogDirPath = FPaths::Combine(*ProjectPath, *Folder);
47
48 if (!std::filesystem::exists(TCHAR_TO_UTF8(*LogDirPath)))
49 {
50 if (!std::filesystem::create_directory(TCHAR_TO_UTF8(*LogDirPath)))
51 {
52 // Failed to create the directory, return
53 return;
54 }
55 }
56
57 FString LogFilePath;
58
59 if (Settings.OverrideFilePath && !Settings.FilePath.IsEmpty())
60 {
61 LogFilePath = FPaths::Combine(*Settings.FilePath, *FileNameWithoutExtension) + TEXT(".txt");
62 }
63 else
64 {
65 LogFilePath = FPaths::Combine(*LogDirPath, *FileNameWithoutExtension) + TEXT(".txt");
66 }
67
68 FilePath = LogFilePath;
69
71 {
73 // Clear file if it exists
74 Clear();
75 break;
76
78 // Append to existing file if it exists
79 break;
80
82 {
83 // Create unique file by adding number to the end until we find unique filename
84 int suffix = 0;
85 FString UniqueFileName = FileNameWithoutExtension + TEXT("_") + FString::FromInt(suffix++);
86 FString UniqueLogFilePath = FPaths::Combine(*LogDirPath, *UniqueFileName) + TEXT(".txt");
87
88 while (std::filesystem::exists(TCHAR_TO_UTF8(*UniqueLogFilePath)))
89 {
90 UniqueFileName = FileNameWithoutExtension + TEXT("_") + FString::FromInt(suffix++) + TEXT(".txt");
91 UniqueLogFilePath = FPaths::Combine(*LogDirPath, *UniqueFileName);
92 }
93
94 LogFilePath = UniqueLogFilePath;
95 break;
96 }
97
98 default:
99 break;
100 }
101
102 // Try to create the .txt file
103 std::ofstream newFile(TCHAR_TO_UTF8(*LogFilePath), std::ios::app);
104 if (newFile.is_open())
105 {
106 newFile.close();
107 }
108 else
109 {
110 std::ifstream file(TCHAR_TO_UTF8(*LogFilePath));
111 if (file.good())
112 {
113 // The file already exists, return
114 file.close();
115 }
116 }
117}
static FString GetDataFolder()
void Clear()
Definition: LogFile.cpp:161
FLogFileSettings CurrentSettings
Definition: LogFile.h:145
FString FilePath
Definition: LogFile.h:54
bool OverrideFilePath
Definition: LogFile.h:51
FFileCreationOptions FileCreationOptions
Definition: LogFile.h:36

References Append, Clear(), CreateUnique, CurrentSettings, FLogFileSettings::FileCreationOptions, FLogFileSettings::FilePath, FilePath, UAgrarsensePaths::GetDataFolder(), FLogFileSettings::OverrideFilePath, and Overwrite.

Referenced by ASensor::CreateLogFile(), ACamera::CreateLogFile(), and CreateLogFile().

◆ CreateLogFile()

ULogFile * ULogFile::CreateLogFile ( const FString &  FileNameWithoutExtension,
FLogFileSettings  Settings = FLogFileSettings() 
)
static

Static way of creating a ULogFile instance and creating the file at the same time.

Parameters
FileNameWithoutExtensionThe name of the log file without extension.
Settings(Optional) The settings for creating the log file. Default is an empty settings object.
Returns
A pointer to the created ULogFile instance or nullptr.

Definition at line 17 of file LogFile.cpp.

18{
19 ULogFile* LogFile = NewObject<ULogFile>(ULogFile::StaticClass());
20 LogFile->AddToRoot();
21 if (LogFile)
22 {
23 LogFile->Create(FileNameWithoutExtension, Settings);
24 }
25
26 return LogFile;
27}
void Create(const FString &FileNameWithoutExtension, FLogFileSettings Settings)
Definition: LogFile.cpp:40

References Create().

Referenced by SimulatorLog::Create().

◆ Destroy()

void ULogFile::Destroy ( )

Start destroying this UObject.

Definition at line 170 of file LogFile.cpp.

171{
172 if (IsValid(this))
173 {
175 Close();
176 ConditionalBeginDestroy();
177 }
178}

References Close(), and WriteQueuedMessages().

Referenced by ASensor::EndPlay(), and SimulatorLog::Shutdown().

◆ GetLogFilePath()

FString ULogFile::GetLogFilePath ( )
inline

Get the full file path as an FString.

Definition at line 123 of file LogFile.h.

124 {
125 return FilePath;
126 }

◆ OpenFilePath()

void ULogFile::OpenFilePath ( )

Open the file explorer at the path of the file.

Definition at line 180 of file LogFile.cpp.

181{
183}
static void OpenFileExplorer(FString Path)

References FilePath, and UFileUtilities::OpenFileExplorer().

◆ Write()

void ULogFile::Write ( const FString &  Text)

Write a message to the file.

Parameters
TextThe text message to write to the log file.

Definition at line 119 of file LogFile.cpp.

120{
121 if (FilePath.IsEmpty() || !IsValid(this))
122 {
123 return;
124 }
125
127 {
129 {
131 }
132 else
133 {
134 // Add message to queue (with timestamp if CurrentSettings.Timestamp) and return
135 FString MessageToAdd = Text;
137 {
138 std::time_t now = std::time(nullptr);
139 std::tm tm_time = *std::localtime(&now);
140 char timestamp[12];
141 std::strftime(timestamp, sizeof(timestamp), "[%H:%M:%S] ", &tm_time);
142 MessageToAdd = FString(timestamp) + Text;
143 }
144 MessageQueue.Add(MessageToAdd);
145 }
146
147 return;
148 }
149
150 WriteToFile(Text, true);
151}
void WriteToFile(const FString &Text, bool UseTimestamp)
Definition: LogFile.cpp:216
TArray< FString > MessageQueue
Definition: LogFile.h:143
bool Timestamp
Definition: LogFile.h:39
FFileWriteOptions FileWriteOptions
Definition: LogFile.h:45
int32 QueueLength
Definition: LogFile.h:48

References CurrentSettings, FilePath, FLogFileSettings::FileWriteOptions, MessageQueue, Queue, FLogFileSettings::QueueLength, FLogFileSettings::Timestamp, WriteQueuedMessages(), and WriteToFile().

Referenced by SimulatorLog::Log(), and ASensor::WriteToLogFile().

◆ WriteQueuedMessages()

void ULogFile::WriteQueuedMessages ( )
private

Write any queued messages to .txt file.

Definition at line 185 of file LogFile.cpp.

186{
187 if (MessageQueue.IsEmpty() || FilePath.IsEmpty())
188 {
189 return;
190 }
191
192 if (!LogFileStream.is_open())
193 {
194 std::string filePathString(TCHAR_TO_UTF8(*FilePath));
195 LogFileStream.open(filePathString, std::ios::app);
196 if (!LogFileStream.is_open())
197 {
198 // Failed to open the file, return
199 return;
200 }
201 }
202
203 for (const FString& Message : MessageQueue)
204 {
205 WriteToFile(Message, false);
206 }
207
208 MessageQueue.Empty();
209
211 {
212 LogFileStream.close();
213 }
214}
bool KeepFileOpen
Definition: LogFile.h:42

References CurrentSettings, FilePath, FLogFileSettings::KeepFileOpen, LogFileStream, MessageQueue, and WriteToFile().

Referenced by Destroy(), Write(), and ~ULogFile().

◆ WriteToFile()

void ULogFile::WriteToFile ( const FString &  Text,
bool  UseTimestamp 
)
private

Actual method where text is written to the .txt file.

Parameters
TextThe text message to write to the log file.
UseTimestampIndicates whether to include a timestamp in the log message.

Definition at line 216 of file LogFile.cpp.

217{
218 if (FilePath.IsEmpty() || !IsValid(this))
219 {
220 return;
221 }
222
223 if (!LogFileStream.is_open())
224 {
225 std::string filePathString(TCHAR_TO_UTF8(*FilePath));
226 LogFileStream.open(filePathString, std::ios::app);
227 if (!LogFileStream.is_open())
228 {
229 // Failed to open the file, return
230 return;
231 }
232 }
233
234 if (UseTimestamp && CurrentSettings.Timestamp)
235 {
236 std::time_t now = std::time(nullptr);
237 std::tm tm_time = *std::localtime(&now);
238 char timestamp[12];
239 std::strftime(timestamp, sizeof(timestamp), "[%H:%M:%S] ", &tm_time);
240
241 LogFileStream << timestamp << TCHAR_TO_UTF8(*Text) << std::endl;
242 }
243 else
244 {
245 LogFileStream << TCHAR_TO_UTF8(*Text) << std::endl;
246 }
247
249 {
250 LogFileStream.close();
251 }
252}

References CurrentSettings, FilePath, FLogFileSettings::KeepFileOpen, LogFileStream, and FLogFileSettings::Timestamp.

Referenced by Write(), and WriteQueuedMessages().

Member Data Documentation

◆ CurrentSettings

FLogFileSettings ULogFile::CurrentSettings
private

Definition at line 145 of file LogFile.h.

Referenced by Create(), Write(), WriteQueuedMessages(), and WriteToFile().

◆ FilePath

FString ULogFile::FilePath
private

Definition at line 147 of file LogFile.h.

Referenced by Clear(), Create(), OpenFilePath(), Write(), WriteQueuedMessages(), and WriteToFile().

◆ LogFileStream

std::ofstream ULogFile::LogFileStream
private

Definition at line 149 of file LogFile.h.

Referenced by Close(), WriteQueuedMessages(), and WriteToFile().

◆ MessageQueue

TArray<FString> ULogFile::MessageQueue
private

Definition at line 143 of file LogFile.h.

Referenced by Write(), and WriteQueuedMessages().


The documentation for this class was generated from the following files: