start custom logger

This commit is contained in:
2025-07-23 08:59:16 -07:00
parent 8b5be228fe
commit a1f46438d0
14 changed files with 253 additions and 196 deletions

View File

@@ -2,8 +2,8 @@ package service
import (
"bytes"
"epochsilicon/pkg/log"
"fmt"
"github.com/rs/zerolog/log"
"os/exec"
"path/filepath"
"strings"
@@ -27,7 +27,7 @@ var (
// CleanupExistingServices kills any existing rosettax87 processes
func CleanupExistingServices() error {
log.Info().Msg("Cleaning up any existing rosettax87 processes...")
log.Info("Cleaning up any existing rosettax87 processes...")
// Find all rosettax87 processes
cmd := exec.Command("pgrep", "-f", "rosettax87")
@@ -49,16 +49,16 @@ func CleanupExistingServices() error {
err := killCmd.Run()
if err != nil {
// If regular kill fails, try with sudo (but this might fail too)
log.Info().Msgf("Regular kill failed for process %s, trying sudo: %v", pid, err)
log.Infof("Regular kill failed for process %s, trying sudo: %v", pid, err)
sudoKillCmd := exec.Command("sudo", "kill", "-9", pid)
err2 := sudoKillCmd.Run()
if err2 != nil {
log.Info().Msgf("Failed to kill process %s with sudo: %v", pid, err2)
log.Infof("Failed to kill process %s with sudo: %v", pid, err2)
} else {
log.Info().Msgf("Killed existing rosettax87 process with sudo: %s", pid)
log.Infof("Killed existing rosettax87 process with sudo: %s", pid)
}
} else {
log.Info().Msgf("Killed existing rosettax87 process: %s", pid)
log.Infof("Killed existing rosettax87 process: %s", pid)
}
}
@@ -77,7 +77,7 @@ func isRosettaSocketActive() bool {
// StartRosettaX87Service starts the RosettaX87 service with sudo privileges
func StartRosettaX87Service(myWindow fyne.Window, updateAllStatuses func()) {
log.Info().Msg("Starting RosettaX87 service...")
log.Info("Starting RosettaX87 service...")
if paths.EpochPath == "" {
dialog.ShowError(fmt.Errorf("Epoch path not set. Please set it first"), myWindow)
@@ -103,7 +103,7 @@ func StartRosettaX87Service(myWindow fyne.Window, updateAllStatuses func()) {
// Load user preferences
prefs, err := utils.LoadPrefs()
if err != nil {
log.Info().Msgf("Failed to load preferences: %v", err)
log.Infof("Failed to load preferences: %v", err)
prefs = &utils.UserPrefs{} // Use default prefs
}
@@ -163,7 +163,7 @@ func StartRosettaX87Service(myWindow fyne.Window, updateAllStatuses func()) {
if shouldSavePassword {
// Save password to keychain
if err := utils.SaveSudoPassword(password); err != nil {
log.Info().Msgf("Failed to save password to keychain: %v", err)
log.Infof("Failed to save password to keychain: %v", err)
// Don't block the service start, just log the error
}
} else {
@@ -174,7 +174,7 @@ func StartRosettaX87Service(myWindow fyne.Window, updateAllStatuses func()) {
// Update preferences
prefs.SaveSudoPassword = shouldSavePassword
if err := utils.SavePrefs(prefs); err != nil {
log.Info().Msgf("Failed to save preferences: %v", err)
log.Infof("Failed to save preferences: %v", err)
}
// Close the dialog
@@ -191,13 +191,13 @@ func StartRosettaX87Service(myWindow fyne.Window, updateAllStatuses func()) {
err := startServiceWithPassword(rosettaX87Dir, rosettaX87Exe, password)
paths.ServiceStarting = false
if err != nil {
log.Info().Msgf("Failed to start RosettaX87 service: %v", err)
log.Infof("Failed to start RosettaX87 service: %v", err)
fyne.Do(func() {
dialog.ShowError(fmt.Errorf("failed to start RosettaX87 service: %v", err), myWindow)
})
ServiceRunning = false
} else {
log.Info().Msg("RosettaX87 service started successfully")
log.Info("RosettaX87 service started successfully")
ServiceRunning = true
}
fyne.Do(func() {
@@ -215,7 +215,7 @@ func StartRosettaX87Service(myWindow fyne.Window, updateAllStatuses func()) {
passwordContainer,
func(confirmed bool) {
if !confirmed {
log.Info().Msg("Service start cancelled by user")
log.Info("Service start cancelled by user")
return
}
confirmFunc()
@@ -263,7 +263,7 @@ func startServiceWithPassword(workingDir, executable, password string) error {
stderrOutput := stderr.String()
stdoutOutput := stdout.String()
log.Info().Msgf("Password test - Exit code: %v, Stderr: %q, Stdout: %q", err, stderrOutput, stdoutOutput)
log.Infof("Password test - Exit code: %v, Stderr: %q, Stdout: %q", err, stderrOutput, stdoutOutput)
// Check for authentication failure indicators
if strings.Contains(stderrOutput, "Sorry, try again") ||
@@ -283,7 +283,7 @@ func startServiceWithPassword(workingDir, executable, password string) error {
}
// If we get here, the password is correct, now start the actual service
log.Info().Msg("Password validated successfully, starting rosettax87 service...")
log.Info("Password validated successfully, starting rosettax87 service...")
cmd := exec.Command("sudo", "-S", executable)
cmd.Dir = workingDir
@@ -325,25 +325,25 @@ func startServiceWithPassword(workingDir, executable, password string) error {
if cmd.ProcessState != nil && cmd.ProcessState.Exited() {
stderrOutput := stderr.String()
stdoutOutput := stdout.String()
log.Info().Msgf("Process exited - Stdout: %q, Stderr: %q", stdoutOutput, stderrOutput)
log.Infof("Process exited - Stdout: %q, Stderr: %q", stdoutOutput, stderrOutput)
return fmt.Errorf("process exited prematurely with code: %d. Stderr: %s", cmd.ProcessState.ExitCode(), stderrOutput)
}
// Verify the service is actually listening
time.Sleep(1 * time.Second)
if !isRosettaSocketActive() {
log.Info().Msgf("Service started but socket not active - Stdout: %q, Stderr: %q", stdout.String(), stderr.String())
log.Infof("Service started but socket not active - Stdout: %q, Stderr: %q", stdout.String(), stderr.String())
cmd.Process.Kill()
return fmt.Errorf("service started but is not listening on socket")
}
log.Info().Msgf("RosettaX87 service started successfully with PID: %d", servicePID)
log.Infof("RosettaX87 service started successfully with PID: %d", servicePID)
return nil
}
// StopRosettaX87Service stops the running RosettaX87 service
func StopRosettaX87Service(myWindow fyne.Window, updateAllStatuses func()) {
log.Info().Msg("Stopping RosettaX87 service...")
log.Info("Stopping RosettaX87 service...")
if !ServiceRunning {
dialog.ShowInformation("Service Status", "RosettaX87 service is not running.", myWindow)
@@ -354,11 +354,11 @@ func StopRosettaX87Service(myWindow fyne.Window, updateAllStatuses func()) {
// Send SIGTERM to gracefully stop the process
err := serviceCmd.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Info().Msgf("Failed to send SIGTERM to process: %v", err)
log.Infof("Failed to send SIGTERM to process: %v", err)
// Try SIGKILL as fallback
err = serviceCmd.Process.Kill()
if err != nil {
log.Info().Msgf("Failed to kill process: %v", err)
log.Infof("Failed to kill process: %v", err)
dialog.ShowError(fmt.Errorf("failed to stop service: %v", err), myWindow)
return
}
@@ -370,7 +370,7 @@ func StopRosettaX87Service(myWindow fyne.Window, updateAllStatuses func()) {
ServiceRunning = false
serviceCmd = nil
servicePID = 0
log.Info().Msg("RosettaX87 service stopped")
log.Info("RosettaX87 service stopped")
fyne.Do(func() {
dialog.ShowInformation("Service Stopped", "RosettaX87 service has been stopped.", myWindow)
updateAllStatuses()
@@ -407,7 +407,7 @@ func IsServiceRunning() bool {
// CleanupService ensures the service is stopped when the application exits
func CleanupService() {
log.Info().Msg("Cleaning up RosettaX87 service on application exit...")
log.Info("Cleaning up RosettaX87 service on application exit...")
CleanupExistingServices()
ServiceRunning = false
serviceCmd = nil