Skip to content

Commit

Permalink
Update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nikodemas committed Jun 22, 2023
1 parent ed11376 commit 0685593
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions udp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func sendDataToStomp(data []byte) {

// udp server implementation
func udpServer() {
const maxFailedPacketLength = 1000 // maximum length of the failed packet to be printed
udpAddr := &net.UDPAddr{Port: Config.Port}
// if configuration provides explicitly IPAddr to bind use it here
if Config.IPAddr != "" {
Expand Down Expand Up @@ -235,8 +236,13 @@ func udpServer() {
log.Printf("unable to unmarshal UDP packet into JSON, error %v\n", err)
e := string(err.Error())
if strings.Contains(e, "invalid character") {
// nothing we can do about mailformed JSON, let's dump it
log.Println(string(data))
// truncate the malformed JSON if it exceeds the maximum length
// and dump it
failedData := string(data)
if len(failedData) > maxFailedPacketLength {
failedData = failedData[:maxFailedPacketLength] + "..."
}
log.Println(failedData)
} else if strings.Contains(e, "unexpected end of JSON input") {
// let's increse buf size to adjust to the packet size
bufSize = bufSize * 2
Expand All @@ -257,7 +263,7 @@ func udpServer() {
log.Printf("received: %s from %s\n", sdata, remote)
}

// Check if the message has a key named "type" and rename it to "read_type"
// check if the message has a key named "type" and rename it to "read_type"
if val, ok := packet["type"]; ok {
packet["read_type"] = val
delete(packet, "type")
Expand All @@ -268,11 +274,20 @@ func udpServer() {
newData, err := json.Marshal(packet)
if err != nil {
log.Printf("unable to marshal UDP packet into JSON, error %v\n", err)
// truncate the failed packet if it exceeds the maximum length
failedPacket := fmt.Sprint(packet)
if len(failedPacket) > maxFailedPacketLength {
failedPacket = failedPacket[:maxFailedPacketLength] + "..."
}
log.Println(failedPacket) // dump the truncated message to the log
// clear-up our buffer
buffer = buffer[:0]
continue
}
log.Printf("sent: %s \n", newData)
if Config.Verbose {
sNewData := strings.TrimSpace(string(newData))
log.Printf("sent to AMQ: %s\n", sNewData)
}
sendDataToStomp(newData)
}

Expand Down

0 comments on commit 0685593

Please sign in to comment.