package main
import (
"fmt"
"log"
"net"
"os"
"strings"
"sync"
"time"
)
type TcpTransporter struct {
Address string
Timeout time.Duration
IdleTimeout time.Duration
Logger *log.Logger
mu sync.Mutex
conn net.Conn
/*Timer类型代表单次时间事件*/
closeTimer *time.Timer
lastActive time.Time
}
func (mb *TcpTransporter) logf(format string, v ...interface{}) {
if mb.Logger != nil {
mb.Logger.Printf(format, v...)
} else {
goPath := os.Getenv("GOPATH")
fmt.Println(goPath)
goPath = strings.Replace(goPath, "\\", "/", -1)
goPath = strings.TrimSuffix(goPath, ";")
file := goPath + "/log.txt"
fmt.Println(file)
logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND|os.O_TRUNC, 0766)
if nil != err {
panic(err)
}
//创建一个Logger
//参数1:日志写入目的地
//参数2:每条日志的前缀
//参数3:日志属性
mb.Logger = log.New(logFile, "前缀", log.Ldate|log.Ltime|log.Llongfile)
mb.Logger.Printf(format, v...)//将行号打印出来了
}
}
func main() {
a := new(TcpTransporter)
a.logf("modbus: received % s\n", "123")
}
os.OpenFile(file, os.O_APPEND, 0766)//保证文件必须存在的。