-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudplogger.py
31 lines (25 loc) · 883 Bytes
/
udplogger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import socket
class UdpLogger:
def __init__(self, host, is_esp32=True):
self.host = "localhost", 9999
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.set_host(host)
self.is_esp32 = is_esp32
def set_host(self, host):
parts = str(host).rsplit(":", 1)
if len(parts) == 1:
self.host = parts[0], 9999
else:
self.host = parts[0], int(parts[1])
def log_msg(self, msg, *args):
try:
log_line = msg
if args:
log_line += " " + (" ".join(map(str, args)))
log_line += "\n"
if self.is_esp32:
self.socket.sendto(log_line, self.host)
else:
self.socket.sendto(log_line.encode("utf8"), self.host)
except Exception as e:
print("Could not send", e)