refactor: standardize logging on zap across all packages

Replace all fmt.Printf/Println and log.Printf/Fatal with structured
zap.Logger calls to eliminate inconsistent logging (anti-pattern #12).

- network/crypt_conn: inject logger via NewCryptConn, replace 6 fmt calls
- signserver/session: use existing s.logger for debug packet dumps
- entranceserver: use s.logger for inbound/outbound debug logging
- api/utils: accept logger param in verifyPath, replace fmt.Println
- api/endpoints: use s.logger for screenshot path diagnostics
- config: replace log.Fatal with error return in getOutboundIP4
- deltacomp: replace log.Printf with zap.L() global logger
This commit is contained in:
Houmgaor
2026-02-20 18:59:12 +01:00
parent e5133e5dcf
commit 06cb3afa57
15 changed files with 134 additions and 117 deletions

View File

@@ -1,7 +1,7 @@
package _config
import (
"log"
"fmt"
"net"
"strings"
@@ -305,19 +305,18 @@ func (c *EntranceChannelInfo) IsEnabled() bool {
return *c.Enabled
}
// getOutboundIP4 gets the preferred outbound ip4 of this machine
// From https://stackoverflow.com/a/37382208
func getOutboundIP4() net.IP {
func getOutboundIP4() (net.IP, error) {
conn, err := net.Dial("udp4", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
return nil, fmt.Errorf("detecting outbound IP: %w", err)
}
defer func() { _ = conn.Close() }()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.To4()
return localAddr.IP.To4(), nil
}
// LoadConfig loads the given config toml file.
@@ -342,7 +341,11 @@ func LoadConfig() (*Config, error) {
}
if c.Host == "" {
c.Host = getOutboundIP4().To4().String()
ip, err := getOutboundIP4()
if err != nil {
return nil, fmt.Errorf("failed to detect host IP: %w", err)
}
c.Host = ip.To4().String()
}
for i := range versionStrings {
@@ -365,4 +368,3 @@ func LoadConfig() (*Config, error) {
return c, nil
}

View File

@@ -72,7 +72,7 @@ func TestLoadConfigClientModeMapping(t *testing.T) {
// TestLoadConfigFeatureWeaponConstraint tests MinFeatureWeapons > MaxFeatureWeapons constraint
func TestLoadConfigFeatureWeaponConstraint(t *testing.T) {
tests := []struct {
name string
name string
minWeapons int
maxWeapons int
expected int
@@ -107,7 +107,11 @@ func TestLoadConfigDefaultHost(t *testing.T) {
// When Host is empty, it should be set to the outbound IP
if cfg.Host == "" {
// Simulate the logic: if empty, set to outbound IP
cfg.Host = getOutboundIP4().To4().String()
ip, err := getOutboundIP4()
if err != nil {
t.Fatalf("getOutboundIP4() error: %v", err)
}
cfg.Host = ip.To4().String()
if cfg.Host == "" {
t.Error("Host should be set to outbound IP, got empty string")
}
@@ -271,12 +275,12 @@ func TestEntranceServerConfig(t *testing.T) {
Port: 10000,
Entries: []EntranceServerInfo{
{
IP: "192.168.1.100",
Type: 1, // open
Season: 0, // green
Recommended: 1,
Name: "Main Server",
Description: "Main hunting server",
IP: "192.168.1.100",
Type: 1, // open
Season: 0, // green
Recommended: 1,
Name: "Main Server",
Description: "Main hunting server",
AllowedClientFlags: 8192,
Channels: []EntranceChannelInfo{
{Port: 10001, MaxPlayers: 4, CurrentPlayers: 2},
@@ -486,9 +490,9 @@ func BenchmarkConfigCreation(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = &Config{
Host: "localhost",
Language: "en",
ClientMode: "ZZ",
Host: "localhost",
Language: "en",
ClientMode: "ZZ",
RealClientMode: ZZ,
}
}

View File

@@ -108,14 +108,14 @@ func TestVersionStringsContent(t *testing.T) {
index int
expected string
}{
{0, "S1.0"}, // S1
{14, "S10"}, // S10
{15, "FW.1"}, // F1
{19, "FW.5"}, // F5
{20, "G1"}, // G1
{38, "Z1"}, // Z1
{39, "Z2"}, // Z2
{40, "ZZ"}, // ZZ
{0, "S1.0"}, // S1
{14, "S10"}, // S10
{15, "FW.1"}, // F1
{19, "FW.5"}, // F5
{20, "G1"}, // G1
{38, "Z1"}, // Z1
{39, "Z2"}, // Z2
{40, "ZZ"}, // ZZ
}
for _, tt := range tests {
@@ -127,7 +127,10 @@ func TestVersionStringsContent(t *testing.T) {
// TestGetOutboundIP4 tests IP detection
func TestGetOutboundIP4(t *testing.T) {
ip := getOutboundIP4()
ip, err := getOutboundIP4()
if err != nil {
t.Fatalf("getOutboundIP4() returned error: %v", err)
}
if ip == nil {
t.Error("getOutboundIP4() returned nil IP")
}
@@ -276,17 +279,17 @@ func TestDebugOptions(t *testing.T) {
// TestGameplayOptions verifies GameplayOptions struct
func TestGameplayOptions(t *testing.T) {
opts := GameplayOptions{
MinFeatureWeapons: 2,
MaxFeatureWeapons: 10,
MaximumNP: 999999,
MaximumRP: 9999,
MaximumFP: 999999999,
MezFesSoloTickets: 100,
MezFesGroupTickets: 50,
DisableHunterNavi: true,
EnableKaijiEvent: true,
EnableHiganjimaEvent: false,
EnableNierEvent: false,
MinFeatureWeapons: 2,
MaxFeatureWeapons: 10,
MaximumNP: 999999,
MaximumRP: 9999,
MaximumFP: 999999999,
MezFesSoloTickets: 100,
MezFesGroupTickets: 50,
DisableHunterNavi: true,
EnableKaijiEvent: true,
EnableHiganjimaEvent: false,
EnableNierEvent: false,
}
if opts.MinFeatureWeapons != 2 {
@@ -486,12 +489,12 @@ func TestEntrance(t *testing.T) {
// TestEntranceServerInfo verifies EntranceServerInfo struct
func TestEntranceServerInfo(t *testing.T) {
info := EntranceServerInfo{
IP: "192.168.1.1",
Type: 1,
Season: 0,
Recommended: 0,
Name: "Server 1",
Description: "Main server",
IP: "192.168.1.1",
Type: 1,
Season: 0,
Recommended: 0,
Name: "Server 1",
Description: "Main server",
AllowedClientFlags: 4096,
Channels: []EntranceChannelInfo{
{Port: 10001, MaxPlayers: 4, CurrentPlayers: 2},
@@ -624,9 +627,9 @@ func TestGameplayOptionsConstraints(t *testing.T) {
{
name: "valid multipliers",
opts: GameplayOptions{
HRPMultiplier: 1.5,
GRPMultiplier: 1.2,
ZennyMultiplier: 1.0,
HRPMultiplier: 1.5,
GRPMultiplier: 1.2,
ZennyMultiplier: 1.0,
MaterialMultiplier: 1.3,
},
ok: true,
@@ -671,7 +674,7 @@ func TestModeValueRanges(t *testing.T) {
// TestConfigDefaults tests default configuration creation
func TestConfigDefaults(t *testing.T) {
cfg := &Config{
ClientMode: "ZZ",
ClientMode: "ZZ",
RealClientMode: ZZ,
}
@@ -696,7 +699,6 @@ func BenchmarkModeString(b *testing.B) {
func BenchmarkGetOutboundIP4(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = getOutboundIP4()
_, _ = getOutboundIP4()
}
}