|
1 | 1 | #pragma once |
2 | 2 |
|
3 | | -#ifdef ESP_PLATFORM |
4 | | - |
5 | | -#ifndef LOG_LOCAL_LEVEL |
6 | | -#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG |
| 3 | +#ifndef TESLA_LOG_TAG |
| 4 | +#define TESLA_LOG_TAG "TeslaBLE" |
7 | 5 | #endif |
8 | | -#include "esp_log.h" |
9 | | -static const char *const TAG = "tesla_ble"; |
10 | | -#define LOG(...) ESP_LOGI(TAG, __VA_ARGS__) |
11 | | -#define LOG_INFO(...) ESP_LOGI(TAG, __VA_ARGS__) |
12 | | -#define LOG_DEBUG(...) ESP_LOGD(TAG, __VA_ARGS__) |
13 | | -#define LOG_ERROR(...) ESP_LOGE(TAG, __VA_ARGS__) |
14 | | -#define LOG_WARNING(...) ESP_LOGW(TAG, __VA_ARGS__) |
15 | | - |
16 | | -#else |
17 | | - |
18 | | -#include <cstdio> |
19 | | -#include <cstring> |
20 | | -#include <iostream> |
21 | | - |
22 | | -#define RESET_COLOR "\x1B[0m" |
23 | | -#define INFO_COLOR "\x1B[1;34m" |
24 | | -#define DEBUG_COLOR "\x1B[1;30m" |
25 | | -#define WARNING_COLOR "\x1B[1;33m" |
26 | | -#define ERROR_COLOR "\x1B[31m" |
27 | | -#define LOG(...) log("[LOG]", RESET_COLOR, __VA_ARGS__) |
28 | | -#define LOG_INFO(...) log("[INFO]", INFO_COLOR, __VA_ARGS__) |
29 | | -#define LOG_DEBUG(...) log("[DEBUG]", DEBUG_COLOR, __VA_ARGS__) |
30 | | -#define LOG_WARNING(...) log("[WARNING]", WARNING_COLOR, __VA_ARGS__) |
31 | | -#define LOG_ERROR(...) log("[ERROR]", ERROR_COLOR, __VA_ARGS__) |
32 | | -template <typename... Args> |
33 | | -void log(const char *type, const char *color, const char *s, Args... args) |
34 | | -{ |
35 | | - printf("%s%s - ", color, type); |
36 | | - if constexpr (sizeof...(args) > 0) { |
37 | | - printf(s, args...); |
38 | | - } else { |
39 | | - printf("%s", s); |
40 | | - } |
41 | | - printf("%s\n", RESET_COLOR); |
42 | | -} |
43 | 6 |
|
| 7 | +#include <cstdarg> |
| 8 | +#include "adapters.h" |
| 9 | + |
| 10 | +#ifdef ESP_PLATFORM |
| 11 | +#include <esp_log.h> |
44 | 12 | #endif |
| 13 | + |
| 14 | +namespace TeslaBLE { |
| 15 | + |
| 16 | +enum class LogLevel { |
| 17 | + ERROR, |
| 18 | + WARN, |
| 19 | + INFO, |
| 20 | + DEBUG, |
| 21 | + VERBOSE |
| 22 | +}; |
| 23 | + |
| 24 | +typedef void (*LogCallback)(LogLevel level, const char* tag, int line, const char* format, va_list args); |
| 25 | + |
| 26 | +extern LogCallback g_log_callback; |
| 27 | + |
| 28 | +void log_internal(LogLevel level, const char* tag, int line, const char* format, ...); |
| 29 | + |
| 30 | +} // namespace TeslaBLE |
| 31 | + |
| 32 | +#define LOG_ERROR(format, ...) TeslaBLE::log_internal(TeslaBLE::LogLevel::ERROR, TESLA_LOG_TAG, __LINE__, format, ##__VA_ARGS__) |
| 33 | +#define LOG_WARNING(format, ...) TeslaBLE::log_internal(TeslaBLE::LogLevel::WARN, TESLA_LOG_TAG, __LINE__, format, ##__VA_ARGS__) |
| 34 | +#define LOG_INFO(format, ...) TeslaBLE::log_internal(TeslaBLE::LogLevel::INFO, TESLA_LOG_TAG, __LINE__, format, ##__VA_ARGS__) |
| 35 | +#define LOG_DEBUG(format, ...) TeslaBLE::log_internal(TeslaBLE::LogLevel::DEBUG, TESLA_LOG_TAG, __LINE__, format, ##__VA_ARGS__) |
| 36 | +#define LOG_VERBOSE(format, ...) TeslaBLE::log_internal(TeslaBLE::LogLevel::VERBOSE, TESLA_LOG_TAG, __LINE__, format, ##__VA_ARGS__) |
0 commit comments