Add pretty byte size output to download log

This commit is contained in:
Tony Blyler 2016-05-13 19:03:55 -04:00
parent 6899802247
commit 3ebb4798dc
No known key found for this signature in database
GPG key ID: 25C9D3A655D1A65C

View file

@ -11,6 +11,7 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -49,6 +50,42 @@ type Queue struct {
lock sync.RWMutex lock sync.RWMutex
} }
var prettyBytesValues = []int64{
1024,
1024 * 1024,
1024 * 1024 * 1024,
1024 * 1024 * 1024 * 1024,
1024 * 1024 * 1024 * 1024 * 1024,
1024 * 1024 * 1024 * 1024 * 1024 * 1024,
}
var prettyBytesNames = []string{
"KiB",
"MiB",
"GiB",
"TiB",
"PiB",
"EiB",
}
func prettyBytes(bytes int64) string {
output := strconv.FormatInt(bytes, 10) + "B"
for i, divisor := range prettyBytesValues {
newBytes := bytes / divisor
if newBytes > 1024 {
continue
}
if newBytes < 1 {
break
}
output = strconv.FormatInt(newBytes, 10) + prettyBytesNames[i]
}
return output
}
// NewQueue establishes all connections and watchers // NewQueue establishes all connections and watchers
func NewQueue(config *Config, logger *log.Logger) (*Queue, error) { func NewQueue(config *Config, logger *log.Logger) (*Queue, error) {
if config.WatchDownloadPaths == nil || len(config.WatchDownloadPaths) == 0 { if config.WatchDownloadPaths == nil || len(config.WatchDownloadPaths) == 0 {
@ -235,7 +272,7 @@ func (q *Queue) downloadTorrents(torrents []rtorrent.Torrent) {
} }
go func(torrentFilePath string, downloadPath string, torrent rtorrent.Torrent) { go func(torrentFilePath string, downloadPath string, torrent rtorrent.Torrent) {
q.logger.Printf("Downloading '%s' (%s) to '%s' (%s)", torrent.Name, torrentFilePath, downloadPath, destDownloadPath) q.logger.Printf("Downloading '%s' (%s) to '%s' (%s) %s", torrent.Name, torrentFilePath, downloadPath, destDownloadPath, prettyBytes(int64(torrent.Size)))
err := q.sftpClient.Mirror(torrent.Path, downloadPath) err := q.sftpClient.Mirror(torrent.Path, downloadPath)
if err != nil { if err != nil {
q.logger.Printf("Failed to download '%s' to '%s' error '%s'", torrent.Path, downloadPath, err) q.logger.Printf("Failed to download '%s' to '%s' error '%s'", torrent.Path, downloadPath, err)