Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
Logger Class Reference

Basic timestamped logger with optional SD file logging. More...

#include <Logger.h>

+ Collaboration diagram for Logger:

Static Public Member Functions

static void info (const char *msg)
 Log an informational message.
 
static void warn (const char *msg)
 Log a warning message.
 
static void error (const char *msg)
 Log an error message.
 
static void setMinLogLevel (LogLevel level)
 Set the minimum log level (filters lower levels).
 
static void enableFileLogging (const std::string &path)
 Enable writing logs to SD card via a storage manager.
 
static bool forEachLine (const std::function< void(const char *line)> &handler)
 Streams each line of the log file to the provided handler.
 

Static Private Member Functions

static void log (LogLevel level, const char *msg)
 
static void getTimeString (char *buffer, size_t len)
 
static const char * levelToString (LogLevel level)
 

Static Private Attributes

static LogLevel minLevel = LOG_INFO
 
static std::string logPath = ""
 
static bool logToFile = false
 

Detailed Description

This logger is designed for embedded use, providing filtered logging based on severity and timestamp, and supporting optional output to SD.

Definition at line 50 of file Logger.h.

Member Function Documentation

◆ enableFileLogging()

void Logger::enableFileLogging ( const std::string &  path)
static

Enable writing logs to SD card via a storage manager.

Parameters
pathPath to the log file on SD card (e.g. "/log/system.log").
Parameters
pathPath to the log file on SD card (e.g. "/log/system.log").

Definition at line 47 of file Logger.cpp.

48{
49 logPath = path;
50 logToFile = !logPath.empty();
51}
static bool logToFile
Definition Logger.h:97
static std::string logPath
Definition Logger.h:96

References logPath, and logToFile.

◆ error()

void Logger::error ( const char *  msg)
static

Log an error message.

Parameters
msgThe message to log.
Parameters
msgThe message to log.

Definition at line 35 of file Logger.cpp.

36{
37 log(LOG_ERROR, msg);
38}
@ LOG_ERROR
Errors (potentially requiring user action)
Definition Logger.h:40
static void log(LogLevel level, const char *msg)
Definition Logger.cpp:54

References log(), and LOG_ERROR.

+ Here is the call graph for this function:

◆ forEachLine()

bool Logger::forEachLine ( const std::function< void(const char *line)> &  handler)
static
Parameters
handlerFunction to call with each line (null-terminated C-string).
Returns
false if the log file could not be opened, true otherwise.

Definition at line 102 of file Logger.cpp.

102 {
103 if (logPath.empty()) return false;
104
105 auto* storage = AppContext::get<StorageManager>();
106 auto reader = storage->openReader(logPath);
107 if (!reader) return false;
108
109 char line[128];
110 while (reader->readLine(line, sizeof(line))) {
111 handler(line);
112 }
113
114 reader->close();
115 return true;
116}
static constexpr std::uintptr_t getTypeKey()
Definition AppContext.h:91

References AppContext::getTypeKey(), and logPath.

+ Here is the call graph for this function:

◆ getTimeString()

void Logger::getTimeString ( char *  buffer,
size_t  len 
)
staticprivate

Definition at line 79 of file Logger.cpp.

80{
81 time_t now = PicoTime::now();
82 struct tm *t = gmtime(&now); // Use UTC
83 strftime(buffer, len, "%Y-%m-%dT%H:%M:%SZ", t); // ISO format
84}
static time_t now()
Get the current epoch time using platform RTC or AON.
Definition PicoTime.cpp:28

References PicoTime::now().

Referenced by log().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ info()

void Logger::info ( const char *  msg)
static

Log an informational message.

Parameters
msgThe message to log.
Parameters
msgThe message to log.

Definition at line 23 of file Logger.cpp.

24{
25 log(LOG_INFO, msg);
26}
@ LOG_INFO
Informational messages.
Definition Logger.h:38

References log(), and LOG_INFO.

+ Here is the call graph for this function:

◆ levelToString()

const char * Logger::levelToString ( LogLevel  level)
staticprivate

Definition at line 87 of file Logger.cpp.

88{
89 switch (level)
90 {
91 case LOG_INFO:
92 return "INFO";
93 case LOG_WARN:
94 return "WARN";
95 case LOG_ERROR:
96 return "ERROR";
97 default:
98 return "???";
99 }
100}
@ LOG_WARN
Warnings (non-fatal)
Definition Logger.h:39

References LOG_ERROR, LOG_INFO, and LOG_WARN.

Referenced by log().

+ Here is the caller graph for this function:

◆ log()

void Logger::log ( LogLevel  level,
const char *  msg 
)
staticprivate

Definition at line 54 of file Logger.cpp.

55{
56 if (level < minLevel)
57 return;
58
59 char timeBuf[32];
60 getTimeString(timeBuf, sizeof(timeBuf));
61 const char *levelStr = levelToString(level);
62
63 // Output to stdout
64 printf("[%s] [%s] %s\n", timeBuf, levelStr, msg);
65
66 // Optionally append to log file
67 if (logToFile) {
68 auto* storage = AppContext::get<StorageManager>();
69 if (storage) {
70 char fullMsg[256];
71 snprintf(fullMsg, sizeof(fullMsg), "[%s] [%s] %s\n", timeBuf, levelStr, msg);
72 storage->appendToFile(logPath, reinterpret_cast<const uint8_t*>(fullMsg), strlen(fullMsg));
73 }
74 }
75
76}
static void getTimeString(char *buffer, size_t len)
Definition Logger.cpp:79
static LogLevel minLevel
Definition Logger.h:91
static const char * levelToString(LogLevel level)
Definition Logger.cpp:87

References getTimeString(), AppContext::getTypeKey(), levelToString(), logPath, logToFile, and minLevel.

Referenced by error(), info(), and warn().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setMinLogLevel()

void Logger::setMinLogLevel ( LogLevel  level)
static

Set the minimum log level (filters lower levels).

Parameters
levelMessages below this level will be suppressed.
Parameters
levelMessages below this level will be suppressed.

Definition at line 41 of file Logger.cpp.

42{
43 minLevel = level;
44}

References minLevel.

◆ warn()

void Logger::warn ( const char *  msg)
static

Log a warning message.

Parameters
msgThe message to log.
Parameters
msgThe message to log.

Definition at line 29 of file Logger.cpp.

30{
31 log(LOG_WARN, msg);
32}

References log(), and LOG_WARN.

+ Here is the call graph for this function:

Member Data Documentation

◆ logPath

std::string Logger::logPath = ""
inlinestaticprivate

Definition at line 96 of file Logger.h.

Referenced by enableFileLogging(), forEachLine(), and log().

◆ logToFile

bool Logger::logToFile = false
inlinestaticprivate

Definition at line 97 of file Logger.h.

Referenced by enableFileLogging(), and log().

◆ minLevel

LogLevel Logger::minLevel = LOG_INFO
staticprivate

Definition at line 91 of file Logger.h.

Referenced by log(), and setMinLogLevel().


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