Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame^] | 1 | // Go support for leveled logs, analogous to https://code.google.com/p/google-glog/ |
| 2 | // |
| 3 | // Copyright 2013 Google Inc. All Rights Reserved. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | // File I/O for logs. |
| 18 | |
| 19 | package glog |
| 20 | |
| 21 | import ( |
| 22 | "errors" |
| 23 | "flag" |
| 24 | "fmt" |
| 25 | "os" |
| 26 | "os/user" |
| 27 | "path/filepath" |
| 28 | "strings" |
| 29 | "sync" |
| 30 | "time" |
| 31 | ) |
| 32 | |
| 33 | // MaxSize is the maximum size of a log file in bytes. |
| 34 | var MaxSize uint64 = 1024 * 1024 * 1800 |
| 35 | |
| 36 | // logDirs lists the candidate directories for new log files. |
| 37 | var logDirs []string |
| 38 | |
| 39 | // If non-empty, overrides the choice of directory in which to write logs. |
| 40 | // See createLogDirs for the full list of possible destinations. |
| 41 | var logDir = flag.String("log_dir", "", "If non-empty, write log files in this directory") |
| 42 | |
| 43 | func createLogDirs() { |
| 44 | if *logDir != "" { |
| 45 | logDirs = append(logDirs, *logDir) |
| 46 | } |
| 47 | logDirs = append(logDirs, os.TempDir()) |
| 48 | } |
| 49 | |
| 50 | var ( |
| 51 | pid = os.Getpid() |
| 52 | program = filepath.Base(os.Args[0]) |
| 53 | host = "unknownhost" |
| 54 | userName = "unknownuser" |
| 55 | ) |
| 56 | |
| 57 | func init() { |
| 58 | h, err := os.Hostname() |
| 59 | if err == nil { |
| 60 | host = shortHostname(h) |
| 61 | } |
| 62 | |
| 63 | current, err := user.Current() |
| 64 | if err == nil { |
| 65 | userName = current.Username |
| 66 | } |
| 67 | |
| 68 | // Sanitize userName since it may contain filepath separators on Windows. |
| 69 | userName = strings.Replace(userName, `\`, "_", -1) |
| 70 | } |
| 71 | |
| 72 | // shortHostname returns its argument, truncating at the first period. |
| 73 | // For instance, given "www.google.com" it returns "www". |
| 74 | func shortHostname(hostname string) string { |
| 75 | if i := strings.Index(hostname, "."); i >= 0 { |
| 76 | return hostname[:i] |
| 77 | } |
| 78 | return hostname |
| 79 | } |
| 80 | |
| 81 | // logName returns a new log file name containing tag, with start time t, and |
| 82 | // the name for the symlink for tag. |
| 83 | func logName(tag string, t time.Time) (name, link string) { |
| 84 | name = fmt.Sprintf("%s.%s.%s.log.%s.%04d%02d%02d-%02d%02d%02d.%d", |
| 85 | program, |
| 86 | host, |
| 87 | userName, |
| 88 | tag, |
| 89 | t.Year(), |
| 90 | t.Month(), |
| 91 | t.Day(), |
| 92 | t.Hour(), |
| 93 | t.Minute(), |
| 94 | t.Second(), |
| 95 | pid) |
| 96 | return name, program + "." + tag |
| 97 | } |
| 98 | |
| 99 | var onceLogDirs sync.Once |
| 100 | |
| 101 | // create creates a new log file and returns the file and its filename, which |
| 102 | // contains tag ("INFO", "FATAL", etc.) and t. If the file is created |
| 103 | // successfully, create also attempts to update the symlink for that tag, ignoring |
| 104 | // errors. |
| 105 | func create(tag string, t time.Time) (f *os.File, filename string, err error) { |
| 106 | onceLogDirs.Do(createLogDirs) |
| 107 | if len(logDirs) == 0 { |
| 108 | return nil, "", errors.New("log: no log dirs") |
| 109 | } |
| 110 | name, link := logName(tag, t) |
| 111 | var lastErr error |
| 112 | for _, dir := range logDirs { |
| 113 | fname := filepath.Join(dir, name) |
| 114 | f, err := os.Create(fname) |
| 115 | if err == nil { |
| 116 | symlink := filepath.Join(dir, link) |
| 117 | os.Remove(symlink) // ignore err |
| 118 | os.Symlink(name, symlink) // ignore err |
| 119 | return f, fname, nil |
| 120 | } |
| 121 | lastErr = err |
| 122 | } |
| 123 | return nil, "", fmt.Errorf("log: cannot create log: %v", lastErr) |
| 124 | } |