github.com/nxtrace/NTrace-core@v1.3.1-0.20240513132635-39169291e8c9/tracelog/log.go (about)

     1  package tracelog
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"log"
     7  	"net"
     8  	"os"
     9  	"strconv"
    10  	"strings"
    11  
    12  	"github.com/nxtrace/NTrace-core/trace"
    13  )
    14  
    15  func RealtimePrinter(res *trace.Result, ttl int) {
    16  	f, err := os.OpenFile("/tmp/trace.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, os.ModePerm)
    17  	if err != nil {
    18  		return
    19  	}
    20  	defer func(f *os.File) {
    21  		err := f.Close()
    22  		if err != nil {
    23  			log.Fatal(err)
    24  		}
    25  	}(f)
    26  
    27  	multiWriter := io.MultiWriter(os.Stdout, f)
    28  	log.SetOutput(multiWriter)
    29  	log.SetFlags(0)
    30  	var resStr string
    31  	resStr += fmt.Sprintf("%-2d  ", ttl+1)
    32  
    33  	// 去重
    34  	var latestIP string
    35  	tmpMap := make(map[string][]string)
    36  	for i, v := range res.Hops[ttl] {
    37  		if v.Address == nil && latestIP != "" {
    38  			tmpMap[latestIP] = append(tmpMap[latestIP], fmt.Sprintf("%s ms", "*"))
    39  			continue
    40  		} else if v.Address == nil {
    41  			continue
    42  		}
    43  
    44  		if _, exist := tmpMap[v.Address.String()]; !exist {
    45  			tmpMap[v.Address.String()] = append(tmpMap[v.Address.String()], strconv.Itoa(i))
    46  			// 首次进入
    47  			if latestIP == "" {
    48  				for j := 0; j < i; j++ {
    49  					tmpMap[v.Address.String()] = append(tmpMap[v.Address.String()], fmt.Sprintf("%s ms", "*"))
    50  				}
    51  			}
    52  			latestIP = v.Address.String()
    53  		}
    54  
    55  		tmpMap[v.Address.String()] = append(tmpMap[v.Address.String()], fmt.Sprintf("%.2f ms", v.RTT.Seconds()*1000))
    56  	}
    57  
    58  	if latestIP == "" {
    59  		resStr += fmt.Sprintf("%s\n", "*")
    60  		log.Print(resStr)
    61  		return
    62  	}
    63  
    64  	var blockDisplay = false
    65  	for ip, v := range tmpMap {
    66  		if blockDisplay {
    67  			resStr += fmt.Sprintf("%4s", "")
    68  		}
    69  		if net.ParseIP(ip).To4() == nil {
    70  			resStr += fmt.Sprintf("%-25s ", ip)
    71  		} else {
    72  			resStr += fmt.Sprintf("%-15s ", ip)
    73  		}
    74  
    75  		i, _ := strconv.Atoi(v[0])
    76  
    77  		if res.Hops[ttl][i].Geo.Asnumber != "" {
    78  			resStr += fmt.Sprintf("AS%-7s", res.Hops[ttl][i].Geo.Asnumber)
    79  		} else {
    80  			resStr += fmt.Sprintf(" %-8s", "*")
    81  		}
    82  
    83  		if net.ParseIP(ip).To4() != nil {
    84  			whoisFormat := strings.Split(res.Hops[ttl][i].Geo.Whois, "-")
    85  			if len(whoisFormat) > 1 {
    86  				whoisFormat[0] = strings.Join(whoisFormat[:2], "-")
    87  			}
    88  
    89  			if whoisFormat[0] != "" {
    90  				whoisFormat[0] = "[" + whoisFormat[0] + "]"
    91  			}
    92  			resStr += fmt.Sprintf("%-16s", whoisFormat[0])
    93  		}
    94  
    95  		if res.Hops[ttl][i].Geo.Country == "" {
    96  			res.Hops[ttl][i].Geo.Country = "LAN Address"
    97  		}
    98  
    99  		if net.ParseIP(ip).To4() != nil {
   100  
   101  			resStr += fmt.Sprintf(" %s %s %s %s %-6s\n    %-39s   ", res.Hops[ttl][i].Geo.Country, res.Hops[ttl][i].Geo.Prov, res.Hops[ttl][i].Geo.City, res.Hops[ttl][i].Geo.District, res.Hops[ttl][i].Geo.Owner, res.Hops[ttl][i].Hostname)
   102  		} else {
   103  			resStr += fmt.Sprintf(" %s %s %s %s %-6s\n    %-35s ", res.Hops[ttl][i].Geo.Country, res.Hops[ttl][i].Geo.Prov, res.Hops[ttl][i].Geo.City, res.Hops[ttl][i].Geo.District, res.Hops[ttl][i].Geo.Owner, res.Hops[ttl][i].Hostname)
   104  		}
   105  
   106  		for j := 1; j < len(v); j++ {
   107  			if len(v) == 2 || j == 1 {
   108  				resStr += v[j]
   109  			} else {
   110  				resStr += fmt.Sprintf("/ %s", v[j])
   111  			}
   112  		}
   113  		log.Print(resStr)
   114  		blockDisplay = true
   115  	}
   116  }