opensnitch/daemon/log/formats/rfc5424.go
Gustavo Iñiguez Goia df8390e57f
minor performance improvements
Replaced calls Sprint/Sprintf in hot paths with internal ConcatStrings,
improving string concatenation performance by approximately ~2x-~10x.
2026-07-06 01:46:11 +02:00

69 lines
1.5 KiB
Go

package formats
import (
"fmt"
"strconv"
"time"
"github.com/evilsocket/opensnitch/daemon/core"
taskBase "github.com/evilsocket/opensnitch/daemon/tasks/base"
"github.com/evilsocket/opensnitch/daemon/ui/protocol"
)
// RFC5424 name of the output format, used in our json config
const RFC5424 = "rfc5424"
// Rfc5424 object
type Rfc5424 struct {
seq int
}
// NewRfc5424 returns a new Rfc5424 object, that transforms a message to
// RFC5424 format (sort of).
func NewRfc5424() *Rfc5424 {
return &Rfc5424{}
}
// Transform takes input arguments and formats them to RFC5424 format.
func (r *Rfc5424) Transform(args ...interface{}) (out string) {
hostname := ""
tag := ""
event := "GENERIC"
arg1 := args[0]
if len(args) > 1 {
arg2 := args[1]
arg3 := args[2]
hostname = arg2.(string)
tag = arg3.(string)
}
values := arg1.([]interface{})
for n, val := range values {
switch val.(type) {
case *protocol.Connection:
event = "CONNECTION"
out = connToSD(out, val)
case taskBase.TaskNotification:
event = "TASK_NOTIFICATION"
tsk := val.(taskBase.TaskNotification)
out = core.ConcatStrings(
out,
" TASK=\"", tsk.Name,
"\" DATA=\"", fmt.Sprint(tsk.Data), "\"",
)
default:
out = core.ConcatStrings(out, " ARG", strconv.Itoa(n), "=\"", fmt.Sprint(val), "\"")
}
}
// <%s>1 %s %s %s %s %s - [%s]\n
out = "<" + syslogLevel + ">1 " +
time.Now().Format(time.RFC3339) + " " +
hostname + " " +
tag + " " +
ourPid + " " +
event + " " +
"- [" + out[1:] + "]\n"
return
}