"golang.org/x/sys/unix"
)
+const LogFdPrefix = "FD:"
+
+var LogFd *os.File
+
type LE struct {
K string
V interface{}
switch v := le.V.(type) {
case int, int8, uint8, int64, uint64:
val = fmt.Sprintf("%d", v)
+ case bool:
+ val = fmt.Sprintf("%v", v)
default:
val = fmt.Sprintf("%s", v)
}
}
func (ctx *Ctx) Log(rec string) {
+ if LogFd != nil {
+ LogFd.WriteString(rec)
+ return
+ }
fdLock, err := os.OpenFile(
ctx.LogPath+".lock",
os.O_CREATE|os.O_WRONLY,
fd.Close() // #nosec G104
}
-func (ctx *Ctx) LogD(who string, les LEs, msg string) {
+func (ctx *Ctx) LogD(who string, les LEs, msg func(LEs) string) {
if !ctx.Debug {
return
}
les = append(LEs{{"Debug", true}, {"Who", who}}, les...)
- if msg != "" {
- les = append(les, LE{"Msg", msg})
- }
+ les = append(les, LE{"Msg", msg(les)})
fmt.Fprint(os.Stderr, les.Rec())
}
-func (ctx *Ctx) LogI(who string, les LEs, msg string) {
+func (ctx *Ctx) LogI(who string, les LEs, msg func(LEs) string) {
les = append(LEs{{"Who", who}}, les...)
- if msg != "" {
- les = append(les, LE{"Msg", msg})
- }
+ les = append(les, LE{"Msg", msg(les)})
rec := les.Rec()
if !ctx.Quiet {
fmt.Fprintln(os.Stderr, ctx.HumanizeRec(rec))
ctx.Log(rec)
}
-func (ctx *Ctx) LogE(who string, les LEs, err error, msg string) {
+func (ctx *Ctx) LogE(who string, les LEs, err error, msg func(LEs) string) {
les = append(LEs{{"Err", err.Error()}, {"Who", who}}, les...)
- if msg != "" {
- les = append(les, LE{"Msg", msg})
- }
+ les = append(les, LE{"Msg", msg(les)})
rec := les.Rec()
if !ctx.Quiet {
fmt.Fprintln(os.Stderr, ctx.HumanizeRec(rec))