Skip to content

Commit d7d16fd

Browse files
authored
Create stat.go
1 parent ab22926 commit d7d16fd

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

stat.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"sync/atomic"
7+
)
8+
9+
var upload_size int64
10+
var download_size int64
11+
12+
func calcUnit(cnt int64) string {
13+
if cnt < 1024 {
14+
return fmt.Sprintf("%d B", cnt)
15+
} else if cnt < 1024*1024 {
16+
return fmt.Sprintf("%.2f KB", float32(cnt)/1024)
17+
} else if cnt < 1024*1024*1024 {
18+
return fmt.Sprintf("%.2f MB", float32(cnt)/(1024*1024))
19+
} else {
20+
return fmt.Sprintf("%.2f GB", float32(cnt)/(1024*1024*1024))
21+
}
22+
}
23+
24+
func StatUpdate(up int64, down int64) {
25+
atomic.AddInt64(&upload_size, up)
26+
atomic.AddInt64(&download_size, down)
27+
log.SetPrefix(fmt.Sprintf("[UP:%s DOWN:%s] ", calcUnit(upload_size), calcUnit(download_size)))
28+
}

0 commit comments

Comments
 (0)