Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
1
15#include "utility/Logger.h"
16#include <cstring>
18#include "time/PicoTime.h"
19
21
23void Logger::info(const char *msg)
24{
25 log(LOG_INFO, msg);
26}
27
29void Logger::warn(const char *msg)
30{
31 log(LOG_WARN, msg);
32}
33
35void Logger::error(const char *msg)
36{
37 log(LOG_ERROR, msg);
38}
39
42{
43 minLevel = level;
44}
45
47void Logger::enableFileLogging(const std::string &path)
48{
49 logPath = path;
50 logToFile = !logPath.empty();
51}
52
54void Logger::log(LogLevel level, const char *msg)
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}
77
79void Logger::getTimeString(char *buffer, size_t len)
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}
85
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}
101
102bool Logger::forEachLine(const std::function<void(const char* line)>& handler) {
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}
117
118
119
Lightweight structured logging class for embedded applications.
LogLevel
Severity levels for logging.
Definition Logger.h:37
@ LOG_ERROR
Errors (potentially requiring user action)
Definition Logger.h:40
@ LOG_INFO
Informational messages.
Definition Logger.h:38
@ LOG_WARN
Warnings (non-fatal)
Definition Logger.h:39
Time utility functions for the Pico platform (RP2040/RP2350).
static constexpr std::uintptr_t getTypeKey()
Definition AppContext.h:91
static void getTimeString(char *buffer, size_t len)
Definition Logger.cpp:79
static bool forEachLine(const std::function< void(const char *line)> &handler)
Streams each line of the log file to the provided handler.
Definition Logger.cpp:102
static LogLevel minLevel
Definition Logger.h:91
static const char * levelToString(LogLevel level)
Definition Logger.cpp:87
static void info(const char *msg)
Log an informational message.
Definition Logger.cpp:23
static bool logToFile
Definition Logger.h:97
static void error(const char *msg)
Log an error message.
Definition Logger.cpp:35
static void setMinLogLevel(LogLevel level)
Set the minimum log level (filters lower levels).
Definition Logger.cpp:41
static void enableFileLogging(const std::string &path)
Enable writing logs to SD card via a storage manager.
Definition Logger.cpp:47
static void warn(const char *msg)
Log a warning message.
Definition Logger.cpp:29
static std::string logPath
Definition Logger.h:96
static void log(LogLevel level, const char *msg)
Definition Logger.cpp:54
static time_t now()
Get the current epoch time using platform RTC or AON.
Definition PicoTime.cpp:28