OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [log/] [log.go] - Blame information for rev 775

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// Package log implements a simple logging package. It defines a type, Logger,
6
// with methods for formatting output. It also has a predefined 'standard'
7
// Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and
8
// Panic[f|ln], which are easier to use than creating a Logger manually.
9
// That logger writes to standard error and prints the date and time
10
// of each logged message.
11
// The Fatal functions call os.Exit(1) after writing the log message.
12
// The Panic functions call panic after writing the log message.
13
package log
14
 
15
import (
16
        "bytes"
17
        "fmt"
18
        "io"
19
        "os"
20
        "runtime"
21
        "sync"
22
        "time"
23
)
24
 
25
// These flags define which text to prefix to each log entry generated by the Logger.
26
const (
27
        // Bits or'ed together to control what's printed. There is no control over the
28
        // order they appear (the order listed here) or the format they present (as
29
        // described in the comments).  A colon appears after these items:
30
        //      2009/0123 01:23:23.123123 /a/b/c/d.go:23: message
31
        Ldate         = 1 << iota     // the date: 2009/0123
32
        Ltime                         // the time: 01:23:23
33
        Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
34
        Llongfile                     // full file name and line number: /a/b/c/d.go:23
35
        Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
36
        LstdFlags     = Ldate | Ltime // initial values for the standard logger
37
)
38
 
39
// A Logger represents an active logging object that generates lines of
40
// output to an io.Writer.  Each logging operation makes a single call to
41
// the Writer's Write method.  A Logger can be used simultaneously from
42
// multiple goroutines; it guarantees to serialize access to the Writer.
43
type Logger struct {
44
        mu     sync.Mutex   // ensures atomic writes; protects the following fields
45
        prefix string       // prefix to write at beginning of each line
46
        flag   int          // properties
47
        out    io.Writer    // destination for output
48
        buf    bytes.Buffer // for accumulating text to write
49
}
50
 
51
// New creates a new Logger.   The out variable sets the
52
// destination to which log data will be written.
53
// The prefix appears at the beginning of each generated log line.
54
// The flag argument defines the logging properties.
55
func New(out io.Writer, prefix string, flag int) *Logger {
56
        return &Logger{out: out, prefix: prefix, flag: flag}
57
}
58
 
59
var std = New(os.Stderr, "", LstdFlags)
60
 
61
// Cheap integer to fixed-width decimal ASCII.  Give a negative width to avoid zero-padding.
62
// Knows the buffer has capacity.
63
func itoa(buf *bytes.Buffer, i int, wid int) {
64
        var u uint = uint(i)
65
        if u == 0 && wid <= 1 {
66
                buf.WriteByte('0')
67
                return
68
        }
69
 
70
        // Assemble decimal in reverse order.
71
        var b [32]byte
72
        bp := len(b)
73
        for ; u > 0 || wid > 0; u /= 10 {
74
                bp--
75
                wid--
76
                b[bp] = byte(u%10) + '0'
77
        }
78
 
79
        // avoid slicing b to avoid an allocation.
80
        for bp < len(b) {
81
                buf.WriteByte(b[bp])
82
                bp++
83
        }
84
}
85
 
86
func (l *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line int) {
87
        buf.WriteString(l.prefix)
88
        if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 {
89
                if l.flag&Ldate != 0 {
90
                        year, month, day := t.Date()
91
                        itoa(buf, year, 4)
92
                        buf.WriteByte('/')
93
                        itoa(buf, int(month), 2)
94
                        buf.WriteByte('/')
95
                        itoa(buf, day, 2)
96
                        buf.WriteByte(' ')
97
                }
98
                if l.flag&(Ltime|Lmicroseconds) != 0 {
99
                        hour, min, sec := t.Clock()
100
                        itoa(buf, hour, 2)
101
                        buf.WriteByte(':')
102
                        itoa(buf, min, 2)
103
                        buf.WriteByte(':')
104
                        itoa(buf, sec, 2)
105
                        if l.flag&Lmicroseconds != 0 {
106
                                buf.WriteByte('.')
107
                                itoa(buf, t.Nanosecond()/1e3, 6)
108
                        }
109
                        buf.WriteByte(' ')
110
                }
111
        }
112
        if l.flag&(Lshortfile|Llongfile) != 0 {
113
                if l.flag&Lshortfile != 0 {
114
                        short := file
115
                        for i := len(file) - 1; i > 0; i-- {
116
                                if file[i] == '/' {
117
                                        short = file[i+1:]
118
                                        break
119
                                }
120
                        }
121
                        file = short
122
                }
123
                buf.WriteString(file)
124
                buf.WriteByte(':')
125
                itoa(buf, line, -1)
126
                buf.WriteString(": ")
127
        }
128
}
129
 
