Skip to content

Commit

Permalink
clearing stack trace log file instead of deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecon committed Jan 31, 2025
1 parent f83cd5d commit eaaa6e7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
3 changes: 1 addition & 2 deletions genesyscloud/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ func configure(version string) schema.ConfigureContextFunc {
}
orgDefaultCountryCode = *currentOrg.DefaultCountryCode

stackTraceLogsFilePath, _ := data.Get("log_stack_traces_file_path").(string)
prl.InitPanicRecoveryLoggerInstance(data.Get("log_stack_traces").(bool), stackTraceLogsFilePath)
prl.InitPanicRecoveryLoggerInstance(data.Get("log_stack_traces").(bool), data.Get("log_stack_traces_file_path").(string))

return &ProviderMeta{
Version: version,
Expand Down
17 changes: 9 additions & 8 deletions genesyscloud/util/panic_recovery_logger/panic_recovery_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type PanicRecoveryLogger struct {
var panicRecoverLogger *PanicRecoveryLogger

func InitPanicRecoveryLoggerInstance(enabled bool, filepath string) {
if err := deleteFileIfExists(filepath); err != nil {
if err := clearFileContentsIfExists(filepath); err != nil {
log.Println(err)
}
panicRecoverLogger = &PanicRecoveryLogger{
Expand Down Expand Up @@ -96,30 +96,31 @@ func appendToFile(filename string, data []byte) (err error) {
return err
}

// deleteFileIfExists deletes file at filepath if it exists and does nothing if it doesn't
func deleteFileIfExists(filepath string) (err error) {
// clearFileContentsIfExists deletes file at filepath if it exists and does nothing if it doesn't
func clearFileContentsIfExists(filepath string) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("deleteFileIfExists failed: %w (%s may contain stack traces from the previous deployment)", err, filepath)
err = fmt.Errorf("clearFileContentsIfExists failed: %w (%s may contain stack traces from the previous deployment)", err, filepath)
}
}()

// Check if file exists
_, err = os.Stat(filepath)
if err != nil {
if os.IsNotExist(err) {
// File doesn't exist, return nil as this is not an error condition
// File doesn't exist
return nil
}
// Return other errors (permission issues, etc.)
return fmt.Errorf("failed to check file existence: %w", err)
}

// File exists, try to remove it
err = os.Remove(filepath)
// Open file with truncate flag to clear contents
file, err := os.OpenFile(filepath, os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to delete file %s: %w", filepath, err)
return fmt.Errorf("failed to clear contents of %s: %w", filepath, err)
}
_ = file.Close()

return err
}
Expand Down

0 comments on commit eaaa6e7

Please sign in to comment.