2018-12-03 04:39:13 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Copyright (C) Microsoft. All rights reserved.
|
|
|
|
// Licensed under the MIT license.
|
|
|
|
// See LICENSE.txt file in the project root for full license information.
|
|
|
|
//-----------------------------------------------------------------------------
|
2019-10-08 05:24:01 +08:00
|
|
|
package ethrLog
|
2018-12-03 04:39:13 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
2019-10-08 05:24:01 +08:00
|
|
|
|
|
|
|
"github.com/microsoft/ethr/utils"
|
2018-12-03 04:39:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type logMessage struct {
|
2019-01-21 01:13:47 +08:00
|
|
|
Time string
|
2018-12-03 04:39:13 +08:00
|
|
|
Type string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
type logLatencyData struct {
|
|
|
|
Time string
|
|
|
|
Type string
|
|
|
|
RemoteAddr string
|
|
|
|
Protocol string
|
|
|
|
Avg string
|
|
|
|
Min string
|
|
|
|
P50 string
|
|
|
|
P90 string
|
|
|
|
P95 string
|
|
|
|
P99 string
|
|
|
|
P999 string
|
|
|
|
P9999 string
|
|
|
|
Max string
|
|
|
|
}
|
|
|
|
|
|
|
|
type logTestResults struct {
|
2019-01-21 01:13:47 +08:00
|
|
|
Time string
|
2018-12-03 04:39:13 +08:00
|
|
|
Type string
|
|
|
|
RemoteAddr string
|
|
|
|
Protocol string
|
|
|
|
BitsPerSecond string
|
|
|
|
ConnectionsPerSecond string
|
|
|
|
PacketsPerSecond string
|
|
|
|
AverageLatency string
|
|
|
|
}
|
|
|
|
|
|
|
|
var loggingActive = false
|
|
|
|
var logChan = make(chan string, 64)
|
|
|
|
|
2019-10-08 05:24:01 +08:00
|
|
|
func LogInit(fileName string) {
|
2018-12-03 04:39:13 +08:00
|
|
|
if fileName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logFile, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Unable to open the log file %s, Error: %v", fileName, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.SetFlags(0)
|
|
|
|
log.SetOutput(logFile)
|
|
|
|
loggingActive = true
|
|
|
|
go runLogger(logFile)
|
|
|
|
}
|
|
|
|
|
2019-10-08 05:24:01 +08:00
|
|
|
func LogFini() {
|
2018-12-03 04:39:13 +08:00
|
|
|
loggingActive = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLogger(logFile *os.File) {
|
|
|
|
for loggingActive {
|
|
|
|
s := <-logChan
|
|
|
|
log.Println(s)
|
|
|
|
}
|
|
|
|
logFile.Close()
|
|
|
|
}
|
|
|
|
|
2019-10-08 05:24:01 +08:00
|
|
|
func logMsg(prefix, msg string) {
|
2018-12-03 04:39:13 +08:00
|
|
|
if loggingActive {
|
|
|
|
logData := logMessage{}
|
2019-01-21 01:13:47 +08:00
|
|
|
logData.Time = time.Now().UTC().Format(time.RFC3339)
|
2018-12-03 04:39:13 +08:00
|
|
|
logData.Type = prefix
|
|
|
|
logData.Message = msg
|
2018-12-11 23:28:33 +08:00
|
|
|
logJSON, _ := json.Marshal(logData)
|
|
|
|
logChan <- string(logJSON)
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 05:24:01 +08:00
|
|
|
func Info(msg string) {
|
|
|
|
logMsg("INFO", msg)
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
|
2019-10-08 05:24:01 +08:00
|
|
|
func Error(msg string) {
|
|
|
|
logMsg("ERROR", msg)
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
|
2019-10-08 05:24:01 +08:00
|
|
|
func Debug(msg string) {
|
|
|
|
logMsg("DEBUG", msg)
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
|
2019-10-08 05:24:01 +08:00
|
|
|
func LogResults(s []string) {
|
2018-12-03 04:39:13 +08:00
|
|
|
if loggingActive {
|
|
|
|
logData := logTestResults{}
|
2019-01-21 01:13:47 +08:00
|
|
|
logData.Time = time.Now().UTC().Format(time.RFC3339)
|
2018-12-03 04:39:13 +08:00
|
|
|
logData.Type = "TestResult"
|
|
|
|
logData.RemoteAddr = s[0]
|
|
|
|
logData.Protocol = s[1]
|
|
|
|
logData.BitsPerSecond = s[2]
|
|
|
|
logData.ConnectionsPerSecond = s[3]
|
|
|
|
logData.PacketsPerSecond = s[4]
|
|
|
|
logData.AverageLatency = s[5]
|
2018-12-11 23:28:33 +08:00
|
|
|
logJSON, _ := json.Marshal(logData)
|
|
|
|
logChan <- string(logJSON)
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 05:24:01 +08:00
|
|
|
func LogLatency(remoteAddr, proto string, avg, min, p50, p90, p95, p99, p999, p9999, max time.Duration) {
|
2018-12-03 04:39:13 +08:00
|
|
|
if loggingActive {
|
|
|
|
logData := logLatencyData{}
|
|
|
|
logData.Time = time.Now().UTC().Format(time.RFC3339)
|
|
|
|
logData.Type = "LatencyResult"
|
|
|
|
logData.RemoteAddr = remoteAddr
|
|
|
|
logData.Protocol = proto
|
2019-10-08 05:24:01 +08:00
|
|
|
logData.Avg = utils.DurationToString(avg)
|
|
|
|
logData.Min = utils.DurationToString(min)
|
|
|
|
logData.P50 = utils.DurationToString(p50)
|
|
|
|
logData.P90 = utils.DurationToString(p90)
|
|
|
|
logData.P95 = utils.DurationToString(p95)
|
|
|
|
logData.P99 = utils.DurationToString(p99)
|
|
|
|
logData.P999 = utils.DurationToString(p999)
|
|
|
|
logData.P9999 = utils.DurationToString(p9999)
|
|
|
|
logData.Max = utils.DurationToString(max)
|
2018-12-11 23:28:33 +08:00
|
|
|
logJSON, _ := json.Marshal(logData)
|
|
|
|
logChan <- string(logJSON)
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
}
|