130
// Output writes the output for a logging event.  The string s contains
131
// the text to print after the prefix specified by the flags of the
132
// Logger.  A newline is appended if the last character of s is not
133
// already a newline.  Calldepth is used to recover the PC and is
134
// provided for generality, although at the moment on all pre-defined
135
// paths it will be 2.
136
func (l *Logger) Output(calldepth int, s string) error {
137
        now := time.Now() // get this early.
138
        var file string
139
        var line int
140
        l.mu.Lock()
141
        defer l.mu.Unlock()
142
        if l.flag&(Lshortfile|Llongfile) != 0 {
143
                // release lock while getting caller info - it's expensive.
144
                l.mu.Unlock()
145
                var ok bool
146
                _, file, line, ok = runtime.Caller(calldepth)
147
                if !ok {
148
                        file = "???"
149
                        line = 0
150
                }
151
                l.mu.Lock()
152
        }
153
        l.buf.Reset()
154
        l.formatHeader(&l.buf, now, file, line)
155
        l.buf.WriteString(s)
156
        if len(s) > 0 && s[len(s)-1] != '\n' {
157
                l.buf.WriteByte('\n')
158
        }
159
        _, err := l.out.Write(l.buf.Bytes())
160
        return err
161
}
162
 
163
// Printf calls l.Output to print to the logger.
164
// Arguments are handled in the manner of fmt.Printf.
165
func (l *Logger) Printf(format string, v ...interface{}) {
166
        l.Output(2, fmt.Sprintf(format, v...))
167
}
168
 
169
// Print calls l.Output to print to the logger.
170
// Arguments are handled in the manner of fmt.Print.
171
func (l *Logger) Print(v ...interface{}) { l.Output(2, fmt.Sprint(v...)) }
172
 
173
// Println calls l.Output to print to the logger.
174
// Arguments are handled in the manner of fmt.Println.
175
func (l *Logger) Println(v ...interface{}) { l.Output(2, fmt.Sprintln(v...)) }
176
 
177
// Fatal is equivalent to l.Print() followed by a call to os.Exit(1).
178
func (l *Logger) Fatal(v ...interface{}) {
179
        l.Output(2, fmt.Sprint(v...))
180
        os.Exit(1)
181
}
182
 
183
// Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1).
184
func (l *Logger) Fatalf(format string, v ...interface{}) {
185
        l.Output(2, fmt.Sprintf(format, v...))
186
        os.Exit(1)
187
}
188
 
189
// Fatalln is equivalent to l.Println() followed by a call to os.Exit(1).
190
func (l *Logger) Fatalln(v ...interface{}) {
191
        l.Output(2, fmt.Sprintln(v...))
192
        os.Exit(1)
193
}
194
 
195
// Panic is equivalent to l.Print() followed by a call to panic().
196
func (l *Logger) Panic(v ...interface{}) {
197
        s := fmt.Sprint(v...)
198
        l.Output(2, s)
199
        panic(s)
200
}
201
 
202
// Panicf is equivalent to l.Printf() followed by a call to panic().
203
func (l *Logger) Panicf(format string, v ...interface{}) {
204
        s := fmt.Sprintf(format, v...)
205
        l.Output(2, s)
206
        panic(s)
207
}
208
 
