From f571d8ea184733532d216797baa0b7bad489ce98 Mon Sep 17 00:00:00 2001 From: Greg Date: Thu, 31 Oct 2024 11:14:35 +0700 Subject: [PATCH] update log --- HTTPServer.go | 11 ++++------- TCPClient.go | 31 +++++++++++++++---------------- TCPServer.go | 13 ++++++------- config.yml | 2 +- database.go | 4 ++-- flexradio.go | 16 ++++++++-------- main.go | 6 +++--- xml.go | 2 +- 8 files changed, 40 insertions(+), 45 deletions(-) diff --git a/HTTPServer.go b/HTTPServer.go index 39a4734..9ad5562 100644 --- a/HTTPServer.go +++ b/HTTPServer.go @@ -7,7 +7,6 @@ import ( "net/http" "github.com/gorilla/mux" - log "github.com/sirupsen/logrus" ) //go:embed templates/* @@ -33,12 +32,11 @@ type HTTPServer struct { router *mux.Router Log4OMRepo Log4OMContactsRepository Repo FlexDXClusterRepository - Log *log.Logger TCPServer TCPServer SpotChanToHTTPServer chan TelnetSpot } -func NewHTTPServer(cRepo Log4OMContactsRepository, fRepo FlexDXClusterRepository, TCPServer *TCPServer, SpotChanToHTTPServer chan TelnetSpot, log *log.Logger) *HTTPServer { +func NewHTTPServer(cRepo Log4OMContactsRepository, fRepo FlexDXClusterRepository, TCPServer *TCPServer, SpotChanToHTTPServer chan TelnetSpot) *HTTPServer { gRouter := mux.NewRouter() @@ -46,7 +44,6 @@ func NewHTTPServer(cRepo Log4OMContactsRepository, fRepo FlexDXClusterRepository router: gRouter, Log4OMRepo: cRepo, Repo: fRepo, - Log: log, TCPServer: *TCPServer, SpotChanToHTTPServer: SpotChanToHTTPServer, } @@ -73,17 +70,17 @@ func (s *HTTPServer) StartHTTPServer() { tmpl, _ = template.ParseGlob("templates/*.html") s.SetRoutes() - s.Log.Infof("starting HTTP server on %s:%s", Cfg.HTTPServer.Host, Cfg.HTTPServer.Port) + Log.Infof("HTTP server started on %s:%s", Cfg.HTTPServer.Host, Cfg.HTTPServer.Port) err := http.ListenAndServe(Cfg.HTTPServer.Host+":"+Cfg.HTTPServer.Port, s.router) if err != nil { - s.Log.Warn("cannot start HTTP server: ", err) + Log.Warn("Cannot start HTTP server: ", err) } } func (s *HTTPServer) Homepage(w http.ResponseWriter, r *http.Request) { err := tmpl.ExecuteTemplate(w, "home.html", nil) if err != nil { - s.Log.Error("error executing home template: ", err) + Log.Error("error executing home template: ", err) } } diff --git a/TCPClient.go b/TCPClient.go index 65df7c8..e08959f 100644 --- a/TCPClient.go +++ b/TCPClient.go @@ -34,7 +34,7 @@ type TCPClient struct { Countries Countries } -func NewTCPClient(TCPServer *TCPServer, log *log.Logger, Countries Countries) *TCPClient { +func NewTCPClient(TCPServer *TCPServer, Countries Countries) *TCPClient { return &TCPClient{ Address: Cfg.Cluster.Server, Port: Cfg.Cluster.Port, @@ -42,7 +42,6 @@ func NewTCPClient(TCPServer *TCPServer, log *log.Logger, Countries Countries) *T MsgChan: TCPServer.MsgChan, CmdChan: TCPServer.CmdChan, SpotChanToFlex: make(chan TelnetSpot, 100), - Log: log, TCPServer: *TCPServer, SpotChanToHTTPServer: make(chan TelnetSpot, 100), Countries: Countries, @@ -63,19 +62,19 @@ func (c *TCPClient) StartClient() { addr, err := net.ResolveTCPAddr("tcp", c.Address+":"+c.Port) if err != nil { - c.Log.Error("Cannot resolve Telnet Client address:", err) + Log.Error("Cannot resolve Telnet Client address:", err) } c.setDefaultParams() c.Conn, err = net.DialTCP("tcp", nil, addr) if err != nil { - c.Log.Error("Cannot connect to Telnet Client:", err) + Log.Error("Cannot connect to Telnet Client:", err) } - c.Log.Infof("Connected to DX cluster %s:%s", c.Address, c.Port) + Log.Infof("Connected to DX cluster %s:%s", c.Address, c.Port) err = c.Conn.SetKeepAlive(true) if err != nil { - c.Log.Error("Error while setting keep alive:", err) + Log.Error("Error while setting keep alive:", err) } c.Reader = bufio.NewReader(c.Conn) @@ -83,7 +82,7 @@ func (c *TCPClient) StartClient() { go func() { for message := range c.TCPServer.CmdChan { - c.Log.Infof("Received DX Command: %s", message) + Log.Infof("Received DX Command: %s", message) message := message + "\n" c.WriteString(message) } @@ -100,32 +99,32 @@ func (c *TCPClient) Close() { func (c *TCPClient) SetFilters() { if Cfg.Cluster.FT8 { c.Write([]byte("set/ft8\r\n")) - c.Log.Info("FT8: On") + Log.Info("FT8: On") } if Cfg.Cluster.Skimmer { c.Write([]byte("set/skimmer\r\n")) - c.Log.Info("Skimmer: On") + Log.Info("Skimmer: On") } if Cfg.Cluster.FT4 { c.Write([]byte("set/ft4\r\n")) - c.Log.Info("FT4: On") + Log.Info("FT4: On") } if !Cfg.Cluster.FT8 { c.Write([]byte("set/noft8\r\n")) - c.Log.Info("FT8: Off") + Log.Info("FT8: Off") } if !Cfg.Cluster.FT4 { c.Write([]byte("set/noft4\r\n")) - c.Log.Info("FT4: Off") + Log.Info("FT4: Off") } if !Cfg.Cluster.Skimmer { c.Write([]byte("set/noskimmer\r\n")) - c.Log.Info("Skimmer: Off") + Log.Info("Skimmer: Off") } } @@ -134,12 +133,12 @@ func (c *TCPClient) ReadLine() { for { message, err := c.Reader.ReadString('\n') if err != nil { - c.Log.Errorf("Error reading message: %s", err) + Log.Errorf("Error reading message: %s", err) continue } if strings.Contains(message, Cfg.Cluster.LoginPrompt+" \r\n") { - c.Log.Debug("Found login prompt...sending callsign") + Log.Debug("Found login prompt...sending callsign") c.Write([]byte(c.Login + "\r\n")) time.Sleep(time.Second * 2) c.SetFilters() @@ -147,7 +146,7 @@ func (c *TCPClient) ReadLine() { if Cfg.Cluster.Command != "" { c.WriteString(Cfg.Cluster.Command) } - c.Log.Info("Start receiving spots") + Log.Info("Start receiving spots") } // start := time.Now() diff --git a/TCPServer.go b/TCPServer.go index 9f2d99b..b3cb261 100644 --- a/TCPServer.go +++ b/TCPServer.go @@ -31,14 +31,13 @@ type TCPServer struct { Config *Config } -func NewTCPServer(address string, port string, log *log.Logger) *TCPServer { +func NewTCPServer(address string, port string) *TCPServer { return &TCPServer{ Address: address, Port: port, Clients: make(map[net.Conn]bool), MsgChan: make(chan string, 100), CmdChan: make(chan string), - Log: log, Mutex: new(sync.Mutex), } } @@ -47,12 +46,12 @@ func (s *TCPServer) StartServer() { s.LogWriter = bufio.NewWriter(os.Stdout) s.Listener, err = net.Listen("tcp", Cfg.TelnetServer.Host+":"+Cfg.TelnetServer.Port) if err != nil { - s.Log.Info("Could not create telnet server") + Log.Info("Could not create telnet server") } defer s.Listener.Close() - s.Log.Infof("Telnet server listening on %s:%s", Cfg.TelnetServer.Host, Cfg.TelnetServer.Port) + Log.Infof("Telnet server listening on %s:%s", Cfg.TelnetServer.Host, Cfg.TelnetServer.Port) go func() { for message := range s.MsgChan { @@ -62,9 +61,9 @@ func (s *TCPServer) StartServer() { for { s.Conn, err = s.Listener.Accept() - s.Log.Info("Client connected", s.Conn.RemoteAddr().String()) + Log.Info("Client connected", s.Conn.RemoteAddr().String()) if err != nil { - s.Log.Error("Could not accept connections to telnet server") + Log.Error("Could not accept connections to telnet server") continue } s.Mutex.Lock() @@ -99,7 +98,7 @@ func (s *TCPServer) handleConnection() { delete(s.Clients, s.Conn) s.Mutex.Unlock() s.Conn.Close() - s.Log.Infof("client %s disconnected", s.Conn.RemoteAddr().String()) + Log.Infof("client %s disconnected", s.Conn.RemoteAddr().String()) } if strings.Contains(message, "DX") || strings.Contains(message, "SH/DX") || strings.Contains(message, "set") || strings.Contains(message, "SET") { diff --git a/config.yml b/config.yml index 20dd6f5..08562f1 100644 --- a/config.yml +++ b/config.yml @@ -1,7 +1,7 @@ general: delete_log_file_at_start: true log_to_file: true - log_level: DEBUG + log_level: INFO httpserver: true telnetserver: true flexradiospot: true diff --git a/database.go b/database.go index ebd6dd2..8b2c65a 100644 --- a/database.go +++ b/database.go @@ -39,7 +39,7 @@ type FlexDXClusterRepository struct { func NewLog4OMContactsRepository(filePath string) *Log4OMContactsRepository { db, err := sql.Open("sqlite3", filePath) if err != nil { - fmt.Println("Cannot open db", err) + Log.Errorf("Cannot open db", err) } _, err = db.Exec("PRAGMA journal_mode=WAL") if err != nil { @@ -58,7 +58,7 @@ func NewFlexDXDatabase(filePath string) *FlexDXClusterRepository { fmt.Println("Cannot open db", err) } - Log.Info("Opening SQLite database") + Log.Debugln("Opening SQLite database") _, err = db.ExecContext( context.Background(), diff --git a/flexradio.go b/flexradio.go index 97b6ec7..e80776b 100644 --- a/flexradio.go +++ b/flexradio.go @@ -98,16 +98,16 @@ func (fc *FlexClient) StartFlexClient() { fc.Timeout = 600 * time.Second - Log.Infof("Trying to connect to flex radio at %s:%s", fc.Address, fc.Port) + Log.Debugf("Trying to connect to FlexRadio at %s:%s", fc.Address, fc.Port) fc.Conn, err = net.DialTCP("tcp", nil, addr) if err != nil { - Log.Errorf("Could not connect to flex radio on %s", Cfg.Flex.IP) - Log.Error("Retrying to connect to flex radio in 5 seconds") + Log.Errorf("Could not connect to FlexRadio on %s", Cfg.Flex.IP) + Log.Error("Retrying to connect to FlexRadio in 5 seconds") time.Sleep(time.Second * 5) fc.StartFlexClient() } - Log.Infof("Connected to flex radio at %s:%s", fc.Address, fc.Port) + Log.Infof("Connected to FlexRadio at %s:%s", fc.Address, fc.Port) fc.IsConnected = true go func() { @@ -208,7 +208,7 @@ func (fc *FlexClient) SendSpottoFlex(spot TelnetSpot) { srcFlexSpot, err := fc.Repo.FindDXSameBand(flexSpot) if err != nil { - Log.Debugf("could not find the DX in the database: ", err) + Log.Debugf("Could not find the DX in the database: ", err) } var stringSpot string @@ -303,9 +303,9 @@ func DiscoverFlexRadio() (bool, *Discovery) { if Cfg.Flex.Discover { Log.Infoln("FlexRadio Discovery is turned on...searching for radio on the network") - pc, err := net.ListenPacket("udp4", ":4992") + pc, err := net.ListenPacket("udp", ":4992") if err != nil { - Log.Errorln("Could not receive UDP packets to discover FlexRadio") + Log.Errorf("Could not receive UDP packets to discover FlexRadio: ", err) } buf := make([]byte, 1024) @@ -316,7 +316,7 @@ func DiscoverFlexRadio() (bool, *Discovery) { Log.Errorln("Could not read data on UDP port 4992") } - discoverRe := regexp.MustCompile(`discovery_protocol_version=.*\smodel=(.*)\sserial=(.*)\sversion=(.*)\snickname=(.*)\scallsign=.*\sip=(.*)\sport=.*\s+`) + discoverRe := regexp.MustCompile(`discovery_protocol_version=.*\smodel=(.*)\sserial=(.*)\sversion=(.*)\snickname=(.*)\scallsign=.*\sip=(.*)\sport=.*`) match := discoverRe.FindStringSubmatch(string(buf[:n])) if len(match) > 0 { diff --git a/main.go b/main.go index ee0d8cf..e285ef6 100644 --- a/main.go +++ b/main.go @@ -66,10 +66,10 @@ func main() { cRepo := NewLog4OMContactsRepository(cfg.SQLite.SQLitePath) defer cRepo.db.Close() - TCPServer := NewTCPServer(cfg.TelnetServer.Host, cfg.TelnetServer.Port, log) - TCPClient := NewTCPClient(TCPServer, log, Countries) + TCPServer := NewTCPServer(cfg.TelnetServer.Host, cfg.TelnetServer.Port) + TCPClient := NewTCPClient(TCPServer, Countries) FlexClient := NewFlexClient(*fRepo, TCPServer, TCPClient.SpotChanToFlex) - HTTPServer := NewHTTPServer(*cRepo, *fRepo, TCPServer, TCPClient.SpotChanToHTTPServer, log) + HTTPServer := NewHTTPServer(*cRepo, *fRepo, TCPServer, TCPClient.SpotChanToHTTPServer) sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM) diff --git a/xml.go b/xml.go index 3aa314c..87e40df 100644 --- a/xml.go +++ b/xml.go @@ -51,7 +51,7 @@ func LoadCountryFile() Countries { Log.Errorln(err) } - Log.Infoln("Successfully loaded country.xml") + Log.Debugln("Successfully loaded country.xml") // defer the closing of our xmlFile so that we can parse it later on defer xmlFile.Close()