Skip to content

Commit 192bd01

Browse files
committed
build: add logger utility functions and docstrings
1 parent 35179a7 commit 192bd01

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

utils/logger.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,72 @@
1+
/*
2+
Logger utility functions for verbose logging.
3+
This package provides functions to set verbose logging and log messages
4+
*/
15
package utils
26

37
import (
8+
"errors"
9+
"fmt"
410
"log"
11+
"strings"
512
"sync"
613
)
714

815
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
1020
muVerbose = &sync.RWMutex{}
1121
)
1222

23+
// SetVerbose sets the verbose flag to the given value.
24+
// It is safe to call this function from multiple goroutines.
1325
func SetVerbose(v bool) {
1426
muVerbose.Lock()
1527
defer muVerbose.Unlock()
1628
verbose = v
1729
}
1830

31+
// GetVerbose returns the current value of the verbose flag.
32+
// It is safe to call this function from multiple goroutines.
1933
func getVerbose() bool {
2034
muVerbose.RLock()
2135
defer muVerbose.RUnlock()
2236
return verbose
2337
}
2438

39+
// LogVerbose logs a message if verbose logging is enabled.
40+
// It is safe to call this function from multiple goroutines.
2541
func LogVerbose(message string) {
2642
if getVerbose() {
2743
log.Println(message)
2844
}
2945
}
3046

47+
// LogVerbosef logs a formatted message if verbose logging is enabled.
48+
// It is safe to call this function from multiple goroutines.
3149
func LogVerbosef(format string, args ...any) {
3250
if getVerbose() {
3351
log.Printf(format, args...)
3452
}
3553
}
3654

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
3972
}

0 commit comments

Comments
 (0)