Skip to content

Commit

Permalink
chore(prj): add some logs
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Jan 30, 2023
1 parent aea16ac commit ff6f71d
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 11 deletions.
32 changes: 28 additions & 4 deletions app/dl/dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
"github.com/iyear/tdl/pkg/downloader"
"github.com/iyear/tdl/pkg/key"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/logger"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/viper"
"go.uber.org/multierr"
"go.uber.org/zap"
"time"
)

Expand Down Expand Up @@ -66,6 +68,8 @@ func Run(ctx context.Context, opts *Options) error {
if err != nil {
return err
}
logger.From(ctx).Debug("Collect dialogs",
zap.Any("dialogs", dialogs))

iter, err := dliter.New(&dliter.Options{
Pool: pool,
Expand All @@ -85,7 +89,7 @@ func Run(ctx context.Context, opts *Options) error {
}
defer func() { // save progress
if rerr != nil { // download is interrupted
multierr.AppendInto(&rerr, saveProgress(kvd, iter))
multierr.AppendInto(&rerr, saveProgress(ctx, kvd, iter))
} else { // if finished, we should clear resume key
multierr.AppendInto(&rerr, kvd.Delete(key.Resume(iter.Fingerprint())))
}
Expand All @@ -100,7 +104,17 @@ func Run(ctx context.Context, opts *Options) error {
Threads: viper.GetInt(consts.FlagThreads),
Iter: iter,
}
return downloader.New(options).Download(ctx, viper.GetInt(consts.FlagLimit))
limit := viper.GetInt(consts.FlagLimit)

logger.From(ctx).Info("Start download",
zap.String("dir", options.Dir),
zap.Bool("rewrite_ext", options.RewriteExt),
zap.Bool("skip_same", options.SkipSame),
zap.Int("part_size", options.PartSize),
zap.Int("threads", options.Threads),
zap.Int("limit", limit))

return downloader.New(options).Download(ctx, limit)
})
}

Expand All @@ -117,6 +131,9 @@ func collectDialogs(ctx context.Context, pool dcpool.Pool, kvd kv.KV, parsers []
}

func resume(ctx context.Context, kvd kv.KV, iter *dliter.Iter) error {
logger.From(ctx).Debug("Check resume key",
zap.String("fingerprint", iter.Fingerprint()))

b, err := kvd.Get(key.Resume(iter.Fingerprint()))
if err != nil && !errors.Is(err, kv.ErrNotFound) {
return err
Expand All @@ -142,6 +159,9 @@ func resume(ctx context.Context, kvd kv.KV, iter *dliter.Iter) error {
return err
}

logger.From(ctx).Debug("Resume download",
zap.Int("finished", len(finished)))

if !confirm {
// clear resume key
return kvd.Delete(key.Resume(iter.Fingerprint()))
Expand All @@ -151,8 +171,12 @@ func resume(ctx context.Context, kvd kv.KV, iter *dliter.Iter) error {
return nil
}

func saveProgress(kvd kv.KV, it *dliter.Iter) error {
b, err := json.Marshal(it.Finished())
func saveProgress(ctx context.Context, kvd kv.KV, it *dliter.Iter) error {
finished := it.Finished()
logger.From(ctx).Debug("Save progress",
zap.Int("finished", len(finished)))

b, err := json.Marshal(finished)
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions app/dl/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"github.com/iyear/tdl/app/internal/dliter"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/logger"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/utils"
"github.com/mitchellh/mapstructure"
"go.uber.org/zap"
"io"
"os"
"strconv"
Expand Down Expand Up @@ -42,6 +44,9 @@ func parseFiles(ctx context.Context, pool dcpool.Pool, kvd kv.KV, files []string
return nil, err
}

logger.From(ctx).Debug("Parse file",
zap.String("file", file),
zap.Int("num", len(d.Messages)))
dialogs = append(dialogs, d)
}

Expand All @@ -61,6 +66,9 @@ func parseFile(ctx context.Context, client *tg.Client, kvd kv.KV, file string) (
if err != nil {
return nil, err
}
logger.From(ctx).Debug("Got peer info",
zap.Int64("id", peer.ID()),
zap.String("name", peer.VisibleName()))

if _, err = f.Seek(0, io.SeekStart); err != nil {
return nil, err
Expand Down
7 changes: 7 additions & 0 deletions app/dl/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"github.com/iyear/tdl/app/internal/dliter"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/logger"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/utils"
"go.uber.org/zap"
)

func parseURLs(ctx context.Context, pool dcpool.Pool, kvd kv.KV, urls []string) ([]*dliter.Dialog, error) {
Expand All @@ -20,6 +22,11 @@ func parseURLs(ctx context.Context, pool dcpool.Pool, kvd kv.KV, urls []string)
if err != nil {
return nil, err
}
logger.From(ctx).Debug("Parse URL",
zap.String("url", u),
zap.Int64("peer_id", ch.ID()),
zap.String("peer_name", ch.VisibleName()),
zap.Int("msg", msgid))

// init map value
if _, ok := msgMap[ch.ID()]; !ok {
Expand Down
6 changes: 6 additions & 0 deletions app/internal/tgc/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"github.com/gotd/td/telegram"
"github.com/iyear/tdl/pkg/logger"
"go.uber.org/zap"
)

func RunWithAuth(ctx context.Context, client *telegram.Client, f func(ctx context.Context) error) error {
Expand All @@ -16,6 +18,10 @@ func RunWithAuth(ctx context.Context, client *telegram.Client, f func(ctx contex
return fmt.Errorf("not authorized. please login first")
}

logger.From(ctx).Info("Authorized",
zap.Int64("id", status.User.ID),
zap.String("username", status.User.Username))

return f(ctx)
})
}
6 changes: 6 additions & 0 deletions app/internal/tgc/tgc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/utils"
"github.com/spf13/viper"
"go.uber.org/zap"
"time"
)

Expand Down Expand Up @@ -44,6 +45,11 @@ func New(ctx context.Context, login bool, middlewares ...telegram.Middleware) (*
return nil, nil, fmt.Errorf("can't find app: %s, please try re-login", mode)
}

logger.From(ctx).Info("New telegram client",
zap.Int("app", app.AppID),
zap.String("mode", string(mode)),
zap.Bool("is_login", login))

return telegram.NewClient(app.AppID, app.AppHash, telegram.Options{
Resolver: dcs.Plain(dcs.PlainOptions{
Dial: utils.Proxy.GetDial(viper.GetString(consts.FlagProxy)).DialContext,
Expand Down
3 changes: 2 additions & 1 deletion cmd/chat/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chat

import (
"github.com/iyear/tdl/app/chat"
"github.com/iyear/tdl/pkg/logger"
"github.com/spf13/cobra"
"time"
)
Expand All @@ -25,7 +26,7 @@ var cmdExport = &cobra.Command{
expOpts.From, expOpts.To = expOpts.To, expOpts.From
}

return chat.Export(cmd.Context(), expOpts)
return chat.Export(logger.Named(cmd.Context(), "export"), expOpts)
},
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/chat/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package chat

import (
"github.com/iyear/tdl/app/chat"
"github.com/iyear/tdl/pkg/logger"
"github.com/spf13/cobra"
)

var cmdList = &cobra.Command{
Use: "ls",
Short: "List your chats",
RunE: func(cmd *cobra.Command, args []string) error {
return chat.List(cmd.Context())
return chat.List(logger.Named(cmd.Context(), "ls"))
},
}
3 changes: 2 additions & 1 deletion cmd/dl/dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"github.com/iyear/tdl/app/dl"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
Expand All @@ -27,7 +28,7 @@ var Cmd = &cobra.Command{
}

opts.Template = viper.GetString(consts.FlagDlTemplate)
return dl.Run(cmd.Context(), opts)
return dl.Run(logger.Named(cmd.Context(), "dl"), opts)
},
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/fatih/color"
"github.com/iyear/tdl/app/login"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/logger"
"github.com/spf13/cobra"
)

Expand All @@ -20,7 +21,7 @@ var Cmd = &cobra.Command{
if desktop != "" {
return login.Desktop(cmd.Context(), desktop, passcode)
}
return login.Code(cmd.Context())
return login.Code(logger.Named(cmd.Context(), "login"))
},
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ var cmd = &cobra.Command{
if debug {
level = zap.DebugLevel
}
cmd.SetContext(logger.With(cmd.Context(), logger.New(level).Named("tdl")))
cmd.SetContext(logger.With(cmd.Context(), logger.New(level)))

ns := viper.GetString(consts.FlagNamespace)
if ns != "" {
color.Cyan("Namespace: %s", ns)
logger.From(cmd.Context()).Info("Namespace",
zap.String("namespace", ns))
}
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
Expand Down
3 changes: 2 additions & 1 deletion cmd/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package up
import (
"github.com/iyear/tdl/app/up"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/logger"
"github.com/spf13/cobra"
)

Expand All @@ -13,7 +14,7 @@ var Cmd = &cobra.Command{
Aliases: []string{"upload"},
Short: "Upload anything to Telegram",
RunE: func(cmd *cobra.Command, args []string) error {
return up.Run(cmd.Context(), opts)
return up.Run(logger.Named(cmd.Context(), "up"), opts)
},
}

Expand Down
9 changes: 8 additions & 1 deletion pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"github.com/gabriel-vasile/mimetype"
"github.com/gotd/td/telegram/downloader"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/logger"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/utils"
"github.com/jedib0t/go-pretty/v6/progress"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"os"
"path/filepath"
Expand Down Expand Up @@ -56,7 +58,7 @@ func New(opts *Options) *Downloader {
}

func (d *Downloader) Download(ctx context.Context, limit int) error {
d.pw.Log(color.GreenString("All files will be downloaded to '%s' dir", d.dir))
color.Green("All files will be downloaded to '%s' dir", d.dir)

total := d.iter.Total(ctx)
d.pw.SetNumTrackersExpected(total)
Expand All @@ -71,6 +73,8 @@ func (d *Downloader) Download(ctx context.Context, limit int) error {
wg.Go(func() (rerr error) {
item, err := d.iter.Next(errctx)
if err != nil {
logger.From(errctx).Debug("Iter next failed",
zap.String("error", err.Error()))
// skip error means we don't need to log error
if !errors.Is(err, ErrSkip) && !errors.Is(err, context.Canceled) {
d.pw.Log(color.RedString("failed: %v", err))
Expand Down Expand Up @@ -119,6 +123,9 @@ func (d *Downloader) download(ctx context.Context, item *Item) error {
default:
}

logger.From(ctx).Debug("Start download item",
zap.Any("item", item))

name := replacer.Replace(item.Name)
if d.skipSame {
if stat, err := os.Stat(filepath.Join(d.dir, name)); err == nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func With(ctx context.Context, logger *zap.Logger) context.Context {
return context.WithValue(ctx, ctxKey{}, logger)
}

func Named(ctx context.Context, name string) context.Context {
return With(ctx, From(ctx).Named(name))
}

func New(level zapcore.LevelEnabler) *zap.Logger {
rotate := &lumberjack.Logger{
Filename: "log/latest.log",
Expand Down

0 comments on commit ff6f71d

Please sign in to comment.