New logging and more (#446)

- `TT_LOG_*` macros are replaced by `Logger` via `#include<Tactility/Logger.h>`
- Changed default timezone to Europe/Amsterdam
- Fix for logic bug in unPhone hardware
- Fix for init/deinit in DRV2605 driver
- Other fixes
- Removed optimization that broke unPhone (disabled the moving of heap-related functions to flash)
This commit is contained in:
Ken Van Hoeylandt
2026-01-06 22:35:39 +01:00
committed by GitHub
parent 719f7bcece
commit f620255c41
188 changed files with 1973 additions and 1755 deletions
+12 -12
View File
@@ -1,28 +1,28 @@
#include <Tactility/Check.h>
#include <Tactility/Log.h>
#include <Tactility/Logger.h>
#include <Tactility/freertoscompat/Task.h>
constexpr auto TAG = "kernel";
static const auto LOGGER = tt::Logger("kernel");
static void logMemoryInfo() {
#ifdef ESP_PLATFORM
TT_LOG_E(TAG, "default caps:");
TT_LOG_E(TAG, " total: %u", heap_caps_get_total_size(MALLOC_CAP_DEFAULT));
TT_LOG_E(TAG, " free: %u", heap_caps_get_free_size(MALLOC_CAP_DEFAULT));
TT_LOG_E(TAG, " min free: %u", heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT));
TT_LOG_E(TAG, "internal caps:");
TT_LOG_E(TAG, " total: %u", heap_caps_get_total_size(MALLOC_CAP_INTERNAL));
TT_LOG_E(TAG, " free: %u", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
TT_LOG_E(TAG, " min free: %u", heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL));
LOGGER.error("default caps:");
LOGGER.error(" total: {}", heap_caps_get_total_size(MALLOC_CAP_DEFAULT));
LOGGER.error(" free: {}", heap_caps_get_free_size(MALLOC_CAP_DEFAULT));
LOGGER.error(" min free: {}", heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT));
LOGGER.error("internal caps:");
LOGGER.error(" total: {}", heap_caps_get_total_size(MALLOC_CAP_INTERNAL));
LOGGER.error(" free: {}", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
LOGGER.error(" min free: {}", heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL));
#endif
}
static void logTaskInfo() {
const char* name = pcTaskGetName(nullptr);
const char* safe_name = name ? name : "main";
TT_LOG_E(TAG, "Task: %s", safe_name);
TT_LOG_E(TAG, "Stack watermark: %u", uxTaskGetStackHighWaterMark(NULL) * 4);
LOGGER.error("Task: {}", safe_name);
LOGGER.error("Stack watermark: {}", uxTaskGetStackHighWaterMark(NULL) * 4);
}
namespace tt {
-86
View File
@@ -1,86 +0,0 @@
#ifndef ESP_PLATFORM
#include "Tactility/Log.h"
#include <cstdint>
#include <iomanip>
#include <sstream>
#include <sys/time.h>
namespace tt {
static 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:
return 'V';
default:
return ' ';
}
}
static 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 "";
}
}
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 base = 0U;
struct timeval time {};
gettimeofday(&time, nullptr);
uint64_t now = ((uint64_t)time.tv_sec * 1000U) + (time.tv_usec / 1000U);
if (base == 0U) {
base = now;
}
return now - base;
}
void log(LogLevel level, const char* tag, const char* format, ...) {
std::stringstream buffer;
buffer << getLogTimestamp() << " [" << toTagColour(level) << toPrefix(level) << "\033[0m" << "] [" << tag << "] " << toMessageColour(level) << format << "\033[0m\n";
va_list args;
va_start(args, format);
vprintf(buffer.str().c_str(), args);
va_end(args);
}
} // namespace tt
#endif
+12 -11
View File
@@ -1,7 +1,7 @@
#include "Tactility/crypt/Crypt.h"
#include <Tactility/crypt/Crypt.h>
#include "Tactility/Check.h"
#include "Tactility/Log.h"
#include <Tactility/Check.h>
#include <Tactility/Logger.h>
#include <mbedtls/aes.h>
#include <cstring>
@@ -15,7 +15,8 @@
namespace tt::crypt {
#define TAG "secure"
static const auto LOGGER = Logger("Crypt");
#define TT_NVS_NAMESPACE "tt_secure"
#ifdef ESP_PLATFORM
@@ -27,7 +28,7 @@ static void get_hardware_key(uint8_t key[32]) {
uint8_t mac[8];
// MAC can be 6 or 8 bytes
size_t mac_length = esp_mac_addr_len_get(ESP_MAC_EFUSE_FACTORY);
TT_LOG_I(TAG, "Using MAC with length %u", mac_length);
LOGGER.info("Using MAC with length {}", mac_length);
tt_check(mac_length <= 8);
ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_EFUSE_FACTORY));
@@ -66,13 +67,13 @@ static void get_nvs_key(uint8_t key[32]) {
esp_err_t result = nvs_open(TT_NVS_NAMESPACE, NVS_READWRITE, &handle);
if (result != ESP_OK) {
TT_LOG_E(TAG, "Failed to get key from NVS (%s)", esp_err_to_name(result));
LOGGER.error("Failed to get key from NVS ({})", esp_err_to_name(result));
tt_crash("NVS error");
}
size_t length = 32;
if (nvs_get_blob(handle, "key", key, &length) == ESP_OK) {
TT_LOG_I(TAG, "Fetched key from NVS (%d bytes)", length);
LOGGER.info("Fetched key from NVS ({} bytes)", length);
tt_check(length == 32);
} else {
// TODO: Improved randomness
@@ -83,7 +84,7 @@ static void get_nvs_key(uint8_t key[32]) {
key[i] = (uint8_t)(rand());
}
ESP_ERROR_CHECK(nvs_set_blob(handle, "key", key, 32));
TT_LOG_I(TAG, "Stored new key in NVS");
LOGGER.info("Stored new key in NVS");
}
nvs_close(handle);
@@ -109,8 +110,8 @@ static void xorKey(const uint8_t* inLeft, const uint8_t* inRight, uint8_t* out,
*/
static void getKey(uint8_t key[32]) {
#if !defined(CONFIG_SECURE_BOOT) || !defined(CONFIG_SECURE_FLASH_ENC_ENABLED)
TT_LOG_W(TAG, "Using tt_secure_* code with secure boot and/or flash encryption disabled.");
TT_LOG_W(TAG, "An attacker with physical access to your ESP32 can decrypt your secure data.");
LOGGER.warn("Using tt_secure_* code with secure boot and/or flash encryption disabled.");
LOGGER.warn("An attacker with physical access to your ESP32 can decrypt your secure data.");
#endif
#ifdef ESP_PLATFORM
@@ -121,7 +122,7 @@ static void getKey(uint8_t key[32]) {
get_nvs_key(nvs_key);
xorKey(hardware_key, nvs_key, key, 32);
#else
TT_LOG_W(TAG, "Using unsafe key for debugging purposes.");
LOGGER.warn("Using unsafe key for debugging purposes.");
memset(key, 0, 32);
#endif
}
+25 -23
View File
@@ -1,8 +1,10 @@
#include "Tactility/file/File.h"
#include <Tactility/file/File.h>
#include <cstring>
#include <fstream>
#include <unistd.h>
#include <Tactility/Logger.h>
#include <Tactility/StringUtils.h>
namespace tt::hal::sdcard {
@@ -11,7 +13,7 @@ class SdCardDevice;
namespace tt::file {
constexpr auto* TAG = "file";
static const auto LOGGER = Logger("file");
class NoLock final : public Lock {
bool lock(TickType_t timeout) const override { return true; }
@@ -23,7 +25,7 @@ static std::function<std::shared_ptr<Lock>(const std::string&)> findLockFunction
std::shared_ptr<Lock> getLock(const std::string& path) {
if (findLockFunction == nullptr) {
TT_LOG_W(TAG, "File lock function not set!");
LOGGER.warn("File lock function not set!");
return noLock;
}
@@ -69,10 +71,10 @@ bool listDirectory(
auto lock = getLock(path)->asScopedLock();
lock.lock();
TT_LOG_I(TAG, "listDir start %s", path.c_str());
LOGGER.info("listDir start {}", path);
DIR* dir = opendir(path.c_str());
if (dir == nullptr) {
TT_LOG_E(TAG, "Failed to open dir %s", path.c_str());
LOGGER.error("Failed to open dir {}", path);
return false;
}
@@ -83,7 +85,7 @@ bool listDirectory(
closedir(dir);
TT_LOG_I(TAG, "listDir stop %s", path.c_str());
LOGGER.info("listDir stop {}", path);
return true;
}
@@ -96,10 +98,10 @@ int scandir(
auto lock = getLock(path)->asScopedLock();
lock.lock();
TT_LOG_I(TAG, "scandir start");
LOGGER.info("scandir start");
DIR* dir = opendir(path.c_str());
if (dir == nullptr) {
TT_LOG_E(TAG, "Failed to open dir %s", path.c_str());
LOGGER.error("Failed to open dir {}", path);
return -1;
}
@@ -116,7 +118,7 @@ int scandir(
std::ranges::sort(outList, sortMethod);
}
TT_LOG_I(TAG, "scandir finish");
LOGGER.info("scandir finish");
return outList.size();
}
@@ -125,18 +127,18 @@ long getSize(FILE* file) {
long original_offset = ftell(file);
if (fseek(file, 0, SEEK_END) != 0) {
TT_LOG_E(TAG, "fseek failed");
LOGGER.error("fseek failed");
return -1;
}
long file_size = ftell(file);
if (file_size == -1) {
TT_LOG_E(TAG, "Could not get file length");
LOGGER.error("Could not get file length");
return -1;
}
if (fseek(file, original_offset, SEEK_SET) != 0) {
TT_LOG_E(TAG, "fseek Failed");
LOGGER.error("fseek Failed");
return -1;
}
@@ -152,26 +154,26 @@ static std::unique_ptr<uint8_t[]> readBinaryInternal(const std::string& filepath
FILE* file = fopen(filepath.c_str(), "rb");
if (file == nullptr) {
TT_LOG_E(TAG, "Failed to open %s", filepath.c_str());
LOGGER.error("Failed to open {}", filepath);
return nullptr;
}
long content_length = getSize(file);
if (content_length == -1) {
TT_LOG_E(TAG, "Failed to determine content length for %s", filepath.c_str());
LOGGER.error("Failed to determine content length for {}", filepath);
return nullptr;
}
auto data = std::make_unique<uint8_t[]>(content_length + sizePadding);
if (data == nullptr) {
TT_LOG_E(TAG, "Insufficient memory. Failed to allocate %ldl bytes.", content_length);
LOGGER.error("Insufficient memory. Failed to allocate {} bytes.", content_length);
return nullptr;
}
size_t buffer_offset = 0;
while (buffer_offset < content_length) {
size_t bytes_read = fread(&data.get()[buffer_offset], 1, content_length - buffer_offset, file);
TT_LOG_D(TAG, "Read %d bytes", bytes_read);
LOGGER.debug("Read {} bytes", bytes_read);
if (bytes_read > 0) {
buffer_offset += bytes_read;
} else { // Something went wrong?
@@ -268,7 +270,7 @@ bool findOrCreateDirectory(const std::string& path, mode_t mode) {
if (path.empty()) {
return true;
}
TT_LOG_D(TAG, "findOrCreate: %s %lu", path.c_str(), mode);
LOGGER.debug("findOrCreate: {} {}", path, mode);
const char separator_to_find[] = {SEPARATOR, 0x00};
auto first_index = path[0] == SEPARATOR ? 1 : 0;
@@ -280,10 +282,10 @@ bool findOrCreateDirectory(const std::string& path, mode_t mode) {
auto to_create = is_last_segment ? path : path.substr(0, separator_index);
should_break = is_last_segment;
if (!findOrCreateDirectoryInternal(to_create, mode)) {
TT_LOG_E(TAG, "Failed to create %s", to_create.c_str());
LOGGER.error("Failed to create {}", to_create);
return false;
} else {
TT_LOG_D(TAG, " - got: %s", to_create.c_str());
LOGGER.debug(" - got: {}", to_create);
}
// Find next file separator index
@@ -309,7 +311,7 @@ bool deleteRecursively(const std::string& path) {
if (isDirectory(path)) {
std::vector<dirent> entries;
if (scandir(path, entries) < 0) {
TT_LOG_E(TAG, "Failed to scan directory %s", path.c_str());
LOGGER.error("Failed to scan directory {}", path);
return false;
}
@@ -319,16 +321,16 @@ bool deleteRecursively(const std::string& path) {
return false;
}
}
TT_LOG_I(TAG, "Deleting %s", path.c_str());
LOGGER.info("Deleting {}", path);
return deleteDirectory(path);
} else if (isFile(path)) {
TT_LOG_I(TAG, "Deleting %s", path.c_str());
LOGGER.info("Deleting {}", path);
return deleteFile(path);
} else if (path == "/" || path == "." || path == "..") {
// No-op
return true;
} else {
TT_LOG_E(TAG, "Failed to delete \"%s\": unknown type", path.c_str());
LOGGER.error("Failed to delete \"{}\": unknown type", path);
return false;
}
}