| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package logs |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
stdlog "log" |
| 11 |
"os" |
| 12 |
"runtime" |
| 13 |
"strconv" |
| 14 |
"strings" |
| 15 |
"sync" |
| 16 |
"time" |
| 17 |
|
| 18 |
. "github.com/etix/mirrorbits/config" |
| 19 |
"github.com/etix/mirrorbits/core" |
| 20 |
"github.com/etix/mirrorbits/mirrors" |
| 21 |
"github.com/op/go-logging" |
| 22 |
) |
| 23 |
|
| 24 |
var ( |
| 25 |
log = logging.MustGetLogger("main") |
| 26 |
rlogger runtimeLogger |
| 27 |
dlogger downloadsLogger |
| 28 |
) |
| 29 |
|
| 30 |
type runtimeLogger struct { |
| 31 |
f *os.File |
| 32 |
} |
| 33 |
|
| 34 |
type downloadsLogger struct { |
| 35 |
sync.RWMutex |
| 36 |
l *stdlog.Logger |
| 37 |
f io.WriteCloser |
| 38 |
} |
| 39 |
|
| 40 |
func (d *downloadsLogger) Close() { |
| 41 |
if d.f != nil { |
| 42 |
d.f.Close() |
| 43 |
d.f = nil |
| 44 |
} |
| 45 |
d.l = nil |
| 46 |
} |
| 47 |
|
| 48 |
// ReloadLogs will reopen the logs to allow rotations |
| 49 |
func ReloadLogs() { |
| 50 |
ReloadRuntimeLogs() |
| 51 |
if core.Daemon { |
| 52 |
ReloadDownloadLogs() |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func isTerminal(f *os.File) bool { |
| 57 |
stat, _ := f.Stat() |
| 58 |
if (stat.Mode() & os.ModeCharDevice) != 0 { |
| 59 |
return true |
| 60 |
} |
| 61 |
return false |
| 62 |
} |
| 63 |
|
| 64 |
// ReloadRuntimeLogs reopens the runtime logs for writing |
| 65 |
func ReloadRuntimeLogs() { |
| 66 |
if rlogger.f == os.Stderr && core.RunLog == "" { |
| 67 |
// Logger already set up and connected to the console. |
| 68 |
// Don't reload to avoid breaking journald. |
| 69 |
return |
| 70 |
} |
| 71 |
|
| 72 |
if rlogger.f != nil && rlogger.f != os.Stderr { |
| 73 |
rlogger.f.Close() |
| 74 |
} |
| 75 |
|
| 76 |
if core.RunLog != "" { |
| 77 |
var err error |
| 78 |
rlogger.f, _, err = openLogFile(core.RunLog) |
| 79 |
if err != nil { |
| 80 |
fmt.Fprintln(os.Stderr, "Cannot open log file for writing") |
| 81 |
rlogger.f = os.Stderr |
| 82 |
} |
| 83 |
} else { |
| 84 |
rlogger.f = os.Stderr |
| 85 |
} |
| 86 |
|
| 87 |
logBackend := logging.NewLogBackend(rlogger.f, "", 0) |
| 88 |
logBackend.Color = isTerminal(rlogger.f) //TODO make color optional |
| 89 |
|
| 90 |
logging.SetBackend(logBackend) |
| 91 |
|
| 92 |
if core.Debug { |
| 93 |
logging.SetFormatter(logging.MustStringFormatter("%{shortfile:-20s}%{time:2006/01/02 15:04:05.000 MST} %{message}")) |
| 94 |
logging.SetLevel(logging.DEBUG, "main") |
| 95 |
} else { |
| 96 |
logging.SetFormatter(logging.MustStringFormatter("%{time:2006/01/02 15:04:05.000 MST} %{message}")) |
| 97 |
logging.SetLevel(logging.INFO, "main") |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func openLogFile(logfile string) (*os.File, bool, error) { |
| 102 |
newfile := true |
| 103 |
|
| 104 |
s, _ := os.Stat(logfile) |
| 105 |
if s != nil && s.Size() > 0 { |
| 106 |
newfile = false |
| 107 |
} |
| 108 |
|
| 109 |
f, err := os.OpenFile(logfile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0664) |
| 110 |
if err != nil { |
| 111 |
return nil, false, err |
| 112 |
} |
| 113 |
|
| 114 |
return f, newfile, nil |
| 115 |
} |
| 116 |
|
| 117 |
func setDownloadLogWriter(writer io.Writer, createHeader bool) { |
| 118 |
dlogger.l = stdlog.New(writer, "", stdlog.Ldate|stdlog.Lmicroseconds) |
| 119 |
|
| 120 |
if createHeader { |
| 121 |
var buf bytes.Buffer |
| 122 |
hostname, _ := os.Hostname() |
| 123 |
fmt.Fprintf(&buf, "# Log file created at: %s\n", time.Now().Format("2006/01/02 15:04:05")) |
| 124 |
fmt.Fprintf(&buf, "# Running on machine: %s\n", hostname) |
| 125 |
fmt.Fprintf(&buf, "# Binary: Built with %s %s for %s/%s\n", runtime.Compiler, runtime.Version(), runtime.GOOS, runtime.GOARCH) |
| 126 |
writer.Write(buf.Bytes()) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// ReloadDownloadLogs reopens the download logs for writing |
| 131 |
func ReloadDownloadLogs() { |
| 132 |
dlogger.Lock() |
| 133 |
defer dlogger.Unlock() |
| 134 |
|
| 135 |
dlogger.Close() |
| 136 |
|
| 137 |
if GetConfig().LogDir == "" { |
| 138 |
return |
| 139 |
} |
| 140 |
|
| 141 |
logfile := GetConfig().LogDir + "/downloads.log" |
| 142 |
f, createHeader, err := openLogFile(logfile) |
| 143 |
if err != nil { |
| 144 |
log.Criticalf("Cannot open log file %s", logfile) |
| 145 |
return |
| 146 |
} |
| 147 |
|
| 148 |
setDownloadLogWriter(f, createHeader) |
| 149 |
} |
| 150 |
|
| 151 |
// LogDownload writes a download result to the logs |
| 152 |
func LogDownload(typ string, method string, statuscode int, p *mirrors.Results, err error) { |
| 153 |
dlogger.RLock() |
| 154 |
defer dlogger.RUnlock() |
| 155 |
|
| 156 |
if dlogger.l == nil { |
| 157 |
// Logs are disabled |
| 158 |
return |
| 159 |
} |
| 160 |
|
| 161 |
var path, ip string |
| 162 |
if p != nil { |
| 163 |
path = p.FileInfo.Path |
| 164 |
ip = p.IP |
| 165 |
} |
| 166 |
|
| 167 |
errstr := "<unknown>" |
| 168 |
if err != nil { |
| 169 |
errstr = err.Error() |
| 170 |
} |
| 171 |
|
| 172 |
line := fmt.Sprintf("%s %d %s \"%s\" ip:%s", typ, statuscode, method, path, ip) |
| 173 |
|
| 174 |
if (statuscode == 302 || statuscode == 200) && p != nil && len(p.MirrorList) > 0 { |
| 175 |
var distance, countries string |
| 176 |
m := p.MirrorList[0] |
| 177 |
distance = strconv.FormatFloat(float64(m.Distance), 'f', 2, 32) |
| 178 |
countries = strings.Join(m.CountryFields, ",") |
| 179 |
fallback := "" |
| 180 |
if p.Fallback == true { |
| 181 |
fallback = " fallback:true" |
| 182 |
} |
| 183 |
sameASNum := "" |
| 184 |
if m.Asnum > 0 && m.Asnum == p.ClientInfo.ASNum { |
| 185 |
sameASNum = "same" |
| 186 |
} |
| 187 |
line += fmt.Sprintf(" mirror:%s%s %sasn:%d distance:%skm countries:%s", |
| 188 |
m.Name, fallback, sameASNum, m.Asnum, distance, countries) |
| 189 |
} else if statuscode == 404 && p != nil { |
| 190 |
// nothing to add to the log line |
| 191 |
} else if statuscode == 500 && p != nil { |
| 192 |
mirrorName := "unknown" |
| 193 |
if len(p.MirrorList) > 0 { |
| 194 |
mirrorName = p.MirrorList[0].Name |
| 195 |
} |
| 196 |
line += fmt.Sprintf(" mirror:%s error:%s", mirrorName, errstr) |
| 197 |
} else { |
| 198 |
line += fmt.Sprintf(" error:%s", errstr) |
| 199 |
} |
| 200 |
|
| 201 |
dlogger.l.Print(line) |
| 202 |
} |