diff --git a/internal/config/editors/editor.go b/internal/config/editors/editor.go index 77ea18b..7102961 100644 --- a/internal/config/editors/editor.go +++ b/internal/config/editors/editor.go @@ -31,17 +31,6 @@ var AllTrackingTypes = []TrackingType{ TrackingTypeProcess, } -type EditorRunType string - -func (e EditorRunType) String() string { - return string(e) -} - -const ( - EditorRunTypeStart EditorRunType = "start" - EditorRunTypeRun EditorRunType = "run" -) - func GetTrackingType(value string) (TrackingType, error) { for _, t := range AllTrackingTypes { if t.String() == value { @@ -54,7 +43,6 @@ func GetTrackingType(value string) (TrackingType, error) { type Editor struct { Name EditorName `json:"name"` - RunType EditorRunType `json:"run_type"` ExecPath string `json:"path"` Args string `json:"args"` ProcessCaptureDelay time.Duration `json:"process_capture_delay"` diff --git a/internal/config/editors/editor.notepad++.go b/internal/config/editors/editor.notepad++.go index 0c45610..e1240a7 100644 --- a/internal/config/editors/editor.notepad++.go +++ b/internal/config/editors/editor.notepad++.go @@ -9,10 +9,9 @@ const ( func NotepadPlusPlusEditor() Editor { return Editor{ Name: EditorNameNotepadPlusPlus, - RunType: EditorRunTypeStart, ExecPath: "C:\\Program Files\\Notepad++\\notepad++.exe", Args: "-multiInst -openFoldersAsWorkspace", - ProcessCaptureDelay: time.Second * 2, + ProcessCaptureDelay: time.Second * 5, TrackingType: TrackingTypeProcess, } } diff --git a/internal/config/editors/editor.vscode.go b/internal/config/editors/editor.vscode.go index 7fb8955..c76497e 100644 --- a/internal/config/editors/editor.vscode.go +++ b/internal/config/editors/editor.vscode.go @@ -30,10 +30,9 @@ func VSCodeEditor() Editor { return Editor{ Name: EditorNameVSCode, - RunType: EditorRunTypeRun, ExecPath: vsCodePath, Args: "-n", - ProcessCaptureDelay: time.Second * 2, + ProcessCaptureDelay: time.Second * 5, TrackingType: TrackingTypeFileAccess, } } diff --git a/internal/logging/log.go b/internal/logging/log.go index 47f7fec..11e1e76 100644 --- a/internal/logging/log.go +++ b/internal/logging/log.go @@ -53,6 +53,13 @@ func (l *logger) File() *os.File { return nil } +func (l *logger) ChildLogger(prefix string) *logger { + return &logger{ + Logger: log.New(l.Writer(), fmt.Sprintf("%s%s: ", logPrefix, prefix), log.LstdFlags), + cfg: l.cfg, + } +} + func New(cfg *LoggerConfig) (*logger, error) { storageManager := storage.GetStorage() diff --git a/internal/manager/cmd.create-session.go b/internal/manager/cmd.create-session.go index f6c0392..5a80a6b 100644 --- a/internal/manager/cmd.create-session.go +++ b/internal/manager/cmd.create-session.go @@ -11,7 +11,6 @@ import ( "github.com/go-git/go-git/v5" "github.com/nathan-fiscaletti/letstry/internal/config" - "github.com/nathan-fiscaletti/letstry/internal/config/editors" "github.com/nathan-fiscaletti/letstry/internal/environment" "github.com/nathan-fiscaletti/letstry/internal/logging" "github.com/nathan-fiscaletti/letstry/internal/util/identifier" @@ -98,12 +97,7 @@ func (s *manager) CreateSession(ctx context.Context, args CreateSessionArguments cfgArgs := strings.Split(editor.Args, " ") cmdArgs := append(cfgArgs, tempDir) cmd := exec.Command(editor.ExecPath, cmdArgs...) - switch editor.RunType { - case editors.EditorRunTypeRun: - err = cmd.Run() - case editors.EditorRunTypeStart: - err = cmd.Start() - } + err = cmd.Start() if err != nil { return zeroValue, fmt.Errorf("failed to run editor: %v", err) } @@ -132,7 +126,7 @@ func (s *manager) CreateSession(ctx context.Context, args CreateSessionArguments if appEnvironment.DebuggerAttached { logger.Printf("skipping monitor process for session %s (debugger attached)\n", newSession.FormattedID()) - logger.Printf("starting monitor session for session %s\n", newSession.FormattedID()) + logger.Printf("starting monitor session for session %s with delay %v\n", newSession.FormattedID(), newSession.Editor.ProcessCaptureDelay) err = s.MonitorSession(ctx, MonitorSessionArguments{ Delay: newSession.Editor.ProcessCaptureDelay, TrackingType: newSession.Editor.TrackingType, @@ -145,7 +139,7 @@ func (s *manager) CreateSession(ctx context.Context, args CreateSessionArguments } else { // Call this application again, but start it in the background as it's own process. // This will allow the user to continue using the current terminal session. - logger.Printf("starting monitor process for session %s\n", newSession.FormattedID()) + logger.Printf("starting monitor session for session %s with delay %v\n", newSession.FormattedID(), newSession.Editor.ProcessCaptureDelay) cmd = exec.Command(os.Args[0], "monitor", fmt.Sprintf("%v", editor.ProcessCaptureDelay), newSession.Location, fmt.Sprintf("%v", newSession.PID), editor.TrackingType.String()) err = cmd.Start() if err != nil { diff --git a/internal/manager/cmd.monitor-session.go b/internal/manager/cmd.monitor-session.go index 9e77905..f0efef3 100644 --- a/internal/manager/cmd.monitor-session.go +++ b/internal/manager/cmd.monitor-session.go @@ -29,20 +29,29 @@ type MonitorSessionArguments struct { func (s *manager) MonitorSession(ctx context.Context, args MonitorSessionArguments) error { // delay the start of the monitoring + logger, err := logging.LoggerFromContext(ctx) + if err != nil { + return err + } + + logger.Printf("delaying monitoring for %v\n", args.Delay) time.Sleep(args.Delay) - handler := func() error { - session, err := s.GetSessionForPath(ctx, args.Location) - if err != nil { - return err - } + session, err := s.GetSessionForPath(ctx, args.Location) + if err != nil { + return err + } + logger.Printf("monitoring session: %s\n", session.ID) - logger, err := logging.LoggerFromContext(ctx) - if err != nil { - return err - } + logger = logger.ChildLogger(fmt.Sprintf("sess-%s", session.ID)) - logger.Printf("cleaning up session: %s (directory no longer being accessed)\n", session.ID) + handler := func() error { + switch session.Editor.TrackingType { + case editors.TrackingTypeFileAccess: + logger.Printf("cleaning up session: %s (directory no longer being accessed)\n", session.ID) + case editors.TrackingTypeProcess: + logger.Printf("cleaning up session: %s (process no longer running)\n", session.ID) + } err = s.removeSession(ctx, session.ID) if err != nil { @@ -54,8 +63,10 @@ func (s *manager) MonitorSession(ctx context.Context, args MonitorSessionArgumen switch args.TrackingType { case editors.TrackingTypeProcess: + logger.Printf("using tracking type: %v\n", editors.TrackingTypeProcess) return s.monitorProcess(args.PID, handler) case editors.TrackingTypeFileAccess: + logger.Printf("using tracking type: %v\n", editors.TrackingTypeFileAccess) _, err := os.Stat(args.Location) if err != nil { return err