This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVerbose.go
104 lines (79 loc) · 2.1 KB
/
Verbose.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) 2019-present unTill Pro, Ltd. and Contributors
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/*
Simple logging for CLI utilities using functions:
- Error
- Doing
- Info
- Verbose
All functions use Output to actually output the final string
- VerboseWriters() are intended to be used with PipedExec.Run()
*/
package gochips
import (
"fmt"
"io"
"io/ioutil"
"os"
)
// Doing printlns "$obj..." to stdout
var Doing func(arg interface{})
// Info printlns to stdout
var Info func(args ...interface{})
// Error printlns to stdout
var Error func(args ...interface{})
// Verbose If IsVerbose is true printlns VerbosePrefix + subj + ": " + ...args
// args are printed in %#+v format (json-like)
var Verbose func(subj string, args ...interface{})
// IsVerbose enables Verbose
var IsVerbose = false
// Output is used by all functions
var Output func(funcName, s string)
// VerbosePrefix prefixes Verbose output
var VerbosePrefix = "--- "
// ErrorPrefix prefixes Verbose output
var ErrorPrefix = "*** "
// VerboseWriters returns (os.Stdout, os.Stderr) if IsVerbose, (ioutil.Discard, os.Stderr) otherwise
var VerboseWriters func() (out io.Writer, err io.Writer)
func init() {
Doing = implDoing
Info = implInfo
Verbose = implVerbose
Error = implError
Output = implOutput
VerboseWriters = implVerboseWriters
}
func implDoing(arg interface{}) {
Output("Doing", fmt.Sprintln(fmt.Sprintf("%v...", arg)))
}
func implInfo(args ...interface{}) {
Output("Info", fmt.Sprintln(args...))
}
func implError(args ...interface{}) {
Output("Error", ErrorPrefix+fmt.Sprintln(args...))
}
func implVerbose(subj string, args ...interface{}) {
if IsVerbose {
res := VerbosePrefix + subj + ": "
for idx, arg := range args {
if idx > 0 {
res += ", "
}
res += fmt.Sprintf("%#+v", arg)
}
Output("Verbose", fmt.Sprintln(res))
}
}
func implOutput(funcName, s string) {
fmt.Print(s)
}
func implVerboseWriters() (out io.Writer, err io.Writer) {
if IsVerbose {
return os.Stdout, os.Stderr
}
return ioutil.Discard, os.Stderr
}