| 329 | } |
| 330 | |
| 331 | func initPanicFile(path string) error { |
| 332 | var err error |
| 333 | panicFile, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) |
| 334 | if err != nil { |
| 335 | return err |
| 336 | } |
| 337 | |
| 338 | st, err := panicFile.Stat() |
| 339 | if err != nil { |
| 340 | return err |
| 341 | } |
| 342 | |
| 343 | // If there are contents in the file already, move the file out of the way |
| 344 | // and replace it. |
| 345 | if st.Size() > 0 { |
| 346 | panicFile.Close() |
| 347 | os.Rename(path, path+".old") |
| 348 | panicFile, err = os.Create(path) |
| 349 | if err != nil { |
| 350 | return err |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // Update STD_ERROR_HANDLE to point to the panic file so that Go writes to |
| 355 | // it when it panics. Remember the old stderr to restore it before removing |
| 356 | // the panic file. |
| 357 | h, err := windows.GetStdHandle(windows.STD_ERROR_HANDLE) |
| 358 | if err != nil { |
| 359 | return err |
| 360 | } |
| 361 | oldStderr = h |
| 362 | |
| 363 | return windows.SetStdHandle(windows.STD_ERROR_HANDLE, windows.Handle(panicFile.Fd())) |
| 364 | } |
| 365 | |
| 366 | func removePanicFile() { |
| 367 | if st, err := panicFile.Stat(); err == nil { |