Kernel and buildscript improvements (#477)
* **New Features** * Centralized module management with global symbol resolution * Level-aware logging with colored prefixes and millisecond timestamps * **Breaking Changes** * ModuleParent hierarchy and getModuleParent() removed * Logging API and adapter model replaced; LogLevel-driven log_generic signature changed * **Improvements** * Unified, simplified module registration across build targets * Tests updated to reflect new module lifecycle and global symbol resolution
This commit is contained in:
committed by
GitHub
parent
1a61eac8e0
commit
a935410f82
@@ -1,72 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include "LoggerAdapter.h"
|
||||
#include "LoggerSettings.h"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "LoggerAdapterEsp.h"
|
||||
#else
|
||||
#include "LoggerAdapterGeneric.h"
|
||||
#endif
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <format>
|
||||
|
||||
namespace tt {
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
static LoggerAdapter defaultLoggerAdapter = espLoggerAdapter;
|
||||
#else
|
||||
static LoggerAdapter defaultLoggerAdapter = genericLoggerAdapter;
|
||||
#endif
|
||||
|
||||
class Logger {
|
||||
|
||||
const char* tag;
|
||||
|
||||
LogLevel level = LOG_LEVEL_INFO;
|
||||
|
||||
public:
|
||||
|
||||
explicit Logger(const char* tag) : tag(tag) {}
|
||||
|
||||
template <typename... Args>
|
||||
void log(LogLevel level, std::format_string<Args...> format, Args&&... args) const {
|
||||
std::string message = std::format(format, std::forward<Args>(args)...);
|
||||
defaultLoggerAdapter(level, tag, message.c_str());
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void verbose(std::format_string<Args...> format, Args&&... args) const {
|
||||
log(LogLevel::Verbose, format, std::forward<Args>(args)...);
|
||||
std::string message = std::format(format, std::forward<Args>(args)...);
|
||||
LOG_V(tag, "%s", message.c_str());
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void debug(std::format_string<Args...> format, Args&&... args) const {
|
||||
log(LogLevel::Debug, format, std::forward<Args>(args)...);
|
||||
std::string message = std::format(format, std::forward<Args>(args)...);
|
||||
LOG_D(tag, "%s", message.c_str());
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void info(std::format_string<Args...> format, Args&&... args) const {
|
||||
log(LogLevel::Info, format, std::forward<Args>(args)...);
|
||||
std::string message = std::format(format, std::forward<Args>(args)...);
|
||||
LOG_I(tag, "%s", message.c_str());
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void warn(std::format_string<Args...> format, Args&&... args) const {
|
||||
log(LogLevel::Warning, format, std::forward<Args>(args)...);
|
||||
std::string message = std::format(format, std::forward<Args>(args)...);
|
||||
LOG_W(tag, "%s", message.c_str());
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void error(std::format_string<Args...> format, Args&&... args) const {
|
||||
log(LogLevel::Error, format, std::forward<Args>(args)...);
|
||||
std::string message = std::format(format, std::forward<Args>(args)...);
|
||||
LOG_E(tag, "%s", message.c_str());
|
||||
}
|
||||
|
||||
bool isLoggingVerbose() const { return LogLevel::Verbose <= LOG_LEVEL; }
|
||||
bool isLoggingVerbose() const {
|
||||
return LOG_LEVEL_VERBOSE <= level;
|
||||
}
|
||||
|
||||
bool isLoggingDebug() const { return LogLevel::Debug <= LOG_LEVEL; }
|
||||
bool isLoggingDebug() const {
|
||||
return LOG_LEVEL_DEBUG <= level;
|
||||
}
|
||||
|
||||
bool isLoggingInfo() const { return LogLevel::Info <= LOG_LEVEL; }
|
||||
bool isLoggingInfo() const {
|
||||
return LOG_LEVEL_INFO <= level;
|
||||
}
|
||||
|
||||
bool isLoggingWarning() const { return LogLevel::Warning <= LOG_LEVEL; }
|
||||
bool isLoggingWarning() const {
|
||||
return LOG_LEVEL_WARNING <= level;
|
||||
}
|
||||
|
||||
bool isLoggingError() const { return LogLevel::Error <= LOG_LEVEL; }
|
||||
bool isLoggingError() const {
|
||||
return LOG_LEVEL_ERROR <= level;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "LoggerCommon.h"
|
||||
#include <functional>
|
||||
|
||||
namespace tt {
|
||||
|
||||
typedef std::function<void(LogLevel level, const char* tag, const char* message)> LoggerAdapter;
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "LoggerAdapter.h"
|
||||
#include "LoggerAdapterShared.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <sstream>
|
||||
|
||||
namespace tt {
|
||||
|
||||
inline esp_log_level_t toEspLogLevel(LogLevel level) {
|
||||
switch (level) {
|
||||
case LogLevel::Error:
|
||||
return ESP_LOG_ERROR;
|
||||
case LogLevel::Warning:
|
||||
return ESP_LOG_WARN;
|
||||
case LogLevel::Info:
|
||||
return ESP_LOG_INFO;
|
||||
case LogLevel::Debug:
|
||||
return ESP_LOG_DEBUG;
|
||||
case LogLevel::Verbose:
|
||||
default:
|
||||
return ESP_LOG_VERBOSE;
|
||||
}
|
||||
}
|
||||
|
||||
static const LoggerAdapter espLoggerAdapter = [](LogLevel level, const char* tag, const char* message) {
|
||||
constexpr auto COLOR_RESET = "\033[0m";
|
||||
constexpr auto COLOR_GREY = "\033[37m";
|
||||
std::stringstream buffer;
|
||||
buffer << COLOR_GREY << esp_log_timestamp() << ' ' << toTagColour(level) << toPrefix(level) << COLOR_GREY << " [" << COLOR_RESET << tag << COLOR_GREY << "] " << toMessageColour(level) << message << COLOR_RESET << std::endl;
|
||||
esp_log_write(toEspLogLevel(level), tag, "%s", buffer.str().c_str());
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "LoggerAdapter.h"
|
||||
#include "LoggerAdapterShared.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <sys/time.h>
|
||||
|
||||
namespace tt {
|
||||
|
||||
static uint64_t getLogTimestamp() {
|
||||
static uint64_t base = 0U;
|
||||
static std::once_flag init_flag;
|
||||
std::call_once(init_flag, []() {
|
||||
timeval time {};
|
||||
gettimeofday(&time, nullptr);
|
||||
base = ((uint64_t)time.tv_sec * 1000U) + (time.tv_usec / 1000U);
|
||||
});
|
||||
timeval time {};
|
||||
gettimeofday(&time, nullptr);
|
||||
uint64_t now = ((uint64_t)time.tv_sec * 1000U) + (time.tv_usec / 1000U);
|
||||
return now - base;
|
||||
}
|
||||
|
||||
static const LoggerAdapter genericLoggerAdapter = [](LogLevel level, const char* tag, const char* message) {
|
||||
constexpr auto COLOR_RESET = "\033[0m";
|
||||
constexpr auto COLOR_GREY = "\033[37m";
|
||||
std::stringstream buffer;
|
||||
buffer << COLOR_GREY << getLogTimestamp() << ' ' << toTagColour(level) << toPrefix(level) << COLOR_GREY << " [" << COLOR_RESET << tag << COLOR_GREY << "] " << toMessageColour(level) << message << COLOR_RESET << std::endl;
|
||||
printf("%s", buffer.str().c_str());
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "LoggerCommon.h"
|
||||
|
||||
namespace tt {
|
||||
|
||||
inline const char* toTagColour(LogLevel level) {
|
||||
using enum LogLevel;
|
||||
switch (level) {
|
||||
case Error:
|
||||
return "\033[1;31m";
|
||||
case Warning:
|
||||
return "\033[1;33m";
|
||||
case Info:
|
||||
return "\033[32m";
|
||||
case Debug:
|
||||
return "\033[36m";
|
||||
case Verbose:
|
||||
return "\033[37m";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
inline 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 "";
|
||||
}
|
||||
}
|
||||
|
||||
inline char toPrefix(LogLevel level) {
|
||||
using enum LogLevel;
|
||||
switch (level) {
|
||||
case Error:
|
||||
return 'E';
|
||||
case Warning:
|
||||
return 'W';
|
||||
case Info:
|
||||
return 'I';
|
||||
case Debug:
|
||||
return 'D';
|
||||
case Verbose:
|
||||
default:
|
||||
return 'V';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace tt {
|
||||
|
||||
/** Used for log output filtering */
|
||||
enum class LogLevel : int {
|
||||
Error, /*!< Critical errors, software module can not recover on its own */
|
||||
Warning, /*!< Error conditions from which recovery measures have been taken */
|
||||
Info, /*!< Information messages which describe normal flow of events */
|
||||
Debug, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
|
||||
Verbose /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "LoggerCommon.h"
|
||||
|
||||
namespace tt {
|
||||
|
||||
constexpr auto LOG_LEVEL = LogLevel::Info;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user