Logging improvements & other cleanup (#227)

This commit is contained in:
Ken Van Hoeylandt 2025-02-21 14:38:51 +01:00 committed by GitHub
parent 933bc5fb97
commit d29e47f0eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 40 additions and 31 deletions

View File

@ -22,9 +22,7 @@ private:
lv_obj_t* labelWidget = nullptr; lv_obj_t* labelWidget = nullptr;
static inline bool shouldShowLog(LogLevel filterLevel, LogLevel logLevel) { static inline bool shouldShowLog(LogLevel filterLevel, LogLevel logLevel) {
return (filterLevel != LogLevel::None) && return filterLevel >= logLevel;
(logLevel != LogLevel::None) &&
filterLevel >= logLevel;
} }
void updateLogEntries() { void updateLogEntries() {

View File

@ -23,7 +23,7 @@
namespace tt { namespace tt {
struct LogEntry { struct LogEntry {
LogLevel level = LogLevel::None; LogLevel level = LogLevel::Verbose;
char message[TT_LOG_MESSAGE_SIZE] = { 0 }; char message[TT_LOG_MESSAGE_SIZE] = { 0 };
}; };

View File

@ -4,7 +4,6 @@ namespace tt {
/** Used for log output filtering */ /** Used for log output filtering */
enum class LogLevel : int { enum class LogLevel : int {
None = 0, /*!< No log output */
Error, /*!< Critical errors, software module can not recover on its own */ Error, /*!< Critical errors, software module can not recover on its own */
Warning, /*!< Error conditions from which recovery measures have been taken */ Warning, /*!< Error conditions from which recovery measures have been taken */
Info, /*!< Information messages which describe normal flow of events */ Info, /*!< Information messages which describe normal flow of events */

View File

@ -46,4 +46,5 @@ std::unique_ptr<std::array<LogEntry, TT_LOG_ENTRY_COUNT>> copyLogEntries(std::si
} }
} }
} // namespace tt } // namespace tt

View File

@ -3,8 +3,9 @@
#include "Tactility/Log.h" #include "Tactility/Log.h"
#include <cstdint> #include <cstdint>
#include <sys/time.h> #include <iomanip>
#include <sstream> #include <sstream>
#include <sys/time.h>
namespace tt { namespace tt {
@ -22,23 +23,21 @@ static char toPrefix(LogLevel level) {
case Debug: case Debug:
return 'D'; return 'D';
case Verbose: case Verbose:
return 'T'; return 'V';
default:
return '?';
} }
} }
static const char* toColour(LogLevel level) { static const char* toTagColour(LogLevel level) {
using enum LogLevel; using enum LogLevel;
switch (level) { switch (level) {
case Error: case Error:
return "\033[1;31m"; return "\033[1;31m";
case Warning: case Warning:
return "\033[33m"; return "\033[1;33m";
case Info: case Info:
return "\033[32m"; return "\033[32m";
case Debug: case Debug:
return "\033[1;37m"; return "\033[36m";
case Verbose: case Verbose:
return "\033[37m"; return "\033[37m";
default: default:
@ -46,6 +45,21 @@ static const char* toColour(LogLevel level) {
} }
} }
static const char* toMessageColour(LogLevel level) {
using enum LogLevel;
switch (level) {
case Error:
return "\033[1;31m";
case Warning:
return "\033[1;33m";
case Info:
case Debug:
case Verbose:
return "\033[0m";
default:
return "";
}
}
static uint64_t getLogTimestamp() { static uint64_t getLogTimestamp() {
static uint64_t base = 0U; static uint64_t base = 0U;
struct timeval time {}; struct timeval time {};
@ -59,7 +73,7 @@ static uint64_t getLogTimestamp() {
void log(LogLevel level, const char* tag, const char* format, ...) { void log(LogLevel level, const char* tag, const char* format, ...) {
std::stringstream buffer; std::stringstream buffer;
buffer << toColour(level) << toPrefix(level) << " (" << getLogTimestamp() << ") " << tag << ": " << format << "\033[0m\n"; buffer << getLogTimestamp() << " [" << toTagColour(level) << toPrefix(level) << "\033[0m" << "] [" << tag << "] " << toMessageColour(level) << format << "\033[0m\n";
va_list args; va_list args;
va_start(args, format); va_start(args, format);

View File

@ -35,12 +35,12 @@ static const char* toString(InitMode mode) {
} }
static void printInfo(const Data& data) { static void printInfo(const Data& data) {
TT_LOG_V(TAG, "I2C info for port %d", data.configuration.port); TT_LOG_D(TAG, "I2C info for port %d", data.configuration.port);
TT_LOG_V(TAG, " isStarted: %d", data.isStarted); TT_LOG_D(TAG, " isStarted: %d", data.isStarted);
TT_LOG_V(TAG, " isConfigured: %d", data.isConfigured); TT_LOG_D(TAG, " isConfigured: %d", data.isConfigured);
TT_LOG_V(TAG, " initMode: %s", toString(data.configuration.initMode)); TT_LOG_D(TAG, " initMode: %s", toString(data.configuration.initMode));
TT_LOG_V(TAG, " canReinit: %d", data.configuration.canReinit); TT_LOG_D(TAG, " canReinit: %d", data.configuration.canReinit);
TT_LOG_V(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration); TT_LOG_D(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration);
#ifdef ESP_PLATFORM #ifdef ESP_PLATFORM
TT_LOG_V(TAG, " SDA pin: %d", data.configuration.config.sda_io_num); TT_LOG_V(TAG, " SDA pin: %d", data.configuration.config.sda_io_num);
TT_LOG_V(TAG, " SCL pin: %d", data.configuration.config.scl_io_num); TT_LOG_V(TAG, " SCL pin: %d", data.configuration.config.scl_io_num);

View File

@ -29,15 +29,15 @@ static const char* toString(InitMode mode) {
} }
static void printInfo(const Data& data) { static void printInfo(const Data& data) {
TT_LOG_V(TAG, "SPI info for device %d", data.configuration.device); TT_LOG_D(TAG, "SPI info for device %d", data.configuration.device);
TT_LOG_V(TAG, " isStarted: %d", data.isStarted); TT_LOG_D(TAG, " isStarted: %d", data.isStarted);
TT_LOG_V(TAG, " isConfigured: %d", data.isConfigured); TT_LOG_D(TAG, " isConfigured: %d", data.isConfigured);
TT_LOG_V(TAG, " initMode: %s", toString(data.configuration.initMode)); TT_LOG_D(TAG, " initMode: %s", toString(data.configuration.initMode));
TT_LOG_V(TAG, " canReinit: %d", data.configuration.canReinit); TT_LOG_D(TAG, " canReinit: %d", data.configuration.canReinit);
TT_LOG_V(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration); TT_LOG_D(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration);
TT_LOG_V(TAG, " MISO pin: %d", data.configuration.config.miso_io_num); TT_LOG_D(TAG, " MISO pin: %d", data.configuration.config.miso_io_num);
TT_LOG_V(TAG, " MOSI pin: %d", data.configuration.config.mosi_io_num); TT_LOG_D(TAG, " MOSI pin: %d", data.configuration.config.mosi_io_num);
TT_LOG_V(TAG, " SCLK pin: %d", data.configuration.config.sclk_io_num); TT_LOG_D(TAG, " SCLK pin: %d", data.configuration.config.sclk_io_num);
} }
bool init(const std::vector<spi::Configuration>& configurations) { bool init(const std::vector<spi::Configuration>& configurations) {
@ -163,9 +163,6 @@ bool isStarted(spi_host_device_t device) {
auto lock = getLock(device).asScopedLock(); auto lock = getLock(device).asScopedLock();
lock.lock(); lock.lock();
Data& data = dataArray[device];
Configuration& config = data.configuration;
return dataArray[device].isStarted; return dataArray[device].isStarted;
} }