summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/dcat/main.go10
-rw-r--r--cmd/dgrep/main.go9
-rw-r--r--cmd/dmap/main.go10
-rw-r--r--cmd/drun/main.go10
-rw-r--r--cmd/dserver/main.go6
-rw-r--r--cmd/dtail/main.go12
-rw-r--r--internal/clients/handlers/basehandler.go10
-rw-r--r--internal/io/logger/logger.go152
8 files changed, 69 insertions, 150 deletions
diff --git a/cmd/dcat/main.go b/cmd/dcat/main.go
index af46db1..bd29f72 100644
--- a/cmd/dcat/main.go
+++ b/cmd/dcat/main.go
@@ -23,7 +23,7 @@ func main() {
var files string
var noColor bool
var serversStr string
- var silentEnable bool
+ var quietEnable bool
var sshPort int
var trustAllHosts bool
@@ -32,7 +32,7 @@ func main() {
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayVersion, "version", false, "Display version")
flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
- flag.BoolVar(&silentEnable, "silent", false, "Reduce output")
+ flag.BoolVar(&quietEnable, "quiet", false, "Reduce output")
flag.BoolVar(&trustAllHosts, "trustAllHosts", false, "Auto trust all unknown host keys")
flag.IntVar(&connectionsPerCPU, "cpc", 10, "How many connections established per CPU core concurrently")
flag.IntVar(&sshPort, "port", 2222, "SSH server port")
@@ -51,10 +51,8 @@ func main() {
version.PrintAndExit()
}
- ctx := context.Background()
- serverEnable := false
-
- logger.Start(ctx, serverEnable, debugEnable, silentEnable, silentEnable)
+ ctx := context.TODO()
+ logger.Start(ctx, logger.Modes{Debug: debugEnable, Quiet: quietEnable})
args := clients.Args{
ConnectionsPerCPU: connectionsPerCPU,
diff --git a/cmd/dgrep/main.go b/cmd/dgrep/main.go
index 9393cf6..6ba90fc 100644
--- a/cmd/dgrep/main.go
+++ b/cmd/dgrep/main.go
@@ -24,7 +24,7 @@ func main() {
var noColor bool
var regex string
var serversStr string
- var silentEnable bool
+ var quietEnable bool
var sshPort int
var trustAllHosts bool
@@ -33,7 +33,7 @@ func main() {
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayVersion, "version", false, "Display version")
flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
- flag.BoolVar(&silentEnable, "silent", false, "Reduce output")
+ flag.BoolVar(&quietEnable, "quiet", false, "Reduce output")
flag.BoolVar(&trustAllHosts, "trustAllHosts", false, "Auto trust all unknown host keys")
flag.IntVar(&connectionsPerCPU, "cpc", 10, "How many connections established per CPU core concurrently")
flag.IntVar(&sshPort, "port", 2222, "SSH server port")
@@ -53,9 +53,8 @@ func main() {
version.PrintAndExit()
}
- ctx := context.Background()
- serverEnable := false
- logger.Start(ctx, serverEnable, debugEnable, silentEnable, silentEnable)
+ ctx := context.TODO()
+ logger.Start(ctx, logger.Modes{Debug: debugEnable, Quiet: quietEnable})
args := clients.Args{
ConnectionsPerCPU: connectionsPerCPU,
diff --git a/cmd/dmap/main.go b/cmd/dmap/main.go
index dea4706..24f81b8 100644
--- a/cmd/dmap/main.go
+++ b/cmd/dmap/main.go
@@ -25,7 +25,7 @@ func main() {
var noColor bool
var queryStr string
var serversStr string
- var silentEnable bool
+ var quietEnable bool
var sshPort int
var timeout int
var trustAllHosts bool
@@ -35,7 +35,7 @@ func main() {
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayVersion, "version", false, "Display version")
flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
- flag.BoolVar(&silentEnable, "silent", false, "Reduce output")
+ flag.BoolVar(&quietEnable, "quiet", false, "Reduce output")
flag.BoolVar(&trustAllHosts, "trustAllHosts", false, "Auto trust all unknown host keys")
flag.IntVar(&connectionsPerCPU, "cpc", 10, "How many connections established per CPU core concurrently")
flag.IntVar(&sshPort, "port", 2222, "SSH server port")
@@ -56,10 +56,8 @@ func main() {
version.PrintAndExit()
}
- ctx := context.Background()
- serverEnable := false
-
- logger.Start(ctx, serverEnable, debugEnable, silentEnable, silentEnable)
+ ctx := context.TODO()
+ logger.Start(ctx, logger.Modes{Debug: debugEnable, Quiet: quietEnable})
args := clients.Args{
ConnectionsPerCPU: connectionsPerCPU,
diff --git a/cmd/drun/main.go b/cmd/drun/main.go
index 3cc19e7..3817d0e 100644
--- a/cmd/drun/main.go
+++ b/cmd/drun/main.go
@@ -27,7 +27,7 @@ func main() {
var jobName string
var noColor bool
var serversStr string
- var silentEnable bool
+ var quietEnable bool
var sshPort int
var timeout int
var trustAllHosts bool
@@ -37,7 +37,7 @@ func main() {
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayVersion, "version", false, "Display version")
flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
- flag.BoolVar(&silentEnable, "silent", false, "Reduce output")
+ flag.BoolVar(&quietEnable, "quiet", false, "Reduce output")
flag.BoolVar(&trustAllHosts, "trustAllHosts", false, "Auto trust all unknown host keys")
flag.IntVar(&connectionsPerCPU, "cpc", 10, "How many connections established per CPU core concurrently")
flag.IntVar(&sshPort, "port", 2222, "SSH server port")
@@ -59,14 +59,10 @@ func main() {
version.PrintAndExit()
}
- // TODO: Change other commands to use TODO contexts too.
ctx := context.TODO()
- serverEnable := false
-
- logger.Start(ctx, serverEnable, debugEnable, silentEnable, silentEnable)
+ logger.Start(ctx, logger.Modes{Debug: debugEnable, Quiet: quietEnable})
command, commandArgs := readCommand(command)
-
args := clients.Args{
ConnectionsPerCPU: connectionsPerCPU,
ServersStr: serversStr,
diff --git a/cmd/dserver/main.go b/cmd/dserver/main.go
index f9207d5..865bf95 100644
--- a/cmd/dserver/main.go
+++ b/cmd/dserver/main.go
@@ -51,7 +51,6 @@ func main() {
ctx, cancel := context.WithCancel(context.Background())
if shutdownAfter > 0 {
- logger.Info("Enabling auto shutdown timer", shutdownAfter)
ctx, cancel = context.WithTimeout(ctx, time.Duration(shutdownAfter)*time.Second)
}
@@ -66,10 +65,7 @@ func main() {
}
}()
- serverEnable := true
- silentEnable := false
- nothingEnable := false
- logger.Start(ctx, serverEnable, debugEnable, silentEnable, nothingEnable)
+ logger.Start(ctx, logger.Modes{Server: true, Debug: debugEnable})
if pprof > -1 {
// For debugging purposes only
diff --git a/cmd/dtail/main.go b/cmd/dtail/main.go
index f062cdc..bb3cf8c 100644
--- a/cmd/dtail/main.go
+++ b/cmd/dtail/main.go
@@ -32,7 +32,7 @@ func main() {
var queryStr string
var regex string
var serversStr string
- var silentEnable bool
+ var quietEnable bool
var sshPort int
var timeout int
var trustAllHosts bool
@@ -43,7 +43,7 @@ func main() {
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayVersion, "version", false, "Display version")
flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
- flag.BoolVar(&silentEnable, "silent", false, "Reduce output")
+ flag.BoolVar(&quietEnable, "quiet", false, "Reduce output")
flag.BoolVar(&trustAllHosts, "trustAllHosts", false, "Auto trust all unknown host keys")
flag.IntVar(&connectionsPerCPU, "cpc", 10, "How many connections established per CPU core concurrently")
flag.IntVar(&sshPort, "port", 2222, "SSH server port")
@@ -66,18 +66,14 @@ func main() {
version.PrintAndExit()
}
- ctx := context.Background()
+ ctx := context.TODO()
if checkHealth {
healthClient, _ := clients.NewHealthClient(omode.HealthClient)
os.Exit(healthClient.Start(ctx))
}
- serverEnable := false
- if checkHealth {
- silentEnable = true
- }
- logger.Start(ctx, serverEnable, debugEnable, silentEnable, silentEnable)
+ logger.Start(ctx, logger.Modes{Debug: debugEnable, Quiet: quietEnable})
if pprof > -1 {
// For debugging purposes only
diff --git a/internal/clients/handlers/basehandler.go b/internal/clients/handlers/basehandler.go
index 7b071c9..65bbfd7 100644
--- a/internal/clients/handlers/basehandler.go
+++ b/internal/clients/handlers/basehandler.go
@@ -84,16 +84,6 @@ func (h *baseHandler) handleMessageType(message string) {
return
}
- // Silent mode will only print out remote logs but not remote server
- // commands. But remote server commands will be still logged to ./log/.
- if logger.Mode == logger.SilentMode {
- if h.receiveBuf[0] == 'R' {
- logger.Raw(message)
- }
- h.receiveBuf = h.receiveBuf[:0]
- return
- }
-
logger.Raw(message)
h.receiveBuf = h.receiveBuf[:0]
}
diff --git a/internal/io/logger/logger.go b/internal/io/logger/logger.go
index e30b907..dc30c32 100644
--- a/internal/io/logger/logger.go
+++ b/internal/io/logger/logger.go
@@ -27,16 +27,22 @@ const (
traceStr string = "TRACE"
)
+// The configured logging mode(s)
+var mode Modes
+
+// Strategy is the current log strattegy used.
+var strategy Strategy
+
// Synchronise access to logging.
var mutex sync.Mutex
-// File descriptor of log file when logToFile enabled.
+// File descriptor of log file when mode.logToFile enabled.
var fd *os.File
-// File write buffer of log file when logToFile enabled.
+// File write buffer of log file when mode.logToFile enabled.
var writer *bufio.Writer
-// File write buffer of stdout when logToStdout enabled.
+// File write buffer of stdout when mode.logToStdout enabled.
var stdoutWriter *bufio.Writer
// Current hostname.
@@ -45,9 +51,6 @@ var hostname string
// Used to detect change of day (create one log file per day0
var lastDateStr string
-// True if log in server mode, false if log in client mode.
-var serverEnable bool
-
// Used to make logging non-blocking.
var fileLogBufCh chan buf
var stdoutBufCh chan string
@@ -59,40 +62,6 @@ var resumeCh chan struct{}
// Tell the logger about logrotation
var rotateCh chan os.Signal
-// LogMode allows to specify the verbosity of logging.
-type LogMode int
-
-// Possible log modes.
-const (
- NormalMode LogMode = iota
- DebugMode LogMode = iota
- SilentMode LogMode = iota
- TraceMode LogMode = iota
- NothingMode LogMode = iota
-)
-
-// Mode is the current log mode in use.
-var Mode LogMode
-
-// LogStrategy allows to specify a log rotation strategy.
-type LogStrategy int
-
-// Possible log strategies.
-const (
- NormalStrategy LogStrategy = iota
- DailyStrategy LogStrategy = iota
- StdoutStrategy LogStrategy = iota
-)
-
-// Strategy is the current log strattegy used.
-var Strategy LogStrategy
-
-// Enables logging to stdout.
-var logToStdout bool
-
-// Enables logging to file.
-var logToFile bool
-
// Helper type to make logging non-blocking.
type buf struct {
time time.Time
@@ -100,30 +69,30 @@ type buf struct {
}
// Start logging.
-func Start(ctx context.Context, myServerEnable, debugEnable, silentEnable, nothingEnable bool) {
- serverEnable = myServerEnable
-
- mode := logMode(debugEnable, silentEnable, nothingEnable)
- strategy := logStrategy()
-
- stdoutWriter = bufio.NewWriter(os.Stdout)
- Mode = mode
- Strategy = strategy
+func Start(ctx context.Context, myMode Modes) {
+ mode = myMode
- if Mode == NothingMode {
+ if mode.Nothing {
return
}
- switch Strategy {
+ if mode.Trace {
+ mode.Debug = true
+ }
+
+ strategy := logStrategy()
+ stdoutWriter = bufio.NewWriter(os.Stdout)
+
+ switch strategy {
case DailyStrategy:
_, err := os.Stat(config.Common.LogDir)
- logToFile = !os.IsNotExist(err)
- logToStdout = !serverEnable || Mode == DebugMode || Mode == TraceMode
+ mode.logToFile = !os.IsNotExist(err)
+ mode.logToStdout = !mode.Server || mode.Debug || mode.Trace
case StdoutStrategy:
fallthrough
default:
- logToFile = !serverEnable
- logToStdout = true
+ mode.logToFile = !mode.Server
+ mode.logToStdout = true
}
fqdn, err := os.Hostname()
@@ -140,46 +109,20 @@ func Start(ctx context.Context, myServerEnable, debugEnable, silentEnable, nothi
rotateCh = make(chan os.Signal, 1)
signal.Notify(rotateCh, syscall.SIGHUP)
- if logToStdout {
+ if mode.logToStdout {
stdoutBufCh = make(chan string, runtime.NumCPU()*100)
go writeToStdout(ctx)
}
- if logToFile {
+ if mode.logToFile {
fileLogBufCh = make(chan buf, runtime.NumCPU()*100)
go writeToFile(ctx)
}
}
-func logMode(debugEnable, silentEnable, nothingEnable bool) LogMode {
- switch {
- case debugEnable:
- return DebugMode
- case nothingEnable:
- return NothingMode
- case config.Common.TraceEnable:
- return TraceMode
- case config.Common.DebugEnable:
- return DebugMode
- case silentEnable:
- return SilentMode
- default:
- }
- return NormalMode
-}
-
-func logStrategy() LogStrategy {
- switch config.Common.LogStrategy {
- case "daily":
- return DailyStrategy
- default:
- }
- return StdoutStrategy
-}
-
// Info message logging.
func Info(args ...interface{}) string {
- if serverEnable {
+ if mode.Server {
return log(serverStr, infoStr, args)
}
@@ -188,7 +131,7 @@ func Info(args ...interface{}) string {
// Warn message logging.
func Warn(args ...interface{}) string {
- if serverEnable {
+ if mode.Server {
return log(serverStr, warnStr, args)
}
@@ -197,7 +140,7 @@ func Warn(args ...interface{}) string {
// Error message logging.
func Error(args ...interface{}) string {
- if serverEnable {
+ if mode.Server {
return log(serverStr, errorStr, args)
}
@@ -207,7 +150,7 @@ func Error(args ...interface{}) string {
// FatalExit logs an error and exists the process.
func FatalExit(args ...interface{}) {
what := clientStr
- if serverEnable {
+ if mode.Server {
what = serverStr
}
log(what, fatalStr, args)
@@ -222,8 +165,8 @@ func FatalExit(args ...interface{}) {
// Debug message logging.
func Debug(args ...interface{}) string {
- if Mode == DebugMode || Mode == TraceMode {
- if serverEnable {
+ if mode.Debug {
+ if mode.Server {
return log(serverStr, debugStr, args)
}
return log(clientStr, debugStr, args)
@@ -234,8 +177,8 @@ func Debug(args ...interface{}) string {
// Trace message logging.
func Trace(args ...interface{}) string {
- if Mode == TraceMode {
- if serverEnable {
+ if mode.Trace {
+ if mode.Server {
return log(serverStr, traceStr, args)
}
return log(clientStr, traceStr, args)
@@ -246,7 +189,7 @@ func Trace(args ...interface{}) string {
// Write log line to buffer and/or log file.
func write(what, severity, message string) {
- if logToStdout && (Mode != SilentMode || severity != warnStr) {
+ if mode.logToStdout {
line := fmt.Sprintf("%s|%s|%s|%s\n", what, hostname, severity, message)
if color.Colored {
@@ -256,7 +199,7 @@ func write(what, severity, message string) {
stdoutBufCh <- line
}
- if logToFile {
+ if mode.logToFile {
t := time.Now()
timeStr := t.Format("20060102-150405")
fileLogBufCh <- buf{
@@ -268,11 +211,14 @@ func write(what, severity, message string) {
// Generig log message.
func log(what string, severity string, args []interface{}) string {
- if Mode == NothingMode {
+ if mode.Nothing {
+ return ""
+ }
+ if mode.Quiet && severity != errorStr && severity != fatalStr {
return ""
}
- var messages []string
+ messages := []string{severity}
for _, arg := range args {
switch v := arg.(type) {
@@ -290,20 +236,20 @@ func log(what string, severity string, args []interface{}) string {
message := strings.Join(messages, "|")
write(what, severity, message)
- return fmt.Sprintf("%s|%s", severity, message)
+ return message
}
// Raw message logging.
func Raw(message string) {
- if Mode == NothingMode {
+ if mode.Nothing {
return
}
- if logToFile {
+ if mode.logToFile {
fileLogBufCh <- buf{time.Now(), message}
}
- if logToStdout {
+ if mode.logToStdout {
if color.Colored {
message = color.Colorfy(message)
}
@@ -426,20 +372,20 @@ func writeToFile(ctx context.Context) {
// Pause logging.
func Pause() {
- if logToStdout {
+ if mode.logToStdout {
pauseCh <- struct{}{}
}
- if logToFile {
+ if mode.logToFile {
pauseCh <- struct{}{}
}
}
// Resume logging (after pausing).
func Resume() {
- if logToStdout {
+ if mode.logToStdout {
resumeCh <- struct{}{}
}
- if logToFile {
+ if mode.logToFile {
resumeCh <- struct{}{}
}
}