CyberEngineMkIII
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Classes | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
CYB::Engine::Logger Class Reference

The threaded, queued engine logger. More...

#include <CYBLogger.hpp>

Inheritance diagram for CYB::Engine::Logger:
Inheritance graph
[legend]
Collaboration diagram for CYB::Engine::Logger:
Collaboration graph
[legend]

Classes

struct  LogEntry
 Log entries to be inserted into the queue. More...
 

Public Member Functions

 Logger (API::Logger &AEmergencyLogger)
 Initializes and starts the Logger. Changes the current Context. May block for one second if the preferred file name is taken in order to generate a new one. More...
 
 ~Logger ()
 Shutdown the Logger and empty the queue.
 
void Pause (void) noexcept
 Put the logging thread to sleep. More...
 
void Resume (void) noexcept
 Wake the logging thread. More...
 
void Log (const API::String::CStyle &AMessage, const Level ALevel) finaloverride
 Log a message. Will be written to a text file on the Path returned by CurrentLog. More...
 
void Flush (void) const noexceptfinaloverride
 Delays execution until all pending logs from the current thread have been written to the output. More...
 
const API::String::CStyleCurrentLog (void) const noexceptfinaloverride
 Retrieve the string representation of the Path of the File the Logger is currently writing to. More...
 
void SetDebugLogging (const bool AEnable) noexceptfinaloverride
 Enable/Disable filtering of Level::DEV logs. More...
 

Private Member Functions

void EmptyQueue (void)
 Empty FQueue and write it into FFile. More...
 
API::ThreadableSelfAsThreadable (void) noexcept
 Returns the current object as a Threadable reference. Used to get around some weird VS construction issues. More...
 
void BeginThreadedOperation (void) override
 Writer thread. Runs in a loop until CancelThreadedOperation is called. More...
 
void CancelThreadedOperation (void) override
 Stops the writer thread. Does not guarantee an empty queue. More...
 

Static Private Member Functions

static API::String::Dynamic TimeString (const int AHour, const int AMinute, const int ASecond, const bool AColons)
 Retrieve a string of the given time. More...
 
static API::String::Dynamic TimeString (const bool AColons)
 Retrieve a string of the current time. More...
 
static API::String::Dynamic FormatLogMessage (const API::String::CStyle &AMessage, const Level ALevel)
 Prepend the level and time to a log message. More...
 
static Platform::System::File OpenFileImpl (const Platform::System::Path &ABasePath)
 Prepares the logging File for writing. May block for one second if the preferred filename is taken in order to generate a new one. More...
 
static Platform::System::File OpenFile (void)
 Prepares the logging File for writing. May block for one second if the preferred filename is taken in order to generate a new one. More...
 
static void LogShutdownForEntry (API::UniquePointer< LogEntry > &&AEntry, API::Logger &AEmergency) noexcept
 Takes a LogEntry Queue and logs it out to the current given emergency logger. More...
 

Private Attributes

Memory::Heap FHeap
 The isolated Heap.
 
Context FContext
 The Context to be used when calling the Logger.
 
Platform::System::File FFile
 The File being written to.
 
Platform::System::Mutex FQueueLock
 The lock used to acquire access to FQueue.
 
Platform::System::Mutex FFileLock
 The lock used to acquire access to FFile.
 
API::UniquePointer< LogEntryFQueueHead
 The message queue head.
 
LogEntryFQueueTail
 The message queue tail.
 
API::Interop::Object
< Platform::System::Thread
FThread
 The thread used for writing to the log file.
 
std::atomic_bool FCancelled
 Cancel flag for FThread.
 
std::atomic_bool FDevLog
 Flag for enabling/disabling DEV logs.
 
std::atomic_bool FPaused
 Flag for sleeping the logging thread.
 

Additional Inherited Members

- Public Types inherited from CYB::API::Logger
enum  Level : byte { Level::DEV, Level::INFO, Level::WARN, Level::ERR }
 The severity of the log. More...
 

Detailed Description

The threaded, queued engine logger.

Definition at line 7 of file CYBLogger.hpp.

