Skip to content

Commit 4f7ec52

Browse files
committed
feat: Log timestamp relative to the start time
1 parent 979b0e0 commit 4f7ec52

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

backend/pkg/logger/data/logger.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type Logger struct {
2929
saveFiles map[data.ValueName]*file.CSV
3030
// allowedVars contains the full names (board/valueName) to be logged
3131
allowedVars map[string]struct{}
32+
// save the starting time of the logger in Unix microseconds in order to log relative timestamps
33+
startTime int64
3234
}
3335

3436
// Record is a struct that implements the abstraction.LoggerRecord interface
@@ -47,6 +49,7 @@ func NewLogger() *Logger {
4749
running: &atomic.Bool{},
4850
fileLock: &sync.RWMutex{},
4951
allowedVars: nil, // no filter by default
52+
startTime: 0,
5053
}
5154

5255
logger.running.Store(false)
@@ -68,6 +71,8 @@ func (sublogger *Logger) Start() error {
6871
return nil
6972
}
7073

74+
sublogger.startTime = time.Now().UnixMicro() // Update the start time
75+
7176
fmt.Println("Logger started")
7277
return nil
7378
}
@@ -120,7 +125,7 @@ func (sublogger *Logger) PushRecord(record abstraction.LoggerRecord) error {
120125
}
121126

122127
err = saveFile.Write([]string{
123-
fmt.Sprint(dataRecord.Packet.Timestamp().UnixMicro()),
128+
fmt.Sprint(dataRecord.Packet.Timestamp().UnixMicro() - sublogger.startTime), // Save the timestamp relative to the start time
124129
dataRecord.From,
125130
dataRecord.To,
126131
valueRepresentation,

backend/pkg/logger/order/logger.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type Logger struct {
2121
// An atomic boolean is used in order to use CompareAndSwap in the Start and Stop methods
2222
running *atomic.Bool
2323
writer *file.CSV
24+
// save the starting time of the logger in Unix microseconds in order to log relative timestamps
25+
startTime int64
2426
}
2527

2628
type Record struct {
@@ -36,8 +38,9 @@ func (*Record) Name() abstraction.LoggerName {
3638

3739
func NewLogger() *Logger {
3840
return &Logger{
39-
running: &atomic.Bool{},
40-
writer: nil,
41+
running: &atomic.Bool{},
42+
writer: nil,
43+
startTime: 0,
4144
}
4245
}
4346

@@ -51,6 +54,9 @@ func (sublogger *Logger) Start() error {
5154
if err != nil {
5255
return err
5356
}
57+
58+
sublogger.startTime = time.Now().UnixMicro() // Update the start time
59+
5460
sublogger.writer = file.NewCSV(fileRaw)
5561

5662
fmt.Println("Logger started")
@@ -96,7 +102,7 @@ func (sublogger *Logger) PushRecord(record abstraction.LoggerRecord) error {
96102
}
97103

98104
err := sublogger.writer.Write([]string{
99-
fmt.Sprint(orderRecord.Packet.Timestamp().UnixMicro()),
105+
fmt.Sprint(orderRecord.Packet.Timestamp().UnixMicro() - sublogger.startTime),
100106
orderRecord.From,
101107
orderRecord.To,
102108
fmt.Sprint(orderRecord.Packet.Id()),

backend/pkg/logger/protection/logger.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type Logger struct {
2626
saveFiles map[abstraction.BoardId]*file.CSV
2727
// BoardNames is a map that contains the common name of each board
2828
boardNames map[abstraction.BoardId]string
29+
// save the starting time of the logger in Unix microseconds in order to log relative timestamps
30+
startTime int64
2931
}
3032

3133
// Record is a struct that implements the abstraction.LoggerRecord interface
@@ -45,6 +47,7 @@ func NewLogger(boardMap map[abstraction.BoardId]string) *Logger {
4547
fileLock: &sync.Mutex{},
4648
saveFiles: make(map[abstraction.BoardId]*file.CSV),
4749
boardNames: boardMap,
50+
startTime: 0,
4851
}
4952
}
5053

@@ -54,6 +57,8 @@ func (sublogger *Logger) Start() error {
5457
return nil
5558
}
5659

60+
sublogger.startTime = time.Now().UnixMicro() // Update the start time
61+
5762
fmt.Println("Logger started")
5863
return nil
5964
}
@@ -82,7 +87,7 @@ func (sublogger *Logger) PushRecord(record abstraction.LoggerRecord) error {
8287
}
8388

8489
err = saveFile.Write([]string{
85-
fmt.Sprint(infoRecord.Timestamp.UnixMicro()),
90+
fmt.Sprint(infoRecord.Timestamp.UnixMicro() - sublogger.startTime),
8691
infoRecord.From,
8792
infoRecord.To,
8893
fmt.Sprint(infoRecord.Packet.Id()),

0 commit comments

Comments
 (0)