Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
DebugTrace.h
Go to the documentation of this file.
1
22#pragma once
23
24#include <cstdio>
25#include <cstdarg>
26#include <ctime>
27#include <string>
28#include <cstring>
29#include "framework_config.h"
32#include "time/TimeManager.h"
33
34#define TRACE_LVL_INFO 0
35#define TRACE_LVL_WARN 1
36#define TRACE_LVL_ERROR 2
37
38#ifndef TRACE_INCLUDE_TIMESTAMP
39#define TRACE_INCLUDE_TIMESTAMP 1
40#endif
41
42// File trace output (set by framework setup)
43static inline std::string tracePath;
44static inline bool traceToFile = false;
45
51inline void setTraceOutputToFile(StorageManager *sm, const std::string &path)
52{
53 tracePath = path;
54 traceToFile = (sm != nullptr && !path.empty());
55}
56
60inline const char *traceLevelToString(int level)
61{
62 switch (level)
63 {
64 case TRACE_LVL_INFO:
65 return "INFO";
66 case TRACE_LVL_WARN:
67 return "WARN";
68 case TRACE_LVL_ERROR:
69 return "ERROR";
70 default:
71 return "???";
72 }
73}
74
78inline void shortenFilePath(const char *fullPath, char *buffer, size_t maxLen)
79{
80 const char *slash = strrchr(fullPath, '/');
81 snprintf(buffer, maxLen, "%s", slash ? slash + 1 : fullPath);
82}
83
90
95inline std::string getFormattedTimestamp()
96{
97 static bool timeAvailable = false;
98 static const TimeManager *tm = nullptr;
99
100 if (!timeAvailable)
101 {
103 timeAvailable = tm != nullptr;
104 }
105
106 if (tm)
107 {
108 std::string formatted = tm->currentTimeForTrace();
109 return tm->currentTimeForTrace();
110 }
111 else
112 {
113
114 return std::string("");
115 }
116}
117
127inline void traceLog(const char *module, int level, const char *file, int line, const char *func, const char *format, ...)
128{
129 if (level < TRACE_LEVEL_MIN)
130 return;
131
132 char fileBuf[64];
133 shortenFilePath(file, fileBuf, sizeof(fileBuf));
134
135 char msgBuf[256];
136 va_list args;
137 va_start(args, format);
138 vsnprintf(msgBuf, sizeof(msgBuf), format, args);
139 va_end(args);
140 std::string formatted;
141 std::string timestamp = "";
142
143#if TRACE_INCLUDE_TIMESTAMP
144 timestamp = getFormattedTimestamp();
145
146 formatted = timestamp + "[" + traceLevelToString(level) + "] [" +
147 module + "] " + fileBuf + ":" + std::to_string(line) +
148 " (" + func + "): " + msgBuf + "\n";
149
150#endif
151 if (traceToFile)
152 {
154 if (storage)
155 {
156 storage->appendToFile(tracePath, reinterpret_cast<const uint8_t *>(formatted.c_str()), formatted.length());
157 }
158 }
159 else
160 {
161
162 fputs(formatted.c_str(), stdout);
163 fflush(stdout);
164 }
165}
166
170#define TRACE_INIT(MODULE_NAME) \
171 static constexpr const char *TRACE_MODULE = #MODULE_NAME; \
172 static constexpr int TRACE_ENABLED = TRACE_##MODULE_NAME;
173
177#define TRACEF(level, ...) \
178 do \
179 { \
180 if (TRACE_ENABLED && (level) >= TRACE_LEVEL_MIN) \
181 traceLog(TRACE_MODULE, level, __FILE__, __LINE__, __func__, __VA_ARGS__); \
182 } while (0)
183
187#define TRACE(...) TRACEF(TRACE_LVL_INFO, __VA_ARGS__)
188
192#define TRACE_WARN(...) TRACEF(TRACE_LVL_WARN, __VA_ARGS__)
193
197#define TRACE_ERROR(...) TRACEF(TRACE_LVL_ERROR, __VA_ARGS__)
#define TRACE_LVL_INFO
Definition DebugTrace.h:34
static std::string tracePath
Definition DebugTrace.h:43
void shortenFilePath(const char *fullPath, char *buffer, size_t maxLen)
Strip file path to filename only.
Definition DebugTrace.h:78
std::string getFormattedTimestamp()
Internal trace log handler.
Definition DebugTrace.h:95
static bool traceToFile
Definition DebugTrace.h:44
void traceLog(const char *module, int level, const char *file, int line, const char *func, const char *format,...)
Internal trace log function.
Definition DebugTrace.h:127
const char * traceLevelToString(int level)
Convert trace level to string.
Definition DebugTrace.h:60
TraceTimeFormat
Definition DebugTrace.h:85
@ TRACE_TIME_LOCAL
Definition DebugTrace.h:87
@ TRACE_TIME_UTC
Definition DebugTrace.h:86
void setTraceOutputToFile(StorageManager *sm, const std::string &path)
Set trace output to SD file using FatFsStorageManager.
Definition DebugTrace.h:51
#define TRACE_LVL_ERROR
Definition DebugTrace.h:36
static TraceTimeFormat traceTimeFormat
Definition DebugTrace.h:89
#define TRACE_LVL_WARN
Definition DebugTrace.h:35
Abstract interface for file and directory storage backends.
static AppContext & getInstance()
T * getService() const
Definition AppContext.h:45
Abstract base class for storage access and file operations.
virtual bool appendToFile(const std::string &path, const uint8_t *data, size_t size)=0
Append data to a file.
std::string currentTimeForTrace() const
Get the current time formatted as a string.
Delegates to user or system configuration.
#define TRACE_LEVEL_MIN