diff --git a/awl.go b/awl.go index 68c770d..3f3242a 100644 --- a/awl.go +++ b/awl.go @@ -3,13 +3,9 @@ package main import ( "fmt" "os" - - "git.froth.zone/sam/awl/logawl" ) func main() { - logger := logawl.New() - logger.Println("Test PLEASE WORK") app := prepareCLI() err := app.Run(os.Args) if err != nil { diff --git a/logawl/doc.go b/logawl/doc.go new file mode 100644 index 0000000..f6cccec --- /dev/null +++ b/logawl/doc.go @@ -0,0 +1,11 @@ +//Package for custom logging needs +package logawl + +/* +LogAwl is the marriage of two logging libraries, the standard library logger and a hint of logorus, by grumbulon. + +LogAwl extends the standard log library with support for log levels and some oop/not oop BS +This is _different_ from the syslog package in the standard library because you do not define a file +Since AWL is a cli utility it writes directly to std err. In the future it may require support for writing to a log file or to syslog directly but for now +logawl will write to std err +*/ diff --git a/logawl/logawl.go b/logawl/logawl.go index 888d777..b903f07 100644 --- a/logawl/logawl.go +++ b/logawl/logawl.go @@ -1,29 +1,54 @@ package logawl -import "fmt" +import ( + "fmt" + "io" + "sync" + "sync/atomic" +) -type Lvl int - -type Level struct { - lvl int - Prefix string +type Level int32 +type Logger struct { + Mu sync.Mutex + Level Level + Prefix string + Out io.Writer + buf []byte + isDiscard int32 } -func (l *Level) GetLevel(i int) (string, error) { - switch i { - case int(FatalLevel): - return "FATAL ", nil - case int(ErrorLevel): - return "ERROR ", nil - case int(InfoLevel): - return "INFO ", nil - case int(DebugLevel): - return "DEBUG ", nil +// Stores whatever input value is in mem address of l.level +func (l *Logger) SetLevel(level Level) { + atomic.StoreInt32((*int32)(&l.Level), int32(level)) +} + +// Mostly nothing +func (l *Logger) GetLevel() Level { + return l.level() +} + +// Retrieves whatever was stored in mem address of l.level +func (l *Logger) level() Level { + return Level(atomic.LoadInt32((*int32)(&l.Level))) +} + +// Unmarshalls the int value of level for writing the header +func (l *Logger) UnMarshalLevel() (string, error) { + lv := l.GetLevel() + switch lv { + case 0: + return "fatal: ", nil + case 1: + return "error: ", nil + case 2: + return "info: ", nil + case 3: + return "debug: ", nil } return "", fmt.Errorf("Invalid log level choice") } -var AllLevels = []Lvl{ +var AllLevels = []Level{ FatalLevel, ErrorLevel, InfoLevel, @@ -32,7 +57,7 @@ var AllLevels = []Lvl{ const ( // Fatal logs (will call exit(1)) - FatalLevel Lvl = iota + FatalLevel Level = iota // Error logs ErrorLevel diff --git a/logawl/logger.go b/logawl/logger.go index b0917e3..c5416e6 100644 --- a/logawl/logger.go +++ b/logawl/logger.go @@ -2,22 +2,11 @@ package logawl import ( "fmt" - "io" "os" - "sync" "sync/atomic" "time" ) -type Logger struct { - Mu sync.Mutex - Level Lvl - Prefix string - Out io.Writer - buf []byte - isDiscard int32 -} - // Calling New instantiates Logger // // Level can be changed to one of the other log levels (FatalLevel, ErrorLevel, InfoLevel, DebugLevel) @@ -49,13 +38,9 @@ func (l *Logger) Println(v ...any) { } // Formats the log header as such YYYY/MM/DD HH:MM:SS (local time) -func (l *Logger) formatHeader(buf *[]byte, t time.Time, level int, line int) { - var lv Level - if logLvl, err := lv.GetLevel(level); err != nil { - fmt.Printf("Invalid log level %s. Error: %v", logLvl, err) - os.Exit(1) - } else { - *buf = append(*buf, logLvl...) +func (l *Logger) formatHeader(buf *[]byte, t time.Time, line int) { + if lvl, err := l.UnMarshalLevel(); err == nil { + *buf = append(*buf, lvl...) year, month, day := t.Date() formatter(buf, year, 4) *buf = append(*buf, '/') @@ -70,6 +55,9 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, level int, line int) { *buf = append(*buf, ':') formatter(buf, sec, 2) *buf = append(*buf, ' ') + } else { + fmt.Printf("Unable to unmarshal log level: %v", err) + os.Exit(2) //Fucking kill him } } @@ -82,7 +70,7 @@ func (l *Logger) Printer(level int, s string) error { defer l.Mu.Unlock() l.buf = l.buf[:0] - l.formatHeader(&l.buf, now, level, line) + l.formatHeader(&l.buf, now, line) l.buf = append(l.buf, s...) if len(s) == 0 || s[len(s)-1] != '\n' { l.buf = append(l.buf, '\n')