summaryrefslogtreecommitdiff
path: root/src/lib/log.go
blob: a54ba9f98b6d15c2a742c54934b493935ce41837 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Rudimentary logging package. Defines a type, Logger, with simple
// methods for formatting output to one or two destinations. Also has
// predefined Loggers accessible through helper functions Stdout[f],
// Stderr[f], Exit[f], and Crash[f].
// Exit exits when written to.
// Crash causes a crash when written to.

package log

import (
	"fmt";
	"io";
	"os";
	"time";
)

// Lshortname can be or'd in to cause only the last element of the file name to be printed.
const (
	Lok = iota;
	Lexit;	// terminate execution when written
	Lcrash;	// crash (panic) when written
	Lshortname = 1 << 5;
)

type Logger struct {
	out0	io.Write;
	out1	io.Write;
	flag int;
}

func NewLogger(out0, out1 io.Write, flag int) *Logger {
	return &Logger{out0, out1, flag}
}

var (
	stdout = NewLogger(os.Stdout, nil, Lok);
	stderr = NewLogger(os.Stderr, nil, Lok);
	exit = NewLogger(os.Stderr, nil, Lexit);
	crash = NewLogger(os.Stderr, nil, Lcrash);
)

func timestamp(ns int64) string {
	t := time.SecondsToLocalTime(ns/1e9);
	// why are time fields private?
	s := t.RFC1123();
	return s[5:12] + s[17:25];	// TODO(r): placeholder. this gives "24 Jan 15:50:18"
}

var shortnames = make(map[string] string)	// cache of short names to avoid allocation.

// The calldepth is provided for generality, although at the moment on all paths it will be 2.
func (l *Logger) output(calldepth int, s string) {
	now := time.Nanoseconds();	// get this early.
	newline := "\n";
	if len(s) > 0 && s[len(s)-1] == '\n' {
		newline = ""
	}
	pc, file, line, ok := sys.Caller(calldepth);
	if ok {
		if l.flag & Lshortname == Lshortname {
			short, ok := shortnames[file];
			if !ok {
				short = file;
				for i := len(file) - 1; i > 0; i-- {
					if file[i] == '/' {
						short = file[i+1:len(file)];
						shortnames[file] = short;
						break;
					}
				}
			}
			file = short;
		}
	} else {
		file = "???";
		line = 0;
	}
	s = fmt.Sprintf("%s %s:%d: %s%s", timestamp(now), file, line, s, newline);
	io.WriteString(l.out0, s);
	if l.out1 != nil {
		io.WriteString(l.out1, s);
	}
	switch l.flag & ^Lshortname {
	case Lcrash:
		panic("log: fatal error");
	case Lexit:
		sys.Exit(1);
	}
}

// Basic methods on Logger, analogous to Printf and Print
func (l *Logger) Logf(format string, v ...) {
	l.output(2, fmt.Sprintf(format, v))
}

func (l *Logger) Log(v ...) {
	l.output(2, fmt.Sprintln(v))
}

// Helper functions for lightweight simple logging to predefined Loggers.
func Stdout(v ...) {
	stdout.output(2, fmt.Sprint(v))
}

func Stderr(v ...) {
	stdout.output(2, fmt.Sprintln(v))
}

func Stdoutf(format string, v ...) {
	stdout.output(2, fmt.Sprintf(format, v))
}

func Stderrf(format string, v ...) {
	stderr.output(2, fmt.Sprintf(format, v))
}

func Exit(v ...) {
	exit.output(2, fmt.Sprintln(v))
}

func Exitf(format string, v ...) {
	exit.output(2, fmt.Sprintf(format, v))
}

func Crash(v ...) {
	crash.output(2, fmt.Sprintln(v))
}

func Crashf(format string, v ...) {
	crash.output(2, fmt.Sprintf(format, v))
}