Improve logging

This commit is contained in:
Neeraj Gupta 2023-10-16 23:26:31 +05:30
parent 6285220c1b
commit ae1b191765
2 changed files with 16 additions and 1 deletions

View file

@ -4,6 +4,7 @@ import (
"archive/zip"
"cli-go/internal/crypto"
"cli-go/pkg/model"
"cli-go/utils"
"cli-go/utils/encoding"
"context"
"fmt"
@ -21,7 +22,7 @@ func (c *ClICtrl) downloadAndDecrypt(
) (*string, error) {
dir := c.tempFolder
downloadPath := fmt.Sprintf("%s/%d", dir, file.ID)
log.Printf("Downloading file %d to %s", file.ID, downloadPath)
log.Printf("Downloading %s (%s)", file.GetTitle(), utils.ByteCountDecimal(file.Info.FileSize))
err := c.Client.DownloadFile(ctx, file.ID, downloadPath)
if err != nil {
return nil, fmt.Errorf("error downloading file %d: %w", file.ID, err)

View file

@ -1,6 +1,7 @@
package utils
import (
"fmt"
"log"
"time"
)
@ -9,3 +10,16 @@ func TimeTrack(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s", name, elapsed)
}
func ByteCountDecimal(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}