diff options
author | github-classroom[bot] <66690702+github-classroom[bot]@users.noreply.github.com> | 2022-02-28 19:36:23 +0000 |
---|---|---|
committer | github-classroom[bot] <66690702+github-classroom[bot]@users.noreply.github.com> | 2022-02-28 19:36:23 +0000 |
commit | 1dd0508d5d3c737f1ee9c723f580baf73b1cfd70 (patch) | |
tree | 6adcc5ef85f9cf0bbb205c577da0bac9148114dd /pkg/utils |
Initial commit
Diffstat (limited to 'pkg/utils')
-rw-r--r-- | pkg/utils/logging.go | 55 | ||||
-rw-r--r-- | pkg/utils/utils.go | 13 |
2 files changed, 68 insertions, 0 deletions
diff --git a/pkg/utils/logging.go b/pkg/utils/logging.go new file mode 100644 index 0000000..d98e78a --- /dev/null +++ b/pkg/utils/logging.go @@ -0,0 +1,55 @@ +package utils + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "strconv" + "strings" +) + +/* + * Brown University, CS1951L, Summer 2021 + * Designed by: Colby Anderson, John Roy + */ + +// Debug is optional logger for debugging +var Debug *log.Logger + +// Out is logger to Stdout +var Out *log.Logger + +// Err is logger to Stderr +var Err *log.Logger + +// init initializes the loggers. +func init() { + Debug = log.New(ioutil.Discard, "DEBUG: ", 0) + Out = log.New(os.Stdout, "INFO: ", log.Ltime|log.Lshortfile) + Err = log.New(os.Stderr, "ERROR: ", log.Ltime|log.Lshortfile) +} + +// SetDebug turns debug print statements on or off. +func SetDebug(enabled bool) { + if enabled { + Debug.SetOutput(os.Stdout) + } else { + Debug.SetOutput(ioutil.Discard) + } +} + +func FmtAddr(addr string) string { + if addr == "" { + return "" + } + colors := []string{"\033[41m", "\033[42m", "\033[43m", "\033[44m", "\033[45m", "\033[46m", "\033[47m"} + port, _ := strconv.ParseInt(strings.Split(addr, ":")[1], 10, 64) + randomColor := colors[int(port)%len(colors)] + return fmt.Sprintf("%v\033[97m[%v]\033[0m", randomColor, addr) +} + +func Colorize(s string, seed int) string { + lowestColor, highestColor := 104, 226 + return fmt.Sprintf("\033[38;5;%vm%v\033[0m", seed%(highestColor-lowestColor)+lowestColor, s) +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go new file mode 100644 index 0000000..5055723 --- /dev/null +++ b/pkg/utils/utils.go @@ -0,0 +1,13 @@ +package utils + +import ( + "crypto/sha256" + "fmt" +) + +// Hash Adapted from: https://blog.8bitzen.com/posts/22-08-2019-how-to-hash-a-struct-in-go +func Hash(o interface{}) string { + h := sha256.New() + h.Write([]byte(fmt.Sprintf("%v", o))) + return fmt.Sprintf("%x", h.Sum(nil)) +} |