diff --git a/common/stats/session/session.go b/common/stats/session/session.go index b832d7e..114e5e3 100644 --- a/common/stats/session/session.go +++ b/common/stats/session/session.go @@ -9,9 +9,6 @@ import ( "sync" "sync/atomic" - "golang.org/x/text/language" - "golang.org/x/text/message" - "github.com/xjasonlyu/tun2socks/common/log" "github.com/xjasonlyu/tun2socks/common/stats" ) @@ -48,14 +45,13 @@ func (s *simpleSessionStater) Start() error { return true }) - p := message.NewPrinter(language.English) tablePrint := func(w io.Writer, sessions []*stats.Session) { // Sort by session start time. sort.Slice(sessions, func(i, j int) bool { return sessions[i].SessionStart.Sub(sessions[j].SessionStart) < 0 }) _, _ = fmt.Fprintf(w, "") - _, _ = fmt.Fprintf(w, "") + _, _ = fmt.Fprintf(w, "") sort.Slice(sessions, func(i, j int) bool { return sessions[i].SessionStart.After(sessions[j].SessionStart) }) @@ -68,8 +64,8 @@ func (s *simpleSessionStater) Start() error { duration(sess.SessionStart, sess.SessionClose), sess.ClientAddr, sess.TargetAddr, - p.Sprintf("%d", atomic.LoadInt64(&sess.UploadBytes)), - p.Sprintf("%d", atomic.LoadInt64(&sess.DownloadBytes)), + byteCountSI(atomic.LoadInt64(&sess.UploadBytes)), + byteCountSI(atomic.LoadInt64(&sess.DownloadBytes)), ) } _, _ = fmt.Fprintf(w, "
Process NameNetworkDateDurationClient AddrTarget AddrUpload BytesDownload Bytes
Process NameNetworkDateDurationClient AddrTarget AddrUploadDownload
") diff --git a/common/stats/session/utils.go b/common/stats/session/utils.go index 0728973..df02818 100644 --- a/common/stats/session/utils.go +++ b/common/stats/session/utils.go @@ -110,3 +110,31 @@ func diff(a, b time.Time) (year, month, day, hour, min, sec int) { return } + +func byteCountSI(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]) +} + +func byteCountIEC(b int64) string { + const unit = 1024 + 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 %ciB", + float64(b)/float64(div), "KMGTPE"[exp]) +}