Compare commits

..

No commits in common. "master" and "v0.22.8" have entirely different histories.

12 changed files with 93 additions and 97 deletions

View File

@ -1,11 +1,8 @@
# git remote add github git@github.com:wit-go/log.git # git remote add github git@github.com:wit-go/log.git
all: goimport vet all:
@#GO111MODULE=off go vet -x #@GO111MODULE=off go vet -x
@echo this go library builds ok GO111MODULE=off go vet
vet:
@GO111MODULE=off go vet
redomod: redomod:
rm -f go.* rm -f go.*

12
bool.go Normal file
View File

@ -0,0 +1,12 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package log
func Bool(b bool, a ...any) {
if !b {
return
}
realPrintln(a...)
}

View File

@ -4,16 +4,10 @@
package log package log
import "fmt"
func Error(err error, a ...any) { func Error(err error, a ...any) {
if ERROR.Disabled() { if !ERROR.Get() {
return return
} }
realPrintln("Error:", err) realPrintln("Error:", err)
realPrintln(a...) realPrintln(a...)
} }
func Errorf(f string, a ...any) error {
return fmt.Errorf(f, a...)
}

View File

@ -30,8 +30,6 @@ import (
"sync" "sync"
) )
var off bool // completely shut up until I tell you to talk again
var INFO *LogFlag // toggles log.Info() var INFO *LogFlag // toggles log.Info()
var VERBOSE *LogFlag // toggles log.Verbose() var VERBOSE *LogFlag // toggles log.Verbose()
var SPEW *LogFlag // toggles log.Spew() var SPEW *LogFlag // toggles log.Spew()
@ -58,7 +56,6 @@ type LogFlag struct {
var flags []*LogFlag var flags []*LogFlag
var daemonMode bool var daemonMode bool
var timestamps bool = false
var httpMode http.ResponseWriter var httpMode http.ResponseWriter
func init() { func init() {
@ -144,18 +141,17 @@ func ProcessFlags(callback func(*LogFlag)) {
} }
// is the output flag disabled? // probably a better name than Get()
// if so, don't print anything! // switch to this
func (f *LogFlag) Disabled() bool { func (f *LogFlag) Bool() bool {
if !f.Ok() { if !f.Ok() {
return true return false
} }
return !f.b return f.b
} }
// just the opposite of Disabled() // returns the value of the flag
// both are here just for code readability func (f *LogFlag) Get() bool {
func (f *LogFlag) Enabled() bool {
if !f.Ok() { if !f.Ok() {
return false return false
} }

10
info.go
View File

@ -5,14 +5,20 @@
package log package log
func Info(a ...any) { func Info(a ...any) {
if INFO.Disabled() { if !INFO.Ok() {
return
}
if !INFO.b {
return return
} }
realPrintln(a...) realPrintln(a...)
} }
func Infof(s string, a ...any) { func Infof(s string, a ...any) {
if INFO.Disabled() { if !INFO.Ok() {
return
}
if !INFO.b {
return return
} }
realPrintf(s, a...) realPrintf(s, a...)

6
log.go
View File

@ -24,11 +24,11 @@ In your package, register NETWARN:
func Log(f *LogFlag, a ...any) { func Log(f *LogFlag, a ...any) {
if !f.Ok() { if !f.Ok() {
// if the flag is NULL, notify the user they didn't initialize the flag // if the flag is NULL, notify the user they didn't initialize the flag
a = append([]any{"FLAG = NULL. Normal error output. please ignore for now"}, a...) a = append([]any{"FLAG = NULL"}, a...)
realPrintln(a...) realPrintln(a...)
return return
} }
if f.Disabled() { if !f.Get() {
return return
} }
a = append([]any{f.short}, a...) a = append([]any{f.short}, a...)
@ -36,7 +36,7 @@ func Log(f *LogFlag, a ...any) {
} }
func Logf(f *LogFlag, s string, a ...any) { func Logf(f *LogFlag, s string, a ...any) {
if f.Disabled() { if !f.Get() {
return return
} }
s = f.short + " " + s s = f.short + " " + s

View File

@ -1,7 +1,5 @@
package log package log
import "io"
/* /*
import ( import (
"log" "log"
@ -83,14 +81,6 @@ func Sprintln(a ...any) string {
return realSprintln(a...) return realSprintln(a...)
} }
func Fprintln(w io.Writer, a ...any) (int, error) {
return realFprintln(w, a...)
}
func Fprintf(w io.Writer, s string, a ...any) (int, error) {
return realFprintf(w, s, a...)
}
func Fatalln(a ...any) { func Fatalln(a ...any) {
realFatalln(a...) realFatalln(a...)
} }

View File

@ -11,22 +11,10 @@ import (
"time" "time"
) )
func Off() {
off = true
}
func On() {
off = false
}
func DaemonMode(b bool) { func DaemonMode(b bool) {
daemonMode = b daemonMode = b
} }
func Timestamps(b bool) {
timestamps = b
}
var captureMode io.Writer var captureMode io.Writer
func CaptureMode(w io.Writer) { func CaptureMode(w io.Writer) {
@ -61,9 +49,6 @@ func DaemonShow() bool {
} }
func realPrintln(a ...any) { func realPrintln(a ...any) {
if off {
return
}
if daemonMode { if daemonMode {
// in daemon mode, don't put timestamps on each line // in daemon mode, don't put timestamps on each line
if captureMode == nil { if captureMode == nil {
@ -74,22 +59,15 @@ func realPrintln(a ...any) {
} else { } else {
// put timestamps on each line // put timestamps on each line
if captureMode == nil { if captureMode == nil {
if timestamps { reallog.Println(a...)
reallog.Println(a...)
} else {
fmt.Println(a...)
}
} else { } else {
// TODO: add datestamp // TODO: add datestamp
fmt.Fprintln(captureMode, a...) fmt.Fprintln(captureMode, a...)
} }
} }
if httpMode != nil { if httpMode != nil {
var timestamp string now := time.Now()
if timestamps { timestamp := now.Format("2006/01/02 15:04:05") // bummer. other date doesn't work?
now := time.Now()
timestamp = now.Format("2006/01/02 15:04:05") // todo: fix GO so Nov 5 1955 works here
}
s := timestamp + " " + fmt.Sprint(a...) s := timestamp + " " + fmt.Sprint(a...)
fmt.Fprintln(httpMode, s) fmt.Fprintln(httpMode, s)
if flusher != nil { if flusher != nil {
@ -99,9 +77,6 @@ func realPrintln(a ...any) {
} }
func realPrintf(s string, a ...any) { func realPrintf(s string, a ...any) {
if off {
return
}
if daemonMode { if daemonMode {
// in daemon mode, don't put timestamps on each line // in daemon mode, don't put timestamps on each line
if captureMode == nil { if captureMode == nil {
@ -112,21 +87,14 @@ func realPrintf(s string, a ...any) {
} else { } else {
// put timestamps on each line // put timestamps on each line
if captureMode == nil { if captureMode == nil {
if timestamps { reallog.Printf(s, a...)
reallog.Printf(s, a...)
} else {
fmt.Printf(s, a...)
}
} else { } else {
fmt.Fprintf(captureMode, s, a...) fmt.Fprintf(captureMode, s, a...)
} }
} }
if httpMode != nil { if httpMode != nil {
var timestamp string now := time.Now()
if timestamps { timestamp := now.Format("2006/01/02 15:04:05") // bummer. other date doesn't work?
now := time.Now()
timestamp = now.Format("2006/01/02 15:04:05")
}
s := timestamp + " " + fmt.Sprintf(s, a...) s := timestamp + " " + fmt.Sprintf(s, a...)
fmt.Fprintln(httpMode, s) fmt.Fprintln(httpMode, s)
if flusher != nil { if flusher != nil {
@ -147,14 +115,6 @@ func realSprintln(a ...any) string {
return fmt.Sprintln(a...) return fmt.Sprintln(a...)
} }
func realFprintln(w io.Writer, a ...any) (int, error) {
return fmt.Fprintln(w, a...)
}
func realFprintf(w io.Writer, s string, a ...any) (int, error) {
return fmt.Fprintf(w, s, a...)
}
func realFatalln(a ...any) { func realFatalln(a ...any) {
reallog.Fatalln(a...) reallog.Fatalln(a...)
} }

45
spew.go Normal file
View File

@ -0,0 +1,45 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package log
import (
"go.wit.com/dev/davecgh/spew"
)
func Spew(b any, a ...any) {
if !SPEW.Ok() {
return
}
if !SPEW.b {
return
}
switch b.(type) {
case bool:
if !b.(bool) {
return
}
realPrintln("SPEW:", spew.Sdump(a...))
case LogFlag:
var f LogFlag
f = b.(LogFlag)
if !f.b {
return
}
realPrintln("SPEW:", spew.Sdump(a...))
default:
realPrintln("SPEW b:", spew.Sdump(b))
realPrintln("SPEW a:", spew.Sdump(a...))
}
// realPrintln("SPEW:", spew.Sdump(a...))
/*
scs := spew.ConfigState{Indent: "\t", MaxDepth: 1}
// Output using the ConfigState instance.
v := map[string]int{"one": 1}
scs.Printf("v: %v\n", v)
scs.Dump(v)
scs.Dump(a)
*/
}

View File

@ -1,10 +0,0 @@
package log
import "fmt"
// func Sprint(a ...any) string {
// return realSprint(a...)
func Sscanf(f string, thing string, a ...any) (int, error) {
return fmt.Sscanf(f, thing, a...)
}

View File

@ -5,7 +5,10 @@
package log package log
func Verbose(a ...any) { func Verbose(a ...any) {
if VERBOSE.Disabled() { if !VERBOSE.Ok() {
return
}
if !VERBOSE.b {
return return
} }
realPrintln(a...) realPrintln(a...)

View File

@ -5,7 +5,10 @@
package log package log
func Warn(a ...any) { func Warn(a ...any) {
if WARN.Disabled() { if !WARN.Ok() {
return
}
if !WARN.b {
return return
} }
realPrintln(a...) realPrintln(a...)