2015-04-30 20:21:48 +02:00
// Copyright (C) 2015 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,
2017-02-09 07:52:18 +01:00
// You can obtain one at https://mozilla.org/MPL/2.0/.
2015-04-30 20:21:48 +02:00
2019-07-14 12:43:13 +02:00
package syncthing
2015-04-30 20:21:48 +02:00
import (
2019-11-21 08:41:15 +01:00
"context"
2015-04-30 20:21:48 +02:00
"fmt"
2022-04-05 21:32:06 +02:00
"regexp"
2015-04-30 20:21:48 +02:00
2015-08-06 11:29:25 +02:00
"github.com/syncthing/syncthing/lib/events"
2022-04-05 21:32:06 +02:00
"github.com/syncthing/syncthing/lib/model"
2015-04-30 20:21:48 +02:00
)
// The verbose logging service subscribes to events and prints these in
// verbose format to the console using INFO level.
2015-12-23 16:31:12 +01:00
type verboseService struct {
2020-11-17 13:19:04 +01:00
evLogger events . Logger
2015-04-30 20:21:48 +02:00
}
2019-08-15 16:29:37 +02:00
func newVerboseService ( evLogger events . Logger ) * verboseService {
2020-11-17 13:19:04 +01:00
return & verboseService {
evLogger : evLogger ,
2015-04-30 20:21:48 +02:00
}
}
2019-07-23 10:50:37 +02:00
// serve runs the verbose logging service.
2020-11-17 13:19:04 +01:00
func ( s * verboseService ) Serve ( ctx context . Context ) error {
sub := s . evLogger . Subscribe ( events . AllEvents )
defer sub . Unsubscribe ( )
2015-04-30 20:21:48 +02:00
for {
select {
2021-12-22 20:16:21 +01:00
case ev , ok := <- sub . C ( ) :
if ! ok {
<- ctx . Done ( )
return ctx . Err ( )
}
2015-04-30 20:21:48 +02:00
formatted := s . formatEvent ( ev )
if formatted != "" {
l . Verboseln ( formatted )
}
2019-11-21 08:41:15 +01:00
case <- ctx . Done ( ) :
2020-11-17 13:19:04 +01:00
return ctx . Err ( )
2015-04-30 20:21:48 +02:00
}
}
}
2022-04-05 21:32:06 +02:00
var folderSummaryRemoveDeprecatedRe = regexp . MustCompile ( ` (Invalid|IgnorePatterns|StateChanged):\S+\s? ` )
2022-07-28 17:32:45 +02:00
func ( * verboseService ) formatEvent ( ev events . Event ) string {
2015-04-30 20:21:48 +02:00
switch ev . Type {
lib/api: Improve folder summary event, verbose service (#9370)
This makes a couple of small improvements to the folder summary
mechanism:
- The folder summary includes the local and remote sequence numbers in
clear text, rather than some odd sum that I'm not sure what it was
intended to represent.
- The folder summary event is generated when appropriate, regardless of
whether there is an event listener. We did this before because
generating it was expensive, and we wanted to avoid doing it
unnecessarily. Nowadays, however, it's mostly just reading out
pre-calculated metadata, and anyway, it's nice if it shows up reliably
when running with -verbose.
The point of all this is to make it easier to use these events to judge
when devices are, in fact, in sync. As-is, if I'm looking at two
devices, it's very difficult to reliably determine if they are in sync
or not. The reason is that while we can ask device A if it thinks it's
in sync, we can't see if the answer is "yes" because it has processed
all changes from B, or if it just doesn't know about the changes from B
yet. With proper sequence numbers in the event we can compare the two
and determine the truth. This makes testing a lot easier.
2024-01-31 08:24:39 +01:00
case events . DownloadProgress :
2015-04-30 20:21:48 +02:00
// Skip
return ""
case events . Starting :
return fmt . Sprintf ( "Starting up (%s)" , ev . Data . ( map [ string ] string ) [ "home" ] )
2016-05-19 02:19:26 +02:00
2015-04-30 20:21:48 +02:00
case events . StartupComplete :
return "Startup complete"
case events . DeviceDiscovered :
data := ev . Data . ( map [ string ] interface { } )
return fmt . Sprintf ( "Discovered device %v at %v" , data [ "device" ] , data [ "addrs" ] )
2016-05-19 02:19:26 +02:00
2015-04-30 20:21:48 +02:00
case events . DeviceConnected :
data := ev . Data . ( map [ string ] string )
2015-07-17 22:22:07 +02:00
return fmt . Sprintf ( "Connected to device %v at %v (type %s)" , data [ "id" ] , data [ "addr" ] , data [ "type" ] )
2016-05-19 02:19:26 +02:00
2015-04-30 20:21:48 +02:00
case events . DeviceDisconnected :
data := ev . Data . ( map [ string ] string )
return fmt . Sprintf ( "Disconnected from device %v" , data [ "id" ] )
case events . StateChanged :
data := ev . Data . ( map [ string ] interface { } )
return fmt . Sprintf ( "Folder %q is now %v" , data [ "folder" ] , data [ "to" ] )
2016-05-19 09:01:43 +02:00
case events . LocalChangeDetected :
2016-05-19 02:19:26 +02:00
data := ev . Data . ( map [ string ] string )
2016-05-19 09:01:43 +02:00
return fmt . Sprintf ( "Local change detected in folder %q: %s %s %s" , data [ "folder" ] , data [ "action" ] , data [ "type" ] , data [ "path" ] )
2016-05-19 02:19:26 +02:00
2016-12-21 17:35:20 +01:00
case events . RemoteChangeDetected :
data := ev . Data . ( map [ string ] string )
return fmt . Sprintf ( "Remote change detected in folder %q: %s %s %s" , data [ "folder" ] , data [ "action" ] , data [ "type" ] , data [ "path" ] )
lib/api: Improve folder summary event, verbose service (#9370)
This makes a couple of small improvements to the folder summary
mechanism:
- The folder summary includes the local and remote sequence numbers in
clear text, rather than some odd sum that I'm not sure what it was
intended to represent.
- The folder summary event is generated when appropriate, regardless of
whether there is an event listener. We did this before because
generating it was expensive, and we wanted to avoid doing it
unnecessarily. Nowadays, however, it's mostly just reading out
pre-calculated metadata, and anyway, it's nice if it shows up reliably
when running with -verbose.
The point of all this is to make it easier to use these events to judge
when devices are, in fact, in sync. As-is, if I'm looking at two
devices, it's very difficult to reliably determine if they are in sync
or not. The reason is that while we can ask device A if it thinks it's
in sync, we can't see if the answer is "yes" because it has processed
all changes from B, or if it just doesn't know about the changes from B
yet. With proper sequence numbers in the event we can compare the two
and determine the truth. This makes testing a lot easier.
2024-01-31 08:24:39 +01:00
case events . LocalIndexUpdated :
data := ev . Data . ( map [ string ] interface { } )
return fmt . Sprintf ( "Local index update for %q with %d items (seq: %d)" , data [ "folder" ] , data [ "items" ] , data [ "sequence" ] )
2015-04-30 20:21:48 +02:00
case events . RemoteIndexUpdated :
data := ev . Data . ( map [ string ] interface { } )
lib/api: Improve folder summary event, verbose service (#9370)
This makes a couple of small improvements to the folder summary
mechanism:
- The folder summary includes the local and remote sequence numbers in
clear text, rather than some odd sum that I'm not sure what it was
intended to represent.
- The folder summary event is generated when appropriate, regardless of
whether there is an event listener. We did this before because
generating it was expensive, and we wanted to avoid doing it
unnecessarily. Nowadays, however, it's mostly just reading out
pre-calculated metadata, and anyway, it's nice if it shows up reliably
when running with -verbose.
The point of all this is to make it easier to use these events to judge
when devices are, in fact, in sync. As-is, if I'm looking at two
devices, it's very difficult to reliably determine if they are in sync
or not. The reason is that while we can ask device A if it thinks it's
in sync, we can't see if the answer is "yes" because it has processed
all changes from B, or if it just doesn't know about the changes from B
yet. With proper sequence numbers in the event we can compare the two
and determine the truth. This makes testing a lot easier.
2024-01-31 08:24:39 +01:00
return fmt . Sprintf ( "Device %v sent an index update for %q with %d items (seq: %d)" , data [ "device" ] , data [ "folder" ] , data [ "items" ] , data [ "sequence" ] )
2015-04-30 20:21:48 +02:00
case events . DeviceRejected :
2018-10-16 01:09:24 +02:00
data := ev . Data . ( map [ string ] string )
2015-04-30 20:21:48 +02:00
return fmt . Sprintf ( "Rejected connection from device %v at %v" , data [ "device" ] , data [ "address" ] )
2016-05-19 02:19:26 +02:00
2015-04-30 20:21:48 +02:00
case events . FolderRejected :
data := ev . Data . ( map [ string ] string )
return fmt . Sprintf ( "Rejected unshared folder %q from device %v" , data [ "folder" ] , data [ "device" ] )
case events . ItemStarted :
2015-12-07 08:02:24 +01:00
data := ev . Data . ( map [ string ] string )
2015-04-30 20:21:48 +02:00
return fmt . Sprintf ( "Started syncing %q / %q (%v %v)" , data [ "folder" ] , data [ "item" ] , data [ "action" ] , data [ "type" ] )
2016-05-19 02:19:26 +02:00
2015-04-30 20:21:48 +02:00
case events . ItemFinished :
data := ev . Data . ( map [ string ] interface { } )
2015-06-09 09:04:03 +02:00
if err , ok := data [ "error" ] . ( * string ) ; ok && err != nil {
2015-05-27 11:14:39 +02:00
// If the err interface{} is not nil, it is a string pointer.
// Dereference it to get the actual error or Sprintf will print
// the pointer value....
2015-06-09 09:04:03 +02:00
return fmt . Sprintf ( "Finished syncing %q / %q (%v %v): %v" , data [ "folder" ] , data [ "item" ] , data [ "action" ] , data [ "type" ] , * err )
2015-04-30 20:21:48 +02:00
}
return fmt . Sprintf ( "Finished syncing %q / %q (%v %v): Success" , data [ "folder" ] , data [ "item" ] , data [ "action" ] , data [ "type" ] )
case events . ConfigSaved :
return "Configuration was saved"
case events . FolderCompletion :
data := ev . Data . ( map [ string ] interface { } )
lib/api: Improve folder summary event, verbose service (#9370)
This makes a couple of small improvements to the folder summary
mechanism:
- The folder summary includes the local and remote sequence numbers in
clear text, rather than some odd sum that I'm not sure what it was
intended to represent.
- The folder summary event is generated when appropriate, regardless of
whether there is an event listener. We did this before because
generating it was expensive, and we wanted to avoid doing it
unnecessarily. Nowadays, however, it's mostly just reading out
pre-calculated metadata, and anyway, it's nice if it shows up reliably
when running with -verbose.
The point of all this is to make it easier to use these events to judge
when devices are, in fact, in sync. As-is, if I'm looking at two
devices, it's very difficult to reliably determine if they are in sync
or not. The reason is that while we can ask device A if it thinks it's
in sync, we can't see if the answer is "yes" because it has processed
all changes from B, or if it just doesn't know about the changes from B
yet. With proper sequence numbers in the event we can compare the two
and determine the truth. This makes testing a lot easier.
2024-01-31 08:24:39 +01:00
return fmt . Sprintf ( "Completion for folder %q on device %v is %v%% (state: %s, seq: %d)" , data [ "folder" ] , data [ "device" ] , data [ "completion" ] , data [ "remoteState" ] , data [ "sequence" ] )
2016-05-19 02:19:26 +02:00
2015-04-30 20:21:48 +02:00
case events . FolderSummary :
2022-04-05 21:32:06 +02:00
data := ev . Data . ( model . FolderSummaryEventData )
return folderSummaryRemoveDeprecatedRe . ReplaceAllString ( fmt . Sprintf ( "Summary for folder %q is %+v" , data . Folder , data . Summary ) , "" )
2016-05-19 02:19:26 +02:00
2015-08-27 21:37:41 +02:00
case events . FolderScanProgress :
data := ev . Data . ( map [ string ] interface { } )
folder := data [ "folder" ] . ( string )
current := data [ "current" ] . ( int64 )
total := data [ "total" ] . ( int64 )
2015-11-17 21:08:36 +01:00
rate := data [ "rate" ] . ( float64 ) / 1024 / 1024
2015-08-27 21:41:39 +02:00
var pct int64
if total > 0 {
pct = 100 * current / total
}
2017-01-03 15:03:18 +01:00
return fmt . Sprintf ( "Scanning folder %q, %d%% done (%.01f MiB/s)" , folder , pct , rate )
2015-08-23 21:56:10 +02:00
case events . DevicePaused :
data := ev . Data . ( map [ string ] string )
device := data [ "device" ]
return fmt . Sprintf ( "Device %v was paused" , device )
2016-05-19 02:19:26 +02:00
2015-08-23 21:56:10 +02:00
case events . DeviceResumed :
data := ev . Data . ( map [ string ] string )
device := data [ "device" ]
return fmt . Sprintf ( "Device %v was resumed" , device )
2016-05-19 02:19:26 +02:00
2022-04-22 08:42:20 +02:00
case events . ClusterConfigReceived :
data := ev . Data . ( model . ClusterConfigReceivedEventData )
return fmt . Sprintf ( "Received ClusterConfig from device %v" , data . Device )
2016-12-21 19:41:25 +01:00
case events . FolderPaused :
data := ev . Data . ( map [ string ] string )
id := data [ "id" ]
label := data [ "label" ]
return fmt . Sprintf ( "Folder %v (%v) was paused" , id , label )
case events . FolderResumed :
data := ev . Data . ( map [ string ] string )
id := data [ "id" ]
label := data [ "label" ]
return fmt . Sprintf ( "Folder %v (%v) was resumed" , id , label )
2016-05-04 21:38:12 +02:00
case events . ListenAddressesChanged :
2016-04-10 21:36:38 +02:00
data := ev . Data . ( map [ string ] interface { } )
2016-05-04 21:38:12 +02:00
address := data [ "address" ]
lan := data [ "lan" ]
wan := data [ "wan" ]
return fmt . Sprintf ( "Listen address %s resolution has changed: lan addresses: %s wan addresses: %s" , address , lan , wan )
2016-05-19 02:19:26 +02:00
2015-11-08 21:05:36 +01:00
case events . LoginAttempt :
data := ev . Data . ( map [ string ] interface { } )
username := data [ "username" ] . ( string )
var success string
if data [ "success" ] . ( bool ) {
success = "successful"
} else {
success = "failed"
}
return fmt . Sprintf ( "Login %s for username %s." , success , username )
2015-04-30 20:21:48 +02:00
}
return fmt . Sprintf ( "%s %#v" , ev . Type , ev )
}
2019-11-21 08:41:15 +01:00
func ( s * verboseService ) String ( ) string {
return fmt . Sprintf ( "verboseService@%p" , s )
}