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:
Possible output: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/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/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/ui/embedded_sdl/src/main.cpp
SDL3 — This code demonstrates how to integrate the AUI Framework with SDL3 to create a window with OpenGL rendering.
examples/app/notes/src/main.cpp
Notes App — Note taking app that demonstrates usage of AListModel, AProperty, user data saving and loading.
return;
}
aui::from_json(AJson::fromStream(AFileInputStream("notes.json")), mNotes);
} catch (const AException& e) {
ALogger::info(LOG_TAG) << "Can't load notes: " << e;
}
}
/// [load]
/// [save]
examples/ui/contacts/src/main.cpp
AUI Contacts — Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.
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);
return Vertical {
Label { firstLetter } AUI_OVERRIDE_STYLE {
Opacity(0.5f),
Padding { 12_dp, 0, 4_dp },
Margin { 0 },
examples/ui/views/src/ExampleWindow.cpp
Views Example — All-in-one views building example.
"Title",
"Message",
AMessageBox::Icon::NONE,
AMessageBox::Button::OK);
ALogger::info("Example") << "Response: " << int(response);
/// [AMessageBox]
}),
_new<AButton>("Cause assertion fail")
.connect(&AView::clicked, this, [&] { AUI_ASSERT_NO_CONDITION("assertion fail"); }),
_new<AButton>("Cause hang")
Public Types#
Level#
enum ALogger::Level
| Constant | Description |
|---|---|
Level::INFO
|
|
Level::WARN
|
|
Level::ERR
|
|
Level::DEBUG
|
|
Level::TRACE
|
LogWriter#
struct ALogger::LogWriter
Empty structure.
Public Methods#
constructor#
ALogger::ALogger(AString filename)
Constructor for an extra log file.
- Arguments
filenamefile name
For the global logger, use ALogger::info, ALogger::warn, etc...
Examples:
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.
log#
LogWriter ALogger::log(Level level, AStringView prefix)
Writer a log entry with LogWriter helper.
- Arguments
levellevelprefixprefix
setLogFile#
void ALogger::setLogFile(APath path)
Sets log file.
- Arguments
pathpath 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.
setLogFileForGlobal#
static void ALogger::setLogFileForGlobal(APath path)
Sets log file for ALogger::global().
- Arguments
pathpath to the log file. See also: `ALogger::setLogFile`.