209
// Panicln is equivalent to l.Println() followed by a call to panic().
210
func (l *Logger) Panicln(v ...interface{}) {
211
        s := fmt.Sprintln(v...)
212
        l.Output(2, s)
213
        panic(s)
214
}
215
 
216
// Flags returns the output flags for the logger.
217
func (l *Logger) Flags() int {
218
        l.mu.Lock()
219
        defer l.mu.Unlock()
220
        return l.flag
221
}
222
 
223
// SetFlags sets the output flags for the logger.
224
func (l *Logger) SetFlags(flag int) {
225
        l.mu.Lock()
226
        defer l.mu.Unlock()
227
        l.flag = flag
228
}
229
 
230
// Prefix returns the output prefix for the logger.
231
func (l *Logger) Prefix() string {
232
        l.mu.Lock()
233
        defer l.mu.Unlock()
234
        return l.prefix
235
}
236
 
237
// SetPrefix sets the output prefix for the logger.
238
func (l *Logger) SetPrefix(prefix string) {
239
        l.mu.Lock()
240
        defer l.mu.Unlock()
241
        l.prefix = prefix
242
}
243
 
244
// SetOutput sets the output destination for the standard logger.
245
func SetOutput(w io.Writer) {
246
        std.mu.Lock()
247
        defer std.mu.Unlock()
248
        std.out = w
249
}
250
 
251
// Flags returns the output flags for the standard logger.
252
func Flags() int {
253
        return std.Flags()
254
}
255
 
256
// SetFlags sets the output flags for the standard logger.
257
func SetFlags(flag int) {
258
        std.SetFlags(flag)
259
}
260
 
261
// Prefix returns the output prefix for the standard logger.
262
func Prefix() string {
263
        return std.Prefix()
264
}
265
 
266
// SetPrefix sets the output prefix for the standard logger.
267
func SetPrefix(prefix string) {
268
        std.SetPrefix(prefix)
269
}
270
 
271
// These functions write to the standard logger.
272
 
273
// Print calls Output to print to the standard logger.
274
// Arguments are handled in the manner of fmt.Print.
275
func Print(v ...interface{}) {
276
        std.Output(2, fmt.Sprint(v...))
277
}
278
 
279
// Printf calls Output to print to the standard logger.
280
// Arguments are handled in the manner of fmt.Printf.
281
func Printf(format string, v ...interface{}) {
282
        std.Output(2, fmt.Sprintf(format, v...))
283
}
284
 
285
// Println calls Output to print to the standard logger.
286
// Arguments are handled in the manner of fmt.Println.
287
func Println(v ...interface{}) {
288
        std.Output(2, fmt.Sprintln(v...))
289
}
290
 
291
// Fatal is equivalent to Print() followed by a call to os.Exit(1).
292
func Fatal(v ...interface{}) {
293
        std.Output(2, fmt.Sprint(v...))
294
        os.Exit(1)
295
}
296
 
297
// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
298
func Fatalf(format string, v ...interface{}) {
299
        std.Output(2, fmt.Sprintf(format, v...))
300
        os.Exit(1)
301
}
302
 
303
// Fatalln is equivalent to Println() followed by a call to os.Exit(1).
304
func Fatalln(v ...interface{}) {
305
        std.Output(2, fmt.Sprintln(v...))
306
        os.Exit(1)
307
}
308
 
309
// Panic is equivalent to Print() followed by a call to panic().
310
func Panic(v ...interface{}) {
311
        s := fmt.Sprint(v...)
312
        std.Output(2, s)
313
        panic(s)
314
}
315
 
316
// Panicf is equivalent to Printf() followed by a call to panic().
317
func Panicf(format string, v ...interface{}) {
318
        s := fmt.Sprintf(format, v...)
319
        std.Output(2, s)
320
        panic(s)
321
}
322
 
323
// Panicln is equivalent to Println() followed by a call to panic().
324
func Panicln(v ...interface{}) {
325
        s := fmt.Sprintln(v...)
326
        std.Output(2, s)
327
        panic(s)
328
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.