Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
OutputDeviceFile.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
6#include "CoreTypes.h"
7#include "HAL/CriticalSection.h"
8#include "HAL/Runnable.h"
9#include "HAL/ThreadSafeCounter.h"
10#include "Logging/LogVerbosity.h"
11#include "Misc/OutputDevice.h"
12#include "Misc/ScopeLock.h"
13#include "Misc/SingleThreadRunnable.h"
14#include "Serialization/Archive.h"
15#include "Templates/Atomic.h"
16#include "Templates/Function.h"
17#include "Templates/UniquePtr.h"
18#include "UObject/NameTypes.h"
19
20class FRunnableThread;
21
22
23/** string added to the filename of timestamped backup log files */
24#define BACKUP_LOG_FILENAME_POSTFIX TEXT("-backup-")
25
26/**
27* Provides a thread-safe serialization interface with a background thread doing the actual writes.
28* [] tags identify which thread owns a variable or function
29*/
31{
33 {
34 InitialBufferSize = 128 * 1024
35 };
36
37 /** Thread to run the worker FRunnable on. Serializes the ring buffer to disk. */
39 /** Stops this thread */
41
42 /** Writer archive */
44 /** Data ring buffer */
46 /** [WRITER THREAD] Position where the unserialized data starts in the buffer */
47 TAtomic<int32> BufferStartPos;
48 /** [CLIENT THREAD] Position where the unserialized data ends in the buffer (such as if (BufferEndPos > BufferStartPos) Length = BufferEndPos - BufferStartPos; */
49 TAtomic<int32> BufferEndPos;
50 /** [CLIENT THREAD] Sync object for the buffer pos */
52 /** [CLIENT/WRITER THREAD] Outstanding serialize request counter. This is to make sure we flush all requests. */
54 /** [CLIENT/WRITER THREAD] Tells the writer thread, the client requested flush. */
56 /** Sync object for buffer flushes in forkable mode, when forking hasn't occurred yet. */
58
59 /** [WRITER THREAD] Last time the archive was flushed. used in threaded situations to flush the underlying archive at a certain maximum rate. */
61
62 /** [WRITER THREAD] Flushes the archive and reset the flush timer. */
64
65 /** [WRITER THREAD] Serialize the contents of the ring buffer to disk */
67
68 /** [CLIENT THREAD] Flush the memory buffer (doesn't force the archive to flush). Can only be used from inside of BufferPosCritical lock. */
70
71public:
72
74 {
75 FileName, // Appends the filename in the threadname: FAsyncWriter_FileName
76 Sequential, // Uses a global sequential number to append to the threadname: FAsyncWriter_1
77 };
78
80
81 virtual ~FAsyncWriter();
82
83 /** [CLIENT THREAD] Serialize data to buffer that will later be saved to disk by the async thread */
84 virtual void Serialize(void* InData, int64 Length) override;
85
86 /** Flush all buffers to disk */
87 void Flush();
88
89 //~ Begin FRunnable Interface.
90 virtual bool Init();
91 virtual uint32 Run();
92 virtual void Stop();
93 virtual FSingleThreadRunnable* GetSingleThreadInterface() override { return this; }
94 //~ End FRunnable Interface
95
96protected:
97 //~ Begin FSingleThreadRunnable Interface.
98 /** [CLIENT THREAD] A substitute for Run() for when threading is disabled. */
99 virtual void Tick() override;
100 //~ End FSingleThreadRunnable Interface
101
102};
103
105{
106 UTF8,
108};
109
110/**
111* File output device (Note: Only works if ALLOW_LOG_FILE && !NO_LOGGING is true, otherwise Serialize does nothing).
112*/
114{
115public:
116 /**
117 * Constructor, initializing member variables.
118 *
119 * @param InFilename Filename to use, can be nullptr. If null, a file name will be automatically generated. If a filename is specified but cannot be opened
120 * because it is already open/used by another process, the implementation will try to generate a new name automatically, until the a file
121 * is created or the number of trials exhausted (32).
122 * @param bDisableBackup If true, existing files will not be backed up
123 * @param bCreateWriterLazily If true, delay the creation of the file until something needs to be written, otherwise, open it immediatedly.
124 * @param FileOpenedCallback If bound, invoked when the output file is successfully opened, passing the actual filename.
125 */
126 FOutputDeviceFile(const TCHAR* InFilename = nullptr, bool bDisableBackup = false, bool bAppendIfExists = false, bool bCreateWriterLazily = true, TFunction<void(const TCHAR*)> FileOpenedCallback = TFunction<void(const TCHAR*)>());
127
128 /**
129 * Destructor to perform teardown
130 *
131 */
133
134 /** Sets the filename that the output device writes to. If the output device was already writing to a file, closes that file. */
135 void SetFilename(const TCHAR* InFilename);
136
137 //~ Begin FOutputDevice Interface.
138 /**
139 * Closes output device and cleans up. This can't happen in the destructor
140 * as we have to call "delete" which cannot be done for static/ global
141 * objects.
142 */
143 void TearDown() override;
144
145 /**
146 * Flush the write cache so the file isn't truncated in case we crash right
147 * after calling this function.
148 */
149 void Flush() override;
150
151 virtual void Serialize(const TCHAR* Data, ELogVerbosity::Type Verbosity, const class FName& Category, const double Time) override;
152 virtual void Serialize(const TCHAR* Data, ELogVerbosity::Type Verbosity, const class FName& Category) override;
153 virtual bool CanBeUsedOnAnyThread() const override
154 {
155 return true;
156 }
157 virtual bool CanBeUsedOnPanicThread() const override
158 {
159 return true;
160 }
161 //~ End FOutputDevice Interface.
162
163 /** Creates a backup copy of a log file if it already exists */
164 static void CreateBackupCopy(const TCHAR* Filename);
165
166 /** Checks if the filename represents a backup copy of a log file */
167 static bool IsBackupCopy(const TCHAR* Filename);
168
169 /** Add a category name to our inclusion filter. As soon as one inclusion exists, all others will be ignored */
170 void IncludeCategory(const class FName& InCategoryName);
171
172 /** Returns the filename associated with this output device */
173 const TCHAR* GetFilename() const { return Filename; }
174
175 bool IsOpened() const;
176
177private:
178
179 /** Writes to a file on a separate thread */
181 /** Archive used by the async writer */
183 /** In bound, invoked when the log file is open successfully for writing, reporting the actual log filename. */
184 TFunction<void(const TCHAR*)> OnFileOpenedFn;
185
186 TCHAR Filename[1024];
188 bool Dead;
189
190 /** Internal data for category inclusion. Must be declared inside CPP file as it uses a TSet<FName> */
191 struct FCategoryInclusionInternal;
192
194
195 /** If true, existing files will not be backed up */
197
198 void WriteRaw(const TCHAR* C);
199
200 /** Creates the async writer and its archive. Returns true if successful. */
201 bool CreateWriter(uint32 MaxAttempts = 32);
202
204};
EByteOrderMark
Definition Enums.h:17834
#define TEXT(x)
Definition Platform.h:1108
FWindowsCriticalSection FCriticalSection
void FlushBuffer()
void FlushArchiveAndResetTimer()
FAsyncWriter(FArchive &InAr, FAsyncWriter::EThreadNameOption NameOption=FAsyncWriter::EThreadNameOption::FileName)
FCriticalSection BufferPosCritical
virtual uint32 Run()
FCriticalSection RunCritical
double LastArchiveFlushTime
virtual FSingleThreadRunnable * GetSingleThreadInterface() override
TArray< uint8 > Buffer
TAtomic< int32 > BufferEndPos
virtual ~FAsyncWriter()
volatile FRunnableThread * Thread
FThreadSafeCounter StopTaskCounter
TAtomic< int32 > BufferStartPos
FThreadSafeCounter WantsArchiveFlush
virtual bool Init()
void SerializeBufferToArchive()
virtual void Serialize(void *InData, int64 Length) override
FThreadSafeCounter SerializeRequestCounter
virtual void Stop()
virtual void Tick() override
TFunction< void(const TCHAR *) OnFileOpenedFn)
virtual void Serialize(const TCHAR *Data, ELogVerbosity::Type Verbosity, const class FName &Category, const double Time) override
bool IsOpened() const
FOutputDeviceFile(const TCHAR *InFilename=nullptr, bool bDisableBackup=false, bool bAppendIfExists=false, bool bCreateWriterLazily=true, TFunction< void(const TCHAR *)> FileOpenedCallback=TFunction< void(const TCHAR *)>())
virtual bool CanBeUsedOnPanicThread() const override
void WriteByteOrderMarkToArchive(EByteOrderMark ByteOrderMark)
const TCHAR * GetFilename() const
static bool IsBackupCopy(const TCHAR *Filename)
void WriteRaw(const TCHAR *C)
FAsyncWriter * AsyncWriter
bool CreateWriter(uint32 MaxAttempts=32)
void TearDown() override
void IncludeCategory(const class FName &InCategoryName)
void SetFilename(const TCHAR *InFilename)
void Flush() override
static void CreateBackupCopy(const TCHAR *Filename)
TUniquePtr< FCategoryInclusionInternal > CategoryInclusionInternal
virtual bool CanBeUsedOnAnyThread() const override
virtual void Serialize(const TCHAR *Data, ELogVerbosity::Type Verbosity, const class FName &Category) override