From c417f4198fce98198a96a7ec2ef658f1f575a5e0 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 2 Dec 2018 18:23:17 +1300 Subject: [PATCH] Rework log defines for clarity --- src/Alerts.cpp | 3 +-- src/Config.cpp | 19 +++++++++------ src/Core.cpp | 18 +++++++++----- src/Debug.cpp | 44 ++++++++++++++++++++++------------ src/Gps.cpp | 27 ++++++++++++++------- src/GpsTracker.ino | 5 ++-- src/Hardware.cpp | 42 ++++++++++++++++++++++---------- src/Logging.h | 42 +++++++++++++++++++------------- src/MainUnit.cpp | 10 +++++--- src/Network.cpp | 14 +++++------ src/NetworkPositionsBackup.cpp | 21 ++++++++++------ src/Positions.cpp | 21 +++++++++------- src/Rtc.cpp | 17 +++++++++---- src/SdPositionsBackup.cpp | 15 +++++++----- src/SdPositionsConfig.cpp | 15 ++++++++---- 15 files changed, 202 insertions(+), 111 deletions(-) diff --git a/src/Alerts.cpp b/src/Alerts.cpp index 65a4049..da53a73 100644 --- a/src/Alerts.cpp +++ b/src/Alerts.cpp @@ -5,9 +5,8 @@ #include "Rtc.h" #include "Logging.h" -#define LOGGER_NAME "Alerts" - namespace alerts { + #define CURRENT_LOGGER "alerts" uint8_t getTriggered(PositionEntryMetadata &metadata) { config_t* config = &config::main::value; diff --git a/src/Config.cpp b/src/Config.cpp index 3338e9e..f0a2025 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -2,9 +2,10 @@ #include "Hardware.h" #include "Logging.h" -#define LOGGER_NAME "Config" namespace config { + #define CURRENT_LOGGER "config" + namespace main { config_t value; @@ -12,16 +13,17 @@ namespace config { namespace details { void read() { - VERBOSE("read"); + #define CURRENT_LOGGER_FUNCTION "read" + VERBOSE; hardware::i2c::powerOn(); hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right hardware::i2c::powerOff(); - NOTICE_FORMAT("read", "%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); + NOTICE_FORMAT("%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); #if BACKUP_ENABLE_NETWORK - NOTICE_FORMAT("read", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); + NOTICE_FORMAT("%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); //networkConfig_t c = { // POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD, // 0xFFFF, @@ -39,9 +41,11 @@ namespace config { } void write() { - NOTICE_FORMAT("write", "%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); + #define CURRENT_LOGGER_FUNCTION "write" + + NOTICE_FORMAT("%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); #if BACKUP_ENABLE_NETWORK - NOTICE_FORMAT("write", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); + NOTICE_FORMAT("%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); #endif hardware::i2c::powerOn(); int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); @@ -63,7 +67,8 @@ namespace config { } void reset() { - VERBOSE("reset"); + #define CURRENT_LOGGER_FUNCTION "reset" + VERBOSE; config_t config = {}; config.seed = CONFIG_SEED; diff --git a/src/Core.cpp b/src/Core.cpp index a5fed38..ce789a8 100644 --- a/src/Core.cpp +++ b/src/Core.cpp @@ -8,13 +8,13 @@ #include "Alerts.h" #include "Logging.h" -#define LOGGER_NAME "Core" #define SMS_BUFFER_SIZE 140 #define NO_ALERTS_NOTIFIED 0 using namespace utils; namespace core { + #define CURRENT_LOGGER "core" uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS; uint8_t stoppedInARow = SLEEP_DEFAULT_STOPPED_THRESHOLD - 1; @@ -61,6 +61,8 @@ namespace core { } uint8_t notifyFailures(PositionEntryMetadata &metadata) { + #define CURRENT_LOGGER_FUNCTION "notifyFailures" + SIM808_NETWORK_REGISTRATION_STATE networkStatus; char buffer[SMS_BUFFER_SIZE]; const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?"); @@ -69,7 +71,7 @@ namespace core { uint8_t triggered = alerts::getTriggered(metadata); if (!triggered) return NO_ALERTS_NOTIFIED; - NOTICE_FORMAT("notifyFailures", "triggered : %B", triggered); + NOTICE_FORMAT("triggered : %B", triggered); network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); @@ -89,12 +91,12 @@ namespace core { } #if ALERTS_ON_SERIAL - NOTICE_FORMAT("notifyFailure", "%s", buffer); + NOTICE_FORMAT("%s", buffer); notified = true; #else notified = network::sendSms(buffer); #endif - if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !"); + if (!notified) NOTICE_MSG("SMS not sent !"); } network::powerOff(); @@ -108,6 +110,8 @@ namespace core { } TRACKER_MOVING_STATE updateSleepTime() { + #define CURRENT_LOGGER_FUNCTION "updateSleepTime" + TRACKER_MOVING_STATE state = TRACKER_MOVING_STATE::MOVING; uint8_t velocity = gps::getVelocity(); @@ -127,11 +131,13 @@ namespace core { } else stoppedInARow = 0; - NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime); + NOTICE_FORMAT("%dkmh => %d seconds", velocity, sleepTime); return state; } uint16_t mapSleepTime(uint8_t velocity) { + #define CURRENT_LOGGER_FUNCTION "mapSleepTime" + uint16_t result; uint16_t currentTime = 0xFFFF; @@ -154,7 +160,7 @@ namespace core { } - VERBOSE_FORMAT("mapSleepTime", "%d,%d", velocity, result); + VERBOSE_FORMAT("%d,%d", velocity, result); return result; } } \ No newline at end of file diff --git a/src/Debug.cpp b/src/Debug.cpp index 9254119..e23f953 100644 --- a/src/Debug.cpp +++ b/src/Debug.cpp @@ -9,8 +9,6 @@ #include "Alerts.h" #include "Logging.h" -#define LOGGER_NAME "Debug" - #define MENU_ENTRY(name, text) const char MENU_##name[] PROGMEM = text const char FAKE_GPS_ENTRY[] PROGMEM = "1,1,20170924184842.000,49.454862,1.144537,71.900,2.70,172.6,1,,1.3,2.2,1.8,,11,7,,,37,,"; @@ -116,10 +114,12 @@ static const PositionEntryMetadata fakeMetadata PROGMEM = { using namespace utils; namespace debug { + #define CURRENT_LOGGER "debug" namespace details { inline void displayPosition(PositionEntry entry) { - Log.notice(F("%d,%d,%d,%d,%d,%s"), + #define CURRENT_LOGGER_FUNCTION "displayPosition" + NOTICE_FORMAT("%d,%d,%d,%d,%d,%s", entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, @@ -130,7 +130,8 @@ namespace debug { } void displayFreeRam() { - Log.notice(F("RAM: %d\n"), mainunit::freeRam()); + #define CURRENT_LOGGER_FUNCTION "displayFreeRam" + NOTICE_FORMAT("%d", mainunit::freeRam()); } GPSTRACKER_DEBUG_COMMAND parseCommand(char id) { @@ -146,6 +147,8 @@ namespace debug { } GPSTRACKER_DEBUG_COMMAND menu(uint16_t timeout) { + #define CURRENT_LOGGER_FUNCTION "menu" + GPSTRACKER_DEBUG_COMMAND command; size_t menuSize = flash::getArraySize(MENU_ENTRIES); @@ -159,7 +162,7 @@ namespace debug { delay(MENU_INTERMEDIATE_TIMEOUT); timeout -= MENU_INTERMEDIATE_TIMEOUT; if (timeout <= 0) { - NOTICE_MSG("menu", "Timeout expired."); + NOTICE_MSG("Timeout expired."); return GPSTRACKER_DEBUG_COMMAND::RUN; } } @@ -172,32 +175,36 @@ namespace debug { } void getAndDisplayGpsPosition() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplayGpsPosition" SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS); - NOTICE_FORMAT("getAndDisplayGpsPosition", "%d %s", gpsStatus, gps::lastPosition); + NOTICE_FORMAT("%d %s", gpsStatus, gps::lastPosition); } void setFakeGpsPosition() { + #define CURRENT_LOGGER_FUNCTION "setFakeGpsPosition" strlcpy_P(gps::lastPosition, FAKE_GPS_ENTRY, GPS_POSITION_SIZE); - NOTICE_FORMAT("setFakeGpsPosition", "Last position set to : %s", gps::lastPosition); - NOTICE_FORMAT("setFakeGpsPosition", "Speed : %d", gps::getVelocity()); - NOTICE_FORMAT("setFakeGpsPosition", "Sleep time : %d", core::mapSleepTime(gps::getVelocity())); + NOTICE_FORMAT("Last position set to : %s", gps::lastPosition); + NOTICE_FORMAT("Speed : %d", gps::getVelocity()); + NOTICE_FORMAT("Sleep time : %d", core::mapSleepTime(gps::getVelocity())); } void getAndDisplayBattery() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplayBattery" hardware::sim808::powerOn(); SIM808ChargingStatus status = hardware::sim808::device.getChargingState(); hardware::sim808::powerOff(); - NOTICE_FORMAT("getAndDisplayBattery", "%d %d%% %dmV", status.state, status.level, status.voltage); + NOTICE_FORMAT("%d %d%% %dmV", status.state, status.level, status.voltage); } void getAndDisplayRtcTime() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplayRtcTime" tmElements_t time; rtc::getTime(time); - NOTICE_FORMAT("getAndDisplayRtcTime", "%d/%d/%d %d:%d:%d %t %d", time.year, time.month, time.day, time.hour, time.minute, time.second, rtc::isAccurate(), rtc::getTemperature()); + NOTICE_FORMAT("%d/%d/%d %d:%d:%d %t %d", time.year, time.month, time.day, time.hour, time.minute, time.second, rtc::isAccurate(), rtc::getTemperature()); } void setRtcTime() { @@ -207,6 +214,8 @@ namespace debug { } void getAndDisplaySleepTimes() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplaySleepTimes" + size_t arraySize = flash::getArraySize(config::defaultSleepTimings); sleepTimings_t maxSpeedTiming; utils::flash::read(&config::defaultSleepTimings[arraySize - 1], maxSpeedTiming); @@ -215,7 +224,7 @@ namespace debug { core::mapSleepTime(i); } - NOTICE_MSG("getAndDisplaySleepTimes", "Done"); + NOTICE_MSG("Done"); } void getAndDisplayEepromConfig() { @@ -223,6 +232,8 @@ namespace debug { } void getAndDisplayEepromContent() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplayEepromContent" + char buffer[128]; hardware::i2c::powerOn(); @@ -235,7 +246,7 @@ namespace debug { Serial.println(); hardware::i2c::powerOff(); - NOTICE_MSG("getAndDisplayEepromContent", "Done"); + NOTICE_MSG("Done"); } void getAndDisplayEepromPositions() { @@ -251,11 +262,12 @@ namespace debug { } void getAndDisplayEepromLastPosition() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplayEepromLastPosition" uint16_t lastEntryIndex = config::main::value.lastEntry; PositionEntry lastEntry; if(positions::get(lastEntryIndex, lastEntry)) details::displayPosition(lastEntry); - else Log.notice(F("No position recorded\n")); + else NOTICE_MSG("No position recorded"); } void addLastPositionToEeprom() { @@ -275,13 +287,15 @@ namespace debug { } void notifyFailures() { + #define CURRENT_LOGGER_FUNCTION "notifyFailures" + PositionEntryMetadata metadata = {}; flash::read(&fakeMetadata, metadata); metadata.batteryLevel = 1; metadata.temperature = ALERT_SUSPICIOUS_RTC_TEMPERATURE; uint8_t alerts = core::notifyFailures(metadata); - NOTICE_FORMAT("notifyFailures", "result : %B", alerts); + NOTICE_FORMAT("result : %B", alerts); alerts::add(alerts); } diff --git a/src/Gps.cpp b/src/Gps.cpp index 58001ef..c53dde8 100644 --- a/src/Gps.cpp +++ b/src/Gps.cpp @@ -5,8 +5,6 @@ #include "MainUnit.h" #include "Logging.h" -#define LOGGER_NAME "Gps" - #define TIME_YEAR_OFFSET 0 #define TIME_MONTH_OFFSET 4 #define TIME_DAY_OFFSET 6 @@ -16,7 +14,9 @@ #define EARTH_RADIUS 6371 //kilometers + namespace gps { + #define CURRENT_LOGGER "gps" namespace details { uint16_t parseSubstring(char *buffer, char *start, uint8_t size) { @@ -25,6 +25,7 @@ namespace gps { } } + char lastPosition[GPS_POSITION_SIZE]; SIM808_GPS_STATUS lastStatus; @@ -32,13 +33,15 @@ namespace gps { float previousLng = 0; SIM808_GPS_STATUS acquireCurrentPosition(int32_t timeout) { + #define CURRENT_LOGGER_FUNCTION "acquireCurrentPosition" + SIM808_GPS_STATUS currentStatus = SIM808_GPS_STATUS::OFF; do { currentStatus = hardware::sim808::device.getGpsStatus(lastPosition, GPS_POSITION_SIZE); if (currentStatus > SIM808_GPS_STATUS::FIX) break; //if we have an accurate fix, break right now - NOTICE_FORMAT("acquireCurrentPosition", "%d", currentStatus); + NOTICE_FORMAT("%d", currentStatus); mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); timeout -= GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS; } while (timeout > 1); @@ -47,7 +50,7 @@ namespace gps { lastStatus = currentStatus; } - NOTICE_FORMAT("acquireCurrentPosition", "%d", currentStatus); + NOTICE_FORMAT("%d", currentStatus); return currentStatus; } @@ -62,12 +65,14 @@ namespace gps { } float getDistanceFromPrevious() { + #define CURRENT_LOGGER_FUNCTION "getDistanceFromPrevious" + float lat1, lng1, lat2, lng2; if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LATITUDE, &lat2)) return 0; if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LONGITUDE, &lng2)) return 0; - VERBOSE_FORMAT("distanceFromPrevious", "%s, %F, %F, %F, %F", lastPosition, previousLat, previousLng, lat2, lng2); + VERBOSE_FORMAT("%s, %F, %F, %F, %F", lastPosition, previousLat, previousLng, lat2, lng2); lat1 = radians(previousLat); lng1 = radians(previousLng); @@ -85,25 +90,29 @@ namespace gps { a = EARTH_RADIUS * (2 * atan2(sqrt(a), sqrt(1 - a))); //kilometers - NOTICE_FORMAT("distanceFromPrevious", "%Fkm", a); + NOTICE_FORMAT("%Fkm", a); return a; } uint8_t getVelocity() { + #define CURRENT_LOGGER_FUNCTION "getVelocity" + int16_t velocity; if (!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity)) velocity = 0; - VERBOSE_FORMAT("getVelocity", "%d", velocity); + VERBOSE_FORMAT("%d", velocity); return velocity; } void getTime(tmElements_t &time) { + #define CURRENT_LOGGER_FUNCTION "getTime" + char *timeStr; char buffer[5]; hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::UTC, &timeStr); - VERBOSE_FORMAT("getTime", "%s", timeStr); + VERBOSE_FORMAT("%s", timeStr); time.year = details::parseSubstring(buffer, timeStr + TIME_YEAR_OFFSET, 4); time.month = details::parseSubstring(buffer, timeStr + TIME_MONTH_OFFSET, 2); @@ -112,6 +121,6 @@ namespace gps { time.minute = details::parseSubstring(buffer, timeStr + TIME_MINUTE_OFFSET, 2); time.second = details::parseSubstring(buffer, timeStr + TIME_SECOND_OFFSET, 2); - NOTICE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); + NOTICE_FORMAT("%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); } } \ No newline at end of file diff --git a/src/GpsTracker.ino b/src/GpsTracker.ino index 28332fb..d691f89 100644 --- a/src/GpsTracker.ino +++ b/src/GpsTracker.ino @@ -10,7 +10,7 @@ #include "Positions.h" #include "Logging.h" -#define LOGGER_NAME "GpsTracker" +#define CURRENT_LOGGER "GpsTracker" bool bypassMenu = false; uint16_t menuTimeout = MENU_TIMEOUT; @@ -26,6 +26,7 @@ void setup() { } void loop() { + #define CURRENT_LOGGER_FUNCTION "loop" debug::GPSTRACKER_DEBUG_COMMAND command = debug::GPSTRACKER_DEBUG_COMMAND::RUN; if (Serial && !bypassMenu) command = debug::menu(menuTimeout); @@ -95,6 +96,6 @@ void loop() { mainunit::deepSleep(10); break; default: - NOTICE_MSG("loop", "Unsupported"); + NOTICE_MSG("Unsupported"); } } diff --git a/src/Hardware.cpp b/src/Hardware.cpp index 99d360b..0c8355c 100644 --- a/src/Hardware.cpp +++ b/src/Hardware.cpp @@ -5,16 +5,18 @@ namespace hardware { -#define LOGGER_NAME "Hardware::sim808" - namespace sim808 { + #define CURRENT_LOGGER "hardware::sim808" + SIM_SERIAL_TYPE simSerial = SIM_SERIAL; SIM808 device = SIM808(SIM_RST, SIM_PWR, SIM_STATUS); uint8_t networkPoweredCount = 0; uint8_t gpsPoweredCount = 0; void powerOn() { - VERBOSE("powerOn"); + #define CURRENT_LOGGER_FUNCTION "powerOn" + VERBOSE; + bool poweredOn = device.powerOnOff(true); if (!poweredOn) return; @@ -23,7 +25,9 @@ namespace hardware { } void powerOff() { - VERBOSE("powerOff"); + #define CURRENT_LOGGER_FUNCTION "powerOff" + VERBOSE; + device.powerOnOff(false); networkPoweredCount = gpsPoweredCount = 0; } @@ -41,19 +45,22 @@ namespace hardware { } void setup() { - NOTICE("setup"); + #define CURRENT_LOGGER_FUNCTION "setup" + NOTICE; + simSerial.begin(SIM808_BAUDRATE); device.begin(simSerial); powerOff(); //ensure powerOff on start } void gpsPowerOn() { + #define CURRENT_LOGGER_FUNCTION "gpsPowerOn" if(gpsPoweredCount) { gpsPoweredCount++; return; } - VERBOSE("gpsPowerOn"); + VERBOSE; powerOn(); if(!networkPoweredCount) { //SIM808 turns phone on by default but we don't need it for gps only @@ -63,6 +70,8 @@ namespace hardware { } void gpsPowerOff() { + #define CURRENT_LOGGER_FUNCTION "gpsPowerOff" + if (!device.powered()) { networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0 return; @@ -73,23 +82,27 @@ namespace hardware { return; } - VERBOSE("gpsPowerOff"); + VERBOSE; device.powerOnOffGps(false); powerOffIfUnused(); } void networkPowerOn() { + #define CURRENT_LOGGER_FUNCTION "networkPowerOn" + if(networkPoweredCount) { networkPoweredCount++; return; } - VERBOSE("networkPowerOn"); + VERBOSE; powerOn(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); } void networkPowerOff() { + #define CURRENT_LOGGER_FUNCTION "networkPowerOff" + if (!device.powered()) { networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0 return; @@ -100,27 +113,28 @@ namespace hardware { return; } - VERBOSE("networkPowerOff"); + VERBOSE; device.disableGprs(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); powerOffIfUnused(); } } -#define LOGGER_NAME "Hardware::i2c" - namespace i2c { + #define CURRENT_LOGGER "hardware::i2c" E24 eeprom = E24(E24_SIZE, E24_ADDRESS); uint8_t poweredCount = 0; void powerOn() { + #define CURRENT_LOGGER_FUNCTION "powerOn" + if(poweredCount) { poweredCount++; return; } - VERBOSE("powerOn"); + VERBOSE; digitalWrite(I2C_PWR, HIGH); pinMode(I2C_PWR, OUTPUT); @@ -129,12 +143,14 @@ namespace hardware { } void powerOff(bool forced = false) { + #define CURRENT_LOGGER_FUNCTION "powerOff" + if(poweredCount > 1 && !forced) { poweredCount--; return; } - VERBOSE("powerOff"); + VERBOSE; pinMode(I2C_PWR, INPUT); digitalWrite(I2C_PWR, LOW); diff --git a/src/Logging.h b/src/Logging.h index 6955805..6cef01a 100644 --- a/src/Logging.h +++ b/src/Logging.h @@ -2,33 +2,43 @@ //#define DISABLE_LOGGING 1 #include +#include //reusing flash string utilities #include "Config.h" #define LOG_SERIAL_SPEED 115200 #if _DEBUG -#define LOG_LEVEL LOG_LEVEL_VERBOSE + #define LOG_LEVEL LOG_LEVEL_VERBOSE + #define LOG_LEVEL_VERBOSE_ENABLED + #define LOG_LEVEL_NOTICE_ENABLED #else -#define LOG_LEVEL LOG_LEVEL_NOTICE + #define LOG_LEVEL LOG_LEVEL_NOTICE + #define LOG_LEVEL_NOTICE_ENABLED #endif -#define LOG(level, f) Log.level(F("[" LOGGER_NAME "::" f "]\n")) -#define LOG_MSG(level, f, msg) Log.level(F("[" LOGGER_NAME "::" f "] " msg "\n")) -#define LOG_FORMAT(level, f, msg, ...) Log.level(F("[" LOGGER_NAME "::" f "] " msg "\n"), __VA_ARGS__) +#define NL "\n" +#define LOG(level) LOG_MSG(level, "") +#define LOG_MSG(level, msg) Log.level(F("[" CURRENT_LOGGER "::" CURRENT_LOGGER_FUNCTION "]" msg NL)) +#define LOG_FORMAT(level, msg, ...) Log.level(F("[" CURRENT_LOGGER "::" CURRENT_LOGGER_FUNCTION "]" msg NL), __VA_ARGS__) -#if _DEBUG -#define VERBOSE(f) LOG(verbose, f) -#define VERBOSE_MSG(f, msg) LOG_MSG(verbose, f, msg) -#define VERBOSE_FORMAT(f, msg, ...) LOG_FORMAT(verbose, f, msg, __VA_ARGS__) +#if defined(LOG_LEVEL_VERBOSE_ENABLED) + #define VERBOSE LOG(verbose) + #define VERBOSE_MSG(msg) LOG_MSG(verbose, msg) + #define VERBOSE_FORMAT(msg, ...) LOG_FORMAT(verbose, msg, __VA_ARGS__) #else -#define VERBOSE(f) -#define VERBOSE_MSG(f, msg) -#define VERBOSE_FORMAT(f, msg, ...) + #define VERBOSE + #define VERBOSE_MSG(msg) + #define VERBOSE_FORMAT(msg, ...) #endif -#define NOTICE(f) LOG(notice, f) -#define NOTICE_MSG(f, msg) LOG_MSG(notice, f, msg) -#define NOTICE_FORMAT(f, msg, ...) LOG_FORMAT(notice, f, msg, __VA_ARGS__) - +#if defined(LOG_LEVEL_NOTICE_ENABLED) + #define NOTICE LOG(notice) + #define NOTICE_MSG(msg) LOG_MSG(notice, msg) + #define NOTICE_FORMAT(msg, ...) LOG_FORMAT(notice, msg, __VA_ARGS__) +#else + #define NOTICE + #define NOTICE_MSG(msg) + #define NOTICE_FORMAT(msg, ...) +#endif namespace logging { void setup(); diff --git a/src/MainUnit.cpp b/src/MainUnit.cpp index 1455b8d..369322d 100644 --- a/src/MainUnit.cpp +++ b/src/MainUnit.cpp @@ -4,11 +4,11 @@ #include "config/Pins.h" #include "Logging.h" -#define LOGGER_NAME "MainUnit" extern int __heap_start, *__brkval; namespace mainunit { + #define CURRENT_LOGGER "mainunit" namespace details { @@ -18,9 +18,11 @@ namespace mainunit { } void wokeUp() { + #define CURRENT_LOGGER_FUNCTION "wokeUp" + tmElements_t wokeUpTime; rtc::getTime(wokeUpTime); - VERBOSE_FORMAT("wokeUp", "%d:%d:%d", wokeUpTime.hour, wokeUpTime.minute, wokeUpTime.second); + VERBOSE_FORMAT("%d:%d:%d", wokeUpTime.hour, wokeUpTime.minute, wokeUpTime.second); hardware::sim808::simSerial.listen(); } @@ -39,7 +41,9 @@ namespace mainunit { } void deepSleep(uint16_t seconds) { - NOTICE_FORMAT("deepSleep", "%d seconds", seconds); + #define CURRENT_LOGGER_FUNCTION "deepSleep" + NOTICE_FORMAT("%d seconds", seconds); + interruptIn(seconds); details::prepareSleep(); LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); diff --git a/src/Network.cpp b/src/Network.cpp index 463d380..86a0ecc 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -5,10 +5,8 @@ #include "Config.h" #include "Logging.h" -#define LOGGER_NAME "Network" - namespace network { - + #define CURRENT_LOGGER "network" timestamp_t _poweredOnTime; void powerOn() { @@ -23,6 +21,7 @@ namespace network { __attribute__((__optimize__("O2"))) SIM808_NETWORK_REGISTRATION_STATE waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true) { + #define CURRENT_LOGGER_FUNCTION "waitForRegistered" SIM808_NETWORK_REGISTRATION_STATE currentStatus; SIM808SignalQualityReport report; @@ -36,12 +35,12 @@ namespace network { do { if (isAvailable(currentStatus)) break; - NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); + NOTICE_FORMAT("%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); if (report.rssi < NETWORK_DEFAULT_NO_NETWORK_QUALITY_THRESHOLD) noReliableNetwork++; else noReliableNetwork = 0; if (noReliableNetwork > NETWORK_DEFAULT_NO_NETWORK_TRIES) { - NOTICE_MSG("waitForRegistered", "No reliable signal"); + NOTICE_MSG("No reliable signal"); break; //after a while, no network really means no network. Bailing out } @@ -52,7 +51,7 @@ namespace network { report = hardware::sim808::device.getSignalQuality(); } while (timeout > 1); - NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); + NOTICE_FORMAT("%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); return currentStatus; } @@ -69,9 +68,10 @@ namespace network { #endif bool sendSms(const char * msg) { + #define CURRENT_LOGGER_FUNCTION "sendSms" const char * phoneNumber = config::main::value.contactPhone; - NOTICE_FORMAT("sendSms", "%s, %s", phoneNumber, msg); + NOTICE_FORMAT("%s, %s", phoneNumber, msg); return hardware::sim808::device.sendSms(phoneNumber, msg); } } \ No newline at end of file diff --git a/src/NetworkPositionsBackup.cpp b/src/NetworkPositionsBackup.cpp index 183c9e2..b13f8b2 100644 --- a/src/NetworkPositionsBackup.cpp +++ b/src/NetworkPositionsBackup.cpp @@ -9,11 +9,12 @@ #include "Network.h" #include "Logging.h" -#define LOGGER_NAME "Positions::backup::network" - #define BUFFER_SIZE 170 + namespace positions { + #define CURRENT_LOGGER "Positions::backup::network" + namespace backup { namespace net { @@ -27,6 +28,8 @@ namespace positions { } bool NetworkPositionsBackup::appendPosition(PositionEntry &entry, int8_t signalAttenuation = 0) { + #define CURRENT_LOGGER_FUNCTION "appendPosition" + char buffer[BUFFER_SIZE]; if(signalAttenuation == 0) signalAttenuation = hardware::sim808::device.getSignalQuality().attenuation; @@ -40,7 +43,7 @@ namespace positions { entry.metadata.timeToFix, entry.position); - NOTICE_FORMAT("appendPosition", "Sending : %s", buffer); + NOTICE_FORMAT("Sending : %s", buffer); uint16_t responseCode = hardware::sim808::device.httpPost( config::main::value.network.url, F("text/gpstracker"), @@ -49,12 +52,14 @@ namespace positions { BUFFER_SIZE ); - NOTICE_FORMAT("appendPosition", "Response : %d", responseCode); + NOTICE_FORMAT("Response : %d", responseCode); return responseCode == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE; } //__attribute__((__optimize__("O2"))) void NetworkPositionsBackup::appendPositions() { + #define CURRENT_LOGGER_FUNCTION "appendPositions" + uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1; PositionEntry currentEntry; SIM808_NETWORK_REGISTRATION_STATE networkStatus; @@ -67,7 +72,7 @@ namespace positions { if (!network::isAvailable(networkStatus) || !network::enableGprs()) { networkUnavailableInARow = min(networkUnavailableInARow + 1, POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD + 1); //avoid increment overflow - NOTICE_MSG("appendPositions", "network or gprs unavailable"); + NOTICE_MSG("network or gprs unavailable"); if (networkUnavailableInARow > POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD) { networkUnavailablePostpone++; @@ -96,14 +101,16 @@ namespace positions { void NetworkPositionsBackup::setup() {} void NetworkPositionsBackup::prepare() { - NOTICE("prepare"); + #define CURRENT_LOGGER_FUNCTION "prepare" + NOTICE; if (!isBackupNeeded(true)) return; network::powerOn(); } void NetworkPositionsBackup::backup(bool force) { - NOTICE("backup"); + #define CURRENT_LOGGER_FUNCTION "backup" + NOTICE; if (force || isBackupNeeded(false)) { appendPositions(); diff --git a/src/Positions.cpp b/src/Positions.cpp index d4c8440..8996839 100644 --- a/src/Positions.cpp +++ b/src/Positions.cpp @@ -16,12 +16,12 @@ #include "NetworkPositionsBackup.h" #endif -#define LOGGER_NAME "Positions" - #define ENTRY_RESERVED_SIZE 128 #define ENTRIES_ADDR CONFIG_RESERVED_SIZE namespace positions { + #define CURRENT_LOGGER "positions" + #if BACKUPS_ENABLED > 1 backup::PositionsBackup **_backups; #elif BACKUPS_ENABLED == 1 @@ -73,7 +73,8 @@ namespace positions { } bool acquire(PositionEntryMetadata &metadata) { - NOTICE("acquire"); + #define CURRENT_LOGGER_FUNCTION "acquire" + NOTICE; timestamp_t before; @@ -85,7 +86,7 @@ namespace positions { gps::powerOff(); bool acquired = gpsStatus >= SIM808_GPS_STATUS::FIX; //prety useless wins 14 bytes on the hex size rather than return gpStatus >= ... - NOTICE_FORMAT("acquire", "Status : %d", gpsStatus); + NOTICE_FORMAT("Status : %d", gpsStatus); metadata = { battery.level, @@ -99,7 +100,8 @@ namespace positions { } void appendLast(const PositionEntryMetadata &metadata) { - VERBOSE("appendLast"); + #define CURRENT_LOGGER_FUNCTION "appendLast" + VERBOSE; uint16_t entryIndex; uint16_t entryAddress; @@ -114,7 +116,7 @@ namespace positions { hardware::i2c::powerOn(); hardware::i2c::eeprom.writeBlock(entryAddress, entry); - NOTICE_FORMAT("appendLast", "Saved @ %X : %d,%d,%d,%d,%d,%s", + NOTICE_FORMAT("Saved @ %X : %d,%d,%d,%d,%d,%s", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, @@ -133,18 +135,19 @@ namespace positions { } bool get(uint16_t index, PositionEntry &entry) { - VERBOSE("get"); + #define CURRENT_LOGGER_FUNCTION "get" + VERBOSE; uint16_t entryAddress = details::getEntryAddress(index); if (entryAddress == -1) return false; - VERBOSE_FORMAT("get", "Reading entry %d @ %X", index, entryAddress); + VERBOSE_FORMAT("Reading entry %d @ %X", index, entryAddress); hardware::i2c::powerOn(); hardware::i2c::eeprom.readBlock(entryAddress, entry); hardware::i2c::powerOff(); - NOTICE_FORMAT("get", "Read from EEPROM @ %X : %d,%d,%d,%d,%d,%s", + NOTICE_FORMAT("Read from EEPROM @ %X : %d,%d,%d,%d,%d,%s", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, diff --git a/src/Rtc.cpp b/src/Rtc.cpp index 508411e..e4b2c46 100644 --- a/src/Rtc.cpp +++ b/src/Rtc.cpp @@ -5,15 +5,17 @@ #include -#define LOGGER_NAME "Rtc" using namespace utils; namespace rtc { + #define CURRENT_LOGGER "rtc" RTC_A_CLASS RTC_A; void setup() { - VERBOSE("setup"); + #define CURRENT_LOGGER_FUNCTION "setup" + VERBOSE; + hardware::i2c::powerOn(); RTC_A.control(DS3231_12H, DS3231_OFF); //24 hours clock RTC_A.control(DS3231_A1_INT_ENABLE, DS3231_OFF); //Alarm 1 OFF @@ -44,15 +46,18 @@ namespace rtc { } void getTime(tmElements_t &time) { + #define CURRENT_LOGGER_FUNCTION "getTime" + hardware::i2c::powerOn(); RTC_A.readTime(time); hardware::i2c::powerOff(); - VERBOSE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); + VERBOSE_FORMAT("%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); } void setTime(const tmElements_t &time) { - VERBOSE_FORMAT("setTime", "%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); + #define CURRENT_LOGGER_FUNCTION "setTime" + VERBOSE_FORMAT("%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); hardware::i2c::powerOn(); RTC_A.writeTime(time); @@ -71,6 +76,8 @@ namespace rtc { } void setAlarm(const tmElements_t &time) { + #define CURRENT_LOGGER_FUNCTION "setAlarm" + hardware::i2c::powerOn(); WRITE_ALARM_1(time); @@ -78,7 +85,7 @@ namespace rtc { RTC_A.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON RTC_A.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON - NOTICE_FORMAT("setAlarm", "Next alarm : %d:%d:%d", time.hour, time.minute, time.second); + NOTICE_FORMAT("Next alarm : %d:%d:%d", time.hour, time.minute, time.second); hardware::i2c::powerOff(); } diff --git a/src/SdPositionsBackup.cpp b/src/SdPositionsBackup.cpp index fceeee9..d2d40cb 100644 --- a/src/SdPositionsBackup.cpp +++ b/src/SdPositionsBackup.cpp @@ -6,10 +6,10 @@ #include "Positions.h" #include "Logging.h" -#define LOGGER_NAME "Positions::backup::sd" - #if BACKUP_ENABLE_SDCARD namespace positions { + #define CURRENT_LOGGER "positions::backup::sd" + namespace backup { namespace sd { @@ -60,7 +60,8 @@ namespace positions { } void appendPosition(File &file, SdPositionConfig_t &sdConfig, PositionEntry &entry) { - VERBOSE("appendPosition"); + #define CURRENT_LOGGER_FUNCTION "appendPosition" + VERBOSE; const char fieldTerminator = ','; @@ -75,7 +76,8 @@ namespace positions { } void appendPositions(SdPositionConfig_t &sdConfig) { - VERBOSE("appendPositions"); + #define CURRENT_LOGGER_FUNCTION "appendPositions" + VERBOSE; uint16_t currentEntryIndex = sdConfig.lastSavedEntry + 1; PositionEntry currentEntry; @@ -100,10 +102,11 @@ namespace positions { } void SdPositionsBackup::backup(bool force) { - VERBOSE("backup"); + #define CURRENT_LOGGER_FUNCTION "backup" + VERBOSE; if (!hardware::sdcard::available) { - VERBOSE_MSG("backup", "not available"); + VERBOSE_MSG("not available"); return; } diff --git a/src/SdPositionsConfig.cpp b/src/SdPositionsConfig.cpp index 29a0550..416bee3 100644 --- a/src/SdPositionsConfig.cpp +++ b/src/SdPositionsConfig.cpp @@ -2,10 +2,11 @@ #include "SdCard.h" #include "Logging.h" -#define LOGGER_NAME "Config::backup::sd" #if BACKUP_ENABLE_SDCARD namespace config { + #define CURRENT_LOGGER "config::backup::sd" + namespace backup { namespace sd { @@ -24,14 +25,18 @@ namespace config { } void read() { - VERBOSE("read"); + #define CURRENT_LOGGER_FUNCTION "read" + VERBOSE; + ensureOpened(); configFile.read((void*)&value, sizeof(value)); if (value.seed != POSITIONS_CONFIG_SEED) reset(); } void write() { - VERBOSE("write"); + #define CURRENT_LOGGER_FUNCTION "write" + VERBOSE; + ensureOpened(); configFile.write((void*)&value, sizeof(value)); } @@ -50,7 +55,9 @@ namespace config { } void reset() { - VERBOSE("reset"); + #define CURRENT_LOGGER_FUNCTION "reset" + VERBOSE; + SdPositionConfig_t config = { POSITIONS_CONFIG_SEED, POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD,