From aa0064db4d13c4217a6818cce491d2083c3f42c8 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 13 Sep 2022 16:30:40 -0700 Subject: [PATCH] logpolicy: add NewWithConfigPath (#5625) The version.CmdName implementation is buggy such that it does not correctly identify the binary name if it embeds other go binaries. For now, add a NewWithConfigPath API that allows the caller to explicitly specify this information. Signed-off-by: Joe Tsai --- logpolicy/logpolicy.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/logpolicy/logpolicy.go b/logpolicy/logpolicy.go index 638b8c8e7..701bb4dc2 100644 --- a/logpolicy/logpolicy.go +++ b/logpolicy/logpolicy.go @@ -438,6 +438,13 @@ func tryFixLogStateLocation(dir, cmdname string) { // New returns a new log policy (a logger and its instance ID) for a // given collection name. func New(collection string) *Policy { + return NewWithConfigPath(collection, "", "") +} + +// NewWithConfigPath is identical to New, +// but uses the specified directory and command name. +// If either is empty, it derives them automatically. +func NewWithConfigPath(collection, dir, cmdName string) *Policy { var lflags int if term.IsTerminal(2) || runtime.GOOS == "windows" { lflags = 0 @@ -460,9 +467,12 @@ func New(collection string) *Policy { earlyErrBuf.WriteByte('\n') } - dir := logsDir(earlyLogf) - - cmdName := version.CmdName() + if dir == "" { + dir = logsDir(earlyLogf) + } + if cmdName == "" { + cmdName = version.CmdName() + } tryFixLogStateLocation(dir, cmdName) cfgPath := filepath.Join(dir, fmt.Sprintf("%s.log.conf", cmdName))