From 0c507cd5ede97f385bcc5bdb0a52c145abe60d89 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 7 Feb 2020 08:17:19 +0100 Subject: [PATCH] Add log module --- log/log.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 log/log.go diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..66ea0c9 --- /dev/null +++ b/log/log.go @@ -0,0 +1,49 @@ + +package log + +import "fmt" + +// Colors +// --------------------------------------------------------- +const ( + InfoColor = "\033[1;34m%s\033[0m" + NoticeColor = "\033[1;36m%s\033[0m" + WarningColor = "\033[1;33m%s\033[0m" + ErrorColor = "\033[1;31m%s\033[0m" +) + +// Define LogFunc prototype +// Function that takes a format string and variadic number +// of arguments (like printf) +// --------------------------------------------------------- +type LogFunc func(format string, args ...interface{}) + +// Create a log function. +// This is the base logging function. by providing a prefix +// a new log function of LogFunc type will be created +// appending "[ ]" before the message. +// --------------------------------------------------------- +func logfn(prefix string) LogFunc { + return func(format string, args ...interface{}) { + format = "[" + prefix + "] " + format + "\n" + fmt.Printf(format, args...) + } +} + +// Declare our different log functions. +// --------------------------------------------------------- +var Info LogFunc +var Notice LogFunc +var Warning LogFunc +var Error LogFunc + +// Initilize log module +// --------------------------------------------------------- +func init() { + + // Initilize functions. + Info = logfn(fmt.Sprintf(InfoColor, "INFO")) + Notice = logfn(fmt.Sprintf(NoticeColor, "NOTICE")) + Warning = logfn(fmt.Sprintf(WarningColor, "WARN")) + Error = logfn(fmt.Sprintf(ErrorColor, "ERROR")) +}