Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVTOOLING-926: Optional stack trace recovery & logging #1516

Merged
merged 19 commits into from
Feb 13, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Appending to file instead of overwriting
charliecon committed Jan 29, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit d1ce42e33b93d10c0854ebf0e77f274660135846
25 changes: 25 additions & 0 deletions genesyscloud/util/files/util_files.go
Original file line number Diff line number Diff line change
@@ -254,3 +254,28 @@ func WriteToFile(bytes []byte, path string) diag.Diagnostics {
}
return nil
}

// AppendToFile appends data to a file. If the file does not exist, it will be created.
func AppendToFile(filename string, data []byte) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("AppendToFile: %w", err)
}
}()

// Open file with append mode (O_APPEND), create if it doesn't exist (O_CREATE),
// and set write permission (O_WRONLY)
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}

// Write the data to the file
_, err = file.Write(data)
if err != nil {
_ = file.Close()
return err
}
_ = file.Close()
return err
}
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@ package panic_recovery_logger

import (
"fmt"
"os"
"runtime/debug"
"terraform-provider-genesyscloud/genesyscloud/util/files"
)

type PanicRecoveryLogger struct {
@@ -31,7 +31,7 @@ func GetPanicRecoveryLoggerInstance() *PanicRecoveryLogger {

func (p *PanicRecoveryLogger) WriteStackTracesToFile(r any) error {
tracesToWrite := fmt.Sprintf("\nStacktrace recovered: %v. %s", r, string(debug.Stack()))
if err := os.WriteFile(p.filePath, []byte(tracesToWrite), os.ModePerm); err != nil {
if err := files.AppendToFile(p.filePath, []byte(tracesToWrite)); err != nil {
return fmt.Errorf("WriteStackTracesToFile: failed to write to file %s: %w", p.filePath, err)
}
return nil