| // Go support for leveled logs, analogous to https://code.google.com/p/google-glog/ |
| // |
| // Copyright 2013 The Go Authors. All rights reserved. |
| // http://github.com/golang/glog |
| // |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are |
| // met: |
| // |
| // * Redistributions of source code must retain the above copyright |
| // notice, this list of conditions and the following disclaimer. |
| // * Redistributions in binary form must reproduce the above |
| // copyright notice, this list of conditions and the following disclaimer |
| // in the documentation and/or other materials provided with the |
| // distribution. |
| // * Neither the name of Google Inc. nor the names of its |
| // contributors may be used to endorse or promote products derived from |
| // this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| // File I/O for logs. |
| |
| package glog |
| |
| import ( |
| "errors" |
| "flag" |
| "fmt" |
| "os" |
| "os/user" |
| "path" |
| "strings" |
| "sync" |
| "time" |
| ) |
| |
| // MaxSize is the maximum size of a log file in bytes. |
| var MaxSize uint64 = 1024 * 1024 * 1800 |
| |
| // logDirs lists the candidate directories for new log files. |
| var logDirs []string |
| |
| // If non-empty, overrides the choice of directory in which to write logs. |
| // See createLogDirs for the full list of possible destinations. |
| var log_dir = flag.String("log_dir", "", "If non-empty, write log files in this directory") |
| |
| func createLogDirs() { |
| candidates := []string{ |
| *log_dir, |
| |
| // Explicitly-supplied temp dirs |
| os.Getenv("TMPDIR"), |
| os.Getenv("TMP"), |
| |
| // If all else fails |
| "/tmp", |
| } |
| for _, dir := range candidates { |
| if len(dir) == 0 { |
| continue |
| } |
| if dir[0] != '/' { |
| dir = "/" + dir |
| } |
| logDirs = append(logDirs, dir) |
| } |
| } |
| |
| var ( |
| pid = os.Getpid() |
| program = path.Base(os.Args[0]) |
| host = "unknownhost" |
| userName = "unknownuser" |
| ) |
| |
| func init() { |
| h, err := os.Hostname() |
| if err == nil { |
| host = shortHostname(h) |
| } |
| |
| current, err := user.Current() |
| if err == nil { |
| userName = current.Username |
| } |
| } |
| |
| // shortHostname returns its argument, truncating at the first period. |
| // For instance, given "www.google.com" it returns "www". |
| func shortHostname(hostname string) string { |
| if i := strings.Index(hostname, "."); i >= 0 { |
| return hostname[:i] |
| } |
| return hostname |
| } |
| |
| // logName returns a new log file name containing tag, with start time t, and |
| // the name for the symlink for tag. |
| func logName(tag string, t time.Time) (name, link string) { |
| name = fmt.Sprintf("%s.%s.%s.log.%s.%04d%02d%02d-%02d%02d%02d.%d", |
| program, |
| host, |
| userName, |
| tag, |
| t.Year(), |
| t.Month(), |
| t.Day(), |
| t.Hour(), |
| t.Minute(), |
| t.Second(), |
| pid) |
| return name, program + "." + tag |
| } |
| |
| var onceLogDirs sync.Once |
| |
| // create creates a new log file and returns the file and its filename, which |
| // contains tag ("INFO", "FATAL", etc.) and t. If the file is created |
| // successfully, create also attempts to update the symlink for that tag, ignoring |
| // errors. |
| func create(tag string, t time.Time) (f *os.File, filename string, err error) { |
| onceLogDirs.Do(createLogDirs) |
| if len(logDirs) == 0 { |
| return nil, "", errors.New("log: no log dirs") |
| } |
| name, link := logName(tag, t) |
| var lastErr error |
| for _, dir := range logDirs { |
| fname := path.Join(dir, name) |
| f, err := os.Create(fname) |
| if err == nil { |
| symlink := path.Join(dir, link) |
| os.Remove(symlink) // ignore err |
| os.Symlink(fname, symlink) // ignore err |
| return f, fname, nil |
| } |
| lastErr = err |
| } |
| return nil, "", fmt.Errorf("log: cannot create log: %v", lastErr) |
| } |