AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ALogger Class Referencefinal

A logger class. More...

#include <AUI/Logging/ALogger.h>

Classes#

struct  LogWriter
 

Public Types#

enum  Level { INFO , WARN , ERR , DEBUG }
 

Public Member Functions#

 ALogger (AString filename)
 Constructor for an extra log file.
 
void setDebugMode (bool debug)
 
bool isDebug ()
 
void setLogFile (APath path)
 Sets log file.
 
APath logFile ()
 
void onLogged (std::function< void(const AString &prefix, const AString &message, Level level)> callback)
 
template<aui::invocable Callable>
void doLogFileAccessSafe (Callable action)
 Allows to perform some action (access safely) on log file (which is opened all over the execution process)
 
LogWriter log (Level level, const AString &prefix)
 Writer a log entry with LogWriter helper.
 

Static Public Member Functions#

static ALoggerglobal ()
 
static void setLogFileForGlobal (APath path)
 Sets log file for ALogger::global().
 
static LogWriter info (const AString &str)
 
static LogWriter warn (const AString &str)
 
static LogWriter err (const AString &str)
 
static LogWriter debug (const AString &str)
 

Detailed Description#

Logger is used for journaling application events like errors or some user actions.

It provides some extra functions like logging to file and output formatting.

It provides std::cout-like API.

Example:

ALogger::info("MyApp") << "Hello world!";

Possible output:

[00:47:02][UI Thread][Logger][INFO]: Hello world!

It's convenient to define LOG_TAG variable for your class:

static constexpr auto LOG_TAG = "MyDownloader";
class MyDownloader {
public:
  void someAction() {
    ALogger::info(LOG_TAG) << "someAction() called";
  }
  void downloadFile(const AString& name) {
    ALogger::info(LOG_TAG) << "Downloading file: " << name;
  }
}
Represents a Unicode character string.
Definition AString.h:38

Constructor & Destructor Documentation#

◆ ALogger()#

ALogger::ALogger ( AString filename)
inline
Parameters
filenamefile name

For the global logger, use ALogger::info, ALogger::warn, etc...

Member Function Documentation#

◆ doLogFileAccessSafe()#

template<aui::invocable Callable>
void ALogger::doLogFileAccessSafe ( Callable action)
inline

Useful when sending log file to remote server.

Note
Windows, for instance, doesn't allow to read the file when it's already opened

◆ log()#

LogWriter ALogger::log ( Level level,
const AString & prefix )
inline
Parameters
levellevel
prefixprefix

◆ setLogFile()#

void ALogger::setLogFile ( APath path)
inline
Parameters
pathpath to the log file.

Log file is opened immediately in setLogFile.

Note
If you want to change the log file of ALogger::global(), consider using ALogger::setLogFileForGlobal instead. ALogger::global().setLogFile(...) expression would cause the default log file location to open and to close immediately, when opening a log file in the specified location, causing empty file and two Log file: entries.

◆ setLogFileForGlobal()#

static void ALogger::setLogFileForGlobal ( APath path)
static
Parameters
pathpath to the log file.
See also
ALogger::setLogFile