dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 81b89cdfc12788e73f5c06e221bafb3ef31bb508
parent 393dcf4279b75a2ed04856cc9609aa8f5eef1358
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon, 30 Jan 2023 23:33:25 -0800

improve logs

Diffstat:
Mcmd/dkfupload/main.go | 40++++++++++++++++++++++++++++++++++++++--
Mpkg/web/handlers/handlers.go | 2+-
2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/cmd/dkfupload/main.go b/cmd/dkfupload/main.go @@ -92,12 +92,15 @@ func main() { log.Printf(" chunks size: %s (%s)\n", humanize.Bytes(uint64(maxChunkSize)), humanize.Comma(maxChunkSize)) log.Printf(" nb chunks: %d\n", nbChunks) log.Printf(" nb threads: %d\n", nbThreads) + log.Printf(" http timeout: %s\n", ShortDur(httpTimeout)) if dry { log.Printf(" dry run: %t\n", dry) } log.Println(strings.Repeat("-", 80)) } + start := time.Now() + // Init the filedrop and send metadata about the file if !dry { log.Println("sending metadata") @@ -140,7 +143,7 @@ func main() { // Wait for all workers to have completed wg.Wait() - log.Printf("All done\n") + log.Printf("All done in %s\n", ShortDur(time.Since(start))) } func work(i int, wg *sync.WaitGroup, chunksCh chan int64, isLocal, dry bool, maxChunkSize, nbChunks int64, f *os.File, baseUrl, filedropUUID string, httpTimeout time.Duration) { @@ -187,7 +190,7 @@ func work(i int, wg *sync.WaitGroup, chunksCh chan int64, isLocal, dry bool, max }) } newChunksCompleted := atomic.AddInt64(&chunksCompleted, 1) - log.Printf("Thread #%03d | chunk #%03d | completed in %s (%d/%d)\n", i, chunkNum, time.Since(start), newChunksCompleted, nbChunks) + log.Printf("Thread #%03d | chunk #%03d | completed in %s (%d/%d)\n", i, chunkNum, ShortDur(time.Since(start)), newChunksCompleted, nbChunks) } wg.Done() } @@ -254,3 +257,36 @@ func Ternary[T any](predicate bool, a, b T) T { } return b } + +func ShortDur(v interface{}) string { + if d, ok := v.(time.Duration); ok { + if d < time.Minute { + d = d.Round(time.Millisecond) + } else { + d = d.Round(time.Second) + } + s := d.String() + if strings.HasSuffix(s, "m0s") { + s = s[:len(s)-2] + } + if strings.HasSuffix(s, "h0m") { + s = s[:len(s)-2] + } + return s + } else if d, ok := v.(time.Time); ok { + return ShortDur(time.Until(d)) + } else if d, ok := v.(uint64); ok { + return ShortDur(time.Duration(d) * time.Second) + } else if d, ok := v.(int64); ok { + return ShortDur(time.Duration(d) * time.Second) + } else if d, ok := v.(int32); ok { + return ShortDur(time.Duration(d) * time.Second) + } else if d, ok := v.(int64); ok { + return ShortDur(time.Duration(d) * time.Second) + } else if d, ok := v.(float32); ok { + return ShortDur(time.Duration(d) * time.Second) + } else if d, ok := v.(float64); ok { + return ShortDur(time.Duration(d) * time.Second) + } + return "n/a" +} diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -4376,7 +4376,7 @@ func FileDropTmpInitHandler(c echo.Context) error { fileName := c.Request().PostFormValue("fileName") fileSize := c.Request().PostFormValue("fileSize") fileSha256 := c.Request().PostFormValue("fileSha256") - data := []byte(fileName + "\n" + fileSize + "\n" + fileSha256) + data := []byte(fileName + "\n" + fileSize + "\n" + fileSha256 + "\n") if err := os.WriteFile(filepath.Join(config.Global.ProjectFiledropPath(), filedropUUID, "metadata"), data, 0644); err != nil { logrus.Error(err) return c.NoContent(http.StatusInternalServerError)