ASL
Log Class Reference

Detailed Description

Log is a utility to log messages to either the console, a file or both.

Messages have a category (usuarlly a module or area identifier) and a level (to distinguish errors, warnings, information and debug messages), and are written together with the current date and time. By default messages are written to a file named "log.log" and to the console (with errors and warnings colored). These, as well as the maximum log level of messages logged, can be configured.

Default settings can be changed like this:

Log::setFile("app.log"); // write to this file (default is "log.log")
Log::useConsole(false); // do not write to the console
Log::setMaxLevel(Log::INFO); // skip debug and verbose messages (default is DEBUG)
static void setMaxLevel(int level)
Sets the maximum level of messages to be logged; By default this is 3, so messages up to level DEBUG ...
static void useConsole(bool on)
Enables or disables writing messages to the console.
static void setFile(const String &file)
Sets the name of the file to write messages to.

The easiest way to write messages is with the provided macro ASL_LOG_ similar to a printf call. It uses the current source file as category.

ASL_LOG_(ERR, "Port %i already in use", port);
#define ASL_LOG_(LEVEL,...)
Log a formatted message with the given level (ERR, WARNING, INFO, DEBUG or VERBOSE)
Definition: Log.h:129

Writes something like:

[2021-03-18T12:35:00][Server] ERROR: Port 80 already in use

There are recommended shorter macros for each log level: ASL_LOG_X (with X one of E, W, I, D, V):

ASL_LOG_E("Port %i already in use", port);
#define ASL_LOG_E(...)
Log a formatted message with the ERROR level.
Definition: Log.h:136

On Android this class uses the system logger.

Log files do not grow indefinitely. When reaching about 1 MB, they will be moved to a file with "-1" appended to its name (like "log-1.log"), and a new empty file will be started. Any logs older than that will be lost.

#include <Log.h>

Inheritance diagram for Log:
Singleton< Log >

Public Types

enum  Level
 Message levels (ERR, WARNING, INFO, DEBUG, VERBOSE).
 

Static Public Member Functions

static void setFile (const String &file)
 Sets the name of the file to write messages to.
 
static void enable (bool on)
 Enables or disables logging.
 
static void useConsole (bool on)
 Enables or disables writing messages to the console.
 
static void useFile (bool on)
 Enables or disables writing messages to a file.
 
static void setMaxLevel (int level)
 Sets the maximum level of messages to be logged; By default this is 3, so messages up to level DEBUG are logged.
 
static int maxLevel ()
 Gets the current maximum log level.
 
- Static Public Member Functions inherited from Singleton< Log >
static Loginstance ()
 Returns a pointer to an instance of class T, creating it on the first call.
 

Friends

void log (const String &cat, Log::Level level, const String &message)
 
void log (const String &cat, Log::Level level, const char *fmt,...) ASL_PRINTF_W2(3)
 Writes a printf-like formatted log message with the given category and log level.
 

Friends And Related Function Documentation

◆ log

void log ( const String cat,
Log::Level  level,
const String message 
)
friend
Writes a log message with the given category and log level.

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