|
| 1 | +/* |
| 2 | +Logger utility functions for verbose logging. |
| 3 | +This package provides functions to set verbose logging and log messages |
| 4 | +*/ |
1 | 5 | package utils |
2 | 6 |
|
3 | 7 | import ( |
| 8 | + "errors" |
| 9 | + "fmt" |
4 | 10 | "log" |
| 11 | + "strings" |
5 | 12 | "sync" |
6 | 13 | ) |
7 | 14 |
|
8 | 15 | var ( |
9 | | - verbose bool |
| 16 | + // verbose controls whether verbose logging is enabled |
| 17 | + // It is set to false by default. |
| 18 | + verbose bool |
| 19 | + // muVerbose is a mutex to protect access to the verbose variable |
10 | 20 | muVerbose = &sync.RWMutex{} |
11 | 21 | ) |
12 | 22 |
|
| 23 | +// SetVerbose sets the verbose flag to the given value. |
| 24 | +// It is safe to call this function from multiple goroutines. |
13 | 25 | func SetVerbose(v bool) { |
14 | 26 | muVerbose.Lock() |
15 | 27 | defer muVerbose.Unlock() |
16 | 28 | verbose = v |
17 | 29 | } |
18 | 30 |
|
| 31 | +// GetVerbose returns the current value of the verbose flag. |
| 32 | +// It is safe to call this function from multiple goroutines. |
19 | 33 | func getVerbose() bool { |
20 | 34 | muVerbose.RLock() |
21 | 35 | defer muVerbose.RUnlock() |
22 | 36 | return verbose |
23 | 37 | } |
24 | 38 |
|
| 39 | +// LogVerbose logs a message if verbose logging is enabled. |
| 40 | +// It is safe to call this function from multiple goroutines. |
25 | 41 | func LogVerbose(message string) { |
26 | 42 | if getVerbose() { |
27 | 43 | log.Println(message) |
28 | 44 | } |
29 | 45 | } |
30 | 46 |
|
| 47 | +// LogVerbosef logs a formatted message if verbose logging is enabled. |
| 48 | +// It is safe to call this function from multiple goroutines. |
31 | 49 | func LogVerbosef(format string, args ...any) { |
32 | 50 | if getVerbose() { |
33 | 51 | log.Printf(format, args...) |
34 | 52 | } |
35 | 53 | } |
36 | 54 |
|
37 | | -func Log(message string) { |
38 | | - log.Println(message) |
| 55 | +func MergeErrors(errorsChan chan error, indent int) error { |
| 56 | + // Merge all errors from the channels into a single error |
| 57 | + mergeErrors := []error{} |
| 58 | + for err := range errorsChan { |
| 59 | + if err != nil { |
| 60 | + mergeErrors = append(mergeErrors, err) |
| 61 | + } |
| 62 | + } |
| 63 | + var combinedError error = nil |
| 64 | + if len(mergeErrors) > 0 { |
| 65 | + combinedErrorString := "\n" |
| 66 | + for _, err := range mergeErrors { |
| 67 | + combinedErrorString += fmt.Sprintf("%s- %s\n", strings.Repeat(" ", indent), err.Error()) |
| 68 | + } |
| 69 | + combinedError = errors.New(combinedErrorString) |
| 70 | + } |
| 71 | + return combinedError |
39 | 72 | } |
0 commit comments