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 ff3a904
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 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
21 changes: 10 additions & 11 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 All @@ -34,9 +34,7 @@ func InitPanicRecoveryLoggerInstance(enabled bool, filepath string) {

func GetPanicRecoveryLoggerInstance() *PanicRecoveryLogger {
if panicRecoverLogger == nil {
return &PanicRecoveryLogger{
LoggerEnabled: false,
}
InitPanicRecoveryLoggerInstance(false, "")
}
return panicRecoverLogger
}
Expand Down Expand Up @@ -96,30 +94,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 ff3a904

Please sign in to comment.