Constructor & Destructor Documentation

CYB::Engine::Logger::Logger ( API::Logger AEmergencyLogger)

Initializes and starts the Logger. Changes the current Context. May block for one second if the preferred file name is taken in order to generate a new one.

Parameters
AEmergencyLoggerThe Logger to write errors to if this one fails
Thread Safety
This function requires no thread safety
Exceptions
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::STREAM_NOT_WRITABLE. Thrown if the log File could not be opened
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::SYSTEM_PATH_RETRIEVAL_FAILURE. Thrown if the temporary Path could not be retrieved
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::STREAM_NOT_READABLE. Thrown if Path errors occured while setting up the file
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::STREAM_NOT_WRITABLE. Thrown if the log File could not be written to
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::PATH_TOO_LONG. Thrown if the new Path would exceed the limitation
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::PATH_LOST. Thrown if Path errors occured while setting up the file
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::MUTEX_INITIALIZATION_FAILURE. Thrown if one of the Mutexes could not be initialized
CYB::Exception::InternalError code: CYB::Exception::Internal::MEMORY_RESERVATION_FAILURE. Thrown if the Heap memory could not be reserved
CYB::Exception::InternalError code: CYB::Exception::Internal::MEMORY_COMMITAL_FAILURE. Thrown if the Heap memory could not be committed Theoretically this could throw CYB::Exception::SystemData::HEAP_ALLOCATION_FAILURE on some string allocations, but given the initial empty Heap this is practiacally impossible

Definition at line 66 of file CYBLogger.cpp.

66  :
68  FContext(FHeap, AEmergencyLogger, true),
69  FFile(OpenFile()),
70  FThread(nullptr),
71  FCancelled(false),
72  FDevLog(
73 #ifdef DEBUG
74  true
75 #else
76  false
77 #endif
78  )
79 {
80  FQueueHead.reset(static_cast<Allocator&>(Context::GetContext().FAllocator).RawObject<LogEntry>());
81  FQueueHead->FLevel = Level::INFO;
82  FQueueHead->FMessage = FormatLogMessage(API::String::Static(u8"CyberEngineMkIII logger started..."), Level::INFO);
83 
84  FQueueTail = FQueueHead.get();
85 
86  //Flush this message in case it all goes downhill from here
87  EmptyQueue();
88 
89  FThread = FContext.FAllocator.ConstructObject<Platform::System::Thread, API::Interop::NullConstructor>(SelfAsThreadable());
90 }
The inital amount of memory to be commited by the Logger heap.
#define DEBUG
Should be defined or not by user before all inclusions of CyberEngine.hpp. Enables engine debugging l...
static API::String::Dynamic FormatLogMessage(const API::String::CStyle &AMessage, const Level ALevel)
Prepend the level and time to a log message.
Definition: CYBLogger.cpp:165
static Platform::System::File OpenFile(void)
Prepares the logging File for writing. May block for one second if the preferred filename is taken in...
Definition: CYBLogger.cpp:60
Object< AObject > ConstructObject(AArgs &&...AArguments)
Allocates the Object specified by AObject using a specified Constructor.
std::atomic_bool FCancelled
Cancel flag for FThread.
Definition: CYBLogger.hpp:28
Platform::System::File FFile
The File being written to.
Definition: CYBLogger.hpp:20
Generic information.
Context FContext
The Context to be used when calling the Logger.
Definition: CYBLogger.hpp:18
Memory::Heap FHeap
The isolated Heap.
Definition: CYBLogger.hpp:17
API::Interop::Object< Platform::System::Thread > FThread
The thread used for writing to the log file.
Definition: CYBLogger.hpp:26
Allocator & FAllocator
The Allocator.
Definition: Context.hpp:11
API::UniquePointer< LogEntry > FQueueHead
The message queue head.
Definition: CYBLogger.hpp:24
std::atomic_bool FDevLog
Flag for enabling/disabling DEV logs.
Definition: CYBLogger.hpp:28
API::Threadable & SelfAsThreadable(void) noexcept
Returns the current object as a Threadable reference. Used to get around some weird VS construction i...
Definition: CYBLogger.cpp:148
void EmptyQueue(void)
Empty FQueue and write it into FFile.
Definition: CYBLogger.cpp:112
static Context & GetContext(void) noexcept
Get the API's Context.
Definition: CYBInterop.cpp:35
LogEntry * FQueueTail
The message queue tail.
Definition: CYBLogger.hpp:25

