Skip to content

Commit 79abac7

Browse files
committed
Fixed missing includes
1 parent 846592d commit 79abac7

8 files changed

Lines changed: 176 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ idf_component_register(
1212
"components/ESPOTAUpdate/ESPOTAUpdate.cpp"
1313
"components/FileManager/FileManager.cpp"
1414
"components/LogManager/LogManager.cpp"
15+
"components/LogManager/LoggerPapertrail.cpp"
1516
"components/MQTTManager/MQTTManager.cpp"
1617
"components/NetworkManager/NetworkManager.cpp"
1718
"components/ProtocolExchange/ProtocolExchange.cpp"
@@ -35,4 +36,8 @@ idf_component_register(
3536
"components/WebServer"
3637
REQUIRES
3738
raftcore-src
39+
bt
40+
spiffs
41+
fatfs
42+
app_update
3843
)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Papertrail logger
4+
//
5+
// Rob Dobson 2021
6+
//
7+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
8+
9+
#include "LoggerPapertrail.h"
10+
#include <ConfigBase.h>
11+
#include <Logger.h>
12+
#include <NetworkSystem.h>
13+
#include <ESPUtils.h>
14+
#include <RaftUtils.h>
15+
16+
// Log prefix
17+
static const char *MODULE_PREFIX = "LogPapertrail";
18+
19+
LoggerPapertrail::LoggerPapertrail(const ConfigBase& logDestConfig)
20+
: LoggerBase(logDestConfig)
21+
{
22+
// Get config
23+
_host = logDestConfig.getString("host", "");
24+
memset(&_hostAddrInfo, 0, sizeof(struct addrinfo));
25+
_dnsLookupDone = false;
26+
_port = logDestConfig.getLong("port", 0);
27+
_sysName = logDestConfig.getString("sysName", "");
28+
_sysName += "_" + getSystemMACAddressStr(ESP_MAC_WIFI_STA, "");
29+
}
30+
31+
LoggerPapertrail::~LoggerPapertrail()
32+
{
33+
if (_socketFd >= 0)
34+
{
35+
close(_socketFd);
36+
}
37+
}
38+
39+
void IRAM_ATTR LoggerPapertrail::log(esp_log_level_t level, const char *tag, const char* msg)
40+
{
41+
// Check level
42+
if (level > _level)
43+
return;
44+
45+
// Check if we're connected
46+
if (!networkSystem.isIPConnected())
47+
return;
48+
49+
// Check if DNS lookup done
50+
if (!_dnsLookupDone)
51+
{
52+
// Do DNS lookup
53+
struct addrinfo hints;
54+
memset(&hints,0,sizeof(hints));
55+
hints.ai_family=AF_INET;
56+
hints.ai_socktype=SOCK_DGRAM;
57+
hints.ai_flags=0;
58+
struct addrinfo *addrResult;
59+
if (getaddrinfo(_host.c_str(), _port.c_str(), &hints, &addrResult) != 0)
60+
{
61+
ESP_LOGE(MODULE_PREFIX, "log failed to resolve host %s", _host.c_str());
62+
return;
63+
}
64+
ESP_LOGI(MODULE_PREFIX, "log resolved host %s to %d.%d.%d.%d", _host.c_str(),
65+
addrResult->ai_addr->sa_data[0], addrResult->ai_addr->sa_data[1],
66+
addrResult->ai_addr->sa_data[2], addrResult->ai_addr->sa_data[3]);
67+
_hostAddrInfo = *addrResult;
68+
_dnsLookupDone = true;
69+
70+
// Create UDP socket
71+
ESP_LOGI(MODULE_PREFIX, "log create udp socket");
72+
_socketFd = socket(_hostAddrInfo.ai_family, _hostAddrInfo.ai_socktype, _hostAddrInfo.ai_protocol);
73+
if (_socketFd < 0)
74+
{
75+
ESP_LOGE(MODULE_PREFIX, "log create udp socket failed: %d errno: %d", _socketFd, errno);
76+
}
77+
else
78+
{
79+
// Debug
80+
ESP_LOGI(MODULE_PREFIX, "log hostIP %s port %s level %s sysName %s socketFd %d",
81+
_host.c_str(), _port.c_str(), getLevelStr(), _sysName.c_str(), _socketFd);
82+
}
83+
84+
}
85+
86+
// Start of log window?
87+
if (Raft::isTimeout(millis(), _logWindowStartMs, LOG_WINDOW_SIZE_MS))
88+
{
89+
_logWindowStartMs = millis();
90+
_logWindowCount = 1;
91+
}
92+
else
93+
{
94+
// Count log messages
95+
_logWindowCount++;
96+
if (_logWindowCount >= LOG_WINDOW_MAX_COUNT)
97+
{
98+
// Discard
99+
return;
100+
}
101+
}
102+
103+
// Format message
104+
String logMsg = "<22>" + _sysName + ": " + msg;
105+
106+
// Send to papertrail using UDP socket
107+
int ret = sendto(_socketFd, logMsg.c_str(), logMsg.length(), 0, _hostAddrInfo.ai_addr, _hostAddrInfo.ai_addrlen);
108+
if (ret < 0)
109+
{
110+
ESP_LOGE(MODULE_PREFIX, "log failed: %d errno %d", ret, errno);
111+
return;
112+
}
113+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// Papertrail logger
4+
//
5+
// Rob Dobson 2021
6+
//
7+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
8+
9+
#pragma once
10+
11+
#include <LoggerBase.h>
12+
#include <ArduinoOrAlt.h>
13+
#include <sys/types.h>
14+
#include <sys/socket.h>
15+
#include <netdb.h>
16+
17+
class ConfigBase;
18+
19+
class LoggerPapertrail : public LoggerBase
20+
{
21+
public:
22+
LoggerPapertrail(const ConfigBase& logDestConfig);
23+
virtual ~LoggerPapertrail();
24+
virtual void IRAM_ATTR log(esp_log_level_t level, const char *tag, const char* msg) override final;
25+
26+
private:
27+
String _host;
28+
String _port;
29+
String _sysName;
30+
struct addrinfo _hostAddrInfo;
31+
bool _dnsLookupDone = false;
32+
int _socketFd = -1;
33+
34+
// Avoid swamping the network
35+
uint32_t _logWindowStartMs = 0;
36+
uint32_t _logWindowCount = 0;
37+
static const uint32_t LOG_WINDOW_SIZE_MS = 60000;
38+
static const uint32_t LOG_WINDOW_MAX_COUNT = 60;
39+
uint32_t _logWindowThrottleStartMs = 0;
40+
static const uint32_t LOG_WINDOW_THROTTLE_BACKOFF_MS = 30000;
41+
};

components/MQTTManager/MQTTManager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//
88
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
99

10+
#ifdef FEATURE_MQTT_MANAGER
11+
1012
#include "MQTTManager.h"
1113
#include <Logger.h>
1214
#include <RaftUtils.h>
@@ -160,3 +162,5 @@ bool MQTTManager::readyToSend(uint32_t channelID)
160162
{
161163
return true;
162164
}
165+
166+
#endif

components/MQTTManager/MQTTManager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#pragma once
1111

12+
#ifdef FEATURE_MQTT_MANAGER
13+
1214
#include <SysModBase.h>
1315
#include <RdMQTTClient.h>
1416

@@ -51,3 +53,5 @@ class MQTTManager : public SysModBase
5153
bool sendMQTTMsg(const String& topicName, CommsChannelMsg& msg);
5254
bool readyToSend(uint32_t channelID);
5355
};
56+
57+
#endif

components/WebServer/WebServer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88

9+
#ifdef FEATURE_WEB_SERVER_OR_WEB_SOCKETS
10+
911
#include "WebServer.h"
1012
#include "WebServerResource.h"
1113
#include <Logger.h>
@@ -302,4 +304,5 @@ bool WebServer::restAPIMatchEndpoint(const char* url, RdWebServerMethod method,
302304
return true;
303305
}
304306
return false;
305-
}
307+
}
308+
#endif

components/WebServer/WebServer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#pragma once
1010

11+
#ifdef FEATURE_WEB_SERVER_OR_WEB_SOCKETS
12+
1113
#include <ConfigBase.h>
1214
#include <RestAPIEndpointManager.h>
1315
#include <SysModBase.h>
@@ -94,3 +96,5 @@ class WebServer : public SysModBase
9496
}
9597
}
9698
};
99+
100+
#endif

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "RaftSysMods",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "System modules for the Raft ESP32 framework",
55
"keywords": "esp32, esp32s3, framework",
66
"repository":

0 commit comments

Comments
 (0)