-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
43 lines (38 loc) · 862 Bytes
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package yesql
import (
"log"
)
// a stop-gap measure until structured logging is added to the std lib.
// see: https://go.googlesource.com/proposal/+/master/design/56345-structured-logging.md
func logStatement(quiet bool, stmt string, args []any) {
if quiet {
return
}
log.SetFlags(log.LstdFlags)
log.Println(inline(stmt))
}
func inline(s string) (t string) {
var ignore bool
for i, c := range s {
// Ignore characters inside sql string literals.
if string(c) == "'" {
ignore = !ignore
}
first := i == 0
if !ignore {
switch {
case !first && c == ' ' && t[len(t)-1] == ' ':
// remove duplicate space
continue
case !first && c == '\n' && t[len(t)-1] != ' ':
// convert the first newline to a space
t += string(' ')
continue
case c == '\n', c == '\t':
continue
}
}
t += string(c)
}
return t
}