Here is the call graph for this function:

Member Function Documentation

void CYB::Engine::Logger::BeginThreadedOperation ( void  )
overrideprivatevirtual

Writer thread. Runs in a loop until CancelThreadedOperation is called.

Thread Safety
This function requires no thread safety
Exceptions
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::STREAM_NOT_WRITABLE. Thrown if the log File could not be written to

Implements CYB::API::Threadable.

Definition at line 152 of file CYBLogger.cpp.

152  {
153  PushContext Push(FContext);
154  while (!FCancelled.load(std::memory_order_relaxed)) {
155  if(!FPaused.load(std::memory_order_acquire))
156  EmptyQueue();
158  }
159 }
std::atomic_bool FCancelled
Cancel flag for FThread.
Definition: CYBLogger.hpp:28
Context FContext
The Context to be used when calling the Logger.
Definition: CYBLogger.hpp:18
static void Sleep(const unsigned int AMilliseconds) noexcept
Put the thread to sleep for at least AMilliseconds.
std::atomic_bool FPaused
Flag for sleeping the logging thread.
Definition: CYBLogger.hpp:28
void EmptyQueue(void)
Empty FQueue and write it into FFile.
Definition: CYBLogger.cpp:112

Here is the call graph for this function:

void CYB::Engine::Logger::CancelThreadedOperation ( void  )
overrideprivatevirtual

Stops the writer thread. Does not guarantee an empty queue.

Thread Safety
This function requires no thread safety

Implements CYB::API::Threadable.

Definition at line 161 of file CYBLogger.cpp.

161  {
162  FCancelled.store(true, std::memory_order_relaxed);
163 }
std::atomic_bool FCancelled
Cancel flag for FThread.
Definition: CYBLogger.hpp:28
const CYB::API::String::CStyle & CYB::Engine::Logger::CurrentLog ( void  ) const
finaloverridevirtualnoexcept

Retrieve the string representation of the Path of the File the Logger is currently writing to.

Returns
The string representation of the Path of the File the Logger is currently writing to
Thread Safety
This function requires no thread safety

Implements CYB::API::Logger.

Definition at line 243 of file CYBLogger.cpp.

243  {
244  return FFile.GetPath()();
245 }
Platform::System::File FFile
The File being written to.
Definition: CYBLogger.hpp:20
const API::Path & GetPath(void) const noexceptfinaloverride
Get the Path indicating the current File.
Definition: CYBFile.cpp:61
void CYB::Engine::Logger::EmptyQueue ( void  )
private

Empty FQueue and write it into FFile.

Thread Safety
This function requires no thread safety
Exceptions
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::STREAM_NOT_WRITABLE. Thrown if the log File could not be written to

Definition at line 112 of file CYBLogger.cpp.

