From 6d30a5ad9b5402cec28af460470e490cc7f1b6f1 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Tue, 28 Jun 2022 14:11:49 -0400 Subject: [PATCH] Logging works bretty good now --- cli.go | 1 + logawl/logawl.go | 7 +++++-- logawl/logger.go | 54 ++++++++++++++++++++++++++++++++---------------- query.go | 10 ++++----- 4 files changed, 47 insertions(+), 25 deletions(-) diff --git a/cli.go b/cli.go index 3142f38..5fee552 100644 --- a/cli.go +++ b/cli.go @@ -136,6 +136,7 @@ func prepareCLI() *cli.App { &cli.BoolFlag{ Name: "debug", Usage: "enable debug logging", + Value: false, }, }, Action: doQuery, diff --git a/logawl/logawl.go b/logawl/logawl.go index b903f07..82b6ff5 100644 --- a/logawl/logawl.go +++ b/logawl/logawl.go @@ -33,8 +33,7 @@ func (l *Logger) level() Level { } // Unmarshalls the int value of level for writing the header -func (l *Logger) UnMarshalLevel() (string, error) { - lv := l.GetLevel() +func (l *Logger) UnMarshalLevel(lv Level) (string, error) { switch lv { case 0: return "fatal: ", nil @@ -48,6 +47,10 @@ func (l *Logger) UnMarshalLevel() (string, error) { return "", fmt.Errorf("Invalid log level choice") } +func (l *Logger) IsLevel(level Level) bool { + return l.level() >= level +} + var AllLevels = []Level{ FatalLevel, ErrorLevel, diff --git a/logawl/logger.go b/logawl/logger.go index c5416e6..cb91922 100644 --- a/logawl/logger.go +++ b/logawl/logger.go @@ -12,34 +12,37 @@ import ( // Level can be changed to one of the other log levels (FatalLevel, ErrorLevel, InfoLevel, DebugLevel) func New() *Logger { return &Logger{ - Out: os.Stderr, - //TODO: Make cli.go change this value when calling logawl.New() + Out: os.Stderr, Level: InfoLevel, //Default value is InfoLevel } } // Takes any and prints it out to Logger -> Out (io.Writer (default is std.Err)) -func (l *Logger) Println(v ...any) { +func (l *Logger) Println(level Level, v ...any) { if atomic.LoadInt32(&l.isDiscard) != 0 { return } - switch l.Level { //Goes through log levels and does stuff based on them (Fatal os.Exit...etc) - case 0: - l.Printer(0, fmt.Sprintln(v...)) //Fatal level - os.Exit(1) - case 1: - l.Printer(1, fmt.Sprintln(v...)) //Error level - case 2: - l.Printer(2, fmt.Sprintln(v...)) //Info level - case 3: - l.Printer(3, fmt.Sprintln(v...)) //Debug level + //If verbose is not set --debug etc print _nothing_ + if l.IsLevel(level) { + switch level { //Goes through log levels and does stuff based on them (Fatal os.Exit...etc) + case 0: + l.Printer(0, fmt.Sprintln(v...)) //Fatal level + os.Exit(1) + case 1: + l.Printer(1, fmt.Sprintln(v...)) //Error level + case 2: + l.Printer(2, fmt.Sprintln(v...)) //Info level + case 3: + l.Printer(3, fmt.Sprintln(v...)) //Debug level + default: + break + } } - } // Formats the log header as such YYYY/MM/DD HH:MM:SS (local time) -func (l *Logger) formatHeader(buf *[]byte, t time.Time, line int) { - if lvl, err := l.UnMarshalLevel(); err == nil { +func (l *Logger) formatHeader(buf *[]byte, t time.Time, line int, level Level) { + if lvl, err := l.UnMarshalLevel(level); err == nil { *buf = append(*buf, lvl...) year, month, day := t.Date() formatter(buf, year, 4) @@ -63,14 +66,14 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, line int) { } // Printer prints the formatted message directly to stdErr -func (l *Logger) Printer(level int, s string) error { +func (l *Logger) Printer(level Level, s string) error { now := time.Now() var line int l.Mu.Lock() defer l.Mu.Unlock() l.buf = l.buf[:0] - l.formatHeader(&l.buf, now, line) + l.formatHeader(&l.buf, now, line, level) l.buf = append(l.buf, s...) if len(s) == 0 || s[len(s)-1] != '\n' { l.buf = append(l.buf, '\n') @@ -99,3 +102,18 @@ func formatter(buf *[]byte, i int, wid int) { b[bp] = byte('0' + i) *buf = append(*buf, b[bp:]...) } + +// Call print directly with Debug level +func (l *Logger) Debug(v ...any) { + l.Println(DebugLevel, v...) +} + +// Call print directly with Info level +func (l *Logger) Info(v ...any) { + l.Println(InfoLevel, v...) +} + +// Call print directly with Error level +func (l *Logger) Error(v ...any) { + l.Println(ErrorLevel, v...) +} diff --git a/query.go b/query.go index 7dc3831..52ebdc0 100644 --- a/query.go +++ b/query.go @@ -20,20 +20,20 @@ func doQuery(c *cli.Context) error { err error resp util.Response isHTTPS bool + Logger = logawl.New() //init logger ) resp.Answers, err = parseArgs(c.Args().Slice()) if err != nil { + Logger.Error("Unable to parse args") return err } - - logger := logawl.New() //init logger + port := c.Int("port") if c.Bool("debug") { - logger.SetLevel(3) //set level to debug + Logger.SetLevel(3) } - port := c.Int("port") - + Logger.Debug("Starting awl") // If port is not set, set it if port == 0 { if c.Bool("tls") || c.Bool("quic") {