From 2c88c9716ea33339088f80638c5d1e5c9742bfbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Mart=C3=ADn?= Date: Tue, 16 Aug 2016 12:03:14 +0200 Subject: [PATCH] Debug log reengineered --- AvrNTPClient.cpp | 127 +++++++++++++++++++---------------------------- ESPNTPClient.cpp | 88 +++++++++++--------------------- 2 files changed, 79 insertions(+), 136 deletions(-) diff --git a/AvrNTPClient.cpp b/AvrNTPClient.cpp index 3beab8f..8783d15 100644 --- a/AvrNTPClient.cpp +++ b/AvrNTPClient.cpp @@ -11,9 +11,17 @@ #define DBG_PORT Serial -AvrNTPClient NTP; +#ifdef DEBUG_NTPCLIENT +#define DEBUGLOG(A) DBG_PORT.print(A) +#define DEBUGLOGCR(A) DBG_PORT.println(A) +#else +#define DEBUGLOG(A) +#define DEBUGLOGCR(A) +#endif +AvrNTPClient NTP; + #if NETWORK_TYPE == NETWORK_W5100 // send an NTP request to the time server at the given address boolean sendNTPpacket(IPAddress &address, EthernetUDP udp) { @@ -50,65 +58,52 @@ time_t getTime() { IPAddress timeServerIP; //NTP server IP address char ntpPacketBuffer[NTP_PACKET_SIZE]; //Buffer to store response message -#ifdef DEBUG_NTPCLIENT - Serial.println("Starting UDP"); -#endif // DEBUG_NTPCLIENT + + DEBUGLOGCR(F("Starting UDP")); udp.begin(DEFAULT_NTP_PORT); -#ifdef DEBUG_NTPCLIENT - Serial.print("Local port: "); - Serial.println(client->_udp.localPort()); -#endif // DEBUG_NTPCLIENT + DEBUGLOG(F("Remote port: ")); + DEBUGLOGCR(udp.remotePort()); while (udp.parsePacket() > 0); // discard any previously received packets dns.begin(Ethernet.dnsServerIP()); uint8_t dnsResult = dns.getHostByName(NTP.getNtpServerName().c_str(), timeServerIP); -#ifdef DEBUG_NTPCLIENT - Serial.print("NTP Server hostname: "); - Serial.println(NTP.getNtpServerName()); - Serial.print("NTP Server IP address: "); - Serial.println(timeServerIP); - Serial.print("Result code: "); - Serial.print(dnsResult); - Serial.print(" "); - Serial.println("-- IP Connected. Waiting for sync"); - Serial.println("-- Transmit NTP Request"); -#endif // DEBUG_NTPCLIENT + DEBUGLOG(F("NTP Server hostname: ")); + DEBUGLOGCR(NTP.getNtpServerName()); + DEBUGLOG(F("NTP Server IP address: ")); + DEBUGLOGCR(timeServerIP); + DEBUGLOG(F("Result code: ")); + DEBUGLOG(dnsResult); + DEBUGLOG(" "); + DEBUGLOGCR(F("-- IP Connected. Waiting for sync")); + DEBUGLOGCR(F("-- Transmit NTP Request")); + if (dnsResult == 1) { //If DNS lookup resulted ok sendNTPpacket(timeServerIP,udp); uint32_t beginWait = millis(); while (millis() - beginWait < 1500) { int size = udp.parsePacket(); if (size >= NTP_PACKET_SIZE) { -#ifdef DEBUG_NTPCLIENT - Serial.println("-- Receive NTP Response"); -#endif // DEBUG_NTPCLIENT + DEBUGLOGCR(F("-- Receive NTP Response")); udp.read(ntpPacketBuffer, NTP_PACKET_SIZE); // read packet into the buffer time_t timeValue = NTP.decodeNtpMessage(ntpPacketBuffer); setSyncInterval(NTP.getLongInterval()); -#ifdef DEBUG_NTPCLIENT - Serial.println("Sync frequency set low"); -#endif // DEBUG_NTPCLIENT + NTP.getFirstSync(); // Set firstSync value if not set before + DEBUGLOGCR(F("Sync frequency set low")); udp.stop(); NTP.setLastNTPSync(timeValue); -#ifdef DEBUG_NTPCLIENT - Serial.println("Succeccful NTP sync at "); - Serial.println(client->getTimeString(NTP.getLastNTPSync())); -#endif // DEBUG_NTPCLIENT + DEBUGLOG(F("Succeccful NTP sync at ")); + DEBUGLOGCR(NTP.getTimeDateString(NTP.getLastNTPSync())); + return timeValue; } } -#ifdef DEBUG_NTPCLIENT - Serial.println("-- No NTP Response :-("); -#endif // DEBUG_NTPCLIENT + DEBUGLOGCR(F("-- No NTP Response :-(")); udp.stop(); setSyncInterval(NTP.getShortInterval()); // Retry connection more often return 0; // return 0 if unable to get the time } else { -#ifdef DEBUG_NTPCLIENT - Serial.println("-- Invalid address :-(("); -#endif // DEBUG_NTPCLIENT + DEBUGLOGCR(F("-- Invalid address :-((")); udp.stop(); - return 0; // return 0 if unable to get the time } } @@ -131,9 +126,7 @@ boolean AvrNTPClient::begin(String ntpServerName = DEFAULT_NTP_SERVER, int timeO if (!setInterval(DEFAULT_NTP_SHORTINTERVAL, DEFAULT_NTP_INTERVAL)) { return false; } -#ifdef DEBUG_NTPCLIENT - DBG_PORT.println("Time sync started"); -#endif // DEBUG_NTPCLIENT + DEBUGLOGCR(F("Time sync started")); setSyncInterval(getShortInterval()); setSyncProvider(getTime); @@ -143,9 +136,7 @@ boolean AvrNTPClient::begin(String ntpServerName = DEFAULT_NTP_SERVER, int timeO boolean AvrNTPClient::stop() { setSyncProvider(NULL); -#ifdef DEBUG_NTPCLIENT - Serial.println("Time sync disabled"); -#endif // DEBUG_NTPCLIENT + DEBUGLOGCR(F("Time sync disabled")); return true; } @@ -164,23 +155,15 @@ time_t AvrNTPClient::decodeNtpMessage(char *messageBuffer) { if (_daylight) { if (summertime(year(timeTemp), month(timeTemp), day(timeTemp), hour(timeTemp), _timeZone)) { timeTemp += SECS_PER_HOUR; -#ifdef DEBUG_NTPCLIENT - Serial.println("Summer Time"); -#endif // DEBUG_NTPCLIENT + DEBUGLOGCR(F("Summer Time")); } -#ifdef DEBUG_NTPCLIENT else { - Serial.println("Winter Time"); + DEBUGLOGCR(F("Winter Time")); } -#endif // DEBUG_NTPCLIENT } -#ifdef DEBUG_NTPCLIENT - else { - Serial.println("No daylight"); - + DEBUGLOGCR(F("No daylight")); } -#endif // DEBUG_NTPCLIENT return timeTemp; } @@ -195,7 +178,7 @@ String AvrNTPClient::getTimeStr(time_t moment) { return timeStr; } - else return "Time not set"; + else return F("Time not set"); } String AvrNTPClient::getTimeStr() { @@ -214,7 +197,7 @@ String AvrNTPClient::getDateStr(time_t moment) { return timeStr; } - else return "Date not set"; + else return F("Date not set"); } String AvrNTPClient::getDateStr() { @@ -230,7 +213,7 @@ String AvrNTPClient::getTimeDateString(time_t moment) { return timeStr; } else { - return "Time not set"; + return F("Time not set"); } } @@ -277,9 +260,8 @@ String AvrNTPClient::getNtpServerName() boolean AvrNTPClient::setNtpServerName(String ntpServerName) { char * name = (char *)malloc((ntpServerName.length() + 1) * sizeof(char)); ntpServerName.toCharArray(name, ntpServerName.length() + 1); -#ifdef DEBUG_NTPCLIENT - Serial.printf("NTP server set to %s" + name); -#endif // DEBUG_NTPCLIENT + DEBUGLOG(F("NTP server set to ")); + DEBUGLOGCR(name); free(_ntpServerName); _ntpServerName = name; return true; @@ -290,10 +272,10 @@ boolean AvrNTPClient::setInterval(int interval) if (interval >= 10) { if (_longInterval != interval) { _longInterval = interval; -#ifdef DEBUG_NTPCLIENT - Serial.println("Sync interval set to " + interval); -#endif // DEBUG_NTPCLIENT - setSyncInterval(interval); + DEBUGLOG(F("Sync interval set to ")); + DEBUGLOGCR(interval); + if(timeStatus() != timeSet) + setSyncInterval(interval); } return true; } @@ -311,10 +293,8 @@ boolean AvrNTPClient::setInterval(int shortInterval, int longInterval) { else { setSyncInterval(longInterval); } -#ifdef DEBUG_NTPCLIENT - Serial.print("Short sync interval set to "); Serial.println(shortInterval); - Serial.print("Long sync interval set to "); Serial.println(longInterval); -#endif // DEBUG_NTPCLIENT + DEBUGLOG(F("Short sync interval set to ")); DEBUGLOGCR(shortInterval); + DEBUGLOG(F("Long sync interval set to ")); DEBUGLOGCR(longInterval); return true; } else @@ -326,10 +306,8 @@ boolean AvrNTPClient::setTimeZone(int timeZone) { if (timeZone >= -11 || timeZone <= 13) { _timeZone = timeZone; -#ifdef DEBUG_NTPCLIENT - Serial.println("Time zone set to " + _timeZone); -#endif // DEBUG_NTPCLIENT - + DEBUGLOGCR(F("Time zone set to ")); + DEBUGLOGCR(_timeZone); return true; } else @@ -339,11 +317,8 @@ boolean AvrNTPClient::setTimeZone(int timeZone) void AvrNTPClient::setDayLight(boolean daylight) { _daylight = daylight; -#ifdef DEBUG_NTPCLIENT - Serial.print("--Set daylight saving to "); - Serial.println(daylight); - -#endif // DEBUG_NTPCLIENT + DEBUGLOG(F("--Set daylight saving to ")); + DEBUGLOGCR(daylight); } // diff --git a/ESPNTPClient.cpp b/ESPNTPClient.cpp index 82bfa7b..3ad42b2 100644 --- a/ESPNTPClient.cpp +++ b/ESPNTPClient.cpp @@ -8,6 +8,13 @@ #define DBG_PORT Serial +#ifdef DEBUG_NTPCLIENT +#define DEBUGLOG(...) DBG_PORT.printf(__VA_ARGS__) +#else +#define DEBUGLOG(...) +#endif + + ESPNTPClient NTP; ESPNTPClient::ESPNTPClient() @@ -21,9 +28,7 @@ bool ESPNTPClient::setNtpServerName(int idx, String ntpServerName) sntp_stop(); ntpServerName.toCharArray(buffer, ntpServerName.length()+1); sntp_setservername(idx, buffer); -#ifdef DEBUG_NTPCLIENT - DBG_PORT.printf("NTP server %d set to: %s \r\n", idx, buffer); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("NTP server %d set to: %s \r\n", idx, buffer); sntp_init(); return true; } @@ -44,9 +49,7 @@ bool ESPNTPClient::setTimeZone(int timeZone) sntp_stop(); bool result = sntp_set_timezone(timeZone); sntp_init(); -#ifdef DEBUG_NTPCLIENT - DBG_PORT.printf("NTP time zone set to: %d, result: %s\r\n", timeZone, result?"OK":"error"); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("NTP time zone set to: %d, result: %s\r\n", timeZone, result?"OK":"error"); return result; //return true; //} @@ -64,51 +67,33 @@ void ESPNTPClient::setLastNTPSync(time_t moment) { time_t getTime() { -#ifdef DEBUG_NTPCLIENT - DBG_PORT.print("-- NTP Server hostname: "); - DBG_PORT.println(sntp_getservername(0)); - DBG_PORT.println("-- Transmit NTP Request"); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("-- NTP Server hostname: %s\r\n", sntp_getservername(0)); + DEBUGLOG("-- Transmit NTP Request\r\n"); uint32 secsSince1970 = sntp_get_current_timestamp(); NTP.getUptime(); if (secsSince1970) { setSyncInterval(NTP.getInterval()); // Normal refresh frequency -#ifdef DEBUG_NTPCLIENT - DBG_PORT.println("Sync frequency set low"); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("Sync frequency set low\r\n"); if (NTP.getDayLight()) { if (NTP.summertime(year(secsSince1970), month(secsSince1970), day(secsSince1970), hour(secsSince1970), NTP.getTimeZone())) { secsSince1970 += SECS_PER_HOUR; -#ifdef DEBUG_NTPCLIENT - DBG_PORT.println("Summer Time"); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("Summer Time\r\n"); } -#ifdef DEBUG_NTPCLIENT else { - DBG_PORT.println("Winter Time"); + DEBUGLOG("Winter Time\r\n"); } -#endif // DEBUG_NTPCLIENT + } -#ifdef DEBUG_NTPCLIENT else { - DBG_PORT.println("No daylight"); + DEBUGLOG("No daylight\r\n"); } -#endif // DEBUG_NTPCLIENT - /*if (!NTP.getFirstSync()) - NTP.storeFirstSync(secsSince1970);*/ NTP.getFirstSync(); NTP.setLastNTPSync(secsSince1970); -#ifdef DEBUG_NTPCLIENT - DBG_PORT.print("Succeccful NTP sync at "); - DBG_PORT.println(NTP.getTimeDateString(NTP.getLastNTPSync())); -#endif // DEBUG_NTPCLIENT - + DEBUGLOG("Succeccful NTP sync at %s\r\n", NTP.getTimeDateString(NTP.getLastNTPSync()).c_str()); } else { -#ifdef DEBUG_NTPCLIENT - DBG_PORT.println("-- NTP error :-("); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("-- NTP error :-(\r\n"); setSyncInterval(NTP.getShortInterval()); // Fast refresh frequency, until successful sync } @@ -130,9 +115,7 @@ boolean ESPNTPClient::begin(String ntpServerName, int timeOffset, boolean daylig if (!setInterval(DEFAULT_NTP_SHORTINTERVAL, DEFAULT_NTP_INTERVAL)) { return false; } -#ifdef DEBUG_NTPCLIENT - DBG_PORT.println("Time sync started"); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("Time sync started\r\n"); setSyncInterval(getShortInterval()); setSyncProvider(getTime); @@ -142,11 +125,8 @@ boolean ESPNTPClient::begin(String ntpServerName, int timeOffset, boolean daylig boolean ESPNTPClient::stop() { setSyncProvider(NULL); -#ifdef DEBUG_NTPCLIENT - DBG_PORT.println("Time sync disabled"); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("Time sync disabled\r\n"); sntp_stop(); - return true; } @@ -155,16 +135,13 @@ boolean ESPNTPClient::setInterval(int interval) if (interval >= 10) { if (_longInterval != interval) { _longInterval = interval; -#ifdef DEBUG_NTPCLIENT - DBG_PORT.println("Long sync interval set to " + interval); -#endif // DEBUG_NTPCLIENT - setSyncInterval(interval); + DEBUGLOG("Long sync interval set to %d", interval); + if (timeStatus() != timeSet) + setSyncInterval(interval); } return true; } -#ifdef DEBUG_NTPCLIENT - DBG_PORT.printf("Error setting interval %d\r\n", interval); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("Error setting interval %d\r\n", interval); return false; } @@ -179,17 +156,11 @@ boolean ESPNTPClient::setInterval(int shortInterval, int longInterval) { else { setSyncInterval(longInterval); } -#ifdef DEBUG_NTPCLIENT - DBG_PORT.print("Short sync interval set to "); - DBG_PORT.println(shortInterval); - DBG_PORT.print("Long sync interval set to "); - DBG_PORT.println(longInterval); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("Short sync interval set to %d\r\n", shortInterval); + DEBUGLOG("Long sync interval set to %d\r\n", longInterval); return true; } -#ifdef DEBUG_NTPCLIENT - DBG_PORT.printf("Error setting interval %d %d\r\n", shortInterval, longInterval); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("Error setting interval. Short: %d Long: %d\r\n", shortInterval, longInterval); return false; } @@ -206,10 +177,7 @@ int ESPNTPClient::getShortInterval() void ESPNTPClient::setDayLight(boolean daylight) { _daylight = daylight; -#ifdef DEBUG_NTPCLIENT - DBG_PORT.print("--Set daylight saving to "); - DBG_PORT.println(daylight); -#endif // DEBUG_NTPCLIENT + DEBUGLOG("--Set daylight %s", daylight? "ON" : "OFF"); } boolean ESPNTPClient::getDayLight()