112  {
113  API::UniquePointer<LogEntry> Queue, NextNode;
114  {
115  API::LockGuard Lock(FQueueLock);
116  Queue = std::move(FQueueHead);
117  FQueueTail = nullptr;
118  FFileLock.Lock();
119  }
120 
121  API::LockGuard FLock(FFileLock, true);
122 
123  for (; Queue.get() != nullptr; Queue = std::move(NextNode)) {
124  NextNode = std::move(Queue->FNext);
125  const auto Len(static_cast<unsigned int>(Queue->FMessage.RawLength()));
126  auto Written(0ULL);
127  do {
128  const auto CurrentWrite(FFile.Write(Queue->FMessage.CString() + Written, Len - Written));
129  if (CurrentWrite == 0) {
130  //CurrentContext will most certainly be FContext at this point
131  //but just in case we add some weird behaviour overrides in the future....
132  auto& EmergencyLogger(Context::GetContext().FLogger);
133  EmergencyLogger.SetDebugLogging(true);
134  EmergencyLogger.Log(API::String::Static(u8"Failed to write to primary log. Message follows:"), Level::ERR);
135  EmergencyLogger.Log(Queue->FMessage, Queue->FLevel);
136  if (NextNode != nullptr) {
137  EmergencyLogger.Log(API::String::Static(u8"Remaining entries follow:"), Level::INFO);
138  LogShutdownForEntry(std::move(NextNode), EmergencyLogger);
139  }
141  }
142  Written += CurrentWrite;
143  } while (Written < Len);
144  API::Assert::Equal<unsigned long long>(Written, Len);
145  }
146 }
void Lock(void) noexceptfinaloverride
Acquire a lock on a Mutex. This will block the current thread until the lock is aquired. The Mutex must not be owned by the current thread.
Platform::System::File FFile
The File being written to.
Definition: CYBLogger.hpp:20
Generic information.
Platform::System::Mutex FFileLock
The lock used to acquire access to FFile.
Definition: CYBLogger.hpp:21
Exceptions caused by external call failures or invalid external data. Only classifies ones that can p...
Definition: Exception.hpp:65
API::UniquePointer< LogEntry > FQueueHead
The message queue head.
Definition: CYBLogger.hpp:24
static void LogShutdownForEntry(API::UniquePointer< LogEntry > &&AEntry, API::Logger &AEmergency) noexcept
Takes a LogEntry Queue and logs it out to the current given emergency logger.
Definition: CYBLogger.cpp:101
Generic error for write failures. See functions for further documentation.
Definition: Exception.hpp:73
Platform::System::Mutex FQueueLock
The lock used to acquire access to FQueue.
Definition: CYBLogger.hpp:21
unsigned long long Write(const void *const ABuffer, const unsigned long long AAmount) finaloverride
Write data to the Stream at the current cursor position and advance the cursor by that amount...
static Context & GetContext(void) noexcept
Get the API's Context.
Definition: CYBInterop.cpp:35
LogEntry * FQueueTail
The message queue tail.
Definition: CYBLogger.hpp:25

Here is the call graph for this function:

Here is the caller graph for this function:

void CYB::Engine::Logger::Flush ( void  ) const
finaloverridevirtualnoexcept

Delays execution until all pending logs from the current thread have been written to the output.

Thread Safety
This function requires no thread safety
Attention
This does not call Resume and may result in a deadlock if called while paused

Implements CYB::API::Logger.

Definition at line 232 of file CYBLogger.cpp.

232  {
233  do {
234  {
235  API::LockGuard QueueLock(FQueueLock), FileLock(FFileLock);
236  if (FQueueHead == nullptr)
237  break;
238  }
240  } while (true);
241 }
Platform::System::Mutex FFileLock
The lock used to acquire access to FFile.
Definition: CYBLogger.hpp:21
API::UniquePointer< LogEntry > FQueueHead
The message queue head.
Definition: CYBLogger.hpp:24
static void Sleep(const unsigned int AMilliseconds) noexcept
Put the thread to sleep for at least AMilliseconds.
Platform::System::Mutex FQueueLock
The lock used to acquire access to FQueue.
Definition: CYBLogger.hpp:21

Here is the call graph for this function:

CYB::API::String::Dynamic CYB::Engine::Logger::FormatLogMessage ( const API::String::CStyle AMessage,
const Level  ALevel 
)
staticprivate

Prepend the level and time to a log message.

Parameters
AMessageThe message to format
ALevelThe Level of AMessage
Returns
AMessage prepended with the current time and Level string
Thread Safety
This function requires no thread safety
Exceptions
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::HEAP_ALLOCATION_FAILURE. Thrown if the current heap runs out of memory
CYB::Exception::ViolationError code: CYB::Exception::Violation::INVALID_ENUM. Thrown if the ALevel is invalid

Definition at line 165 of file CYBLogger.cpp.

