Remove the file based logging, only go via MQTT from now on.

This commit is contained in:
Jonathan Naylor
2023-06-29 11:03:06 +01:00
parent 3c48b8f61b
commit c2bd065b2d
6 changed files with 6 additions and 170 deletions

View File

@@ -78,10 +78,6 @@ m_description(),
m_url(), m_url(),
m_logMQTTLevel(0U), m_logMQTTLevel(0U),
m_logDisplayLevel(0U), m_logDisplayLevel(0U),
m_logFileLevel(0U),
m_logFilePath(),
m_logFileRoot(),
m_logFileRotate(true),
m_mqttHost("127.0.0.1"), m_mqttHost("127.0.0.1"),
m_mqttPort(1883), m_mqttPort(1883),
m_mqttKeepalive(60U), m_mqttKeepalive(60U),
@@ -457,18 +453,10 @@ bool CConf::read()
else if (::strcmp(key, "URL") == 0) else if (::strcmp(key, "URL") == 0)
m_url = value; m_url = value;
} else if (section == SECTION_LOG) { } else if (section == SECTION_LOG) {
if (::strcmp(key, "FilePath") == 0) if (::strcmp(key, "MQTTLevel") == 0)
m_logFilePath = value;
else if (::strcmp(key, "FileRoot") == 0)
m_logFileRoot = value;
else if (::strcmp(key, "MQTTLevel") == 0)
m_logMQTTLevel = (unsigned int)::atoi(value); m_logMQTTLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "FileLevel") == 0)
m_logFileLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "DisplayLevel") == 0) else if (::strcmp(key, "DisplayLevel") == 0)
m_logDisplayLevel = (unsigned int)::atoi(value); m_logDisplayLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "FileRotate") == 0)
m_logFileRotate = ::atoi(value) == 1;
} else if (section == SECTION_MQTT) { } else if (section == SECTION_MQTT) {
if (::strcmp(key, "Host") == 0) if (::strcmp(key, "Host") == 0)
m_mqttHost = value; m_mqttHost = value;
@@ -1117,26 +1105,6 @@ unsigned int CConf::getLogDisplayLevel() const
return m_logDisplayLevel; return m_logDisplayLevel;
} }
unsigned int CConf::getLogFileLevel() const
{
return m_logFileLevel;
}
std::string CConf::getLogFilePath() const
{
return m_logFilePath;
}
std::string CConf::getLogFileRoot() const
{
return m_logFileRoot;
}
bool CConf::getLogFileRotate() const
{
return m_logFileRotate;
}
std::string CConf::getMQTTHost() const std::string CConf::getMQTTHost() const
{ {
return m_mqttHost; return m_mqttHost;

8
Conf.h
View File

@@ -51,10 +51,6 @@ public:
// The Log section // The Log section
unsigned int getLogMQTTLevel() const; unsigned int getLogMQTTLevel() const;
unsigned int getLogDisplayLevel() const; unsigned int getLogDisplayLevel() const;
unsigned int getLogFileLevel() const;
std::string getLogFilePath() const;
std::string getLogFileRoot() const;
bool getLogFileRotate() const;
// The MQTT section // The MQTT section
std::string getMQTTHost() const; std::string getMQTTHost() const;
@@ -350,10 +346,6 @@ private:
unsigned int m_logMQTTLevel; unsigned int m_logMQTTLevel;
unsigned int m_logDisplayLevel; unsigned int m_logDisplayLevel;
unsigned int m_logFileLevel;
std::string m_logFilePath;
std::string m_logFileRoot;
bool m_logFileRotate;
std::string m_mqttHost; std::string m_mqttHost;
unsigned short m_mqttPort; unsigned short m_mqttPort;

116
Log.cpp
View File

@@ -37,119 +37,18 @@ CMQTTConnection* m_mqtt = NULL;
static unsigned int m_mqttLevel = 2U; static unsigned int m_mqttLevel = 2U;
static unsigned int m_fileLevel = 2U;
static std::string m_filePath;
static std::string m_fileRoot;
static bool m_fileRotate = true;
static FILE* m_fpLog = NULL;
static bool m_daemon = false;
static unsigned int m_displayLevel = 2U; static unsigned int m_displayLevel = 2U;
static struct tm m_tm;
static char LEVELS[] = " DMIWEF"; static char LEVELS[] = " DMIWEF";
static bool logOpenRotate() void LogInitialise(unsigned int displayLevel, unsigned int mqttLevel)
{ {
bool status = false;
if (m_fileLevel == 0U)
return true;
time_t now;
::time(&now);
struct tm* tm = ::gmtime(&now);
if (tm->tm_mday == m_tm.tm_mday && tm->tm_mon == m_tm.tm_mon && tm->tm_year == m_tm.tm_year) {
if (m_fpLog != NULL)
return true;
} else {
if (m_fpLog != NULL)
::fclose(m_fpLog);
}
char filename[200U];
#if defined(_WIN32) || defined(_WIN64)
::sprintf(filename, "%s\\%s-%04d-%02d-%02d.log", m_filePath.c_str(), m_fileRoot.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
#else
::sprintf(filename, "%s/%s-%04d-%02d-%02d.log", m_filePath.c_str(), m_fileRoot.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
#endif
if ((m_fpLog = ::fopen(filename, "a+t")) != NULL) {
status = true;
#if !defined(_WIN32) && !defined(_WIN64)
if (m_daemon)
dup2(fileno(m_fpLog), fileno(stderr));
#endif
}
m_tm = *tm;
return status;
}
static bool logOpenNoRotate()
{
bool status = false;
if (m_fileLevel == 0U)
return true;
if (m_fpLog != NULL)
return true;
char filename[200U];
#if defined(_WIN32) || defined(_WIN64)
::sprintf(filename, "%s\\%s.log", m_filePath.c_str(), m_fileRoot.c_str());
#else
::sprintf(filename, "%s/%s.log", m_filePath.c_str(), m_fileRoot.c_str());
#endif
if ((m_fpLog = ::fopen(filename, "a+t")) != NULL) {
status = true;
#if !defined(_WIN32) && !defined(_WIN64)
if (m_daemon)
dup2(fileno(m_fpLog), fileno(stderr));
#endif
}
return status;
}
bool LogOpen()
{
if (m_fileRotate)
return logOpenRotate();
else
return logOpenNoRotate();
}
bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, unsigned int mqttLevel, bool rotate)
{
m_filePath = filePath;
m_fileRoot = fileRoot;
m_mqttLevel = mqttLevel; m_mqttLevel = mqttLevel;
m_fileLevel = fileLevel;
m_displayLevel = displayLevel; m_displayLevel = displayLevel;
m_daemon = daemon;
m_fileRotate = rotate;
if (m_daemon)
m_displayLevel = 0U;
return ::LogOpen();
} }
void LogFinalise() void LogFinalise()
{ {
if (m_fpLog != NULL)
::fclose(m_fpLog);
if (m_mqtt != NULL) { if (m_mqtt != NULL) {
m_mqtt->close(); m_mqtt->close();
delete m_mqtt; delete m_mqtt;
@@ -186,24 +85,13 @@ void Log(unsigned int level, const char* fmt, ...)
if (m_mqtt != NULL && level >= m_mqttLevel && m_mqttLevel != 0U) if (m_mqtt != NULL && level >= m_mqttLevel && m_mqttLevel != 0U)
m_mqtt->publish("log", buffer); m_mqtt->publish("log", buffer);
if (level >= m_fileLevel && m_fileLevel != 0U) {
bool ret = ::LogOpen();
if (!ret)
return;
::fprintf(m_fpLog, "%s\n", buffer);
::fflush(m_fpLog);
}
if (level >= m_displayLevel && m_displayLevel != 0U) { if (level >= m_displayLevel && m_displayLevel != 0U) {
::fprintf(stdout, "%s\n", buffer); ::fprintf(stdout, "%s\n", buffer);
::fflush(stdout); ::fflush(stdout);
} }
if (level == 6U) { // Fatal if (level == 6U) // Fatal
::fclose(m_fpLog);
exit(1); exit(1);
}
} }
void WriteJSON(const std::string& topLevel, nlohmann::json& json) void WriteJSON(const std::string& topLevel, nlohmann::json& json)

4
Log.h
View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2015,2016,2020,2022 by Jonathan Naylor G4KLX * Copyright (C) 2015,2016,2020,2022,2023 by Jonathan Naylor G4KLX
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -32,7 +32,7 @@
extern void Log(unsigned int level, const char* fmt, ...); extern void Log(unsigned int level, const char* fmt, ...);
extern bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, unsigned int mqttLevel, bool rotate); extern void LogInitialise(unsigned int displayLevel, unsigned int mqttLevel);
extern void LogFinalise(); extern void LogFinalise();
extern void WriteJSON(const std::string& topLevel, nlohmann::json& json); extern void WriteJSON(const std::string& topLevel, nlohmann::json& json);

View File

@@ -24,10 +24,6 @@ URL=www.google.co.uk
# Logging levels, 0=No logging # Logging levels, 0=No logging
MQTTLevel=1 MQTTLevel=1
DisplayLevel=1 DisplayLevel=1
FileLevel=1
FilePath=.
FileRoot=MMDVM
FileRotate=1
[MQTT] [MQTT]
Host=127.0.0.1 Host=127.0.0.1

View File

@@ -282,15 +282,7 @@ int CMMDVMHost::run()
#endif #endif
#endif #endif
#if !defined(_WIN32) && !defined(_WIN64) ::LogInitialise(m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel());
ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel(), m_conf.getLogFileRotate());
#else
ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel(), m_conf.getLogFileRotate());
#endif
if (!ret) {
::fprintf(stderr, "MMDVMHost: unable to open the log file\n");
return 1;
}
std::vector<std::pair<std::string, void (*)(const std::string&)>> subscriptions; std::vector<std::pair<std::string, void (*)(const std::string&)>> subscriptions;
subscriptions.push_back(std::make_pair("display", CMMDVMHost::onDisplay)); subscriptions.push_back(std::make_pair("display", CMMDVMHost::onDisplay));