mirror of
https://github.com/syncthing/syncthing.git
synced 2026-02-07 20:52:54 -05:00
This updates our logging framework from legacy freetext strings using the `log` package to structured log entries using `log/slog`. I have updated all INFO or higher level entries, but not yet DEBUG (😓)... So, at a high level: There is a slight change in log levels, effectively adding a new warning level: - DEBUG is still debug (ideally not for users but developers, though this is something we need to work on) - INFO is still info, though I've added more data here, effectively making Syncthing more verbose by default (more on this below) - WARNING is a new log level that is different from the _old_ WARNING (more below) - ERROR is what was WARNING before -- problems that must be dealt with, and also bubbled as a popup in the GUI. A new feature is that the logging level can be set per package to something other than just debug or info, and hence I feel that we can add a bit more things into INFO while moving some (in fact, most) current INFO level warnings into WARNING. For example, I think it's justified to get a log of synced files in INFO and sync failures in WARNING. These are things that have historically been tricky to debug properly, and having more information by default will be useful to many, while still making it possible get close to told level of inscrutability by setting the log level to WARNING. I'd like to get to a stage where DEBUG is never necessary to just figure out what's going on, as opposed to trying to narrow down a likely bug. Code wise: - Our logging object, generally known as `l` in each package, is now a new adapter object that provides the old API on top of the newer one. (This should go away once all old log entries are migrated.) This is only for `l.Debugln` and `l.Debugf`. - There is a new level tracker that keeps the log level for each package. - There is a nested setup of handlers, since the structure mandated by `log/slog` is slightly convoluted (imho). We do this because we need to do formatting at a "medium" level internally so we can buffer log lines in text format but with separate timestamp and log level for the API/GUI to consume. - The `debug` API call becomes a `loglevels` API call, which can set the log level to `DEBUG`, `INFO`, `WARNING` or `ERROR` per package. The GUI is updated to handle this. - Our custom `sync` package provided some debugging of mutexes quite strongly integrated into the old logging framework, only turned on when `STTRACE` was set to certain values at startup, etc. It's been a long time since this has been useful; I removed it. - The `STTRACE` env var remains and can be used the same way as before, while additionally permitting specific log levels to be specified, `STTRACE=model:WARN,scanner:DEBUG`. - There is a new command line option `--log-level=INFO` to set the default log level. - The command line options `--log-flags` and `--verbose` go away, but are currently retained as hidden & ignored options since we set them by default in some of our startup examples and Syncthing would otherwise fail to start. Sample format messages: ``` 2009-02-13 23:31:30 INF A basic info line (attr1="val with spaces" attr2=2 attr3="val\"quote" a=a log.pkg=slogutil) 2009-02-13 23:31:30 INF An info line with grouped values (attr1=val1 foo.attr2=2 foo.bar.attr3=3 a=a log.pkg=slogutil) 2009-02-13 23:31:30 INF An info line with grouped values via logger (foo.attr1=val1 foo.attr2=2 a=a log.pkg=slogutil) 2009-02-13 23:31:30 INF An info line with nested grouped values via logger (bar.foo.attr1=val1 bar.foo.attr2=2 a=a log.pkg=slogutil) 2009-02-13 23:31:30 WRN A warning entry (a=a log.pkg=slogutil) 2009-02-13 23:31:30 ERR An error (a=a log.pkg=slogutil) ``` --------- Co-authored-by: Ross Smith II <ross@smithii.com>
152 lines
3.6 KiB
Go
152 lines
3.6 KiB
Go
// Copyright (C) 2014 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package scanner
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
)
|
|
|
|
// HashFile hashes the files and returns a list of blocks representing the file.
|
|
func HashFile(ctx context.Context, folderID string, fs fs.Filesystem, path string, blockSize int, counter Counter) ([]protocol.BlockInfo, error) {
|
|
fd, err := fs.Open(path)
|
|
if err != nil {
|
|
l.Debugln("open:", err)
|
|
return nil, err
|
|
}
|
|
defer fd.Close()
|
|
|
|
// Get the size and modtime of the file before we start hashing it.
|
|
|
|
fi, err := fd.Stat()
|
|
if err != nil {
|
|
l.Debugln("stat before:", err)
|
|
return nil, err
|
|
}
|
|
size := fi.Size()
|
|
modTime := fi.ModTime()
|
|
|
|
// Hash the file. This may take a while for large files.
|
|
|
|
blocks, err := Blocks(ctx, fd, blockSize, size, counter)
|
|
if err != nil {
|
|
l.Debugln("blocks:", err)
|
|
return nil, err
|
|
}
|
|
|
|
metricHashedBytes.WithLabelValues(folderID).Add(float64(size))
|
|
|
|
// Recheck the size and modtime again. If they differ, the file changed
|
|
// while we were reading it and our hash results are invalid.
|
|
|
|
fi, err = fd.Stat()
|
|
if err != nil {
|
|
l.Debugln("stat after:", err)
|
|
return nil, err
|
|
}
|
|
if size != fi.Size() || !modTime.Equal(fi.ModTime()) {
|
|
return nil, errors.New("file changed during hashing")
|
|
}
|
|
|
|
return blocks, nil
|
|
}
|
|
|
|
// The parallel hasher reads FileInfo structures from the inbox, hashes the
|
|
// file to populate the Blocks element and sends it to the outbox. A number of
|
|
// workers are used in parallel. The outbox will become closed when the inbox
|
|
// is closed and all items handled.
|
|
type parallelHasher struct {
|
|
folderID string
|
|
fs fs.Filesystem
|
|
outbox chan<- ScanResult
|
|
inbox <-chan protocol.FileInfo
|
|
counter Counter
|
|
done chan<- struct{}
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
func newParallelHasher(ctx context.Context, folderID string, fs fs.Filesystem, workers int, outbox chan<- ScanResult, inbox <-chan protocol.FileInfo, counter Counter, done chan<- struct{}) {
|
|
ph := ¶llelHasher{
|
|
folderID: folderID,
|
|
fs: fs,
|
|
outbox: outbox,
|
|
inbox: inbox,
|
|
counter: counter,
|
|
done: done,
|
|
}
|
|
|
|
ph.wg.Add(workers)
|
|
for i := 0; i < workers; i++ {
|
|
go ph.hashFiles(ctx)
|
|
}
|
|
|
|
go ph.closeWhenDone()
|
|
}
|
|
|
|
func (ph *parallelHasher) hashFiles(ctx context.Context) {
|
|
defer ph.wg.Done()
|
|
|
|
for {
|
|
select {
|
|
case f, ok := <-ph.inbox:
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
l.Debugln("started hashing:", f)
|
|
|
|
if f.IsDirectory() || f.IsDeleted() {
|
|
panic("Bug. Asked to hash a directory or a deleted file.")
|
|
}
|
|
|
|
blocks, err := HashFile(ctx, ph.folderID, ph.fs, f.Name, f.BlockSize(), ph.counter)
|
|
if err != nil {
|
|
handleError(ctx, "hashing", f.Name, err, ph.outbox)
|
|
continue
|
|
}
|
|
|
|
f.Blocks = blocks
|
|
f.BlocksHash = protocol.BlocksHash(blocks)
|
|
|
|
// The size we saw when initially deciding to hash the file
|
|
// might not have been the size it actually had when we hashed
|
|
// it. Update the size from the block list.
|
|
|
|
f.Size = 0
|
|
for _, b := range blocks {
|
|
f.Size += int64(b.Size)
|
|
}
|
|
|
|
l.Debugln("completed hashing:", f)
|
|
select {
|
|
case ph.outbox <- ScanResult{File: f}:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (ph *parallelHasher) closeWhenDone() {
|
|
ph.wg.Wait()
|
|
// In case the hasher aborted on context, wait for filesystem
|
|
// walking/progress routine to finish.
|
|
for range ph.inbox {
|
|
}
|
|
if ph.done != nil {
|
|
close(ph.done)
|
|
}
|
|
close(ph.outbox)
|
|
}
|