165  {
166  const char* LevelString;
167  switch (ALevel) {
168  case Level::DEV:
169  LevelString = u8"Debug: ";
170  break;
171  case Level::INFO:
172  LevelString = u8"Info: ";
173  break;
174  case Level::WARN:
175  LevelString = u8"Warning: ";
176  break;
177  case Level::ERR:
178  LevelString = u8"ERROR: ";
179  break;
180  default:
182  }
183 
184  const auto ThreadID(Core::GetCore().ThreadID());
185  API::String::Dynamic Tabs(":");
186  if (ThreadID > 1) {
187  const API::String::Static Tab(u8"\t");
188  for (auto I(1ULL); I < ThreadID; ++I)
189  Tabs += Tab; //This is pretty performant considering the allocater works in chunks
190  }
191  else
192  Tabs += API::String::Static(" ");
193 
194  return TimeString(true) + Tabs + API::String::Static(LevelString) + AMessage;
195 }
Recoverable warnings.
static API::String::Dynamic TimeString(const int AHour, const int AMinute, const int ASecond, const bool AColons)
Retrieve a string of the given time.
Definition: CYBLogger.cpp:8
Generic information.
static Core & GetCore(void) noexcept
Retrieve the Core singleton.
Definition: CYBCore.cpp:45
Exceptions indicating an API contract violation. Should not be anticipated.
Definition: Exception.hpp:32
Debug messages, enabled/disabled by default in engine debug/release builds respectively.
An operation was attempted with an invalid enum code.
Definition: Exception.hpp:36

Here is the call graph for this function:

Here is the caller graph for this function:

void CYB::Engine::Logger::Log ( const API::String::CStyle AMessage,
const Level  ALevel 
)
finaloverridevirtual

Log a message. Will be written to a text file on the Path returned by CurrentLog.

Parameters
AMessageThe message to log
ALevelThe Level of the message
Thread Safety
This function requires no thread safety
Exceptions
CYB::Exception::ViolationError code: CYB::Exception::Violation::INVALID_ENUM. Thrown if the ALevel is invalid In the near impossible case that the isolated Heap the Logger uses runs out of memory, this function will block and flush the write queue to free space This function may silently fail in the case that the log cannot be written to

Implements CYB::API::Logger.

Definition at line 197 of file CYBLogger.cpp.

197  {
198  if (ALevel != Level::DEV || FDevLog.load(std::memory_order_relaxed)) {
199  PushContext Push(FContext); //Use ourselves for allocation
200  bool CritFail(false);
201  for (auto I(0U); I < (Parameters::LOGGER_HEAP_RETRY_COUNT + 1) && !CritFail; ++I) {
202  try {
203  API::UniquePointer<LogEntry> Entry(static_cast<Allocator&>(Context::GetContext().FAllocator).RawObject<LogEntry>());
204  Entry->FMessage = API::String::Dynamic(u8"\n") + FormatLogMessage(AMessage, ALevel);
205  Entry->FLevel = ALevel;
206  auto Tmp(Entry.get());
207 
208  API::LockGuard Lock(FQueueLock);
209  if (FQueueTail != nullptr)
210  FQueueTail->FNext = std::move(Entry);
211  else
212  FQueueHead = std::move(Entry);
213  FQueueTail = Tmp;
214  break;
215  }
216  catch (CYB::Exception::SystemData& AException) {
217  API::Assert::Equal<unsigned int>(AException.FErrorCode, CYB::Exception::SystemData::HEAP_ALLOCATION_FAILURE);
218  try {
219  //Empty the queue, free the memory, and try again
220  EmptyQueue();
221  }
222  catch (CYB::Exception::SystemData& AInnerException) {
223  API::Assert::Equal<unsigned int>(AInnerException.FErrorCode, CYB::Exception::SystemData::STREAM_NOT_WRITABLE);
224  //Now give up
225  CritFail = true;
226  }
227  }
228  }
229  }
230 }
The number of times to try and log again after a failed allocation before giving up.
static API::String::Dynamic FormatLogMessage(const API::String::CStyle &AMessage, const Level ALevel)
Prepend the level and time to a log message.
Definition: CYBLogger.cpp:165
Context FContext
The Context to be used when calling the Logger.
Definition: CYBLogger.hpp:18
const unsigned int FErrorCode
The assigned error code.
Definition: Exception.hpp:18
Exceptions caused by external call failures or invalid external data. Only classifies ones that can p...
Definition: Exception.hpp:65
A heap has no block large enough for a requested allocation and expansion failed. ...
Definition: Exception.hpp:74
API::UniquePointer< LogEntry > FQueueHead
The message queue head.
Definition: CYBLogger.hpp:24
API::UniquePointer< LogEntry > FNext
Next item in the linked list.
Definition: CYBLogger.hpp:12
std::atomic_bool FDevLog
Flag for enabling/disabling DEV logs.
Definition: CYBLogger.hpp:28
Debug messages, enabled/disabled by default in engine debug/release builds respectively.
void EmptyQueue(void)
Empty FQueue and write it into FFile.
Definition: CYBLogger.cpp:112
Generic error for write failures. See functions for further documentation.
Definition: Exception.hpp:73
Platform::System::Mutex FQueueLock
The lock used to acquire access to FQueue.
Definition: CYBLogger.hpp:21
static Context & GetContext(void) noexcept
Get the API's Context.
Definition: CYBInterop.cpp:35
LogEntry * FQueueTail
The message queue tail.
Definition: CYBLogger.hpp:25

