logging #1

Merged
grumbulon merged 3 commits from logging into master 2022-06-26 04:08:00 +00:00
4 changed files with 149 additions and 1 deletions
Showing only changes of commit ca648cd096 - Show all commits

5
awl.go
View file

@ -3,9 +3,14 @@ package main
import (
"fmt"
"os"
"git.froth.zone/sam/awl/logawl"
)
func main() {
logger := logawl.New()
logger.Println("Test PLEASE WORK")
logger.Println("Test 2")
app := prepareCLI()
err := app.Run(os.Args)
if err != nil {

2
cli.go
View file

@ -186,7 +186,7 @@ func parseArgs(args []string) (util.Answers, error) {
// TODO: Actually find where windows stuffs its dns resolvers
resp.Answers.Server = "8.8.4.4"
} else {
resp.Answers.Server = resolv.Servers[rand.Intn(len(resolv.Servers)-1)]
resp.Answers.Server = resolv.Servers[rand.Intn(len(resolv.Servers))]
}
}

45
logawl/logawl.go Normal file
View file

@ -0,0 +1,45 @@
package logawl
import "fmt"
type Lvl uint32
type Level struct {
lvl uint32
Prefix string
}
func (l *Level) GetLevel(i uint32) (string, error) {
i = l.lvl
switch i {
case uint32(FatalLevel):
return "FATAL ", nil
case uint32(ErrorLevel):
return "ERROR ", nil
case uint32(InfoLevel):
return "INFO ", nil
case uint32(DebugLevel):
return "DEBUG ", nil
}
return "", fmt.Errorf("Invalid log level choice")
}
var AllLevels = []Lvl{
FatalLevel,
ErrorLevel,
InfoLevel,
DebugLevel,
}
const (
// Fatal logs (will call exit(1))
FatalLevel Lvl = iota
// Error logs
ErrorLevel
// What is going on level
InfoLevel
// Verbose log level.
DebugLevel
)

98
logawl/logger.go Normal file
View file

@ -0,0 +1,98 @@
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
}
func New() *Logger {
return &Logger{
Out: os.Stderr,
//TODO: Make cli.go change this value when calling logawl.New()
Level: InfoLevel, //Default value is InfoLevel
}
}
func (l *Logger) Println(v ...any) {
if atomic.LoadInt32(&l.isDiscard) != 0 {
return
}
switch l.Level {
case 0:
l.Printer(0, fmt.Sprintln(v...))
case 1:
l.Printer(1, fmt.Sprintln(v...))
case 2:
l.Printer(2, fmt.Sprintln(v...))
case 3:
l.Printer(3, fmt.Sprintln(v...))
}
}
func (l *Logger) formatHeader(buf *[]byte, t time.Time, level int, line int) {
var lv Level
if logLvl, err := lv.GetLevel(uint32(level)); err != nil {
fmt.Printf("Invalid log level %s. Error: %v", logLvl, err)
os.Exit(1)
} else {
*buf = append(*buf, logLvl...)
year, month, day := t.Date()
itoa(buf, year, 4)
*buf = append(*buf, '/')
itoa(buf, int(month), 2)
*buf = append(*buf, '/')
itoa(buf, day, 2)
*buf = append(*buf, ' ')
}
}
func (l *Logger) Printer(level int, 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, level, line)
l.buf = append(l.buf, s...)
if len(s) == 0 || s[len(s)-1] != '\n' {
l.buf = append(l.buf, '\n')
}
_, err := l.Out.Write(l.buf)
return err
}
// Some line formatting stuff from Golang log stdlib file
//
// Please view https://cs.opensource.google/go/go/+/refs/tags/go1.18.3:src/log/log.go;drc=41e1d9075e428c2fc32d966b3752a3029b620e2c;l=96
// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding.
func itoa(buf *[]byte, i int, wid int) {
// Assemble decimal in reverse order.
var b [20]byte
bp := len(b) - 1
for i >= 10 || wid > 1 {
wid--
q := i / 10
b[bp] = byte('0' + i - q*10)
bp--
i = q
}
// i < 10
b[bp] = byte('0' + i)
*buf = append(*buf, b[bp:]...)
}