Skip to content

Commit

Permalink
log: support millisecond timestamp resolution
Browse files Browse the repository at this point in the history
Existing implementation had only microsecond or second resolution,
but microsecond resolution could be seen as too precise (access to
logs could be used for side channel attacks) while second resolution
is not precise enough.

This adds Lmilliseconds flag.

Fixes #60249
  • Loading branch information
mitar committed May 17, 2023
1 parent d29dd2e commit e57d2bc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
Lmsgprefix // move the "prefix" from the beginning of the line to before the message
Lmilliseconds // millisecond resolution: 01:23:23.123. assumes Ltime.
LstdFlags = Ldate | Ltime // initial values for the standard logger
)

Expand Down Expand Up @@ -115,7 +116,7 @@ func formatHeader(buf *[]byte, t time.Time, prefix string, flag int, file string
if flag&Lmsgprefix == 0 {
*buf = append(*buf, prefix...)
}
if flag&(Ldate|Ltime|Lmicroseconds) != 0 {
if flag&(Ldate|Ltime|Lmicroseconds|Lmilliseconds) != 0 {
if flag&LUTC != 0 {
t = t.UTC()
}
Expand All @@ -128,7 +129,7 @@ func formatHeader(buf *[]byte, t time.Time, prefix string, flag int, file string
itoa(buf, day, 2)
*buf = append(*buf, ' ')
}
if flag&(Ltime|Lmicroseconds) != 0 {
if flag&(Ltime|Lmicroseconds|Lmilliseconds) != 0 {
hour, min, sec := t.Clock()
itoa(buf, hour, 2)
*buf = append(*buf, ':')
Expand All @@ -138,6 +139,9 @@ func formatHeader(buf *[]byte, t time.Time, prefix string, flag int, file string
if flag&Lmicroseconds != 0 {
*buf = append(*buf, '.')
itoa(buf, t.Nanosecond()/1e3, 6)
} else if flag&Lmilliseconds != 0 {
*buf = append(*buf, '.')
itoa(buf, t.Nanosecond()/1e6, 3)
}
*buf = append(*buf, ' ')
}
Expand Down
9 changes: 8 additions & 1 deletion src/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const (
Rdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
Rtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
Rmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
Rline = `(63|65):` // must update if the calls to l.Printf / l.Print below move
Rmilliseconds = `\.[0-9][0-9][0-9]`
Rline = `(70|72):` // must update if the calls to l.Printf / l.Print below move
Rlongfile = `.*/[A-Za-z0-9_\-]+\.go:` + Rline
Rshortfile = `[A-Za-z0-9_\-]+\.go:` + Rline
)
Expand All @@ -43,6 +44,8 @@ var tests = []tester{
{Ltime | Lmsgprefix, "XXX", Rtime + " XXX"},
{Ltime | Lmicroseconds, "", Rtime + Rmicroseconds + " "},
{Lmicroseconds, "", Rtime + Rmicroseconds + " "}, // microsec implies time
{Ltime | Lmilliseconds, "", Rtime + Rmilliseconds + " "},
{Lmilliseconds, "", Rtime + Rmilliseconds + " "}, // millisec implies time
{Llongfile, "", Rlongfile + " "},
{Lshortfile, "", Rshortfile + " "},
{Llongfile | Lshortfile, "", Rshortfile + " "}, // shortfile overrides longfile
Expand All @@ -51,6 +54,10 @@ var tests = []tester{
{Ldate | Ltime | Lmicroseconds | Lshortfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " "},
{Ldate | Ltime | Lmicroseconds | Llongfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmicroseconds + " " + Rlongfile + " XXX"},
{Ldate | Ltime | Lmicroseconds | Lshortfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " XXX"},
{Ldate | Ltime | Lmilliseconds | Llongfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmilliseconds + " " + Rlongfile + " "},
{Ldate | Ltime | Lmilliseconds | Lshortfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmilliseconds + " " + Rshortfile + " "},
{Ldate | Ltime | Lmilliseconds | Llongfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmilliseconds + " " + Rlongfile + " XXX"},
{Ldate | Ltime | Lmilliseconds | Lshortfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmilliseconds + " " + Rshortfile + " XXX"},
}

// Test using Println("hello", 23, "world") or using Printf("hello %d world", 23)
Expand Down

0 comments on commit e57d2bc

Please sign in to comment.