Here is the call graph for this function:

Here is the caller graph for this function:

void CYB::Engine::Logger::LogShutdownForEntry ( API::UniquePointer< LogEntry > &&  AEntry,
API::Logger AEmergency 
)
staticprivatenoexcept

Takes a LogEntry Queue and logs it out to the current given emergency logger.

Parameters
AEntryAn xvalue of a LogEntry. All its contents will be logged and deleted
AEmergencythe current emergency logger
Thread Safety
This function requires no thread safety

Definition at line 101 of file CYBLogger.cpp.

101  {
102  //Emergency log the message
103  auto& Message(AEntry->FMessage);
104  const auto ByteIndex(Message.IndexOfByte(':', 3) + 1); //+1 to comp for the space
105  API::Assert::LessThan(0, ByteIndex);
106  API::Assert::LessThan(ByteIndex, Message.RawLength());
107  AEmergency.Log(API::String::Static(Message.CString() + ByteIndex), AEntry->FLevel);
108  if (AEntry->FNext != nullptr)
109  LogShutdownForEntry(std::move(AEntry->FNext), AEmergency);
110 }
static void LessThan(const AType &ALHS, const AType &ARHS) noexcept
Less than assertion function. May not be evaluated.
static void LogShutdownForEntry(API::UniquePointer< LogEntry > &&AEntry, API::Logger &AEmergency) noexcept
Takes a LogEntry Queue and logs it out to the current given emergency logger.
Definition: CYBLogger.cpp:101

Here is the call graph for this function:

CYB::Platform::System::File CYB::Engine::Logger::OpenFile ( void  )
staticprivate

Prepares the logging File for writing. May block for one second if the preferred filename is taken in order to generate a new one.

Returns
The File to be used for logging
Thread Safety
This function requires no thread safety
Exceptions
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::STREAM_NOT_WRITABLE. Thrown if the log File could not be opened
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::SYSTEM_PATH_RETRIEVAL_FAILURE. Thrown if the temporary Path could not be retrieved
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::STREAM_NOT_READABLE. Thrown if Path errors occured while setting up the file
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::PATH_TOO_LONG. Thrown if the new Path would exceed the limitation
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::PATH_LOST. Thrown if Path errors occured while setting up the file Theoretically this could throw CYB::Exception::SystemData::HEAP_ALLOCATION_FAILURE on some string allocations, but given the initial empty Heap this is practically impossible

Definition at line 60 of file CYBLogger.cpp.

