From a3196ae6ede2356aca97ef1019ac25168b94b868 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 4 Apr 2015 19:33:47 -0700 Subject: [PATCH] Add iodine and other deps --- Godeps/Godeps.json | 10 +- .../src/github.com/minio-io/iodine/.gitignore | 24 ++ .../src/github.com/minio-io/iodine/LICENSE | 202 +++++++++++ .../src/github.com/minio-io/iodine/README.md | 2 + .../src/github.com/minio-io/iodine/iodine.go | 201 +++++++++++ .../github.com/minio-io/iodine/iodine_test.go | 97 ++++++ .../minio/pkg/utils/log/definitions.go | 328 ++++++++++++++++++ .../minio/pkg/utils/split/split_test.go | 2 +- 8 files changed, 864 insertions(+), 2 deletions(-) create mode 100644 Godeps/_workspace/src/github.com/minio-io/iodine/.gitignore create mode 100644 Godeps/_workspace/src/github.com/minio-io/iodine/LICENSE create mode 100644 Godeps/_workspace/src/github.com/minio-io/iodine/README.md create mode 100644 Godeps/_workspace/src/github.com/minio-io/iodine/iodine.go create mode 100644 Godeps/_workspace/src/github.com/minio-io/iodine/iodine_test.go create mode 100644 Godeps/_workspace/src/github.com/minio-io/minio/pkg/utils/log/definitions.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index ed8adb71..791ab2e1 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -34,9 +34,17 @@ "ImportPath": "github.com/minio-io/erasure", "Rev": "3cece1a107115563682604b1430418e28f65dd80" }, + { + "ImportPath": "github.com/minio-io/iodine", + "Rev": "55cc4d4256c68fbd6f0775f1a25e37e6a2f6457e" + }, + { + "ImportPath": "github.com/minio-io/minio/pkg/utils/log", + "Rev": "183df976f3036f9ea4cb2bddce468c9d16e047bc" + }, { "ImportPath": "github.com/minio-io/minio/pkg/utils/split", - "Rev": "4c7a61a68d526105ce57328bbc9156eb00b245b6" + "Rev": "183df976f3036f9ea4cb2bddce468c9d16e047bc" }, { "ImportPath": "gopkg.in/check.v1", diff --git a/Godeps/_workspace/src/github.com/minio-io/iodine/.gitignore b/Godeps/_workspace/src/github.com/minio-io/iodine/.gitignore new file mode 100644 index 00000000..daf913b1 --- /dev/null +++ b/Godeps/_workspace/src/github.com/minio-io/iodine/.gitignore @@ -0,0 +1,24 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof diff --git a/Godeps/_workspace/src/github.com/minio-io/iodine/LICENSE b/Godeps/_workspace/src/github.com/minio-io/iodine/LICENSE new file mode 100644 index 00000000..e06d2081 --- /dev/null +++ b/Godeps/_workspace/src/github.com/minio-io/iodine/LICENSE @@ -0,0 +1,202 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/Godeps/_workspace/src/github.com/minio-io/iodine/README.md b/Godeps/_workspace/src/github.com/minio-io/iodine/README.md new file mode 100644 index 00000000..d7391ea4 --- /dev/null +++ b/Godeps/_workspace/src/github.com/minio-io/iodine/README.md @@ -0,0 +1,2 @@ +# iodine +Iodine is an error logging framework for tracing errors. diff --git a/Godeps/_workspace/src/github.com/minio-io/iodine/iodine.go b/Godeps/_workspace/src/github.com/minio-io/iodine/iodine.go new file mode 100644 index 00000000..f1eead9f --- /dev/null +++ b/Godeps/_workspace/src/github.com/minio-io/iodine/iodine.go @@ -0,0 +1,201 @@ +/* + * Iodine, (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package iodine + +import ( + "bytes" + "encoding/json" + "fmt" + "os" + "path" + "reflect" + "runtime" + "strconv" + "strings" + "sync" +) + +// WrappedError is the iodine error which contains a pointer to the original error +// and stack traces. +type Error struct { + EmbeddedError error `json:"-"` + ErrorMessage string + ErrorType string + + Stack []StackEntry +} + +// StackEntry contains the entry in the stack trace +type StackEntry struct { + Host string + File string + Line int + Data map[string]string +} + +var gopath string + +var globalState = struct { + sync.RWMutex + m map[string]string +}{m: make(map[string]string)} + +// SetGlobalState - set global state +func SetGlobalState(key, value string) { + globalState.Lock() + globalState.m[key] = value + globalState.Unlock() +} + +// ClearGlobalState - clear info in globalState struct +func ClearGlobalState() { + globalState.Lock() + for k := range globalState.m { + delete(globalState.m, k) + } + globalState.Unlock() +} + +// GetGlobalState - get map from globalState struct +func GetGlobalState() map[string]string { + result := make(map[string]string) + globalState.RLock() + for k, v := range globalState.m { + result[k] = v + } + globalState.RUnlock() + return result +} + +// GetGlobalStateKey - get value for key from globalState struct +func GetGlobalStateKey(k string) string { + result, ok := globalState.m[k] + if !ok { + return "" + } + return result +} + +// Error - instantiate an error, turning it into an iodine error. +// Adds an initial stack trace. +func New(err error, data map[string]string) error { + if err != nil { + entry := createStackEntry() + var newErr Error + + // check if error is wrapped + switch typedError := err.(type) { + case Error: + { + newErr = typedError + } + default: + { + newErr = Error{ + EmbeddedError: err, + ErrorMessage: err.Error(), + ErrorType: reflect.TypeOf(err).String(), + Stack: []StackEntry{}, + } + } + } + for k, v := range data { + entry.Data[k] = v + } + newErr.Stack = append(newErr.Stack, entry) + return newErr + } + return nil +} + +// createStackEntry - create stack entries +func createStackEntry() StackEntry { + host, _ := os.Hostname() + _, file, line, _ := runtime.Caller(2) + file = strings.TrimPrefix(file, gopath) // trim gopath from file + + data := GetGlobalState() + for k, v := range getSystemData() { + data[k] = v + } + + entry := StackEntry{ + Host: host, + File: file, + Line: line, + Data: data, + } + return entry +} + +func getSystemData() map[string]string { + host, err := os.Hostname() + if err != nil { + host = "" + } + memstats := &runtime.MemStats{} + runtime.ReadMemStats(memstats) + return map[string]string{ + "sys.host": host, + "sys.os": runtime.GOOS, + "sys.arch": runtime.GOARCH, + "sys.go": runtime.Version(), + "sys.cpus": strconv.Itoa(runtime.NumCPU()), + "sys.mem.used": strconv.FormatUint(memstats.Alloc, 10), + "sys.mem.allocated": strconv.FormatUint(memstats.TotalAlloc, 10), + "sys.mem.heap.used": strconv.FormatUint(memstats.HeapAlloc, 10), + "sys.mem.heap.allocated": strconv.FormatUint(memstats.HeapSys, 10), + } +} + +// Annotate an error with a stack entry and returns itself +//func (err *WrappedError) Annotate(info map[string]string) *WrappedError { +// entry := createStackEntry() +// for k, v := range info { +// entry.Data[k] = v +// } +// err.Stack = append(err.Stack, entry) +// return err +//} + +// EmitJSON writes JSON output for the error +func (err Error) EmitJSON() ([]byte, error) { + return json.Marshal(err) +} + +// EmitHumanReadable returns a human readable error message +func (err Error) EmitHumanReadable() string { + var errorBuffer bytes.Buffer + fmt.Fprintln(&errorBuffer, err.ErrorMessage) + for i, entry := range err.Stack { + fmt.Fprintln(&errorBuffer, "-", i, entry.Host+":"+entry.File+":"+strconv.Itoa(entry.Line), entry.Data) + } + return string(errorBuffer.Bytes()) +} + +// Emits the original error message +func (err Error) Error() string { + return err.EmitHumanReadable() +} + +func init() { + _, iodineFile, _, _ := runtime.Caller(0) + iodineFile = path.Dir(iodineFile) // trim iodine.go + iodineFile = path.Dir(iodineFile) // trim iodine + iodineFile = path.Dir(iodineFile) // trim minio-io + gopath = path.Dir(iodineFile) + "/" // trim github.com +} diff --git a/Godeps/_workspace/src/github.com/minio-io/iodine/iodine_test.go b/Godeps/_workspace/src/github.com/minio-io/iodine/iodine_test.go new file mode 100644 index 00000000..9b8ddc0c --- /dev/null +++ b/Godeps/_workspace/src/github.com/minio-io/iodine/iodine_test.go @@ -0,0 +1,97 @@ +/* + * Iodine, (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package iodine + +import ( + "encoding/json" + "errors" + "testing" +) + +func TestIodine(t *testing.T) { + iodineError := New(errors.New("Hello"), nil) + iodineError = New(iodineError, nil) + iodineError = New(iodineError, nil) + iodineError = New(iodineError, nil) + switch typedError := iodineError.(type) { + case Error: + { + if len(typedError.Stack) != 4 { + t.Fail() + } + _, err := json.MarshalIndent(typedError, "", " ") + if err != nil { + t.Fail() + } + } + default: + { + t.Fail() + } + } +} + +func TestState(t *testing.T) { + SetGlobalState("hello", "world") + result := GetGlobalStateKey("hello") + if result != "world" { + t.Error("global state not set: hello->world") + t.Fail() + } + ClearGlobalState() + if len(GetGlobalState()) != 0 { + t.Fail() + } + SetGlobalState("foo", "bar") + err := New(errors.New("a simple error"), nil) + switch typedError := err.(type) { + case Error: + { + if res, ok := typedError.Stack[0].Data["foo"]; ok { + if res != "bar" { + t.Error("global state not set: foo->bar") + } + } else { + t.Fail() + } + typedError = New(typedError, map[string]string{"foo2": "bar2"}).(Error) + if res, ok := typedError.Stack[0].Data["foo"]; ok { + if res != "bar" { + t.Error("annotate should not modify previous data entries") + } + } else { + t.Error("annotate should not remove previous data entries") + } + if res, ok := typedError.Stack[1].Data["foo"]; ok { + if res != "bar" { + t.Error("global state should set value properly in annotate") + } + } else { + t.Error("global state should set key properly in annotate") + } + if res, ok := typedError.Stack[1].Data["foo2"]; ok { + if res != "bar2" { + // typedError = Error(typedError, nil).(WrappedError) + t.Error("foo2 -> bar should be set") + } + } else { + // typedError = Error(typedError, nil).(WrappedError) + t.Error("foo2 should be set") + } + } + } +} diff --git a/Godeps/_workspace/src/github.com/minio-io/minio/pkg/utils/log/definitions.go b/Godeps/_workspace/src/github.com/minio-io/minio/pkg/utils/log/definitions.go new file mode 100644 index 00000000..48d8c631 --- /dev/null +++ b/Godeps/_workspace/src/github.com/minio-io/minio/pkg/utils/log/definitions.go @@ -0,0 +1,328 @@ +// 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. + +// Package log implements a simple logging package. It defines a type, Logger, +// with methods for formatting output. It also has a predefined 'standard' +// Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and +// Panic[f|ln], which are easier to use than creating a Logger manually. +// That logger writes to standard error and prints the date and time +// of each logged message. +// The Fatal functions call os.Exit(1) after writing the log message. +// The Panic functions call panic after writing the log message. +package log + +import ( + "fmt" + "io" + "os" + "runtime" + "sync" + "time" +) + +// These flags define which text to prefix to each log entry generated by the Logger. +// 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/01/23 01:23:23.123123 /a/b/c/d.go:23: message +const ( + Ldate = 1 << iota // the date: 2009/01/23 + Ltime + 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 + LstdFlags = Ldate | Ltime // initial values for the standard logger +) + +// A Logger represents an active logging object that generates lines of +// output to an io.Writer. Each logging operation makes a single call to +// the Writer's Write method. A Logger can be used simultaneously from +// multiple goroutines; it guarantees to serialize access to the Writer. +type Logger struct { + mu sync.Mutex // ensures atomic writes; protects the following fields + prefix string // prefix to write at beginning of each line + flag int // properties + out io.Writer // destination for output + buf []byte // for accumulating text to write +} + +// New creates a new Logger. The out variable sets the +// destination to which log data will be written. +// The prefix appears at the beginning of each generated log line. +// The flag argument defines the logging properties. +func New(out io.Writer, prefix string, flag int) *Logger { + return &Logger{out: out, prefix: prefix, flag: flag} +} + +var std = New(os.Stderr, "", LstdFlags) + +// Error is an error logger +var Error = New(os.Stderr, "", 0) + +// Trace is an error logger +var Trace = New(os.Stderr, "", 0) + +// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding. +// Knows the buffer has capacity. +func itoa(buf *[]byte, i int, wid int) { + var u = uint(i) + if u == 0 && wid <= 1 { + *buf = append(*buf, '0') + return + } + + // Assemble decimal in reverse order. + var b [32]byte + bp := len(b) + for ; u > 0 || wid > 0; u /= 10 { + bp-- + wid-- + b[bp] = byte(u%10) + '0' + } + *buf = append(*buf, b[bp:]...) +} + +func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) { + *buf = append(*buf, l.prefix...) + if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { + if l.flag&Ldate != 0 { + year, month, day := t.Date() + itoa(buf, year, 4) + *buf = append(*buf, '/') + itoa(buf, int(month), 2) + *buf = append(*buf, '/') + itoa(buf, day, 2) + *buf = append(*buf, ' ') + } + if l.flag&(Ltime|Lmicroseconds) != 0 { + hour, min, sec := t.Clock() + itoa(buf, hour, 2) + *buf = append(*buf, ':') + itoa(buf, min, 2) + *buf = append(*buf, ':') + itoa(buf, sec, 2) + if l.flag&Lmicroseconds != 0 { + *buf = append(*buf, '.') + itoa(buf, t.Nanosecond()/1e3, 6) + } + *buf = append(*buf, ' ') + } + } + if l.flag&(Lshortfile|Llongfile) != 0 { + if l.flag&Lshortfile != 0 { + short := file + for i := len(file) - 1; i > 0; i-- { + if file[i] == '/' { + short = file[i+1:] + break + } + } + file = short + } + *buf = append(*buf, file...) + *buf = append(*buf, ':') + itoa(buf, line, -1) + *buf = append(*buf, ": "...) + } +} + +// Output writes the output for a logging event. The string s contains +// the text to print after the prefix specified by the flags of the +// Logger. A newline is appended if the last character of s is not +// already a newline. Calldepth is used to recover the PC and 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) error { + now := time.Now() // get this early. + var file string + var line int + l.mu.Lock() + defer l.mu.Unlock() + if l.flag&(Lshortfile|Llongfile) != 0 { + // release lock while getting caller info - it's expensive. + l.mu.Unlock() + var ok bool + _, file, line, ok = runtime.Caller(calldepth) + if !ok { + file = "???" + line = 0 + } + l.mu.Lock() + } + l.buf = l.buf[:0] + l.formatHeader(&l.buf, now, file, line) + l.buf = append(l.buf, s...) + if len(s) > 0 && s[len(s)-1] != '\n' { + l.buf = append(l.buf, '\n') + } + _, err := l.out.Write(l.buf) + return err +} + +// Printf calls l.Output to print to the logger. +// Arguments are handled in the manner of fmt.Printf. +func (l *Logger) Printf(format string, v ...interface{}) { + l.Output(2, fmt.Sprintf(format, v...)) +} + +// Print calls l.Output to print to the logger. +// Arguments are handled in the manner of fmt.Print. +func (l *Logger) Print(v ...interface{}) { l.Output(2, fmt.Sprint(v...)) } + +// Println calls l.Output to print to the logger. +// Arguments are handled in the manner of fmt.Println. +func (l *Logger) Println(v ...interface{}) { l.Output(2, fmt.Sprintln(v...)) } + +// Fatal is equivalent to l.Print() followed by a call to os.Exit(1). +func (l *Logger) Fatal(v ...interface{}) { + l.Output(2, fmt.Sprint(v...)) + os.Exit(1) +} + +// Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1). +func (l *Logger) Fatalf(format string, v ...interface{}) { + l.Output(2, fmt.Sprintf(format, v...)) + os.Exit(1) +} + +// Fatalln is equivalent to l.Println() followed by a call to os.Exit(1). +func (l *Logger) Fatalln(v ...interface{}) { + l.Output(2, fmt.Sprintln(v...)) + os.Exit(1) +} + +// Panic is equivalent to l.Print() followed by a call to panic(). +func (l *Logger) Panic(v ...interface{}) { + s := fmt.Sprint(v...) + l.Output(2, s) + panic(s) +} + +// Panicf is equivalent to l.Printf() followed by a call to panic(). +func (l *Logger) Panicf(format string, v ...interface{}) { + s := fmt.Sprintf(format, v...) + l.Output(2, s) + panic(s) +} + +// Panicln is equivalent to l.Println() followed by a call to panic(). +func (l *Logger) Panicln(v ...interface{}) { + s := fmt.Sprintln(v...) + l.Output(2, s) + panic(s) +} + +// Flags returns the output flags for the logger. +func (l *Logger) Flags() int { + l.mu.Lock() + defer l.mu.Unlock() + return l.flag +} + +// SetFlags sets the output flags for the logger. +func (l *Logger) SetFlags(flag int) { + l.mu.Lock() + defer l.mu.Unlock() + l.flag = flag +} + +// Prefix returns the output prefix for the logger. +func (l *Logger) Prefix() string { + l.mu.Lock() + defer l.mu.Unlock() + return l.prefix +} + +// SetPrefix sets the output prefix for the logger. +func (l *Logger) SetPrefix(prefix string) { + l.mu.Lock() + defer l.mu.Unlock() + l.prefix = prefix +} + +// SetOutput sets the output destination for the standard logger. +func SetOutput(w io.Writer) { + std.mu.Lock() + defer std.mu.Unlock() + std.out = w +} + +// Flags returns the output flags for the standard logger. +func Flags() int { + return std.Flags() +} + +// SetFlags sets the output flags for the standard logger. +func SetFlags(flag int) { + std.SetFlags(flag) +} + +// Prefix returns the output prefix for the standard logger. +func Prefix() string { + return std.Prefix() +} + +// SetPrefix sets the output prefix for the standard logger. +func SetPrefix(prefix string) { + std.SetPrefix(prefix) +} + +// These functions write to the standard logger. + +// Print calls Output to print to the standard logger. +// Arguments are handled in the manner of fmt.Print. +func Print(v ...interface{}) { + std.Output(2, fmt.Sprint(v...)) +} + +// Printf calls Output to print to the standard logger. +// Arguments are handled in the manner of fmt.Printf. +func Printf(format string, v ...interface{}) { + std.Output(2, fmt.Sprintf(format, v...)) +} + +// Println calls Output to print to the standard logger. +// Arguments are handled in the manner of fmt.Println. +func Println(v ...interface{}) { + std.Output(2, fmt.Sprintln(v...)) +} + +// Fatal is equivalent to Print() followed by a call to os.Exit(1). +func Fatal(v ...interface{}) { + std.Output(2, fmt.Sprint(v...)) + os.Exit(1) +} + +// Fatalf is equivalent to Printf() followed by a call to os.Exit(1). +func Fatalf(format string, v ...interface{}) { + std.Output(2, fmt.Sprintf(format, v...)) + os.Exit(1) +} + +// Fatalln is equivalent to Println() followed by a call to os.Exit(1). +func Fatalln(v ...interface{}) { + std.Output(2, fmt.Sprintln(v...)) + os.Exit(1) +} + +// Panic is equivalent to Print() followed by a call to panic(). +func Panic(v ...interface{}) { + s := fmt.Sprint(v...) + std.Output(2, s) + panic(s) +} + +// Panicf is equivalent to Printf() followed by a call to panic(). +func Panicf(format string, v ...interface{}) { + s := fmt.Sprintf(format, v...) + std.Output(2, s) + panic(s) +} + +// Panicln is equivalent to Println() followed by a call to panic(). +func Panicln(v ...interface{}) { + s := fmt.Sprintln(v...) + std.Output(2, s) + panic(s) +} diff --git a/Godeps/_workspace/src/github.com/minio-io/minio/pkg/utils/split/split_test.go b/Godeps/_workspace/src/github.com/minio-io/minio/pkg/utils/split/split_test.go index 6100f9c5..8b679cfd 100644 --- a/Godeps/_workspace/src/github.com/minio-io/minio/pkg/utils/split/split_test.go +++ b/Godeps/_workspace/src/github.com/minio-io/minio/pkg/utils/split/split_test.go @@ -24,8 +24,8 @@ import ( "strconv" "testing" + . "github.com/minio-io/check" "github.com/minio-io/minio/pkg/utils/split" - . "gopkg.in/check.v1" ) type MySuite struct{}