Skip to content

ALogger#

A logger class.

Header:#include <AUI/Logging/ALogger.h>
CMake:aui_link(my_target PUBLIC aui::core)

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;
  }
}

Examples#

examples/basic/hello_world/src/main.cpp

Console Hello World Example - Basic CLI Hello World application.

static constexpr auto LOG_TAG = "MyApp";

AUI_ENTRY {
    ALogger::info(LOG_TAG) << "Hello world!";
    return 0;
}

examples/7guis/cells/src/Formula.cpp

7GUIs Cells - Spreadsheet processor (Excel).

                return "#{}!"_format(e.getMessage());
            }
        };
    } catch (const AException& e) {
        ALogger::err("Formula") << "Can't parse expression " << expression << "\n" << e;
        return [msg = e.getMessage()](const Spreadsheet&) { return "#{}!"_format(msg); };
    }
}

examples/ui/contacts/src/main.cpp

AUI Contacts - Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.

        mSelectedContact = nullptr;
    }

    _<AView> indexedList() {
        return AUI_DECLARATIVE_FOR(group, *mContacts | ranges::views::chunk_by([](const _<Contact>& lhs, const _<Contact>& rhs) {
                                return groupLetter(lhs->displayName) == groupLetter(rhs->displayName);
                            }), AVerticalLayout) {
            auto firstContact = *ranges::begin(group);
            auto firstLetter = groupLetter(firstContact->displayName);
            ALogger::info("Test") << "Computing view for group " << AString(1, firstLetter);

Public Types#

Level#


enum ALogger::Level

Examples#

Constant Description
Level::INFO
Level::WARN
Level::ERR
Level::DEBUG

LogWriter#


struct ALogger::LogWriter

Examples#

Empty structure.

Public Methods#

ALogger#


ALogger::ALogger(AString filename)

Constructor for an extra log file.

Arguments
filename
file name

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

Examples#

examples/basic/hello_world/src/main.cpp

Console Hello World Example - Basic CLI Hello World application.

static constexpr auto LOG_TAG = "MyApp";

AUI_ENTRY {
    ALogger::info(LOG_TAG) << "Hello world!";
    return 0;
}
examples/7guis/cells/src/Formula.cpp

7GUIs Cells - Spreadsheet processor (Excel).

                return "#{}!"_format(e.getMessage());
            }
        };
    } catch (const AException& e) {
        ALogger::err("Formula") << "Can't parse expression " << expression << "\n" << e;
        return [msg = e.getMessage()](const Spreadsheet&) { return "#{}!"_format(msg); };
    }
}
examples/ui/contacts/src/main.cpp

AUI Contacts - Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.

        mSelectedContact = nullptr;
    }

    _<AView> indexedList() {
        return AUI_DECLARATIVE_FOR(group, *mContacts | ranges::views::chunk_by([](const _<Contact>& lhs, const _<Contact>& rhs) {
                                return groupLetter(lhs->displayName) == groupLetter(rhs->displayName);
                            }), AVerticalLayout) {
            auto firstContact = *ranges::begin(group);
            auto firstLetter = groupLetter(firstContact->displayName);
            ALogger::info("Test") << "Computing view for group " << AString(1, firstLetter);

doLogFileAccessSafe#


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

Allows to perform some action (access safely) on log file (which is opened all over the execution process)

Useful when sending log file to remote server.

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

Examples#

examples/ui/contacts/src/main.cpp

AUI Contacts - Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.

        mSelectedContact = nullptr;
    }

    _<AView> indexedList() {
        return AUI_DECLARATIVE_FOR(group, *mContacts | ranges::views::chunk_by([](const _<Contact>& lhs, const _<Contact>& rhs) {
                                return groupLetter(lhs->displayName) == groupLetter(rhs->displayName);
                            }), AVerticalLayout) {
            auto firstContact = *ranges::begin(group);
            auto firstLetter = groupLetter(firstContact->displayName);
            ALogger::info("Test") << "Computing view for group " << AString(1, firstLetter);

log#


LogWriter ALogger::log(Level level, const AString& prefix)

Writer a log entry with LogWriter helper.

Arguments
level
level
prefix
prefix

Examples#

examples/ui/contacts/src/main.cpp

AUI Contacts - Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.

        mSelectedContact = nullptr;
    }

    _<AView> indexedList() {
        return AUI_DECLARATIVE_FOR(group, *mContacts | ranges::views::chunk_by([](const _<Contact>& lhs, const _<Contact>& rhs) {
                                return groupLetter(lhs->displayName) == groupLetter(rhs->displayName);
                            }), AVerticalLayout) {
            auto firstContact = *ranges::begin(group);
            auto firstLetter = groupLetter(firstContact->displayName);
            ALogger::info("Test") << "Computing view for group " << AString(1, firstLetter);

setLogFile#


void ALogger::setLogFile(APath path)

Sets log file.

Arguments
path
path to the log file.

Log file is opened immediately in setLogFile.

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.

Examples#

examples/ui/contacts/src/main.cpp

AUI Contacts - Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.

        mSelectedContact = nullptr;
    }

    _<AView> indexedList() {
        return AUI_DECLARATIVE_FOR(group, *mContacts | ranges::views::chunk_by([](const _<Contact>& lhs, const _<Contact>& rhs) {
                                return groupLetter(lhs->displayName) == groupLetter(rhs->displayName);
                            }), AVerticalLayout) {
            auto firstContact = *ranges::begin(group);
            auto firstLetter = groupLetter(firstContact->displayName);
            ALogger::info("Test") << "Computing view for group " << AString(1, firstLetter);

setLogFileForGlobal#


static void ALogger::setLogFileForGlobal(APath path)

Sets log file for ALogger::global().

Arguments
path
path to the log file.

Examples#

examples/ui/contacts/src/main.cpp

AUI Contacts - Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.

        mSelectedContact = nullptr;
    }

    _<AView> indexedList() {
        return AUI_DECLARATIVE_FOR(group, *mContacts | ranges::views::chunk_by([](const _<Contact>& lhs, const _<Contact>& rhs) {
                                return groupLetter(lhs->displayName) == groupLetter(rhs->displayName);
                            }), AVerticalLayout) {
            auto firstContact = *ranges::begin(group);
            auto firstLetter = groupLetter(firstContact->displayName);
            ALogger::info("Test") << "Computing view for group " << AString(1, firstLetter);