diff options
author | Robert Griesemer <gri@golang.org> | 2009-12-15 15:35:38 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2009-12-15 15:35:38 -0800 |
commit | 798ec5bb89391257119365b771f948e95c6fe844 (patch) | |
tree | 05f75a90e239d33be427da4f9c5596d2fcb3dc96 /src/pkg/log | |
parent | 952aa58846b255d68ecebfbead2b025032055e27 (diff) | |
download | go-798ec5bb89391257119365b771f948e95c6fe844.tar.gz |
1) Change default gofmt default settings for
parsing and printing to new syntax.
Use -oldparser to parse the old syntax,
use -oldprinter to print the old syntax.
2) Change default gofmt formatting settings
to use tabs for indentation only and to use
spaces for alignment. This will make the code
alignment insensitive to an editor's tabwidth.
Use -spaces=false to use tabs for alignment.
3) Manually changed src/exp/parser/parser_test.go
so that it doesn't try to parse the parser's
source files using the old syntax (they have
new syntax now).
4) gofmt -w src misc test/bench
3rd set of files.
R=rsc
CC=golang-dev
http://codereview.appspot.com/180048
Diffstat (limited to 'src/pkg/log')
-rw-r--r-- | src/pkg/log/log.go | 122 | ||||
-rw-r--r-- | src/pkg/log/log_test.go | 52 |
2 files changed, 87 insertions, 87 deletions
diff --git a/src/pkg/log/log.go b/src/pkg/log/log.go index 2beb99c3d..ac996a38d 100644 --- a/src/pkg/log/log.go +++ b/src/pkg/log/log.go @@ -12,37 +12,37 @@ package log import ( - "fmt"; - "io"; - "runtime"; - "os"; - "time"; + "fmt" + "io" + "runtime" + "os" + "time" ) // These flags define the properties of the Logger and the output they produce. const ( // Flags - Lok = iota; - Lexit; // terminate execution when written - Lcrash; // crash (panic) when written + Lok = iota + Lexit // terminate execution when written + Lcrash // crash (panic) when written // Bits or'ed together to control what's printed. There is no control over the // order they appear (the order listed here) or the format they present (as // described in the comments). A colon appears after these items: // 2009/0123 01:23:23.123123 /a/b/c/d.go:23: message - Ldate = 1 << iota; // the date: 2009/0123 - Ltime; // the time: 01:23:23 - Lmicroseconds; // microsecond resolution: 01:23:23.123123. assumes Ltime. - Llongfile; // full file name and line number: /a/b/c/d.go:23 - Lshortfile; // final file name element and line number: d.go:23. overrides Llongfile - lAllBits = Ldate | Ltime | Lmicroseconds | Llongfile | Lshortfile; + Ldate = 1 << iota // the date: 2009/0123 + Ltime // the time: 01:23:23 + Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime. + Llongfile // full file name and line number: /a/b/c/d.go:23 + Lshortfile // final file name element and line number: d.go:23. overrides Llongfile + lAllBits = Ldate | Ltime | Lmicroseconds | Llongfile | Lshortfile ) // Logger represents an active logging object. type Logger struct { - out0 io.Writer; // first destination for output - out1 io.Writer; // second destination for output; may be nil - prefix string; // prefix to write at beginning of each line - flag int; // properties + out0 io.Writer // first destination for output + out1 io.Writer // second destination for output; may be nil + prefix string // prefix to write at beginning of each line + flag int // properties } // New creates a new Logger. The out0 and out1 variables set the @@ -54,87 +54,87 @@ func New(out0, out1 io.Writer, prefix string, flag int) *Logger { } var ( - stdout = New(os.Stdout, nil, "", Lok|Ldate|Ltime); - stderr = New(os.Stderr, nil, "", Lok|Ldate|Ltime); - exit = New(os.Stderr, nil, "", Lexit|Ldate|Ltime); - crash = New(os.Stderr, nil, "", Lcrash|Ldate|Ltime); + stdout = New(os.Stdout, nil, "", Lok|Ldate|Ltime) + stderr = New(os.Stderr, nil, "", Lok|Ldate|Ltime) + exit = New(os.Stderr, nil, "", Lexit|Ldate|Ltime) + crash = New(os.Stderr, nil, "", Lcrash|Ldate|Ltime) ) -var shortnames = make(map[string]string) // cache of short names to avoid allocation. +var shortnames = make(map[string]string) // cache of short names to avoid allocation. // Cheap integer to fixed-width decimal ASCII. Use a negative width to avoid zero-padding func itoa(i int, wid int) string { - var u uint = uint(i); + var u uint = uint(i) if u == 0 && wid <= 1 { return "0" } // Assemble decimal in reverse order. - var b [32]byte; - bp := len(b); + var b [32]byte + bp := len(b) for ; u > 0 || wid > 0; u /= 10 { - bp--; - wid--; - b[bp] = byte(u%10) + '0'; + bp-- + wid-- + b[bp] = byte(u%10) + '0' } - return string(b[bp:]); + return string(b[bp:]) } func (l *Logger) formatHeader(ns int64, calldepth int) string { - h := l.prefix; + h := l.prefix if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { - t := time.SecondsToLocalTime(ns / 1e9); + t := time.SecondsToLocalTime(ns / 1e9) if l.flag&(Ldate) != 0 { h += itoa(int(t.Year), 4) + "/" + itoa(t.Month, 2) + "/" + itoa(t.Day, 2) + " " } if l.flag&(Ltime|Lmicroseconds) != 0 { - h += itoa(t.Hour, 2) + ":" + itoa(t.Minute, 2) + ":" + itoa(t.Second, 2); + h += itoa(t.Hour, 2) + ":" + itoa(t.Minute, 2) + ":" + itoa(t.Second, 2) if l.flag&Lmicroseconds != 0 { h += "." + itoa(int(ns%1e9)/1e3, 6) } - h += " "; + h += " " } } if l.flag&(Lshortfile|Llongfile) != 0 { - _, file, line, ok := runtime.Caller(calldepth); + _, file, line, ok := runtime.Caller(calldepth) if ok { if l.flag&Lshortfile != 0 { - short, ok := shortnames[file]; + short, ok := shortnames[file] if !ok { - short = file; + short = file for i := len(file) - 1; i > 0; i-- { if file[i] == '/' { - short = file[i+1:]; - break; + short = file[i+1:] + break } } - shortnames[file] = short; + shortnames[file] = short } - file = short; + file = short } } else { - file = "???"; - line = 0; + file = "???" + line = 0 } - h += file + ":" + itoa(line, -1) + ": "; + h += file + ":" + itoa(line, -1) + ": " } - return h; + return h } // Output writes the output for a logging event. The string s contains the text to print after // the time stamp; calldepth is used to recover the PC. It is provided for generality, although // at the moment on all pre-defined paths it will be 2. func (l *Logger) Output(calldepth int, s string) os.Error { - now := time.Nanoseconds(); // get this early. - newline := "\n"; + now := time.Nanoseconds() // get this early. + newline := "\n" if len(s) > 0 && s[len(s)-1] == '\n' { newline = "" } - s = l.formatHeader(now, calldepth+1) + s + newline; - _, err := io.WriteString(l.out0, s); + s = l.formatHeader(now, calldepth+1) + s + newline + _, err := io.WriteString(l.out0, s) if l.out1 != nil { - _, err1 := io.WriteString(l.out1, s); + _, err1 := io.WriteString(l.out1, s) if err == nil && err1 != nil { err = err1 } @@ -145,35 +145,35 @@ func (l *Logger) Output(calldepth int, s string) os.Error { case Lexit: os.Exit(1) } - return err; + return err } // Logf is analogous to Printf() for a Logger. -func (l *Logger) Logf(format string, v ...) { l.Output(2, fmt.Sprintf(format, v)) } +func (l *Logger) Logf(format string, v ...) { l.Output(2, fmt.Sprintf(format, v)) } // Log is analogous to Print() for a Logger. -func (l *Logger) Log(v ...) { l.Output(2, fmt.Sprintln(v)) } +func (l *Logger) Log(v ...) { l.Output(2, fmt.Sprintln(v)) } // Stdout is a helper function for easy logging to stdout. It is analogous to Print(). -func Stdout(v ...) { stdout.Output(2, fmt.Sprint(v)) } +func Stdout(v ...) { stdout.Output(2, fmt.Sprint(v)) } // Stderr is a helper function for easy logging to stderr. It is analogous to Fprint(os.Stderr). -func Stderr(v ...) { stderr.Output(2, fmt.Sprintln(v)) } +func Stderr(v ...) { stderr.Output(2, fmt.Sprintln(v)) } // Stdoutf is a helper functions for easy formatted logging to stdout. It is analogous to Printf(). -func Stdoutf(format string, v ...) { stdout.Output(2, fmt.Sprintf(format, v)) } +func Stdoutf(format string, v ...) { stdout.Output(2, fmt.Sprintf(format, v)) } // Stderrf is a helper function for easy formatted logging to stderr. It is analogous to Fprintf(os.Stderr). -func Stderrf(format string, v ...) { stderr.Output(2, fmt.Sprintf(format, v)) } +func Stderrf(format string, v ...) { stderr.Output(2, fmt.Sprintf(format, v)) } // Exit is equivalent to Stderr() followed by a call to os.Exit(1). -func Exit(v ...) { exit.Output(2, fmt.Sprintln(v)) } +func Exit(v ...) { exit.Output(2, fmt.Sprintln(v)) } // Exitf is equivalent to Stderrf() followed by a call to os.Exit(1). -func Exitf(format string, v ...) { exit.Output(2, fmt.Sprintf(format, v)) } +func Exitf(format string, v ...) { exit.Output(2, fmt.Sprintf(format, v)) } // Crash is equivalent to Stderr() followed by a call to panic(). -func Crash(v ...) { crash.Output(2, fmt.Sprintln(v)) } +func Crash(v ...) { crash.Output(2, fmt.Sprintln(v)) } // Crashf is equivalent to Stderrf() followed by a call to panic(). -func Crashf(format string, v ...) { crash.Output(2, fmt.Sprintf(format, v)) } +func Crashf(format string, v ...) { crash.Output(2, fmt.Sprintf(format, v)) } diff --git a/src/pkg/log/log_test.go b/src/pkg/log/log_test.go index caef8b134..fd0b36c6e 100644 --- a/src/pkg/log/log_test.go +++ b/src/pkg/log/log_test.go @@ -7,25 +7,25 @@ package log // These tests are too simple. import ( - "bufio"; - "os"; - "regexp"; - "testing"; + "bufio" + "os" + "regexp" + "testing" ) const ( - Rdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`; - Rtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`; - Rmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]`; - Rline = `[0-9]+:`; - Rlongfile = `.*/[A-Za-z0-9_\-]+\.go:` + Rline; - Rshortfile = `[A-Za-z0-9_\-]+\.go:` + Rline; + Rdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]` + Rtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]` + Rmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]` + Rline = `[0-9]+:` + Rlongfile = `.*/[A-Za-z0-9_\-]+\.go:` + Rline + Rshortfile = `[A-Za-z0-9_\-]+\.go:` + Rline ) type tester struct { - flag int; - prefix string; - pattern string; // regexp that log output must match; we add ^ and expected_text$ always + flag int + prefix string + pattern string // regexp that log output must match; we add ^ and expected_text$ always } var tests = []tester{ @@ -35,10 +35,10 @@ var tests = []tester{ tester{Lok | Ldate, "", Rdate + " "}, tester{Lok | Ltime, "", Rtime + " "}, tester{Lok | Ltime | Lmicroseconds, "", Rtime + Rmicroseconds + " "}, - tester{Lok | Lmicroseconds, "", Rtime + Rmicroseconds + " "}, // microsec implies time + tester{Lok | Lmicroseconds, "", Rtime + Rmicroseconds + " "}, // microsec implies time tester{Lok | Llongfile, "", Rlongfile + " "}, tester{Lok | Lshortfile, "", Rshortfile + " "}, - tester{Lok | Llongfile | Lshortfile, "", Rshortfile + " "}, // shortfile overrides longfile + tester{Lok | Llongfile | Lshortfile, "", Rshortfile + " "}, // shortfile overrides longfile // everything at once: tester{Lok | Ldate | Ltime | Lmicroseconds | Llongfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rlongfile + " "}, tester{Lok | Ldate | Ltime | Lmicroseconds | Lshortfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " "}, @@ -46,26 +46,26 @@ var tests = []tester{ // Test using Log("hello", 23, "world") or using Logf("hello %d world", 23) func testLog(t *testing.T, flag int, prefix string, pattern string, useLogf bool) { - r, w, err1 := os.Pipe(); + r, w, err1 := os.Pipe() if err1 != nil { t.Fatal("pipe", err1) } - defer r.Close(); - defer w.Close(); - buf := bufio.NewReader(r); - l := New(w, nil, prefix, flag); + defer r.Close() + defer w.Close() + buf := bufio.NewReader(r) + l := New(w, nil, prefix, flag) if useLogf { l.Logf("hello %d world", 23) } else { l.Log("hello", 23, "world") } - line, err3 := buf.ReadString('\n'); + line, err3 := buf.ReadString('\n') if err3 != nil { t.Fatal("log error", err3) } - line = line[0 : len(line)-1]; - pattern = "^" + pattern + "hello 23 world$"; - matched, err4 := regexp.MatchString(pattern, line); + line = line[0 : len(line)-1] + pattern = "^" + pattern + "hello 23 world$" + matched, err4 := regexp.MatchString(pattern, line) if err4 != nil { t.Fatal("pattern did not compile:", err4) } @@ -76,7 +76,7 @@ func testLog(t *testing.T, flag int, prefix string, pattern string, useLogf bool func TestAllLog(t *testing.T) { for _, testcase := range tests { - testLog(t, testcase.flag, testcase.prefix, testcase.pattern, false); - testLog(t, testcase.flag, testcase.prefix, testcase.pattern, true); + testLog(t, testcase.flag, testcase.prefix, testcase.pattern, false) + testLog(t, testcase.flag, testcase.prefix, testcase.pattern, true) } } |