Agrarsense
LogFile.h
Go to the documentation of this file.
1// Copyright (c) 2024 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#pragma once
7
8#include "CoreMinimal.h"
9#include "UObject/Object.h"
10
11#include <fstream>
12
13#include "LogFile.generated.h"
14
15UENUM(BlueprintType)
16enum class FFileCreationOptions : uint8
17{
18 Overwrite = 0 UMETA(DisplayName = "Overwrite"),
19 Append = 1 UMETA(DisplayName = "Append"),
20 CreateUnique = 2 UMETA(DisplayName = "CreateUnique")
21};
22
23UENUM(BlueprintType)
24enum class FFileWriteOptions : uint8
25{
26 Immediate = 0 UMETA(DisplayName = "Immediate"),
27 Queue = 1 UMETA(DisplayName = "Queue")
28};
29
30USTRUCT(Blueprintable)
31struct AGRARSENSE_API FLogFileSettings
32{
33 GENERATED_BODY()
34
35 UPROPERTY(EditAnywhere, BlueprintReadWrite)
36 FFileCreationOptions FileCreationOptions = FFileCreationOptions::Overwrite;
37
38 UPROPERTY(EditAnywhere, BlueprintReadWrite)
39 bool Timestamp = true;
40
41 UPROPERTY(EditAnywhere, BlueprintReadWrite)
42 bool KeepFileOpen = false;
43
44 UPROPERTY(EditAnywhere, BlueprintReadWrite)
45 FFileWriteOptions FileWriteOptions = FFileWriteOptions::Immediate;
46
47 UPROPERTY(EditAnywhere, BlueprintReadWrite)
48 int32 QueueLength = 10;
49
50 UPROPERTY(EditAnywhere, BlueprintReadWrite)
51 bool OverrideFilePath = false;
52
53 UPROPERTY(EditAnywhere, BlueprintReadWrite)
54 FString FilePath;
55};
56
61UCLASS(Blueprintable)
62class AGRARSENSE_API ULogFile : public UObject
63{
64 GENERATED_BODY()
65
66public:
67
68 ULogFile();
69
70 ~ULogFile();
71
78 static ULogFile* CreateLogFile(const FString& FileNameWithoutExtension, FLogFileSettings Settings = FLogFileSettings());
79
85 UFUNCTION(BlueprintCallable, Category = "Logging")
86 void Create(const FString& FileNameWithoutExtension, FLogFileSettings Settings);
87
92 UFUNCTION(BlueprintCallable, Category = "Logging")
93 void Write(const FString& Text);
94
98 UFUNCTION(BlueprintCallable, Category = "Logging")
99 void Close();
100
104 UFUNCTION(BlueprintCallable, Category = "Logging")
105 void Clear();
106
110 UFUNCTION(BlueprintCallable, Category = "Logging")
111 void Destroy();
112
116 UFUNCTION(BlueprintCallable, Category = "Logging")
117 void OpenFilePath();
118
122 UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Logging")
123 FString GetLogFilePath()
124 {
125 return FilePath;
126 }
127
128private:
129
133 void WriteQueuedMessages();
134
140 void WriteToFile(const FString& Text, bool UseTimestamp);
141
142 UPROPERTY()
143 TArray<FString> MessageQueue;
144
145 FLogFileSettings CurrentSettings;
146
147 FString FilePath;
148
149 std::ofstream LogFileStream;
150};
FFileWriteOptions
Definition: LogFile.h:25
FFileCreationOptions
Definition: LogFile.h:17