Skip to content

Commit 6bf6349

Browse files
authored
Merge pull request #383 from HyperloopUPV-H8/backend/logger
feat(backend, logger): selector of logger precision
2 parents 4f7ec52 + d8b2ffc commit 6bf6349

File tree

10 files changed

+65
-14
lines changed

10 files changed

+65
-14
lines changed

backend/cmd/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ type TCP struct {
4646
KeepAlive int `toml:"keep_alive_ms"`
4747
}
4848

49+
type Logging struct {
50+
TimeUnit string `toml:"time_unit"`
51+
}
52+
4953
type Config struct {
5054
App App
5155
Vehicle vehicle.Config
@@ -56,4 +60,5 @@ type Config struct {
5660
TFTP TFTP
5761
TCP TCP
5862
Blcu Blcu
63+
Logging Logging
5964
}

backend/cmd/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ timeout_ms = 5000 # Timeout between retries in milliseconds
5252
backoff_factor = 2 # Backoff multiplier for retry delays
5353
enable_progress = true # Enable progress callbacks during transfers
5454

55+
# Logger Configuration
56+
time_unit = "us" # Time unit for log timestamps (ns, us, ms, s) Default: ns
57+
5558
# <-- DO NOT TOUCH BELOW THIS LINE -->
5659

5760
# Server Configuration

backend/cmd/dev-config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ file = "backend.log" # Log file path (empty to disable file logging)
8585
max_size_mb = 100 # Maximum log file size in MB
8686
max_backups = 3 # Number of backup files to keep
8787
max_age_days = 7 # Maximum age of log files in days
88+
time_unit = "us" # Time unit for log timestamps (ns, us, ms, s)

backend/cmd/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ import (
4848
state_logger "github.com/HyperloopUPV-H8/h9-backend/pkg/logger/state"
4949
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport"
5050
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/sniffer"
51-
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/udp"
5251
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/tcp"
52+
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/udp"
5353
blcu_packet "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/blcu"
5454
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/data"
5555
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/order"
@@ -92,7 +92,7 @@ var currentVersion string
9292
func main() {
9393
// Parse command line flags
9494
flag.Parse()
95-
95+
9696
// Handle version flag
9797
if *versionFlag {
9898
versionFile := "VERSION.txt"
@@ -104,7 +104,7 @@ func main() {
104104
}
105105
os.Exit(0)
106106
}
107-
107+
108108
// update() // FIXME: Updater disabled due to cross-platform and reliability issues
109109

110110
traceFile := initTrace(*traceLevel, *traceFile)
@@ -184,6 +184,7 @@ func main() {
184184
state_logger.Name: state_logger.NewLogger(),
185185
}
186186

187+
logger.SetFormatTimestamp(logger.TimeUnit(config.Logging.TimeUnit)) // MUST be before creating subloggers
187188
loggerHandler := logger.NewLogger(subloggers, trace.Logger)
188189

189190
// <--- order transfer --->

backend/pkg/logger/data/logger.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (sublogger *Logger) Start() error {
7171
return nil
7272
}
7373

74-
sublogger.startTime = time.Now().UnixMicro() // Update the start time
74+
sublogger.startTime = loggerHandler.FormatTimestamp(time.Now()) // Update the start time
7575

7676
fmt.Println("Logger started")
7777
return nil
@@ -125,7 +125,7 @@ func (sublogger *Logger) PushRecord(record abstraction.LoggerRecord) error {
125125
}
126126

127127
err = saveFile.Write([]string{
128-
fmt.Sprint(dataRecord.Packet.Timestamp().UnixMicro() - sublogger.startTime), // Save the timestamp relative to the start time
128+
fmt.Sprint(loggerHandler.FormatTimestamp(dataRecord.Packet.Timestamp()) - sublogger.startTime), // Save the timestamp relative to the start time
129129
dataRecord.From,
130130
dataRecord.To,
131131
valueRepresentation,
@@ -144,10 +144,12 @@ func (sublogger *Logger) PushRecord(record abstraction.LoggerRecord) error {
144144
return writeErr
145145
}
146146

147+
// Checks if the file for the given valueName exists, and creates it if it doesn't
147148
func (sublogger *Logger) getFile(valueName data.ValueName, board string) (*file.CSV, error) {
148149
sublogger.fileLock.Lock()
149150
defer sublogger.fileLock.Unlock()
150151

152+
// Check if the file already exists
151153
valueFile, ok := sublogger.saveFiles[valueName]
152154
if ok {
153155
return valueFile, nil
@@ -159,6 +161,7 @@ func (sublogger *Logger) getFile(valueName data.ValueName, board string) (*file.
159161
return sublogger.saveFiles[valueName], err
160162
}
161163

164+
// createFile creates the file for the given valueName and board, creating all necessary directories
162165
func (sublogger *Logger) createFile(valueName data.ValueName, board string) (*os.File, error) {
163166
filename := path.Join(
164167
"logger",

backend/pkg/logger/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
const (
1313
Name = "loggerHandler"
1414
HandlerName = "logger"
15-
TimestampFormat = "02-Jan-2006_15-04-05.000"
15+
TimestampFormat = "02-Jan-2006_15-04-05.000"
1616
)
1717

1818
// Logger is a struct that implements the abstraction.Logger interface

backend/pkg/logger/logger_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func TestLogger(t *testing.T) {
7777
t.Error(err)
7878
}
7979

80-
if output[0] != fmt.Sprint(dataPacketTime.UnixMicro()) || output[1] != "test" || output[2] != "test" || output[3] != "true" {
81-
t.Errorf("dataErr: expected [%v test test true], got %v", timestamp.UnixMicro(), output)
80+
if output[0] != fmt.Sprint(logger.FormatTimestamp(dataPacketTime)) || output[1] != "test" || output[2] != "test" || output[3] != "true" {
81+
t.Errorf("dataErr: expected [%v test test true], got %v", logger.FormatTimestamp(timestamp), output)
8282
}
8383

8484
// Order
@@ -111,7 +111,7 @@ func TestLogger(t *testing.T) {
111111
t.Error(err)
112112
}
113113

114-
if output[0] != fmt.Sprint(orderPacketTime.UnixMicro()) || output[1] != "test" || output[2] != "test" {
114+
if output[0] != fmt.Sprint(logger.FormatTimestamp(orderPacketTime)) || output[1] != "test" || output[2] != "test" {
115115
t.Errorf("orderErr: expected [test test], got %v", output)
116116
}
117117

@@ -163,7 +163,7 @@ func TestLogger(t *testing.T) {
163163
t.Error(err)
164164
}
165165

166-
if output[0] != fmt.Sprint(timestamp.UnixMicro()) || output[1] != "test" || output[2] != "test" || output[3] != "0" || output[4] != "7" || output[5] != "3" || output[6] != "test" || output[7] != "&{0 0}" || output[8] != protectionPacketTime.Format(time.RFC3339) {
166+
if output[0] != fmt.Sprint(logger.FormatTimestamp(timestamp)) || output[1] != "test" || output[2] != "test" || output[3] != "0" || output[4] != "7" || output[5] != "3" || output[6] != "test" || output[7] != "&{0 0}" || output[8] != protectionPacketTime.Format(time.RFC3339) {
167167
t.Errorf("orderErr: expected [test test 0], got %v", output)
168168
}
169169

backend/pkg/logger/order/logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (sublogger *Logger) Start() error {
5555
return err
5656
}
5757

58-
sublogger.startTime = time.Now().UnixMicro() // Update the start time
58+
sublogger.startTime = logger.FormatTimestamp(time.Now()) // Update the start time
5959

6060
sublogger.writer = file.NewCSV(fileRaw)
6161

@@ -102,7 +102,7 @@ func (sublogger *Logger) PushRecord(record abstraction.LoggerRecord) error {
102102
}
103103

104104
err := sublogger.writer.Write([]string{
105-
fmt.Sprint(orderRecord.Packet.Timestamp().UnixMicro() - sublogger.startTime),
105+
fmt.Sprint(logger.FormatTimestamp(orderRecord.Packet.Timestamp()) - sublogger.startTime),
106106
orderRecord.From,
107107
orderRecord.To,
108108
fmt.Sprint(orderRecord.Packet.Id()),

backend/pkg/logger/protection/logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (sublogger *Logger) Start() error {
5757
return nil
5858
}
5959

60-
sublogger.startTime = time.Now().UnixMicro() // Update the start time
60+
sublogger.startTime = logger.FormatTimestamp(time.Now()) // Update the start time
6161

6262
fmt.Println("Logger started")
6363
return nil
@@ -87,7 +87,7 @@ func (sublogger *Logger) PushRecord(record abstraction.LoggerRecord) error {
8787
}
8888

8989
err = saveFile.Write([]string{
90-
fmt.Sprint(infoRecord.Timestamp.UnixMicro() - sublogger.startTime),
90+
fmt.Sprint(logger.FormatTimestamp(infoRecord.Timestamp) - sublogger.startTime),
9191
infoRecord.From,
9292
infoRecord.To,
9393
fmt.Sprint(infoRecord.Packet.Id()),

backend/pkg/logger/time.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package logger
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
type TimeUnit string
9+
10+
const (
11+
Nanoseconds TimeUnit = "ns"
12+
Microseconds TimeUnit = "us"
13+
Milliseconds TimeUnit = "ms"
14+
Seconds TimeUnit = "s"
15+
)
16+
17+
// Function used by all the subloggers to format timestamps according to the selected unit, by default is microseconds
18+
var FormatTimestamp = func(t time.Time) int64 { return t.UnixNano() }
19+
20+
// SetTimestampUnit sets the global timestamp unit for all loggers applying functions as objects of first class
21+
22+
func SetFormatTimestamp(unit TimeUnit) {
23+
24+
switch unit {
25+
case Nanoseconds:
26+
FormatTimestamp = func(t time.Time) int64 { return t.UnixNano() }
27+
case Microseconds:
28+
FormatTimestamp = func(t time.Time) int64 { return t.UnixMicro() }
29+
case Milliseconds:
30+
FormatTimestamp = func(t time.Time) int64 { return t.UnixMilli() }
31+
case Seconds:
32+
FormatTimestamp = func(t time.Time) int64 { return t.Unix() }
33+
default:
34+
fmt.Printf("Unknown time unit: %s.\n", unit)
35+
}
36+
}
37+
38+
// SetTimestampUnit sets the global timestamp unit for all loggers

0 commit comments

Comments
 (0)