From b564f8120bf62eeb4c1f3335dee4655de9f458f0 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Tue, 9 Feb 2021 12:51:40 -0800 Subject: [PATCH] alert --- internal_logger.go | 1 + internal_logger_alert.go | 62 +++++++++++++++++++++++++++++++ internal_logger_alert_test.go | 70 +++++++++++++++++++++++++++++++++++ internal_logger_mute.go | 4 ++ internal_logger_unmute.go | 4 ++ 5 files changed, 141 insertions(+) create mode 100644 internal_logger_alert.go create mode 100644 internal_logger_alert_test.go diff --git a/internal_logger.go b/internal_logger.go index 5bd7c4e..d0ec7ef 100644 --- a/internal_logger.go +++ b/internal_logger.go @@ -10,6 +10,7 @@ type internalLogger struct { writer io.Writer + mutedAlert bool mutedDebug bool mutedError bool mutedFatal bool diff --git a/internal_logger_alert.go b/internal_logger_alert.go new file mode 100644 index 0000000..e032fc8 --- /dev/null +++ b/internal_logger_alert.go @@ -0,0 +1,62 @@ +package flog + +import ( + "fmt" + "io" + "strings" +) + +func (receiver internalLogger) AlertMuted() bool { + return receiver.mutedAlert +} + +func (receiver internalLogger) Alert(a ...interface{}) error { + s := fmt.Sprint(a...) + + return receiver.Alertf("%s", s) +} + +func (receiver internalLogger) Alertf(format string, a ...interface{}) error { + err := fmt.Errorf(format, a...) + + if receiver.AlertMuted() { + return err + } + + var writer io.Writer = receiver.writer + if nil == writer { + return err + } + + var newformat string + { + var buffer strings.Builder + + switch receiver.style{ + case"color": + buffer.WriteString("☣️☣️☣️☣️☣️ ") + buffer.WriteString("\x1b[48;2;0;43;54m") + buffer.WriteString("\x1b[38;2;220;50;47m") + case "": + buffer.WriteString("[ALERT] ") + } + + buffer.WriteString(receiver.prefix) + buffer.WriteString(format) + + switch receiver.style { + case "color": + buffer.WriteString("\x1b[0m") + buffer.WriteString(" ☣️☣️☣️☣️☣️") + buffer.WriteRune('\n') + case "": + buffer.WriteRune('\n') + } + + newformat = buffer.String() + } + + fmt.Fprintf(receiver.writer, newformat, a...) + + return err +} diff --git a/internal_logger_alert_test.go b/internal_logger_alert_test.go new file mode 100644 index 0000000..23639d5 --- /dev/null +++ b/internal_logger_alert_test.go @@ -0,0 +1,70 @@ +package flog + +import ( + "bytes" + + "testing" +) + +func TestInternalLogger_Alertf(t *testing.T) { + + tests := []struct{ + Format string + Array []interface{} + Expected string + }{ + { + Format: "", + Array: []interface{}(nil), + Expected: "[ALERT] \n", + }, + { + Format: "", + Array: []interface{}{}, + Expected: "[ALERT] \n", + }, + + + + { + Format: "hello world", + Array: []interface{}(nil), + Expected: "[ALERT] hello world\n", + }, + { + Format: "hello world", + Array: []interface{}{}, + Expected: "[ALERT] hello world\n", + }, + + + + { + Format: "hello %s", + Array: []interface{}{"Joe"}, + Expected: "[ALERT] hello Joe\n", + }, + { + Format: "hello %s %s", + Array: []interface{}{"Joe", "Blow"}, + Expected: "[ALERT] hello Joe Blow\n", + }, + } + + for testNumber, test := range tests { + + var buffer bytes.Buffer + + logger := NewLogger(&buffer) + + logger.Alertf(test.Format, test.Array...) + + if expected, actual := test.Expected, buffer.String(); expected != actual { + t.Errorf("For tst #%d, the actual result is not what was expected.", testNumber) + t.Logf("EXPECTED: %q", expected) + t.Logf("ACTUAL: %q", actual) + t.Logf("ACTUAL: %s", actual) + continue + } + } +} diff --git a/internal_logger_mute.go b/internal_logger_mute.go index e8c8b1b..4725a8b 100644 --- a/internal_logger_mute.go +++ b/internal_logger_mute.go @@ -1,5 +1,9 @@ package flog +func (receiver *internalLogger) MuteAlert() { + receiver.mutedDebug = true +} + func (receiver *internalLogger) MuteDebug() { receiver.mutedDebug = true } diff --git a/internal_logger_unmute.go b/internal_logger_unmute.go index 1b0f7b4..2a7fc43 100644 --- a/internal_logger_unmute.go +++ b/internal_logger_unmute.go @@ -1,5 +1,9 @@ package flog +func (receiver *internalLogger) UnmuteAlert() { + receiver.mutedAlert = false +} + func (receiver *internalLogger) UnmuteDebug() { receiver.mutedDebug = false }