Skip to content

Commit

Permalink
logging: use stderr/esp_rom_printf directly, skipping the esp_log tag…
Browse files Browse the repository at this point in the history
…/level mechanisms
  • Loading branch information
SpComb committed Oct 19, 2024
1 parent ae180d1 commit 5112f83
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions components/logging/include/logging.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef __LOGGING_H__
#define __LOGGING_H__
#pragma once

#include "esp_log.h"
#include <esp_log.h>

#include <stdlib.h>
#include <stdio.h>

#ifdef DEBUG
#undef DEBUG
Expand All @@ -12,28 +12,34 @@
#define DEBUG 0
#endif

#define IF_DEBUG(x) do { if (DEBUG) x } while(0)

#define LOG_ISR_DEBUG(...) IF_DEBUG({ ESP_EARLY_LOGI(__func__, __VA_ARGS__); })
#define LOG_ISR_INFO(...) IF_DEBUG({ ESP_EARLY_LOGI(__func__, __VA_ARGS__); })
#define LOG_ISR_WARN(...) IF_DEBUG({ ESP_EARLY_LOGW(__func__, __VA_ARGS__); })

/* Bypass stdio */
#define LOG_BOOT_DEBUG(...) IF_DEBUG({ ESP_EARLY_LOGD(__func__, __VA_ARGS__); })
#define LOG_BOOT_INFO(...) ESP_EARLY_LOGI(__func__, __VA_ARGS__)
#define LOG_BOOT_ERROR(...) ESP_EARLY_LOGE(__func__, __VA_ARGS__)

// XXX: CONFIG_LOG_DEFAULT_LEVEL defaults to skip ESP_LOG_DEBUG, and raising will include ALL debug output by default - not possible to override this per-call
#define LOG_DEBUG(...) IF_DEBUG({ ESP_LOG_LEVEL(ESP_LOG_INFO, __func__, __VA_ARGS__); })
#define LOG_INFO(...) ESP_LOG_LEVEL(ESP_LOG_INFO, __func__, __VA_ARGS__)
#define LOG_WARN(...) ESP_LOG_LEVEL(ESP_LOG_WARN, __func__, __VA_ARGS__)
#define LOG_ERROR(...) ESP_LOG_LEVEL(ESP_LOG_ERROR, __func__, __VA_ARGS__)
#if CONFIG_LOG_COLORS
#undef LOG_COLOR_D
#define LOG_COLOR_D LOG_COLOR(LOG_COLOR_BLUE)
#endif

// TODO: fprint(stderr) + fflush(stderr) + fsync(stderr) + abort()?
#define LOG_FATAL(...) do { ESP_LOG_LEVEL(ESP_LOG_ERROR, __func__, __VA_ARGS__); abort(); } while(0)
/* Bypass stdio buffering/interrupts, blocking write directly to UART */
#define LOG_ISR_ERROR(fmt, ...) do { if (DEBUG) esp_rom_printf(LOG_FORMAT(E, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)
#define LOG_ISR_WARN(fmt, ...) do { if (DEBUG) esp_rom_printf(LOG_FORMAT(W, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)
#define LOG_ISR_INFO(fmt, ...) do { if (DEBUG) esp_rom_printf(LOG_FORMAT(I, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)
#define LOG_ISR_DEBUG(fmt, ...) do { if (DEBUG) esp_rom_printf(LOG_FORMAT(D, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)

/* Bypass stdio buffering/interrupts, blocking write directly to UART */
#define LOG_BOOT_ERROR(fmt, ...) do { esp_rom_printf(LOG_FORMAT(E, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)
#define LOG_BOOT_WARN(fmt, ...) do { esp_rom_printf(LOG_FORMAT(W, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)
#define LOG_BOOT_INFO(fmt, ...) do { esp_rom_printf(LOG_FORMAT(I, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)
#define LOG_BOOT_DEBUG(fmt, ...) do { if (DEBUG) esp_rom_printf(LOG_FORMAT(D, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)

/* Use stdio stderr for logging, bypass esp_log tag levels */
#define LOG_FATAL(fmt, ...) do { fprintf(stderr, LOG_FORMAT(E, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); abort(); } while(0)
#define LOG_ERROR(fmt, ...) do { fprintf(stderr, LOG_FORMAT(E, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)
#define LOG_WARN(fmt, ...) do { fprintf(stderr, LOG_FORMAT(W, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)
#define LOG_INFO(fmt, ...) do { fprintf(stderr, LOG_FORMAT(I, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)
#define LOG_DEBUG(fmt, ...) do { if (DEBUG) fprintf(stderr, LOG_FORMAT(D, fmt), esp_log_timestamp(), __func__, ##__VA_ARGS__); } while(0)

// XXX: CONFIG_LOG_DEFAULT_LEVEL defaults to skip ESP_LOG_DEBUG, and raising will include ALL debug output by default - not possible to override this per-call
#define LOG_DEBUG_BUFFER(buf, len) IF_DEBUG({ ESP_LOG_BUFFER_HEX_LEVEL(__func__, buf, len, ESP_LOG_INFO); })
#define LOG_INFO_BUFFER(buf, len) ESP_LOG_BUFFER_HEX_LEVEL(__func__, buf, len, ESP_LOG_INFO);

#if DEBUG
#define LOG_DEBUG_BUFFER(buf, len) IF_DEBUG({ ESP_LOG_BUFFER_HEX_LEVEL(__func__, buf, len, ESP_LOG_INFO); })
#else
#define LOG_DEBUG_BUFFER(buf, len)
#endif
#define LOG_INFO_BUFFER(buf, len) ESP_LOG_BUFFER_HEX_LEVEL(__func__, buf, len, ESP_LOG_INFO);

0 comments on commit 5112f83

Please sign in to comment.