Logging changes. Use --debug-file for a replaced log. Use --append to append to the log file if it exists. Also fixed a bug where cthulhu could try to restart without replacing the old running version.
This commit is contained in:
1 parent
4643f8e47f
commit
50e32f24df
9 files changed
+125
-13
No files matched your search
+6
-2
@@ -78,7 +78,8 @@ func run(ctx context.Context, arguments []string, stdout, stderr io.Writer) (res
|
||||
clipboardWatch := options.Bool("clipboard-watch", false, "run the internal clipboard change helper (private pipe protocol)")
|
||||
ocrCapture := options.Bool("ocr-capture", false, "run the internal OCR capture helper (private pipe protocol)")
|
||||
ocrConfirm := options.Bool("ocr-confirm", false, "run the internal OCR click confirmation helper")
|
||||
debugFile := options.String("debug-file", "", "enable debug logging and append logs only to this file")
|
||||
appendLog := options.Bool("append", false, "preserve existing debug log contents (requires --debug-file)")
|
||||
debugFile := options.String("debug-file", "", "enable debug logging only to this file, replacing existing contents")
|
||||
speechSocket := options.String(
|
||||
"speech-socket",
|
||||
"",
|
||||
@@ -123,6 +124,9 @@ func run(ctx context.Context, arguments []string, stdout, stderr io.Writer) (res
|
||||
if options.NArg() != 0 {
|
||||
return fmt.Errorf("unexpected argument %q", options.Arg(0))
|
||||
}
|
||||
if *appendLog && *debugFile == "" {
|
||||
return errors.New("--append requires --debug-file")
|
||||
}
|
||||
if *showVersion || *shortVersion {
|
||||
fmt.Fprintln(stdout, version.Details(
|
||||
atspi.Version(),
|
||||
@@ -174,7 +178,7 @@ func run(ctx context.Context, arguments []string, stdout, stderr io.Writer) (res
|
||||
}
|
||||
logger := slog.New(slog.DiscardHandler)
|
||||
if *debugFile != "" {
|
||||
fileWriter, err := logging.OpenFile(*debugFile, nil)
|
||||
fileWriter, err := logging.OpenFile(*debugFile, nil, *appendLog)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open debug log: %w", err)
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ func TestDebugFileCapturesStartupFailureAndAppends(t *testing.T) {
|
||||
for range 2 {
|
||||
var stdout, stderr bytes.Buffer
|
||||
err := run(context.Background(), []string{
|
||||
"--debug-file", logPath,
|
||||
"--debug-file", logPath, "--append",
|
||||
"--config", configPath,
|
||||
"--speech-socket", filepath.Join(directory, "absent.sock"),
|
||||
}, &stdout, &stderr)
|
||||
@@ -244,3 +244,31 @@ func TestStandaloneDebugFlagRejected(t *testing.T) {
|
||||
t.Fatalf("standalone --debug error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendRequiresDebugFile(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
err := run(context.Background(), []string{"--append"}, &output, &output)
|
||||
if err == nil || err.Error() != "--append requires --debug-file" {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebugFileStartsFresh(t *testing.T) {
|
||||
logPath := filepath.Join(t.TempDir(), "debug.log")
|
||||
configPath := filepath.Join(t.TempDir(), "absent.toml")
|
||||
for range 2 {
|
||||
var output bytes.Buffer
|
||||
if err := run(context.Background(), []string{
|
||||
"--check-config", "--config", configPath, "--debug-file", logPath,
|
||||
}, &output, &output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := os.ReadFile(logPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Count(string(data), `"message":"starting Cthulhu"`) != 1 {
|
||||
t.Fatalf("expected only the current session: %s", data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.stormux.org/storm/cthulhu/internal/remote"
|
||||
"git.stormux.org/storm/cthulhu/internal/sessionbus"
|
||||
"github.com/godbus/dbus/v5"
|
||||
)
|
||||
|
||||
@@ -23,7 +24,9 @@ const replaceWait = 8 * time.Second
|
||||
// replaceRunning identifies the active Go reader through its private session
|
||||
// bus name. It never signals a process merely because its name matches.
|
||||
func replaceRunning(ctx context.Context) error {
|
||||
connection, err := dbus.ConnectSessionBus()
|
||||
// Match the reader's service discovery without rewriting the environment.
|
||||
// Other discovery paths can select another bus when the address is unset.
|
||||
connection, err := sessionbus.Connect(nil, "replacement")
|
||||
if err != nil {
|
||||
return fmt.Errorf("connect to session bus: %w", err)
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.stormux.org/storm/cthulhu/internal/remote"
|
||||
"git.stormux.org/storm/cthulhu/internal/sessionbus"
|
||||
"github.com/godbus/dbus/v5"
|
||||
)
|
||||
|
||||
@@ -23,7 +24,7 @@ func TestReplaceHelper(t *testing.T) {
|
||||
if os.Getenv("CTHULHU_REPLACE_HELPER") != "1" {
|
||||
return
|
||||
}
|
||||
connection, err := dbus.ConnectSessionBus()
|
||||
connection, err := sessionbus.Connect(nil, "replacement-test-reader")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -32,10 +33,10 @@ func TestReplaceHelper(t *testing.T) {
|
||||
if err != nil || reply != dbus.RequestNameReplyPrimaryOwner {
|
||||
t.Fatalf("own reader bus name: %v, %v", reply, err)
|
||||
}
|
||||
fmt.Println("READY")
|
||||
signals := make(chan os.Signal, 1)
|
||||
signal.Notify(signals, syscall.SIGTERM)
|
||||
defer signal.Stop(signals)
|
||||
fmt.Println("READY")
|
||||
<-signals
|
||||
}
|
||||
|
||||
@@ -86,6 +87,9 @@ func TestReplaceRunningNative(t *testing.T) {
|
||||
if err := replaceRunning(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if state, _, err := processIdentity(command.Process.Pid); !os.IsNotExist(err) && (err != nil || state != "Z") {
|
||||
t.Fatalf("replacement returned before reader exit: state=%q, error=%v", state, err)
|
||||
}
|
||||
if err := command.Wait(); err != nil {
|
||||
t.Fatalf("running reader did not exit cleanly: %v", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user