Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
DebugTrace.h File Reference

Macro-based debug trace system with optional SD file logging. More...

#include <cstdio>
#include <cstdarg>
#include <ctime>
#include <string>
#include <cstring>
#include "framework_config.h"
#include "storage/StorageManager.h"
#include "framework/AppContext.h"
#include "time/TimeManager.h"
+ Include dependency graph for DebugTrace.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define TRACE_LVL_INFO   0
 
#define TRACE_LVL_WARN   1
 
#define TRACE_LVL_ERROR   2
 
#define TRACE_INCLUDE_TIMESTAMP   1
 
#define TRACE_INIT(MODULE_NAME)
 Declare trace usage in a source file for a given module.
 
#define TRACEF(level, ...)
 Core trace macro with level support.
 
#define TRACE(...)   TRACEF(TRACE_LVL_INFO, __VA_ARGS__)
 Default trace (INFO level).
 
#define TRACE_WARN(...)   TRACEF(TRACE_LVL_WARN, __VA_ARGS__)
 WARN level trace.
 
#define TRACE_ERROR(...)   TRACEF(TRACE_LVL_ERROR, __VA_ARGS__)
 ERROR level trace.
 

Enumerations

enum  TraceTimeFormat { TRACE_TIME_UTC , TRACE_TIME_LOCAL }
 

Functions

void setTraceOutputToFile (StorageManager *sm, const std::string &path)
 Set trace output to SD file using FatFsStorageManager.
 
const char * traceLevelToString (int level)
 Convert trace level to string.
 
void shortenFilePath (const char *fullPath, char *buffer, size_t maxLen)
 Strip file path to filename only.
 
std::string getFormattedTimestamp ()
 Internal trace log handler.
 
void traceLog (const char *module, int level, const char *file, int line, const char *func, const char *format,...)
 Internal trace log function.
 

Variables

static std::string tracePath
 
static bool traceToFile = false
 
static TraceTimeFormat traceTimeFormat = TRACE_TIME_LOCAL
 

Detailed Description

Author
Ian Archbell

Part of the PicoFramework application framework. Provides compile-time enabled, level-filtered trace logging for embedded applications. Features:

Controlled via framework_config.h. SD output is enabled if TRACE_USE_SD is set to 1.

Version
0.3
Date
2025-03-31
License:\n MIT License

Definition in file DebugTrace.h.

Macro Definition Documentation

◆ TRACE

#define TRACE (   ...)    TRACEF(TRACE_LVL_INFO, __VA_ARGS__)

Definition at line 187 of file DebugTrace.h.

◆ TRACE_ERROR

#define TRACE_ERROR (   ...)    TRACEF(TRACE_LVL_ERROR, __VA_ARGS__)

Definition at line 197 of file DebugTrace.h.

◆ TRACE_INCLUDE_TIMESTAMP

#define TRACE_INCLUDE_TIMESTAMP   1

Definition at line 39 of file DebugTrace.h.

◆ TRACE_INIT

#define TRACE_INIT (   MODULE_NAME)
Value:
static constexpr const char *TRACE_MODULE = #MODULE_NAME; \
static constexpr int TRACE_ENABLED = TRACE_##MODULE_NAME;

Definition at line 170 of file DebugTrace.h.

178 { \
179 if (TRACE_ENABLED && (level) >= TRACE_LEVEL_MIN) \
180 traceLog(TRACE_MODULE, level, __FILE__, __LINE__, __func__, __VA_ARGS__); \
181 } while (0)
182
186#define TRACE(...) TRACEF(TRACE_LVL_INFO, __VA_ARGS__)
187
191#define TRACE_WARN(...) TRACEF(TRACE_LVL_WARN, __VA_ARGS__)
192
196#define TRACE_ERROR(...) TRACEF(TRACE_LVL_ERROR, __VA_ARGS__)
#define TRACE_LEVEL_MIN

◆ TRACE_LVL_ERROR

#define TRACE_LVL_ERROR   2

Definition at line 36 of file DebugTrace.h.

◆ TRACE_LVL_INFO

#define TRACE_LVL_INFO   0

Definition at line 34 of file DebugTrace.h.

◆ TRACE_LVL_WARN

#define TRACE_LVL_WARN   1

Definition at line 35 of file DebugTrace.h.

◆ TRACE_WARN

#define TRACE_WARN (   ...)    TRACEF(TRACE_LVL_WARN, __VA_ARGS__)

Definition at line 192 of file DebugTrace.h.