60  {
61  //All allocation is done using the Logger's Allocator
62  Platform::System::Path ThePath(API::Path::SystemPath::TEMPORARY);
63  return OpenFileImpl(ThePath);
64 }
The directory for storing data relevant only to this execution.
static Platform::System::File OpenFileImpl(const Platform::System::Path &ABasePath)
Prepares the logging File for writing. May block for one second if the preferred filename is taken in...
Definition: CYBLogger.cpp:30
CYB::Platform::System::File CYB::Engine::Logger::OpenFileImpl ( const Platform::System::Path ABasePath)
staticprivate

Prepares the logging File for writing. May block for one second if the preferred filename is taken in order to generate a new one.

Parameters
ABasePathA Path to the directory to log in
Returns
The File to be used for logging
Thread Safety
This function requires no thread safety
Exceptions
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::STREAM_NOT_WRITABLE. Thrown if the log File could not be opened
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::SYSTEM_PATH_RETRIEVAL_FAILURE. Thrown if the temporary Path could not be retrieved
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::STREAM_NOT_READABLE. Thrown if Path errors occured while setting up the file
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::PATH_TOO_LONG. Thrown if the new Path would exceed the limitation
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::PATH_LOST. Thrown if Path errors occured while setting up the file Theoretically this could throw CYB::Exception::SystemData::HEAP_ALLOCATION_FAILURE on some string allocations, but given the initial empty Heap this is practically impossible

Definition at line 30 of file CYBLogger.cpp.

30  {
31  try {
32 
33  auto Time(time(0)); // get time now
34  auto Now(localtime(&Time));
35 
36  const int Year(Now->tm_year + 1900), Month(Now->tm_mon + 1), Day(Now->tm_mday), Hour(Now->tm_hour), Min(Now->tm_min), Sec(Now->tm_sec);
37 
38  char Buffer[50];
39  memset(Buffer, 0, 50);
40  const auto Length(sprintf(Buffer, u8"Engine Log %d-%d-%d %s.txt", Year, Month, Day, TimeString(Hour, Min, Sec, false).CString()));
41 
42  API::Assert::LessThan(-1, Length);
43  API::Assert::LessThan(Length, 50);
44 
45  API::String::Static AsStatic(Buffer);
46  API::String::UTF8 Formatted(AsStatic);
47 
48  auto LogPath(ABaseLogPath);
49  LogPath.Append(std::move(Formatted), false, false);
50 
51  return Platform::System::File(std::move(LogPath), API::File::Mode::WRITE, API::File::Method::CREATE);
52  }
53  catch (CYB::Exception::SystemData& AException) {
55  throw;
56  }
58  return OpenFileImpl(ABaseLogPath);
59 }
static API::String::Dynamic TimeString(const int AHour, const int AMinute, const int ASecond, const bool AColons)
Retrieve a string of the given time.
Definition: CYBLogger.cpp:8
The File must not exist. It will be created.
static void LessThan(const AType &ALHS, const AType &ARHS) noexcept
Less than assertion function. May not be evaluated.
const unsigned int FErrorCode
The assigned error code.
Definition: Exception.hpp:18
Exceptions caused by external call failures or invalid external data. Only classifies ones that can p...
Definition: Exception.hpp:65
static Platform::System::File OpenFileImpl(const Platform::System::Path &ABasePath)
Prepares the logging File for writing. May block for one second if the preferred filename is taken in...
Definition: CYBLogger.cpp:30
A file that was to be created exists.
Definition: Exception.hpp:70
static void Sleep(const unsigned int AMilliseconds) noexcept
Put the thread to sleep for at least AMilliseconds.

Here is the call graph for this function:

void CYB::Engine::Logger::Pause ( void  )
noexcept

Put the logging thread to sleep.

Thread Safety
This function requires no thread safety

Definition at line 251 of file CYBLogger.cpp.

251  {
252  API::LockGuard QueueLock(FQueueLock), FileLock(FFileLock);
253  FPaused.store(true, std::memory_order_relaxed);
254 }
Platform::System::Mutex FFileLock
The lock used to acquire access to FFile.
Definition: CYBLogger.hpp:21
std::atomic_bool FPaused
Flag for sleeping the logging thread.
Definition: CYBLogger.hpp:28
Platform::System::Mutex FQueueLock
The lock used to acquire access to FQueue.
Definition: CYBLogger.hpp:21
void CYB::Engine::Logger::Resume ( void  )
noexcept

