Mixiaoxiao / Arduino-HomeKit-ESP8266

Native Apple HomeKit accessory implementation for the ESP8266 Arduino core.
MIT License
1.48k stars 278 forks source link

Log output #169

Open danilkorotkov opened 2 years ago

danilkorotkov commented 2 years ago

There is a problem to make firmware for tuya devices. Tuya uses serial, but this lib uses serial for log and because it is “C” included it is impossible to remap log with ESP8266 UDP cpp library

danilkorotkov commented 2 years ago

this is a little improvement for apps using serial my_logging.h

#ifndef __MY_LOGGING_H__
#define __MY_LOGGING_H__
void my_log(char *);

#endif /* __MY_LOGGING_H__ */

my_logging.cpp

#include <Arduino.h>
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
WiFiUDP Udp;
IPAddress broadcastIp;
uint16_t  bPort = 2525;

extern "C" {
  #include "my_logging.h"
}

//#define TUYA
//#define TUYA_UDP
//#define TUYA_UDP_SERIAL
//#define UDP_SERIAL

void my_log(char *logtext) {
    #ifdef TUYA
        Serial1.printf(logtext);
        return;
    #endif
    #ifdef TUYA_UDP
        if (WiFi.isConnected()) {
            Udp.beginPacket(broadcastIp,bPort); 
            Udp.write(logtext); 
            Udp.endPacket();
        }
        return;
    #endif
    #ifdef TUYA_UDP_SERIAL
        Serial1.printf(logtext);
        if (WiFi.isConnected()) {
            Udp.beginPacket(broadcastIp,bPort); 
            Udp.write(logtext); 
            Udp.endPacket();
        }
        return;
    #endif
    #ifdef UDP_SERIAL
        printf(logtext);
        if (WiFi.isConnected()) {
            Udp.beginPacket(broadcastIp,bPort); 
            Udp.write(logtext); 
            Udp.endPacket();
        }
        return;
    #endif
    printf(logtext);
}

in esp_xpgm.h #define XPGM_PRINTF(fmt, ...) {char *mess = (char*)malloc(sizeof(char) * 200); sprintf_P(mess, PSTR(fmt), ##__VA_ARGS__); my_log(mess); free(mess);}