@@ -5,17 +5,35 @@ import (
55 "io"
66 "os"
77 "strings"
8+ "time"
89
910 "golang.org/x/crypto/ssh/terminal"
1011)
1112
13+ type Level uint8
14+
15+ const (
16+ DebugLevel Level = iota
17+ InfoLevel
18+ WarningLevel
19+ ErrorLevel
20+ )
21+
22+ var level Level
23+
24+ func SetLevel (l Level ) {
25+ level = l
26+ }
27+
1228// Out represents the writer to print the log messages to.
1329// This is used for tests.
1430var Out io.Writer = os .Stderr
1531
1632var tWidth int
1733
1834func init () {
35+ level = InfoLevel
36+
1937 var err error
2038 tWidth , _ , err = terminal .GetSize (int (os .Stdout .Fd ()))
2139 if err != nil {
@@ -26,55 +44,111 @@ func init() {
2644 }
2745}
2846
47+ func printTimestamp () {
48+ if level > DebugLevel {
49+ return
50+ }
51+ fmt .Printf ("[%s] " , time .Now ().Format (time .RFC3339 ))
52+ }
53+
2954func Info (a ... interface {}) {
55+ if level > InfoLevel {
56+ return
57+ }
58+ printTimestamp ()
3059 fmt .Fprint (Out , a ... )
3160}
3261
3362func Infoln (a ... interface {}) {
63+ if level > InfoLevel {
64+ return
65+ }
66+ printTimestamp ()
3467 fmt .Fprintln (Out , a ... )
3568}
3669
3770func Infor (format string , a ... interface {}) {
38- blank := fmt .Sprintf ("\r %s\r " , strings .Repeat (" " , tWidth ))
39- fmt .Fprint (Out , fmt .Sprintf ("%s%s" , blank , fmt .Sprintf (format , a ... )))
71+ if level > InfoLevel {
72+ return
73+ }
74+
75+ if level > DebugLevel {
76+ blank := fmt .Sprintf ("\r %s\r " , strings .Repeat (" " , tWidth ))
77+ printTimestamp ()
78+ fmt .Fprint (Out , fmt .Sprintf ("%s%s" , blank , fmt .Sprintf (format , a ... )))
79+ } else {
80+ printTimestamp ()
81+ fmt .Fprintln (Out , fmt .Sprintf (format , a ... ))
82+ }
4083}
4184
4285func Infof (format string , a ... interface {}) {
86+ if level > InfoLevel {
87+ return
88+ }
89+ printTimestamp ()
4390 fmt .Fprintf (Out , format , a ... )
4491}
4592
4693func Debug (a ... interface {}) {
94+ if level > DebugLevel {
95+ return
96+ }
97+ printTimestamp ()
4798 fmt .Fprint (Out , a ... )
4899}
49100
50101func Debugln (a ... interface {}) {
102+ if level > DebugLevel {
103+ return
104+ }
105+ printTimestamp ()
51106 fmt .Fprintln (Out , a ... )
52107}
53108
54109func Debugf (format string , a ... interface {}) {
110+ if level > DebugLevel {
111+ return
112+ }
113+ printTimestamp ()
55114 fmt .Fprintf (Out , format , a ... )
56115}
57116
58117func Warning (a ... interface {}) {
118+ if level > WarningLevel {
119+ return
120+ }
121+ printTimestamp ()
59122 fmt .Fprint (Out , a ... )
60123}
61124
62125func Warningln (a ... interface {}) {
126+ if level > WarningLevel {
127+ return
128+ }
129+ printTimestamp ()
63130 fmt .Fprintln (Out , a ... )
64131}
65132
66133func Warningf (format string , a ... interface {}) {
134+ if level > WarningLevel {
135+ return
136+ }
137+ printTimestamp ()
67138 fmt .Fprintf (Out , format , a ... )
68139}
69140
70141func Error (a ... interface {}) {
142+ printTimestamp ()
71143 fmt .Fprint (Out , a ... )
72144}
73145
74146func Errorln (a ... interface {}) {
147+ printTimestamp ()
75148 fmt .Fprintln (Out , a ... )
76149}
77150
78151func Errorf (format string , a ... interface {}) {
152+ printTimestamp ()
79153 fmt .Fprintf (Out , format , a ... )
80154}
0 commit comments