Update operation logging spec to use single timestamp per tool call
This commit is contained in:
@@ -19,47 +19,27 @@
|
||||
|
||||
| Pattern | Functions | How |
|
||||
|---------|------------|-----|
|
||||
| Streaming | MarkAllAsDeps, MarkAsExplicit, InstallAUR | `io.MultiWriter` to tee to both terminal and log |
|
||||
| Captured | SyncPackages, CleanupOrphans | capture with `CombinedOutput()`, write to log, write to terminal |
|
||||
| Captured | All state-modifying functions | capture output, write to log with single timestamp at start, write to terminal |
|
||||
|
||||
### One Timestamp Per Tool Call
|
||||
Instead of streaming with MultiWriter (multiple timestamps), each state-modifying function:
|
||||
1. Writes timestamp + operation name to log
|
||||
2. Runs command, captures output
|
||||
3. Writes captured output to log
|
||||
4. Writes output to terminal
|
||||
|
||||
This ensures exactly 1 timestamp print per tool call.
|
||||
|
||||
### Error Handling
|
||||
- Write error to log BEFORE returning from function
|
||||
- Print error to stderr so user sees it
|
||||
|
||||
### Dependencies
|
||||
- Add to imports: `io`, `os`, `path/filepath`
|
||||
|
||||
### Structure
|
||||
|
||||
```go
|
||||
// pkg/state/state.go
|
||||
var logFile *os.File
|
||||
|
||||
func OpenLog() error {
|
||||
logPath := filepath.Join("/var/log", "declpac.log")
|
||||
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logFile = f
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetLogWriter() io.Writer {
|
||||
return logFile
|
||||
}
|
||||
|
||||
func Close() error {
|
||||
if logFile == nil {
|
||||
return nil
|
||||
}
|
||||
return logFile.Close()
|
||||
}
|
||||
```
|
||||
- Add to imports: `os`, `path/filepath`
|
||||
|
||||
### Flow in Sync() or main entrypoint
|
||||
1. Call `OpenLog()` at program start, defer close
|
||||
2. Each state-modifying function uses `state.GetLogWriter()` via MultiWriter
|
||||
2. Each state-modifying function calls `state.Write()` with timestamp prefix
|
||||
|
||||
### Wire into main.go
|
||||
- Open log at start of `run()`
|
||||
|
||||
@@ -18,15 +18,11 @@ In `cmd/declpac/main.go` `run()`:
|
||||
|
||||
Modify `pkg/pacman/pacman.go`:
|
||||
|
||||
All state-modifying functions use `state.Write()` instead of `state.GetLogWriter().Write()`:
|
||||
|
||||
```
|
||||
// OLD
|
||||
state.GetLogWriter().Write(output)
|
||||
|
||||
// NEW
|
||||
state.Write(output) // auto-prepends timestamp
|
||||
```
|
||||
Each state-modifying function writes timestamp ONCE at start, then captures output:
|
||||
- Write `timestamp - operation name` to log
|
||||
- Run command, capture output
|
||||
- Write captured output to log
|
||||
- Write output to terminal
|
||||
|
||||
**Functions updated:**
|
||||
- `SyncPackages()` - write output with timestamp
|
||||
|
||||
Reference in New Issue
Block a user