1
0
Fork 0
mirror of https://github.com/Luzifer/cloudbox.git synced 2024-12-22 18:51:21 +00:00

Make change human readable in logs

This commit is contained in:
Knut Ahlers 2019-06-16 23:03:05 +02:00
parent d65571a245
commit f4227214de
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 73 additions and 45 deletions

69
sync/change.go Normal file
View file

@ -0,0 +1,69 @@
package sync
import "strings"
type Change uint8
const (
ChangeLocalAdd Change = 1 << iota
ChangeLocalDelete
ChangeLocalUpdate
ChangeRemoteAdd
ChangeRemoteDelete
ChangeRemoteUpdate
)
var changeNameMap = map[Change]string{
ChangeLocalAdd: "local-add",
ChangeLocalDelete: "local-delete",
ChangeLocalUpdate: "local-update",
ChangeRemoteAdd: "remote-add",
ChangeRemoteDelete: "remote-delete",
ChangeRemoteUpdate: "remote-update",
}
func (c Change) Changed() bool {
return c != 0
}
func (c *Change) Register(add Change) {
*c = *c | add
}
func (c Change) HasAll(test ...Change) bool {
for _, t := range test {
if c&t == 0 {
return false
}
}
return true
}
func (c Change) HasOne(test ...Change) bool {
for _, t := range test {
if c&t != 0 {
return true
}
}
return false
}
func (c Change) Is(test Change) bool {
return c == test
}
func (c Change) String() string {
if !c.Changed() {
return "none"
}
names := []string{}
for k, v := range changeNameMap {
if c.HasOne(k) {
names = append(names, v)
}
}
return strings.Join(names, ", ")
}

View file

@ -19,7 +19,7 @@ func (s *Sync) decideAction(syncState *state, fileName string) error {
logger.Warn("File has local and remote updates, sync not possible") logger.Warn("File has local and remote updates, sync not possible")
case change.HasAll(ChangeLocalAdd, ChangeRemoteAdd): case change.HasAll(ChangeLocalAdd, ChangeRemoteAdd):
// Special case: Both are added, check thet are the same file or break // Special case: Both are added, check they are the same file or leave this to manual resolve
logger.Debug("File added locally as well as remotely") logger.Debug("File added locally as well as remotely")
// TODO: Handle special case // TODO: Handle special case
@ -62,8 +62,9 @@ func (s *Sync) decideAction(syncState *state, fileName string) error {
} }
default: default:
// Unhandled case // Unhandled case (i.e. human screwed around in sync process)
logger.WithField("change", change).Warn("Unhandled change case") // Stuff like: LocalUpdate + RemoteDelete, ...
logger.WithField("change", change.String()).Warn("Unhandled change case, sync not possible")
} }
return nil return nil

View file

@ -8,48 +8,6 @@ import (
"github.com/Luzifer/cloudbox/providers" "github.com/Luzifer/cloudbox/providers"
) )
type Change uint8
const (
ChangeLocalAdd Change = 1 << iota
ChangeLocalDelete
ChangeLocalUpdate
ChangeRemoteAdd
ChangeRemoteDelete
ChangeRemoteUpdate
)
func (c Change) Changed() bool {
return c != 0
}
func (c *Change) Register(add Change) {
*c = *c | add
}
func (c Change) HasAll(test ...Change) bool {
for _, t := range test {
if c&t == 0 {
return false
}
}
return true
}
func (c Change) HasOne(test ...Change) bool {
for _, t := range test {
if c&t != 0 {
return true
}
}
return false
}
func (c Change) Is(test Change) bool {
return c == test
}
const ( const (
sideLocal string = "local" sideLocal string = "local"
sideRemote string = "remote" sideRemote string = "remote"