Wake the logging thread.

Thread Safety
This function requires no thread safety

Definition at line 256 of file CYBLogger.cpp.

256  {
257  FPaused.store(false, std::memory_order_relaxed);
258 }
std::atomic_bool FPaused
Flag for sleeping the logging thread.
Definition: CYBLogger.hpp:28
CYB::API::Threadable & CYB::Engine::Logger::SelfAsThreadable ( void  )
privatenoexcept

Returns the current object as a Threadable reference. Used to get around some weird VS construction issues.

Returns
*this;
Thread Safety
This function requires no thread safety

Definition at line 148 of file CYBLogger.cpp.

148  {
149  return *this;
150 }

Here is the caller graph for this function:

void CYB::Engine::Logger::SetDebugLogging ( const bool  AEnable)
finaloverridevirtualnoexcept

Enable/Disable filtering of Level::DEV logs.

Parameters
AEnableEnables DEV logs if true, disables them otherwise
Thread Safety
This function requires no thread safety

Implements CYB::API::Logger.

Definition at line 247 of file CYBLogger.cpp.

247  {
248  FDevLog.store(AEnable, std::memory_order_release);
249 }
std::atomic_bool FDevLog
Flag for enabling/disabling DEV logs.
Definition: CYBLogger.hpp:28
CYB::API::String::Dynamic CYB::Engine::Logger::TimeString ( const int  AHour,
const int  AMinute,
const int  ASecond,
const bool  AColons 
)
staticprivate

Retrieve a string of the given time.

Parameters
AHourThe hour to display
AMinuteThe minute to display
ASecondThe second to display
AColonsIf true, print with brackets and colons. Otherwise use dashes
Returns
An API::String::Dynamic with the given time in the format "[HH:MM:SS]" or "HH-MM-SS" depending on AColons
Thread Safety
This function requires no thread safety
Exceptions
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::HEAP_ALLOCATION_FAILURE. Thrown if the current heap runs out of memory

Definition at line 8 of file CYBLogger.cpp.

8  {
9  char Buffer[50];
10  memset(Buffer, 0, 50);
11  int Length;
12  if (AColons)
13  Length = sprintf(Buffer, u8"[%02d:%02d:%02d]", AHour, AMinute, ASecond);
14  else
15  Length = sprintf(Buffer, u8"%02d-%02d-%02d", AHour, AMinute, ASecond);
16 
17  API::Assert::LessThan(-1, Length);
18  API::Assert::LessThan(Length, 50);
19 
20  return API::String::Dynamic(API::String::Static(Buffer));
21 }
static void LessThan(const AType &ALHS, const AType &ARHS) noexcept
Less than assertion function. May not be evaluated.

Here is the call graph for this function:

CYB::API::String::Dynamic CYB::Engine::Logger::TimeString ( const bool  AColons)
staticprivate

Retrieve a string of the current time.

Parameters
AColonsIf true, print with brackets and colons. Otherwise use dashes
Returns
An API::String::Dynamic with the given time in the format "[HH:MM:SS]" or "HH-MM-SS" depending on AColons
Thread Safety
This function requires no thread safety
Exceptions
CYB::Exception::SystemDataError code: CYB::Exception::SystemData::HEAP_ALLOCATION_FAILURE. Thrown if the current heap runs out of memory

Definition at line 23 of file CYBLogger.cpp.

23  {
24  auto Time(time(0)); // get time now
25  auto Now(localtime(&Time));
26  const auto Hour(Now->tm_hour), Min(Now->tm_min), Sec(Now->tm_sec);
27  return TimeString(Hour, Min, Sec, AColons);
28 }
static API::String::Dynamic TimeString(const int AHour, const int AMinute, const int ASecond, const bool AColons)
Retrieve a string of the given time.
Definition: CYBLogger.cpp:8

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