Files
ESP8266lib/Debug.h
2021-02-21 09:33:08 +01:00

167 lines
6.2 KiB
C++

#ifndef DEBUG_H
#define DEBUG_H
#include <cstdint>
#include "Platforms.h"
/*
extern "C" {
#include "ets_sys.h"
#include "c_types.h"
#include "osapi.h"
//#include "gpio.h"
//#include "os_type.h"
//#include "user_config.h"
#include "user_interface.h"
//#include "wpa2_enterprise.h"
//#include "inttypes.h"
#include "mem.h"
#include "espconn.h"
#include "ESP8266lib/c++.h"
#include "driver/uart.h"
}
*/
class Log {
public:
template<typename... Args> static inline void addInfo(const char* module, const char* fmt, Args... args) {
add('i', module, fmt, args...);
}
template<typename... Args> static inline void addError(const char* module, const char* fmt, Args... args) {
add('e', module, fmt, args...);
//while(true) {}
}
private:
#if defined(WITH_LOG)
template<typename... Args> static void add(char level, const char* module, const char* fmt, Args... args) {
// temporal buffer (NOTE! MUST NOT BE TOO LARGE OR IT CAN KILL THE STACK!)
char buf[512];
char* dst = buf;
char* end = buf + sizeof(buf);
dst = dst + snprintf(dst, (end-dst), "%c[%-10s] ", level, module);
dst = dst + snprintf(dst, (end-dst)-2, fmt, args...);
dst = dst + snprintf(dst, (end-dst), "\n");
#if IS_DESKTOP
printf(buf);
#elif TEENSY
Serial.print(buf);
#elif ESP8266
os_printf(buf);
#elif ESP32
printf(buf);
#else
#error "unsupported platform"
#endif
}
#else
template<typename... Args> static void add(char , const char* , const char* , Args... ) {}
#endif
};
#if (!defined(DEBUG))
// #define debug(str)
// #define debugMod(module, str)
// #define debugMod1(module, str, val)
// #define debugMod2(module, str, v1, v2)
// #define debugMod3(module, str, v1, v2, v3)
// #define debugMod4(module, str, v1, v2, v3, v4)
// #define debugMod5(module, str, v1, v2, v3, v4, v5)
// #define debugMod6(module, str, v1, v2, v3, v4, v5, v6)
// #define debugMod7(module, str, v1, v2, v3, v4, v5, v6, v7)
// #define debugMod8(module, str, v1, v2, v3, v4, v5, v6, v7, v8)
// #define debugMod9(module, str, v1, v2, v3, v4, v5, v6, v7, v8, v9)
// #define IF_DEBUG(a)
// #define debugShow(a, b)
// #warning "not using debug output"
#elif ESP8266
#define debug(str) os_printf(str)
#define debugMod(module, str) os_printf("[%s] %s\n", module, str)
#define debugMod1(module, str, val) os_printf("[%s] ", module); os_printf(str, val); os_printf("\n");
#define debugMod2(module, str, v1, v2) os_printf("[%s] ", module); os_printf(str, v1, v2); os_printf("\n");
#define debugMod3(module, str, v1, v2, v3) os_printf("[%s] ", module); os_printf(str, v1, v2, v3); os_printf("\n");
#define debugMod4(module, str, v1, v2, v3, v4) os_printf("[%s] ", module); os_printf(str, v1, v2, v3, v4); os_printf("\n");
#define debugMod5(module, str, v1, v2, v3, v4, v5) os_printf("[%s] ", module); os_printf(str, v1, v2, v3, v4, v5); os_printf("\n");
#define debugMod6(module, str, v1, v2, v3, v4, v5, v6) os_printf("[%s] ", module); os_printf(str, v1, v2, v3, v4, v5, v6); os_printf("\n");
#define debugMod7(module, str, v1, v2, v3, v4, v5, v6, v7) os_printf("[%s] ", module); os_printf(str, v1, v2, v3, v4, v5, v6, v7); os_printf("\n");
#define debugMod8(module, str, v1, v2, v3, v4, v5, v6, v7, v8) os_printf("[%s] ", module); os_printf(str, v1, v2, v3, v4, v5, v6, v7, v8); os_printf("\n");
#define debugMod9(module, str, v1, v2, v3, v4, v5, v6, v7, v8, v9) os_printf("[%s] ", module); os_printf(str, v1, v2, v3, v4, v5, v6, v7, v8, v9); os_printf("\n");
#define IF_DEBUG(a) a
#define debugShow(buf, len) hexdump(buf,len)
void hexdump(const uint8_t* buf, uint8_t len) {
for (int i = 0; i < len; ++i) {
os_printf("%02x ", buf[i]);
}
os_printf("\n");
}
#elif ESP32
#ifndef CONFIG_LOG_DEFAULT_LEVEL
#define CONFIG_LOG_DEFAULT_LEVEL 3
#endif
#include "esp_log.h"
#define debug(str) printf(str);
#define debugMod(module, str) printf("i[%-10s] ", module); printf(str); printf("\n");
#define debugMod1(module, str, v1) printf("i[%-10s] ", module); printf(str, v1); printf("\n");
#define debugMod2(module, str, v1, v2) printf("i[%-10s] ", module); printf(str, v1, v2); printf("\n");
#define debugMod3(module, str, v1, v2, v3) printf("i[%-10s] ", module); printf(str, v1, v2, v3); printf("\n");
#define debugMod4(module, str, v1, v2, v3, v4) printf("i[%-10s] ", module); printf(str, v1, v2, v3, v4); printf("\n");
#define errorMod(module, str) printf("e[%-10s] ", module); printf(str); printf("\n"); esp_restart();
#define errorMod1(module, str, v1) printf("e[%-10s] ", module); printf(str, v1); printf("\n"); esp_restart();
#define IF_DEBUG(a) a
#define debugShow(buf, len) hexdump(buf,len)
#elif TEENSY
#define debugMod(module, str) {char buf[64]; sprintf(buf, "i[%-10s] ", module); Serial.print(buf); Serial.println(str);}
#define debugMod1(module, str, v1) {char buf[64]; sprintf(buf, "i[%-10s] ", module); Serial.print(buf); sprintf(buf, str, v1); Serial.println(buf);}
#define debugMod2(module, str, v1, v2) {char buf[64]; sprintf(buf, "i[%-10s] ", module); Serial.print(buf); sprintf(buf, str, v1, v2); Serial.println(buf);}
#define debugMod3(module, str, v1, v2, v3) {char buf[64]; sprintf(buf, "i[%-10s] ", module); Serial.print(buf); sprintf(buf, str, v1, v2, v3); Serial.println(buf);}
#define debugMod4(module, str, v1, v2, v3, v4) {char buf[64]; sprintf(buf, "i[%-10s] ", module); Serial.print(buf); sprintf(buf, str, v1, v2, v3, v4); Serial.println(buf);}
#define debugMod5(module, str, v1, v2, v3, v4, v5) {char buf[64]; sprintf(buf, "i[%-10s] ", module); Serial.print(buf); sprintf(buf, str, v1, v2, v3, v4, v5); Serial.println(buf);}
#define errorMod(module, str) debugMod(module, str); while(true) {}
#else
#error "unsupported platform"
#endif
#endif // DEBUG_H