Filter the log data sent to MQTT based on the MQTTLevel setting.

This commit is contained in:
Jonathan Naylor
2022-11-29 18:04:03 +00:00
parent 53b73c6558
commit 2f761603bc
7 changed files with 21 additions and 7 deletions

View File

@@ -82,6 +82,7 @@ m_height(0),
m_location(),
m_description(),
m_url(),
m_logMQTTLevel(0U),
m_logDisplayLevel(0U),
m_logFileLevel(0U),
m_logFilePath(),
@@ -514,6 +515,8 @@ bool CConf::read()
m_logFilePath = value;
else if (::strcmp(key, "FileRoot") == 0)
m_logFileRoot = value;
else if (::strcmp(key, "MQTTLevel") == 0)
m_logMQTTLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "FileLevel") == 0)
m_logFileLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "DisplayLevel") == 0)
@@ -1244,6 +1247,11 @@ std::string CConf::getURL() const
return m_url;
}
unsigned int CConf::getLogMQTTLevel() const
{
return m_logMQTTLevel;
}
unsigned int CConf::getLogDisplayLevel() const
{
return m_logDisplayLevel;

2
Conf.h
View File

@@ -50,6 +50,7 @@ public:
std::string getURL() const;
// The Log section
unsigned int getLogMQTTLevel() const;
unsigned int getLogDisplayLevel() const;
unsigned int getLogFileLevel() const;
std::string getLogFilePath() const;
@@ -395,6 +396,7 @@ private:
std::string m_description;
std::string m_url;
unsigned int m_logMQTTLevel;
unsigned int m_logDisplayLevel;
unsigned int m_logFileLevel;
std::string m_logFilePath;

View File

@@ -35,6 +35,8 @@
CMQTTPublisher* m_mqtt = NULL;
static unsigned int m_mqttLevel = 2U;
static unsigned int m_fileLevel = 2U;
static std::string m_filePath;
static std::string m_fileRoot;
@@ -127,10 +129,11 @@ bool LogOpen()
return logOpenNoRotate();
}
bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, bool rotate)
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_fileLevel = fileLevel;
m_displayLevel = displayLevel;
m_daemon = daemon;
@@ -174,7 +177,7 @@ void Log(unsigned int level, const char* fmt, ...)
va_end(vl);
if (m_mqtt != NULL)
if (m_mqtt != NULL && level >= m_mqttLevel && m_mqttLevel != 0U)
m_mqtt->publish("log", buffer);
if (level >= m_fileLevel && m_fileLevel != 0U) {

4
Log.h
View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015,2016,2020 by Jonathan Naylor G4KLX
* Copyright (C) 2015,2016,2020,2022 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
@@ -30,7 +30,7 @@
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, bool rotate);
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 LogFinalise();
#endif

View File

@@ -23,6 +23,7 @@ URL=www.google.co.uk
[Log]
# Logging levels, 0=No logging
MQTTLevel=1
DisplayLevel=1
FileLevel=1
FilePath=.

View File

@@ -271,9 +271,9 @@ int CMMDVMHost::run()
#endif
#if !defined(_WIN32) && !defined(_WIN64)
ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogFileRotate());
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.getLogFileRotate());
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");

View File

@@ -57,7 +57,7 @@ m_port(port)
{
CUDPSocket::startup();
::LogInitialise(false, ".", "RemoteCommand", 2U, 2U, false);
::LogInitialise(false, ".", "RemoteCommand", 2U, 2U, 2U, false);
}
CRemoteCommand::~CRemoteCommand()