github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/logger/glog/glog_file.go (about) 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 "fmt" 24 "os" 25 "os/user" 26 "path/filepath" 27 "strings" 28 "sync" 29 "time" 30 ) 31 32 // MaxSize is the maximum size of a log file in bytes. 33 var MaxSize uint64 = 1024 * 1024 * 1800 34 35 // logDirs lists the candidate directories for new log files. 36 var logDirs []string 37 38 // If non-empty, overrides the choice of directory in which to write logs. 39 // See createLogDirs for the full list of possible destinations. 40 //var logDir = flag.String("log_dir", "", "If non-empty, write log files in this directory") 41 var logDir *string = new(string) 42 43 func SetLogDir(str string) { 44 *logDir = str 45 } 46 47 func createLogDirs() { 48 if *logDir != "" { 49 logDirs = append(logDirs, *logDir) 50 } 51 logDirs = append(logDirs, os.TempDir()) 52 } 53 54 var ( 55 pid = os.Getpid() 56 program = filepath.Base(os.Args[0]) 57 host = "unknownhost" 58 userName = "unknownuser" 59 ) 60 61 func init() { 62 h, err := os.Hostname() 63 if err == nil { 64 host = shortHostname(h) 65 } 66 67 current, err := user.Current() 68 if err == nil { 69 userName = current.Username 70 } 71 72 // Sanitize userName since it may contain filepath separators on Windows. 73 userName = strings.Replace(userName, `\`, "_", -1) 74 } 75 76 // shortHostname returns its argument, truncating at the first period. 77 // For instance, given "www.google.com" it returns "www". 78 func shortHostname(hostname string) string { 79 if i := strings.Index(hostname, "."); i >= 0 { 80 return hostname[:i] 81 } 82 return hostname 83 } 84 85 // logName returns a new log file name containing tag, with start time t, and 86 // the name for the symlink for tag. 87 func logName(tag string, t time.Time) (name, link string) { 88 name = fmt.Sprintf("%s.%s.%s.log.%s.%04d%02d%02d-%02d%02d%02d.%d", 89 program, 90 host, 91 userName, 92 tag, 93 t.Year(), 94 t.Month(), 95 t.Day(), 96 t.Hour(), 97 t.Minute(), 98 t.Second(), 99 pid) 100 return name, program + "." + tag 101 } 102 103 var onceLogDirs sync.Once 104 105 // create creates a new log file and returns the file and its filename, which 106 // contains tag ("INFO", "FATAL", etc.) and t. If the file is created 107 // successfully, create also attempts to update the symlink for that tag, ignoring 108 // errors. 109 func create(tag string, t time.Time) (f *os.File, filename string, err error) { 110 onceLogDirs.Do(createLogDirs) 111 if len(logDirs) == 0 { 112 return nil, "", errors.New("log: no log dirs") 113 } 114 name, link := logName(tag, t) 115 var lastErr error 116 for _, dir := range logDirs { 117 fname := filepath.Join(dir, name) 118 f, err := os.Create(fname) 119 if err == nil { 120 symlink := filepath.Join(dir, link) 121 os.Remove(symlink) // ignore err 122 os.Symlink(name, symlink) // ignore err 123 return f, fname, nil 124 } 125 lastErr = err 126 } 127 return nil, "", fmt.Errorf("log: cannot create log: %v", lastErr) 128 }