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>
274 lines
8.3 KiB
Go
274 lines
8.3 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 syncthing
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/syncthing/syncthing/internal/db"
|
|
"github.com/syncthing/syncthing/internal/db/olddb"
|
|
"github.com/syncthing/syncthing/internal/db/olddb/backend"
|
|
"github.com/syncthing/syncthing/internal/db/sqlite"
|
|
"github.com/syncthing/syncthing/internal/slogutil"
|
|
"github.com/syncthing/syncthing/lib/build"
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
"github.com/syncthing/syncthing/lib/events"
|
|
"github.com/syncthing/syncthing/lib/fs"
|
|
"github.com/syncthing/syncthing/lib/locations"
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
"github.com/syncthing/syncthing/lib/tlsutil"
|
|
)
|
|
|
|
func EnsureDir(dir string, mode fs.FileMode) error {
|
|
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
|
|
err := fs.MkdirAll(".", mode)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if fi, err := fs.Stat("."); err == nil {
|
|
// Apparently the stat may fail even though the mkdirall passed. If it
|
|
// does, we'll just assume things are in order and let other things
|
|
// fail (like loading or creating the config...).
|
|
currentMode := fi.Mode() & 0o777
|
|
if currentMode != mode {
|
|
err := fs.Chmod(".", mode)
|
|
// This can fail on crappy filesystems, nothing we can do about it.
|
|
if err != nil {
|
|
slog.Warn("Failed to correct directory permissions", slogutil.Error(err))
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func LoadOrGenerateCertificate(certFile, keyFile string) (tls.Certificate, error) {
|
|
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
|
if err != nil {
|
|
return GenerateCertificate(certFile, keyFile)
|
|
}
|
|
return cert, nil
|
|
}
|
|
|
|
func GenerateCertificate(certFile, keyFile string) (tls.Certificate, error) {
|
|
slog.Info("Generating key and certificate", "cn", tlsDefaultCommonName)
|
|
return tlsutil.NewCertificate(certFile, keyFile, tlsDefaultCommonName, deviceCertLifetimeDays, false)
|
|
}
|
|
|
|
func DefaultConfig(path string, myID protocol.DeviceID, evLogger events.Logger, skipPortProbing bool) (config.Wrapper, error) {
|
|
newCfg := config.New(myID)
|
|
|
|
if skipPortProbing {
|
|
slog.Info("Using default network port numbers instead of probing for free ports")
|
|
// Record address override initially
|
|
newCfg.GUI.RawAddress = newCfg.GUI.Address()
|
|
} else if err := newCfg.ProbeFreePorts(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return config.Wrap(path, newCfg, myID, evLogger), nil
|
|
}
|
|
|
|
// LoadConfigAtStartup loads an existing config. If it doesn't yet exist, it
|
|
// creates a default one. Otherwise it checks the version, and archives and
|
|
// upgrades the config if necessary or returns an error, if the version
|
|
// isn't compatible.
|
|
func LoadConfigAtStartup(path string, cert tls.Certificate, evLogger events.Logger, allowNewerConfig, skipPortProbing bool) (config.Wrapper, error) {
|
|
myID := protocol.NewDeviceID(cert.Certificate[0])
|
|
cfg, originalVersion, err := config.Load(path, myID, evLogger)
|
|
if fs.IsNotExist(err) {
|
|
cfg, err = DefaultConfig(path, myID, evLogger, skipPortProbing)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to generate default config: %w", err)
|
|
}
|
|
err = cfg.Save()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to save default config: %w", err)
|
|
}
|
|
slog.Info("Default config saved; edit to taste (with Syncthing stopped) or use the GUI", slogutil.FilePath(cfg.ConfigPath()))
|
|
} else if errors.Is(err, io.EOF) {
|
|
return nil, errors.New("failed to load config: unexpected end of file. Truncated or empty configuration?")
|
|
} else if err != nil {
|
|
return nil, fmt.Errorf("failed to load config: %w", err)
|
|
}
|
|
|
|
if originalVersion != config.CurrentVersion {
|
|
if originalVersion > config.CurrentVersion && !allowNewerConfig {
|
|
return nil, fmt.Errorf("config file version (%d) is newer than supported version (%d); if this is expected, use --allow-newer-config to override", originalVersion, config.CurrentVersion)
|
|
}
|
|
err = archiveAndSaveConfig(cfg, originalVersion)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("config archive: %w", err)
|
|
}
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func archiveAndSaveConfig(cfg config.Wrapper, originalVersion int) error {
|
|
// Copy the existing config to an archive copy
|
|
archivePath := cfg.ConfigPath() + fmt.Sprintf(".v%d", originalVersion)
|
|
slog.Info("Archiving a copy of old config file format", slogutil.FilePath(archivePath))
|
|
if err := copyFile(cfg.ConfigPath(), archivePath); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Do a regular atomic config sve
|
|
return cfg.Save()
|
|
}
|
|
|
|
func copyFile(src, dst string) error {
|
|
bs, err := os.ReadFile(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := os.WriteFile(dst, bs, 0o600); err != nil {
|
|
// Attempt to clean up
|
|
os.Remove(dst)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Opens a database
|
|
func OpenDatabase(path string, deleteRetention time.Duration) (db.DB, error) {
|
|
sql, err := sqlite.Open(path, sqlite.WithDeleteRetention(deleteRetention))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sdb := db.MetricsWrap(sql)
|
|
|
|
return sdb, nil
|
|
}
|
|
|
|
// Attempts migration of the old (LevelDB-based) database type to the new (SQLite-based) type
|
|
func TryMigrateDatabase(deleteRetention time.Duration) error {
|
|
oldDBDir := locations.Get(locations.LegacyDatabase)
|
|
if _, err := os.Lstat(oldDBDir); err != nil {
|
|
// No old database
|
|
return nil
|
|
}
|
|
|
|
be, err := backend.OpenLevelDBRO(oldDBDir)
|
|
if err != nil {
|
|
// Apparently, not a valid old database
|
|
return nil
|
|
}
|
|
defer be.Close()
|
|
|
|
sdb, err := sqlite.OpenForMigration(locations.Get(locations.Database))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer sdb.Close()
|
|
|
|
miscDB := db.NewMiscDB(sdb)
|
|
if when, ok, err := miscDB.Time("migrated-from-leveldb-at"); err == nil && ok {
|
|
slog.Error("Old-style database present but already migrated; please manually move or remove.", slog.Any("migratedAt", when), slogutil.FilePath(oldDBDir))
|
|
return nil
|
|
}
|
|
|
|
slog.Info("Migrating old-style database to SQLite; this may take a while...")
|
|
t0 := time.Now()
|
|
|
|
ll, err := olddb.NewLowlevel(be)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
totFiles, totBlocks := 0, 0
|
|
for _, folder := range ll.ListFolders() {
|
|
// Start a writer routine
|
|
fis := make(chan protocol.FileInfo, 50)
|
|
var writeErr error
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
var batch []protocol.FileInfo
|
|
files, blocks := 0, 0
|
|
t0 := time.Now()
|
|
t1 := time.Now()
|
|
for fi := range fis {
|
|
batch = append(batch, fi)
|
|
files++
|
|
blocks += len(fi.Blocks)
|
|
if len(batch) == 1000 {
|
|
writeErr = sdb.Update(folder, protocol.LocalDeviceID, batch)
|
|
if writeErr != nil {
|
|
return
|
|
}
|
|
batch = batch[:0]
|
|
if time.Since(t1) > 10*time.Second {
|
|
d := time.Since(t0) + 1
|
|
t1 = time.Now()
|
|
slog.Info("Still migrating folder", "folder", folder, "files", files, "blocks", blocks, "duration", d.Truncate(time.Second), "filesrate", float64(files)/d.Seconds())
|
|
}
|
|
}
|
|
}
|
|
if len(batch) > 0 {
|
|
writeErr = sdb.Update(folder, protocol.LocalDeviceID, batch)
|
|
}
|
|
d := time.Since(t0) + 1
|
|
slog.Info("Migrated folder", "folder", folder, "files", files, "blocks", blocks, "duration", d.Truncate(time.Second), "filesrate", float64(files)/d.Seconds())
|
|
totFiles += files
|
|
totBlocks += blocks
|
|
}()
|
|
|
|
// Iterate the existing files
|
|
fs, err := olddb.NewFileSet(folder, ll)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
snap, err := fs.Snapshot()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_ = snap.WithHaveSequence(0, func(fi protocol.FileInfo) bool {
|
|
if deleteRetention > 0 && fi.Deleted && time.Since(fi.ModTime()) > deleteRetention {
|
|
// Skip deleted files that match the garbage collection
|
|
// criteria in the database
|
|
return true
|
|
}
|
|
fis <- fi
|
|
return true
|
|
})
|
|
close(fis)
|
|
snap.Release()
|
|
|
|
// Wait for writes to complete
|
|
wg.Wait()
|
|
if writeErr != nil {
|
|
return writeErr
|
|
}
|
|
}
|
|
|
|
slog.Info("Migrating virtual mtimes...")
|
|
if err := ll.IterateMtimes(sdb.PutMtime); err != nil {
|
|
slog.Warn("Failed to migrate mtimes", slogutil.Error(err))
|
|
}
|
|
|
|
_ = miscDB.PutTime("migrated-from-leveldb-at", time.Now())
|
|
_ = miscDB.PutString("migrated-from-leveldb-by", build.LongVersion)
|
|
|
|
_ = be.Close()
|
|
_ = os.Rename(oldDBDir, oldDBDir+"-migrated")
|
|
|
|
slog.Info("Migration complete", "files", totFiles, "blocks", totBlocks/1000, "duration", time.Since(t0).Truncate(time.Second))
|
|
return nil
|
|
}
|