◆ TRACEF

#define TRACEF (   level,
  ... 
)
Value:
do \
{ \
if (TRACE_ENABLED && (level) >= TRACE_LEVEL_MIN) \
traceLog(TRACE_MODULE, level, __FILE__, __LINE__, __func__, __VA_ARGS__); \
} while (0)

Definition at line 177 of file DebugTrace.h.

179 { \
180 if (TRACE_ENABLED && (level) >= TRACE_LEVEL_MIN) \
181 traceLog(TRACE_MODULE, level, __FILE__, __LINE__, __func__, __VA_ARGS__); \
182 } while (0)

Enumeration Type Documentation

◆ TraceTimeFormat

Enumerator
TRACE_TIME_UTC 
TRACE_TIME_LOCAL 

Definition at line 84 of file DebugTrace.h.

85{
88};
@ TRACE_TIME_LOCAL
Definition DebugTrace.h:87
@ TRACE_TIME_UTC
Definition DebugTrace.h:86

Function Documentation

◆ getFormattedTimestamp()

std::string getFormattedTimestamp ( )
inline

Formats and dispatches output.

Definition at line 95 of file DebugTrace.h.

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}
static AppContext & getInstance()
T * getService() const
Definition AppContext.h:45
std::string currentTimeForTrace() const
Get the current time formatted as a string.

References TimeManager::currentTimeForTrace(), AppContext::getInstance(), and AppContext::getService().

Referenced by traceLog().

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

◆ setTraceOutputToFile()

void setTraceOutputToFile ( StorageManager sm,
const std::string &  path 
)
inline
Parameters
smStorage manager (or nullptr for console only)
pathFile path (e.g. "/log/trace.txt")

Definition at line 51 of file DebugTrace.h.

52{
53 tracePath = path;
54 traceToFile = (sm != nullptr && !path.empty());
55}
static std::string tracePath
Definition DebugTrace.h:43
static bool traceToFile
Definition DebugTrace.h:44

References tracePath, and traceToFile.

Referenced by FrameworkManager::setupTraceFromConfig().

+ Here is the caller graph for this function:

◆ shortenFilePath()

void shortenFilePath ( const char *  fullPath,
char *  buffer,
size_t  maxLen 
)
inline

Definition at line 78 of file DebugTrace.h.

79{
80 const char *slash = strrchr(fullPath, '/');
81 snprintf(buffer, maxLen, "%s", slash ? slash + 1 : fullPath);
82}

Referenced by traceLog().

+ Here is the caller graph for this function:

◆ traceLevelToString()

const char * traceLevelToString ( int  level)
inline

Definition at line 60 of file DebugTrace.h.

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}
#define TRACE_LVL_INFO
Definition DebugTrace.h:34
#define TRACE_LVL_ERROR
Definition DebugTrace.h:36
#define TRACE_LVL_WARN
Definition DebugTrace.h:35

References TRACE_LVL_ERROR, TRACE_LVL_INFO, and TRACE_LVL_WARN.

Referenced by traceLog().

+ Here is the caller graph for this function:

◆ traceLog()

void traceLog ( const char *  module,
int  level,
const char *  file,
int  line,
const char *  func,
const char *  format,
  ... 
)
inline
Parameters
moduleModule name (e.g. "Network")
levelTrace level (TRACE_LVL_INFO, TRACE_LVL_WARN, TRACE_LVL_ERROR)
fileSource file name
lineSource line number
funcFunction name
formatFormat string for the message

Definition at line 127 of file DebugTrace.h.

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}
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
const char * traceLevelToString(int level)
Convert trace level to string.
Definition DebugTrace.h:60
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.

References StorageManager::appendToFile(), getFormattedTimestamp(), AppContext::getInstance(), AppContext::getService(), shortenFilePath(), TRACE_LEVEL_MIN, traceLevelToString(), tracePath, and traceToFile.

+ Here is the call graph for this function:

Variable Documentation

◆ tracePath

std::string tracePath
inlinestatic

Definition at line 43 of file DebugTrace.h.

Referenced by setTraceOutputToFile(), and traceLog().

◆ traceTimeFormat

TraceTimeFormat traceTimeFormat = TRACE_TIME_LOCAL
static

Definition at line 89 of file DebugTrace.h.

◆ traceToFile

bool traceToFile = false
inlinestatic

Definition at line 44 of file DebugTrace.h.

Referenced by setTraceOutputToFile(), and traceLog().