forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
420 lines (371 loc) · 12.2 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Go support for leveled logs, analogous to https://code.google.com/p/google-clog/
//
// Copyright 2013 Google Inc. All Rights Reserved.
//
// 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.
// File I/O for logs.
// Author: Bram Gruneir (bram@cockroachlabs.com)
package log
import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
// MaxSize is the maximum size of a log file in bytes.
var MaxSize uint64 = 1024 * 1024 * 10
// If non-empty, overrides the choice of directory in which to write logs. See
// createLogDirs for the full list of possible destinations. Note that the
// default is to log to stderr independent of this setting. See --logtostderr.
var logDir = os.TempDir()
var logDirSet bool
// DirSet returns true of the log directory has been changed from its default.
func DirSet() bool {
return logDirSet
}
type stringValue struct {
val *string
set *bool
}
var _ flag.Value = &stringValue{}
func newStringValue(val *string, set *bool) *stringValue {
return &stringValue{val: val, set: set}
}
func (s *stringValue) Set(val string) error {
*s.val = val
*s.set = true
return nil
}
func (s *stringValue) Type() string {
return "string"
}
func (s *stringValue) String() string {
return *s.val
}
// logFileRE matches log files to avoid exposing non-log files accidentally
// and it splits the details of the filename into groups for easy parsing.
// The log file format is {process}.{host}.{username}.log.{severity}.{timestamp}
// cockroach.Brams-MacBook-Pro.bram.log.WARNING.2015-06-09T16_10_48-04_00.30209
// All underscore in process, host and username are escaped to double
// underscores and all periods are escaped to an underscore.
// For compatibility with Windows filenames, all colons from the timestamp
// (RFC3339) are converted to underscores.
var logFileRE = regexp.MustCompile(`([^\.]+)\.([^\.]+)\.([^\.]+)\.log\.(ERROR|WARNING|INFO)\.([^\.]+)\.(\d+)`)
var (
pid = os.Getpid()
program = filepath.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
}
// Sanitize userName since it may contain filepath separators on Windows.
userName = strings.Replace(userName, `\`, "_", -1)
}
// 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
}
// removePeriods removes all extraneous periods. This is required to ensure that
// the only periods in the filename are the ones added by logName so it can
// be easily parsed.
func removePeriods(s string) string {
return strings.Replace(s, ".", "", -1)
}
// logName returns a new log file name containing the severity, with start time
// t, and the name for the symlink for the severity.
func logName(severity Severity, t time.Time) (name, link string) {
// Replace the ':'s in the time format with '_'s to allow for log files in
// Windows.
tFormatted := strings.Replace(t.Format(time.RFC3339), ":", "_", -1)
name = fmt.Sprintf("%s.%s.%s.log.%s.%s.%d",
removePeriods(program),
removePeriods(host),
removePeriods(userName),
severity.Name(),
tFormatted,
pid)
return name, removePeriods(program) + "." + severity.Name()
}
// A FileDetails holds all of the particulars that can be parsed by the name of
// a log file.
type FileDetails struct {
Program string
Host string
UserName string
Severity Severity
Time time.Time
PID uint
}
var errMalformedName = errors.New("malformed log filename")
var errMalformedSev = errors.New("malformed severity")
func parseLogFilename(filename string) (FileDetails, error) {
matches := logFileRE.FindStringSubmatch(filename)
if matches == nil || len(matches) != 7 {
return FileDetails{}, errMalformedName
}
sev, sevFound := SeverityByName(matches[4])
if !sevFound {
return FileDetails{}, errMalformedSev
}
// Replace the '_'s with ':'s to restore the correct time format.
fixTime := strings.Replace(matches[5], "_", ":", -1)
time, err := time.Parse(time.RFC3339, fixTime)
if err != nil {
return FileDetails{}, err
}
pid, err := strconv.ParseInt(matches[6], 10, 0)
if err != nil {
return FileDetails{}, err
}
return FileDetails{
Program: matches[1],
Host: matches[2],
UserName: matches[3],
Severity: sev,
Time: time,
PID: uint(pid),
}, nil
}
var errDirectoryNotSet = errors.New("log: log directory not set")
// create creates a new log file and returns the file and its filename, which
// contains severity ("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(severity Severity, t time.Time) (f *os.File, filename string, err error) {
if len(logDir) == 0 {
return nil, "", errDirectoryNotSet
}
name, link := logName(severity, t)
var lastErr error
fname := filepath.Join(logDir, name)
// Open the file os.O_APPEND|os.O_CREATE rather than use os.Create.
// Append is almost always more efficient than O_RDRW on most modern file systems.
f, err = os.OpenFile(fname, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
if err == nil {
symlink := filepath.Join(logDir, link)
_ = os.Remove(symlink) // ignore err
_ = os.Symlink(name, symlink) // ignore err
return f, fname, nil
}
lastErr = err
return nil, "", fmt.Errorf("log: cannot create log: %v", lastErr)
}
var errNotAFile = errors.New("not a regular file")
// getFileDetails verifies that the file specified by filename is a
// regular file and filename matches the expected filename pattern.
// Returns the log file details success; otherwise error.
func getFileDetails(info os.FileInfo) (FileDetails, error) {
if info.Mode()&os.ModeType != 0 {
return FileDetails{}, errNotAFile
}
details, err := parseLogFilename(info.Name())
if err != nil {
return FileDetails{}, err
}
return details, nil
}
func verifyFile(filename string) error {
info, err := os.Stat(filename)
if err != nil {
return err
}
_, err = getFileDetails(info)
return err
}
// A FileInfo holds the filename and size of a log file.
type FileInfo struct {
Name string // base name
SizeBytes int64
ModTimeNanos int64 // most recent mode time in unix nanos
Details FileDetails
}
// ListLogFiles returns a slice of FileInfo structs for each log file
// on the local node, in any of the configured log directories.
func ListLogFiles() ([]FileInfo, error) {
var results []FileInfo
if logDir == "" {
return nil, nil
}
infos, err := ioutil.ReadDir(logDir)
if err != nil {
return results, err
}
for _, info := range infos {
details, err := getFileDetails(info)
if err == nil {
results = append(results, FileInfo{
Name: info.Name(),
SizeBytes: info.Size(),
ModTimeNanos: info.ModTime().UnixNano(),
Details: details,
})
}
}
return results, nil
}
// GetLogReader returns a reader for the specified filename. In
// restricted mode, the filename must be the base name of a file in
// this process's log directory (this is safe for cases when the
// filename comes from external sources, such as the admin UI via
// HTTP). In unrestricted mode any path is allowed, with the added
// feature that relative paths will be searched in both the current
// directory and this process's log directory.
func GetLogReader(filename string, restricted bool) (io.ReadCloser, error) {
if !restricted {
if resolved, err := filepath.EvalSymlinks(filename); err == nil {
if verifyFile(resolved) == nil {
return os.Open(resolved)
}
}
}
// Verify there are no path separators in a restricted-mode pathname.
if restricted && filepath.Base(filename) != filename {
return nil, fmt.Errorf("pathnames must be basenames only: %s", filename)
}
if !filepath.IsAbs(filename) {
filename = filepath.Join(logDir, filename)
}
if !restricted {
var err error
filename, err = filepath.EvalSymlinks(filename)
if err != nil {
return nil, err
}
}
if err := verifyFile(filename); err != nil {
return nil, err
}
return os.Open(filename)
}
// sortableFileInfoSlice is required so we can sort FileInfos.
type sortableFileInfoSlice []FileInfo
func (a sortableFileInfoSlice) Len() int { return len(a) }
func (a sortableFileInfoSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortableFileInfoSlice) Less(i, j int) bool {
return a[i].Details.Time.UnixNano() < a[j].Details.Time.UnixNano()
}
// selectFiles selects all log files that have an timestamp before the endTime and
// the correct severity. It then sorts them in decreasing order, with the most
// recent as the first one.
func selectFiles(logFiles []FileInfo, severity Severity, endTimestamp int64) []FileInfo {
files := sortableFileInfoSlice{}
for _, logFile := range logFiles {
if logFile.Details.Severity == severity && logFile.Details.Time.UnixNano() <= endTimestamp {
files = append(files, logFile)
}
}
// Sort the files in reverse order so we will fetch the newest first.
sort.Sort(sort.Reverse(files))
return files
}
// FetchEntriesFromFiles fetches all available log entires on disk that match
// the log 'severity' (or worse) and are between the 'startTimestamp' and
// 'endTimestamp'. It will stop reading new files if the number of entries
// exceeds 'maxEntries'. Log entries are further filtered by the regexp
// 'pattern' if provided. The logs entries are returned in reverse chronological
// order.
func FetchEntriesFromFiles(severity Severity, startTimestamp, endTimestamp int64, maxEntries int,
pattern *regexp.Regexp) ([]Entry, error) {
logFiles, err := ListLogFiles()
if err != nil {
return nil, err
}
selectedFiles := selectFiles(logFiles, severity, endTimestamp)
entries := []Entry{}
for _, file := range selectedFiles {
newEntries, entryBeforeStart, err := readAllEntriesFromFile(
file,
startTimestamp,
endTimestamp,
maxEntries-len(entries),
pattern)
if err != nil {
return nil, err
}
entries = append(entries, newEntries...)
if len(entries) >= maxEntries {
break
}
if entryBeforeStart {
// Stop processing files that won't have any timestamps after
// startTime.
break
}
}
return entries, nil
}
// readAllEntriesFromFile reads in all log entries from a given file that are
// between the 'startTimestamp' and 'endTimestamp' and match the 'pattern' if it
// exists. It returns the entries in the reverse chronological order. It also
// returns a flag that denotes if any timestamp occurred before the
// 'startTimestamp' to inform the caller that no more log files need to be
// processed. If the number of entries returned exceeds 'maxEntries' then
// processing of new entries is stopped immediately.
func readAllEntriesFromFile(file FileInfo, startTimestamp, endTimestamp int64, maxEntries int,
pattern *regexp.Regexp) ([]Entry, bool, error) {
reader, err := GetLogReader(file.Name, true /* restricted */)
defer reader.Close()
if reader == nil || err != nil {
return nil, false, err
}
entries := []Entry{}
decoder := NewEntryDecoder(reader)
entryBeforeStart := false
for {
entry := Entry{}
if err := decoder.Decode(&entry); err != nil {
if err == io.EOF {
break
}
return nil, false, err
}
var match bool
if pattern == nil {
match = true
} else {
match = pattern.MatchString(entry.Message) ||
pattern.MatchString(entry.File)
}
if match && entry.Time >= startTimestamp && entry.Time <= endTimestamp {
entries = append([]Entry{entry}, entries...)
if len(entries) >= maxEntries {
break
}
}
if entry.Time < startTimestamp {
entryBeforeStart = true
}
}
return entries, entryBeforeStart, nil
}