OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Rudimentary logging package. Defines a type, Logger, with simple | 5 // Rudimentary logging package. Defines a type, Logger, with simple |
6 // methods for formatting output to one or two destinations. Also has | 6 // methods for formatting output to one or two destinations. Also has |
7 // predefined Loggers accessible through helper functions Stdout[f], | 7 // predefined Loggers accessible through helper functions Stdout[f], |
8 // Stderr[f], Exit[f], and Crash[f], which are easier to use than creating | 8 // Stderr[f], Exit[f], and Crash[f], which are easier to use than creating |
9 // a Logger manually. | 9 // a Logger manually. |
10 // Exit exits when written to. | 10 // Exit exits when written to. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 | 71 |
72 // Assemble decimal in reverse order. | 72 // Assemble decimal in reverse order. |
73 var b [32]byte; | 73 var b [32]byte; |
74 bp := len(b); | 74 bp := len(b); |
75 for ; u > 0 || wid > 0; u /= 10 { | 75 for ; u > 0 || wid > 0; u /= 10 { |
76 bp--; | 76 bp--; |
77 wid--; | 77 wid--; |
78 b[bp] = byte(u%10) + '0'; | 78 b[bp] = byte(u%10) + '0'; |
79 } | 79 } |
80 | 80 |
81 » return string(b[bp:len(b)]); | 81 » return string(b[bp:]); |
82 } | 82 } |
83 | 83 |
84 func (l *Logger) formatHeader(ns int64, calldepth int) string { | 84 func (l *Logger) formatHeader(ns int64, calldepth int) string { |
85 h := l.prefix; | 85 h := l.prefix; |
86 if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { | 86 if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { |
87 t := time.SecondsToLocalTime(ns / 1e9); | 87 t := time.SecondsToLocalTime(ns / 1e9); |
88 if l.flag&(Ldate) != 0 { | 88 if l.flag&(Ldate) != 0 { |
89 h += itoa(int(t.Year), 4) + "/" + itoa(t.Month, 2) + "/"
+ itoa(t.Day, 2) + " " | 89 h += itoa(int(t.Year), 4) + "/" + itoa(t.Month, 2) + "/"
+ itoa(t.Day, 2) + " " |
90 } | 90 } |
91 if l.flag&(Ltime|Lmicroseconds) != 0 { | 91 if l.flag&(Ltime|Lmicroseconds) != 0 { |
92 h += itoa(t.Hour, 2) + ":" + itoa(t.Minute, 2) + ":" + i
toa(t.Second, 2); | 92 h += itoa(t.Hour, 2) + ":" + itoa(t.Minute, 2) + ":" + i
toa(t.Second, 2); |
93 if l.flag&Lmicroseconds != 0 { | 93 if l.flag&Lmicroseconds != 0 { |
94 h += "." + itoa(int(ns%1e9)/1e3, 6) | 94 h += "." + itoa(int(ns%1e9)/1e3, 6) |
95 } | 95 } |
96 h += " "; | 96 h += " "; |
97 } | 97 } |
98 } | 98 } |
99 if l.flag&(Lshortfile|Llongfile) != 0 { | 99 if l.flag&(Lshortfile|Llongfile) != 0 { |
100 _, file, line, ok := runtime.Caller(calldepth); | 100 _, file, line, ok := runtime.Caller(calldepth); |
101 if ok { | 101 if ok { |
102 if l.flag&Lshortfile != 0 { | 102 if l.flag&Lshortfile != 0 { |
103 short, ok := shortnames[file]; | 103 short, ok := shortnames[file]; |
104 if !ok { | 104 if !ok { |
105 short = file; | 105 short = file; |
106 for i := len(file) - 1; i > 0; i-- { | 106 for i := len(file) - 1; i > 0; i-- { |
107 if file[i] == '/' { | 107 if file[i] == '/' { |
108 » » » » » » » short = file[i+1 : len(f
ile)]; | 108 » » » » » » » short = file[i+1:]; |
109 break; | 109 break; |
110 } | 110 } |
111 } | 111 } |
112 shortnames[file] = short; | 112 shortnames[file] = short; |
113 } | 113 } |
114 file = short; | 114 file = short; |
115 } | 115 } |
116 } else { | 116 } else { |
117 file = "???"; | 117 file = "???"; |
118 line = 0; | 118 line = 0; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 func Exit(v ...) { exit.Output(2, fmt.Sprintln(v)) } | 166 func Exit(v ...) { exit.Output(2, fmt.Sprintln(v)) } |
167 | 167 |
168 // Exitf is equivalent to Stderrf() followed by a call to os.Exit(1). | 168 // Exitf is equivalent to Stderrf() followed by a call to os.Exit(1). |
169 func Exitf(format string, v ...) { exit.Output(2, fmt.Sprintf(format, v))
} | 169 func Exitf(format string, v ...) { exit.Output(2, fmt.Sprintf(format, v))
} |
170 | 170 |
171 // Crash is equivalent to Stderr() followed by a call to panic(). | 171 // Crash is equivalent to Stderr() followed by a call to panic(). |
172 func Crash(v ...) { crash.Output(2, fmt.Sprintln(v)) } | 172 func Crash(v ...) { crash.Output(2, fmt.Sprintln(v)) } |
173 | 173 |
174 // Crashf is equivalent to Stderrf() followed by a call to panic(). | 174 // Crashf is equivalent to Stderrf() followed by a call to panic(). |
175 func Crashf(format string, v ...) { crash.Output(2, fmt.Sprintf(format, v)
) } | 175 func Crashf(format string, v ...) { crash.Output(2, fmt.Sprintf(format, v)
) } |
OLD | NEW |