Files
kopia/cli/command_logs_show.go
Jarek Kowalski d84c884321 Added content manager internal logging (#1116)
* logging: added logger wrappers for Broadcast and Prefix

* nit: moved max hash size to a named constant

* content: added internal logger

* content: replaced context-based logging with explicit Loggers

This will capture the logger.Logger associated with the context when
the repository is opened and will reuse it for all logs instead of
creating new logger for each log message.

The new logger will also write logs to the internal logger in addition
to writing to a log file/console.

* cli: allow decrypting all blobs whose names start with _

* maintenance: added logs cleanup

* cli: commands to view logs

* cli: log selected command on each write session
2021-06-05 08:48:43 -07:00

80 lines
1.7 KiB
Go

package cli
import (
"bytes"
"context"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
)
type commandLogsShow struct {
logSessionIDs []string
crit logSelectionCriteria
out textOutput
}
func (c *commandLogsShow) setup(svc appServices, parent commandParent) {
cmd := parent.Command("show", "Show contents of the log.").Alias("cat")
cmd.Arg("session-id", "Log Session ID to show").StringsVar(&c.logSessionIDs)
cmd.Action(svc.directRepositoryReadAction(c.run))
c.crit.setup(cmd)
c.out.setup(svc)
}
func (c *commandLogsShow) run(ctx context.Context, rep repo.DirectRepository) error {
sessions, err := getLogSessions(ctx, rep.BlobReader())
if err != nil {
return err
}
sessions = c.crit.filterLogSessions(sessions)
if len(c.logSessionIDs) > 0 {
sessions = filterLogSessions(sessions, func(l *logSessionInfo) bool {
for _, sid := range c.logSessionIDs {
if l.id == sid {
return true
}
}
return false
})
}
if len(sessions) == 0 {
return errors.Errorf("no logs found")
}
// by default show latest one
if !c.crit.any() {
sessions = sessions[len(sessions)-1:]
log(ctx).Infof("Showing latest log (%v)", formatTimestamp(sessions[0].startTime))
}
for _, s := range sessions {
for _, bm := range s.segments {
data, err := rep.BlobReader().GetBlob(ctx, bm.BlobID, 0, -1)
if err != nil {
return errors.Wrap(err, "error getting log")
}
data, err = rep.Crypter().DecryptBLOB(data, bm.BlobID)
if err != nil {
return errors.Wrap(err, "error decrypting log")
}
if err := showContentWithFlags(c.out.stdout(), bytes.NewReader(data), true, false); err != nil {
log(ctx).Errorf("error showing log: %v", err)
}
}
}
return nil
}