update log

This commit is contained in:
Gregory Salaun 2024-10-31 11:14:35 +07:00
parent 91fc5836eb
commit f571d8ea18
8 changed files with 40 additions and 45 deletions

View File

@ -7,7 +7,6 @@ import (
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
) )
//go:embed templates/* //go:embed templates/*
@ -33,12 +32,11 @@ type HTTPServer struct {
router *mux.Router router *mux.Router
Log4OMRepo Log4OMContactsRepository Log4OMRepo Log4OMContactsRepository
Repo FlexDXClusterRepository Repo FlexDXClusterRepository
Log *log.Logger
TCPServer TCPServer TCPServer TCPServer
SpotChanToHTTPServer chan TelnetSpot 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() gRouter := mux.NewRouter()
@ -46,7 +44,6 @@ func NewHTTPServer(cRepo Log4OMContactsRepository, fRepo FlexDXClusterRepository
router: gRouter, router: gRouter,
Log4OMRepo: cRepo, Log4OMRepo: cRepo,
Repo: fRepo, Repo: fRepo,
Log: log,
TCPServer: *TCPServer, TCPServer: *TCPServer,
SpotChanToHTTPServer: SpotChanToHTTPServer, SpotChanToHTTPServer: SpotChanToHTTPServer,
} }
@ -73,17 +70,17 @@ func (s *HTTPServer) StartHTTPServer() {
tmpl, _ = template.ParseGlob("templates/*.html") tmpl, _ = template.ParseGlob("templates/*.html")
s.SetRoutes() 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) err := http.ListenAndServe(Cfg.HTTPServer.Host+":"+Cfg.HTTPServer.Port, s.router)
if err != nil { 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) { func (s *HTTPServer) Homepage(w http.ResponseWriter, r *http.Request) {
err := tmpl.ExecuteTemplate(w, "home.html", nil) err := tmpl.ExecuteTemplate(w, "home.html", nil)
if err != nil { if err != nil {
s.Log.Error("error executing home template: ", err) Log.Error("error executing home template: ", err)
} }
} }

View File

