A fork of https://github.com/crosspoint-reader/crosspoint-reader
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: logPrintf and predefined log level strings (#1546)

## Summary

* More consistent string formatting in logPrintf
* Moved [ and ] from log level strings into new format string
* Behaviour change: Early exit if user string format fails

## Additional Context

* Should not have performance implications, debug monitor etc work as
before
* Clamp may be unnecessary due to information snprintf currently never
being able to exceed max buffer length, but not having it would bug me

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**PARTIALLY, to reason
about correctness**_

authored by

CSCMe and committed by
GitHub
c656673b 1398aeb1

+14 -25
+11 -22
lib/Logging/Logging.cpp
··· 38 38 va_start(args, format); 39 39 char buf[MAX_ENTRY_LEN]; 40 40 char* c = buf; 41 - // add the timestamp 41 + // add timestamp, level and origin 42 42 { 43 43 unsigned long ms = millis(); 44 - int len = snprintf(c, sizeof(buf), "[%lu] ", ms); 44 + int len = snprintf(c, sizeof(buf), "[%lu] [%s] [%s] ", ms, level, origin); 45 + // erro while writing => return 45 46 if (len < 0) { 46 - return; // encoding error, skip logging 47 + va_end(args); 48 + return; 47 49 } 48 - c += len; 50 + // clamp c to be in buffer range 51 + c += std::min(len, MAX_ENTRY_LEN); 49 52 } 50 - // add the level 53 + // add the user message 51 54 { 52 - const char* p = level; 53 - size_t remaining = sizeof(buf) - (c - buf); 54 - while (*p && remaining > 1) { 55 - *c++ = *p++; 56 - remaining--; 57 - } 58 - if (remaining > 1) { 59 - *c++ = ' '; 60 - } 61 - } 62 - // add the origin 63 - { 64 - int len = snprintf(c, sizeof(buf) - (c - buf), "[%s] ", origin); 55 + int len = vsnprintf(c, sizeof(buf) - (c - buf), format, args); 65 56 if (len < 0) { 66 - return; // encoding error, skip logging 57 + va_end(args); 58 + return; 67 59 } 68 - c += len; 69 60 } 70 - // add the user message 71 - vsnprintf(c, sizeof(buf) - (c - buf), format, args); 72 61 va_end(args); 73 62 if (logSerial) { 74 63 logSerial.print(buf);
+3 -3
lib/Logging/Logging.h
··· 33 33 34 34 #ifdef ENABLE_SERIAL_LOG 35 35 #if LOG_LEVEL >= 0 36 - #define LOG_ERR(origin, format, ...) logPrintf("[ERR]", origin, format "\n", ##__VA_ARGS__) 36 + #define LOG_ERR(origin, format, ...) logPrintf("ERR", origin, format "\n", ##__VA_ARGS__) 37 37 #else 38 38 #define LOG_ERR(origin, format, ...) 39 39 #endif 40 40 41 41 #if LOG_LEVEL >= 1 42 - #define LOG_INF(origin, format, ...) logPrintf("[INF]", origin, format "\n", ##__VA_ARGS__) 42 + #define LOG_INF(origin, format, ...) logPrintf("INF", origin, format "\n", ##__VA_ARGS__) 43 43 #else 44 44 #define LOG_INF(origin, format, ...) 45 45 #endif 46 46 47 47 #if LOG_LEVEL >= 2 48 - #define LOG_DBG(origin, format, ...) logPrintf("[DBG]", origin, format "\n", ##__VA_ARGS__) 48 + #define LOG_DBG(origin, format, ...) logPrintf("DBG", origin, format "\n", ##__VA_ARGS__) 49 49 #else 50 50 #define LOG_DBG(origin, format, ...) 51 51 #endif