/* * Copyright (C) 2015,2016,2020,2022,2023 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "Log.h" #include "MQTTConnection.h" #if defined(_WIN32) || defined(_WIN64) #include #else #include #include #endif #include #include #include #include #include #include CMQTTConnection* m_mqtt = NULL; static unsigned int m_mqttLevel = 2U; static unsigned int m_displayLevel = 2U; static char LEVELS[] = " DMIWEF"; void LogInitialise(unsigned int displayLevel, unsigned int mqttLevel) { m_mqttLevel = mqttLevel; m_displayLevel = displayLevel; } void LogFinalise() { if (m_mqtt != NULL) { m_mqtt->close(); delete m_mqtt; m_mqtt = NULL; } } void Log(unsigned int level, const char* fmt, ...) { assert(fmt != NULL); char buffer[501U]; #if defined(_WIN32) || defined(_WIN64) SYSTEMTIME st; ::GetSystemTime(&st); ::sprintf(buffer, "%c: %04u-%02u-%02u %02u:%02u:%02u.%03u ", LEVELS[level], st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); #else struct timeval now; ::gettimeofday(&now, NULL); struct tm* tm = ::gmtime(&now.tv_sec); ::sprintf(buffer, "%c: %04d-%02d-%02d %02d:%02d:%02d.%03lld ", LEVELS[level], tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec / 1000LL); #endif va_list vl; va_start(vl, fmt); ::vsnprintf(buffer + ::strlen(buffer), 500, fmt, vl); va_end(vl); if (m_mqtt != NULL && level >= m_mqttLevel && m_mqttLevel != 0U) m_mqtt->publish("log", buffer); if (level >= m_displayLevel && m_displayLevel != 0U) { ::fprintf(stdout, "%s\n", buffer); ::fflush(stdout); } if (level == 6U) // Fatal exit(1); } void WriteJSON(const std::string& topLevel, nlohmann::json& json) { if (m_mqtt != NULL) { nlohmann::json top; top[topLevel] = json; m_mqtt->publish("json", top.dump()); } }