@ -34,7 +34,7 @@ type TCPClient struct {
Countries Countries Countries Countries
} }
func NewTCPClient(TCPServer *TCPServer, log *log.Logger, Countries Countries) *TCPClient { func NewTCPClient(TCPServer *TCPServer, Countries Countries) *TCPClient {
return &TCPClient{ return &TCPClient{
Address: Cfg.Cluster.Server, Address: Cfg.Cluster.Server,
Port: Cfg.Cluster.Port, Port: Cfg.Cluster.Port,
@ -42,7 +42,6 @@ func NewTCPClient(TCPServer *TCPServer, log *log.Logger, Countries Countries) *T
MsgChan: TCPServer.MsgChan, MsgChan: TCPServer.MsgChan,
CmdChan: TCPServer.CmdChan, CmdChan: TCPServer.CmdChan,
SpotChanToFlex: make(chan TelnetSpot, 100), SpotChanToFlex: make(chan TelnetSpot, 100),
Log: log,
TCPServer: *TCPServer, TCPServer: *TCPServer,
SpotChanToHTTPServer: make(chan TelnetSpot, 100), SpotChanToHTTPServer: make(chan TelnetSpot, 100),
Countries: Countries, Countries: Countries,
@ -63,19 +62,19 @@ func (c *TCPClient) StartClient() {
addr, err := net.ResolveTCPAddr("tcp", c.Address+":"+c.Port) addr, err := net.ResolveTCPAddr("tcp", c.Address+":"+c.Port)
if err != nil { if err != nil {
c.Log.Error("Cannot resolve Telnet Client address:", err) Log.Error("Cannot resolve Telnet Client address:", err)
} }
c.setDefaultParams() c.setDefaultParams()
c.Conn, err = net.DialTCP("tcp", nil, addr) c.Conn, err = net.DialTCP("tcp", nil, addr)
if err != nil { 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) err = c.Conn.SetKeepAlive(true)
if err != nil { 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) c.Reader = bufio.NewReader(c.Conn)
@ -83,7 +82,7 @@ func (c *TCPClient) StartClient() {
go func() { go func() {
for message := range c.TCPServer.CmdChan { for message := range c.TCPServer.CmdChan {
c.Log.Infof("Received DX Command: %s", message) Log.Infof("Received DX Command: %s", message)
message := message + "\n" message := message + "\n"
c.WriteString(message) c.WriteString(message)
} }
@ -100,32 +99,32 @@ func (c *TCPClient) Close() {
func (c *TCPClient) SetFilters() { func (c *TCPClient) SetFilters() {
if Cfg.Cluster.FT8 { if Cfg.Cluster.FT8 {
c.Write([]byte("set/ft8\r\n")) c.Write([]byte("set/ft8\r\n"))
c.Log.Info("FT8: On") Log.Info("FT8: On")
} }
if Cfg.Cluster.Skimmer { if Cfg.Cluster.Skimmer {
c.Write([]byte("set/skimmer\r\n")) c.Write([]byte("set/skimmer\r\n"))
c.Log.Info("Skimmer: On") Log.Info("Skimmer: On")
} }
if Cfg.Cluster.FT4 { if Cfg.Cluster.FT4 {
c.Write([]byte("set/ft4\r\n")) c.Write([]byte("set/ft4\r\n"))
c.Log.Info("FT4: On") Log.Info("FT4: On")
} }
if !Cfg.Cluster.FT8 { if !Cfg.Cluster.FT8 {
c.Write([]byte("set/noft8\r\n")) c.Write([]byte("set/noft8\r\n"))
c.Log.Info("FT8: Off") Log.Info("FT8: Off")
} }
if !Cfg.Cluster.FT4 { if !Cfg.Cluster.FT4 {
c.Write([]byte("set/noft4\r\n")) c.Write([]byte("set/noft4\r\n"))
c.Log.Info("FT4: Off") Log.Info("FT4: Off")
} }
if !Cfg.Cluster.Skimmer { if !Cfg.Cluster.Skimmer {
c.Write([]byte("set/noskimmer\r\n")) 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 { for {
message, err := c.Reader.ReadString('\n') message, err := c.Reader.ReadString('\n')
if err != nil { if err != nil {
c.Log.Errorf("Error reading message: %s", err) Log.Errorf("Error reading message: %s", err)
continue continue
} }
if strings.Contains(message, Cfg.Cluster.LoginPrompt+" \r\n") { 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")) c.Write([]byte(c.Login + "\r\n"))
time.Sleep(time.Second * 2) time.Sleep(time.Second * 2)
c.SetFilters() c.SetFilters()
@ -147,7 +146,7 @@ func (c *TCPClient) ReadLine() {
if Cfg.Cluster.Command != "" { if Cfg.Cluster.Command != "" {
c.WriteString(Cfg.Cluster.Command) c.WriteString(Cfg.Cluster.Command)
} }
c.Log.Info("Start receiving spots") Log.Info("Start receiving spots")
} }
// start := time.Now() // start := time.Now()

View File

@ -31,14 +31,13 @@ type TCPServer struct {
Config *Config Config *Config
} }
func NewTCPServer(address string, port string, log *log.Logger) *TCPServer { func NewTCPServer(address string, port string) *TCPServer {
return &TCPServer{ return &TCPServer{
Address: address, Address: address,
Port: port, Port: port,
Clients: make(map[net.Conn]bool), Clients: make(map[net.Conn]bool),
MsgChan: make(chan string, 100), MsgChan: make(chan string, 100),
CmdChan: make(chan string), CmdChan: make(chan string),
Log: log,
Mutex: new(sync.Mutex), Mutex: new(sync.Mutex),
} }
} }
@ -47,12 +46,12 @@ func (s *TCPServer) StartServer() {
s.LogWriter = bufio.NewWriter(os.Stdout) s.LogWriter = bufio.NewWriter(os.Stdout)
s.Listener, err = net.Listen("tcp", Cfg.TelnetServer.Host+":"+Cfg.TelnetServer.Port) s.Listener, err = net.Listen("tcp", Cfg.TelnetServer.Host+":"+Cfg.TelnetServer.Port)
if err != nil { if err != nil {
s.Log.Info("Could not create telnet server") Log.Info("Could not create telnet server")
} }
defer s.Listener.Close() 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() { go func() {
for message := range s.MsgChan { for message := range s.MsgChan {
@ -62,9 +61,9 @@ func (s *TCPServer) StartServer() {
for { for {
s.Conn, err = s.Listener.Accept() 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 { if err != nil {
s.Log.Error("Could not accept connections to telnet server") Log.Error("Could not accept connections to telnet server")
continue continue
} }
s.Mutex.Lock() s.Mutex.Lock()
@ -99,7 +98,7 @@ func (s *TCPServer) handleConnection() {
delete(s.Clients, s.Conn) delete(s.Clients, s.Conn)
s.Mutex.Unlock() s.Mutex.Unlock()
s.Conn.Close() 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") { if strings.Contains(message, "DX") || strings.Contains(message, "SH/DX") || strings.Contains(message, "set") || strings.Contains(message, "SET") {

View File

@ -1,7 +1,7 @@
general: general:
delete_log_file_at_start: true delete_log_file_at_start: true
log_to_file: true log_to_file: true
log_level: DEBUG log_level: INFO
httpserver: true httpserver: true
telnetserver: true telnetserver: true
flexradiospot: true flexradiospot: true

View File

@ -39,7 +39,7 @@ type FlexDXClusterRepository struct {
func NewLog4OMContactsRepository(filePath string) *Log4OMContactsRepository { func NewLog4OMContactsRepository(filePath string) *Log4OMContactsRepository {
db, err := sql.Open("sqlite3", filePath) db, err := sql.Open("sqlite3", filePath)
if err != nil { if err != nil {
fmt.Println("Cannot open db", err) Log.Errorf("Cannot open db", err)
} }
_, err = db.Exec("PRAGMA journal_mode=WAL") _, err = db.Exec("PRAGMA journal_mode=WAL")
if err != nil { if err != nil {
@ -58,7 +58,7 @@ func NewFlexDXDatabase(filePath string) *FlexDXClusterRepository {
fmt.Println("Cannot open db", err) fmt.Println("Cannot open db", err)
} }
Log.Info("Opening SQLite database") Log.Debugln("Opening SQLite database")
_, err = db.ExecContext( _, err = db.ExecContext(
context.Background(), context.Background(),

View File

@ -98,16 +98,16 @@ func (fc *FlexClient) StartFlexClient() {
fc.Timeout = 600 * time.Second 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) fc.Conn, err = net.DialTCP("tcp", nil, addr)
if err != nil { if err != nil {
Log.Errorf("Could not connect to flex radio on %s", Cfg.Flex.IP) Log.Errorf("Could not connect to FlexRadio on %s", Cfg.Flex.IP)
Log.Error("Retrying to connect to flex radio in 5 seconds") Log.Error("Retrying to connect to FlexRadio in 5 seconds")
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
fc.StartFlexClient() 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 fc.IsConnected = true
go func() { go func() {
@ -208,7 +208,7 @@ func (fc *FlexClient) SendSpottoFlex(spot TelnetSpot) {
srcFlexSpot, err := fc.Repo.FindDXSameBand(flexSpot) srcFlexSpot, err := fc.Repo.FindDXSameBand(flexSpot)
if err != nil { 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 var stringSpot string
@ -303,9 +303,9 @@ func DiscoverFlexRadio() (bool, *Discovery) {
if Cfg.Flex.Discover { if Cfg.Flex.Discover {
Log.Infoln("FlexRadio Discovery is turned on...searching for radio on the network") 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 { 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) buf := make([]byte, 1024)
@ -316,7 +316,7 @@ func DiscoverFlexRadio() (bool, *Discovery) {
Log.Errorln("Could not read data on UDP port 4992") 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])) match := discoverRe.FindStringSubmatch(string(buf[:n]))
if len(match) > 0 { if len(match) > 0 {

View File

@ -66,10 +66,10 @@ func main() {
cRepo := NewLog4OMContactsRepository(cfg.SQLite.SQLitePath) cRepo := NewLog4OMContactsRepository(cfg.SQLite.SQLitePath)
defer cRepo.db.Close() defer cRepo.db.Close()
TCPServer := NewTCPServer(cfg.TelnetServer.Host, cfg.TelnetServer.Port, log) TCPServer := NewTCPServer(cfg.TelnetServer.Host, cfg.TelnetServer.Port)
TCPClient := NewTCPClient(TCPServer, log, Countries) TCPClient := NewTCPClient(TCPServer, Countries)
FlexClient := NewFlexClient(*fRepo, TCPServer, TCPClient.SpotChanToFlex) 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) sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM) signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)

2
xml.go
View File

@ -51,7 +51,7 @@ func LoadCountryFile() Countries {
Log.Errorln(err) 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 the closing of our xmlFile so that we can parse it later on
defer xmlFile.Close() defer xmlFile.Close()