Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging: bypass esp_log output #87

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 38 additions & 24 deletions components/logging/include/logging.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#ifndef __LOGGING_H__
#define __LOGGING_H__
#pragma once

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

#if CONFIG_IDF_TARGET_ESP8266
// compat
#define esp_rom_printf(fmt, ...) ets_printf(fmt, __VA_ARGS__)
#define esp_log_timestamp() esp_log_early_timestamp()

#define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%u) %s: " format LOG_RESET_COLOR "\n"
#endif

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

#ifdef DEBUG
#undef DEBUG
Expand All @@ -12,28 +20,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);
Loading