Compare commits
7 Commits
Author | SHA1 | Date |
---|---|---|
|
854796ba72 | |
|
8c7078908b | |
|
466a3c6076 | |
|
c1c1b8a75e | |
|
f793a05288 | |
|
1631c72f70 | |
|
e1d7a5c2cc |
9
Makefile
9
Makefile
|
@ -1,8 +1,11 @@
|
|||
# git remote add github git@github.com:wit-go/log.git
|
||||
|
||||
all:
|
||||
#@GO111MODULE=off go vet -x
|
||||
GO111MODULE=off go vet
|
||||
all: goimport vet
|
||||
@#GO111MODULE=off go vet -x
|
||||
@echo this go library builds ok
|
||||
|
||||
vet:
|
||||
@GO111MODULE=off go vet
|
||||
|
||||
redomod:
|
||||
rm -f go.*
|
||||
|
|
12
bool.go
12
bool.go
|
@ -1,12 +0,0 @@
|
|||
// 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...)
|
||||
}
|
8
error.go
8
error.go
|
@ -4,10 +4,16 @@
|
|||
|
||||
package log
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Error(err error, a ...any) {
|
||||
if !ERROR.Get() {
|
||||
if ERROR.Disabled() {
|
||||
return
|
||||
}
|
||||
realPrintln("Error:", err)
|
||||
realPrintln(a...)
|
||||
}
|
||||
|
||||
func Errorf(f string, a ...any) error {
|
||||
return fmt.Errorf(f, a...)
|
||||
}
|
||||
|
|
18
flags.go
18
flags.go
|
@ -30,6 +30,8 @@ import (
|
|||
"sync"
|
||||
)
|
||||
|
||||
var off bool // completely shut up until I tell you to talk again
|
||||
|
||||
var INFO *LogFlag // toggles log.Info()
|
||||
var VERBOSE *LogFlag // toggles log.Verbose()
|
||||
var SPEW *LogFlag // toggles log.Spew()
|
||||
|
@ -56,6 +58,7 @@ type LogFlag struct {
|
|||
|
||||
var flags []*LogFlag
|
||||
var daemonMode bool
|
||||
var timestamps bool = false
|
||||
var httpMode http.ResponseWriter
|
||||
|
||||
func init() {
|
||||
|
@ -141,17 +144,18 @@ func ProcessFlags(callback func(*LogFlag)) {
|
|||
|
||||
}
|
||||
|
||||
// probably a better name than Get()
|
||||
// switch to this
|
||||
func (f *LogFlag) Bool() bool {
|
||||
// is the output flag disabled?
|
||||
// if so, don't print anything!
|
||||
func (f *LogFlag) Disabled() bool {
|
||||
if !f.Ok() {
|
||||
return false
|
||||
return true
|
||||
}
|
||||
return f.b
|
||||
return !f.b
|
||||
}
|
||||
|
||||
// returns the value of the flag
|
||||
func (f *LogFlag) Get() bool {
|
||||
// just the opposite of Disabled()
|
||||
// both are here just for code readability
|
||||
func (f *LogFlag) Enabled() bool {
|
||||
if !f.Ok() {
|
||||
return false
|
||||
}
|
||||
|
|
10
info.go
10
info.go
|
@ -5,20 +5,14 @@
|
|||
package log
|
||||
|
||||
func Info(a ...any) {
|
||||
if !INFO.Ok() {
|
||||
return
|
||||
}
|
||||
if !INFO.b {
|
||||
if INFO.Disabled() {
|
||||
return
|
||||
}
|
||||
realPrintln(a...)
|
||||
}
|
||||
|
||||
func Infof(s string, a ...any) {
|
||||
if !INFO.Ok() {
|
||||
return
|
||||
}
|
||||
if !INFO.b {
|
||||
if INFO.Disabled() {
|
||||
return
|
||||
}
|
||||
realPrintf(s, a...)
|
||||
|
|
6
log.go
6
log.go
|
@ -24,11 +24,11 @@ In your package, register NETWARN:
|
|||
func Log(f *LogFlag, a ...any) {
|
||||
if !f.Ok() {
|
||||
// if the flag is NULL, notify the user they didn't initialize the flag
|
||||
a = append([]any{"FLAG = NULL"}, a...)
|
||||
a = append([]any{"FLAG = NULL. Normal error output. please ignore for now"}, a...)
|
||||
realPrintln(a...)
|
||||
return
|
||||
}
|
||||
if !f.Get() {
|
||||
if f.Disabled() {
|
||||
return
|
||||
}
|
||||
a = append([]any{f.short}, a...)
|
||||
|
@ -36,7 +36,7 @@ func Log(f *LogFlag, a ...any) {
|
|||
}
|
||||
|
||||
func Logf(f *LogFlag, s string, a ...any) {
|
||||
if !f.Get() {
|
||||
if f.Disabled() {
|
||||
return
|
||||
}
|
||||
s = f.short + " " + s
|
||||
|
|
10
original.go
10
original.go
|
@ -1,5 +1,7 @@
|
|||
package log
|
||||
|
||||
import "io"
|
||||
|
||||
/*
|
||||
import (
|
||||
"log"
|
||||
|
@ -81,6 +83,14 @@ func Sprintln(a ...any) string {
|
|||
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) {
|
||||
realFatalln(a...)
|
||||
}
|
||||
|
|
44
reallog.go
44
reallog.go
|
@ -11,10 +11,22 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func Off() {
|
||||
off = true
|
||||
}
|
||||
|
||||
func On() {
|
||||
off = false
|
||||
}
|
||||
|
||||
func DaemonMode(b bool) {
|
||||
daemonMode = b
|
||||
}
|
||||
|
||||
func Timestamps(b bool) {
|
||||
timestamps = b
|
||||
}
|
||||
|
||||
var captureMode io.Writer
|
||||
|
||||
func CaptureMode(w io.Writer) {
|
||||
|
@ -49,6 +61,9 @@ func DaemonShow() bool {
|
|||
}
|
||||
|
||||
func realPrintln(a ...any) {
|
||||
if off {
|
||||
return
|
||||
}
|
||||
if daemonMode {
|
||||
// in daemon mode, don't put timestamps on each line
|
||||
if captureMode == nil {
|
||||
|
@ -59,15 +74,22 @@ func realPrintln(a ...any) {
|
|||
} else {
|
||||
// put timestamps on each line
|
||||
if captureMode == nil {
|
||||
if timestamps {
|
||||
reallog.Println(a...)
|
||||
} else {
|
||||
fmt.Println(a...)
|
||||
}
|
||||
} else {
|
||||
// TODO: add datestamp
|
||||
fmt.Fprintln(captureMode, a...)
|
||||
}
|
||||
}
|
||||
if httpMode != nil {
|
||||
var timestamp string
|
||||
if timestamps {
|
||||
now := time.Now()
|
||||
timestamp := now.Format("2006/01/02 15:04:05") // bummer. other date doesn't work?
|
||||
timestamp = now.Format("2006/01/02 15:04:05") // todo: fix GO so Nov 5 1955 works here
|
||||
}
|
||||
s := timestamp + " " + fmt.Sprint(a...)
|
||||
fmt.Fprintln(httpMode, s)
|
||||
if flusher != nil {
|
||||
|
@ -77,6 +99,9 @@ func realPrintln(a ...any) {
|
|||
}
|
||||
|
||||
func realPrintf(s string, a ...any) {
|
||||
if off {
|
||||
return
|
||||
}
|
||||
if daemonMode {
|
||||
// in daemon mode, don't put timestamps on each line
|
||||
if captureMode == nil {
|
||||
|
@ -87,14 +112,21 @@ func realPrintf(s string, a ...any) {
|
|||
} else {
|
||||
// put timestamps on each line
|
||||
if captureMode == nil {
|
||||
if timestamps {
|
||||
reallog.Printf(s, a...)
|
||||
} else {
|
||||
fmt.Printf(s, a...)
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(captureMode, s, a...)
|
||||
}
|
||||
}
|
||||
if httpMode != nil {
|
||||
var timestamp string
|
||||
if timestamps {
|
||||
now := time.Now()
|
||||
timestamp := now.Format("2006/01/02 15:04:05") // bummer. other date doesn't work?
|
||||
timestamp = now.Format("2006/01/02 15:04:05")
|
||||
}
|
||||
s := timestamp + " " + fmt.Sprintf(s, a...)
|
||||
fmt.Fprintln(httpMode, s)
|
||||
if flusher != nil {
|
||||
|
@ -115,6 +147,14 @@ func realSprintln(a ...any) string {
|
|||
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) {
|
||||
reallog.Fatalln(a...)
|
||||
}
|
||||
|
|
45
spew.go
45
spew.go
|
@ -1,45 +0,0 @@
|
|||
// 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)
|
||||
*/
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
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...)
|
||||
}
|
|
@ -5,10 +5,7 @@
|
|||
package log
|
||||
|
||||
func Verbose(a ...any) {
|
||||
if !VERBOSE.Ok() {
|
||||
return
|
||||
}
|
||||
if !VERBOSE.b {
|
||||
if VERBOSE.Disabled() {
|
||||
return
|
||||
}
|
||||
realPrintln(a...)
|
||||
|
|
Loading…
Reference in New Issue