master
Charles Iliya Krempeaux 2021-02-09 12:51:40 -08:00
parent 73a72074c0
commit b564f8120b
5 changed files with 141 additions and 0 deletions

View File

@ -10,6 +10,7 @@ type internalLogger struct {
writer io.Writer
mutedAlert bool
mutedDebug bool
mutedError bool
mutedFatal bool

View File

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

View File

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

View File

@ -1,5 +1,9 @@
package flog
func (receiver *internalLogger) MuteAlert() {
receiver.mutedDebug = true
}
func (receiver *internalLogger) MuteDebug() {
receiver.mutedDebug = true
}

View File

@ -1,5 +1,9 @@
package flog
func (receiver *internalLogger) UnmuteAlert() {
receiver.mutedAlert = false
}
func (receiver *internalLogger) UnmuteDebug() {
receiver.mutedDebug = false
}