Compare commits

..

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

11 changed files with 46 additions and 92 deletions

View File

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

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
import "fmt"
func Error(err error, a ...any) {
if ERROR.Disabled() {
if !ERROR.Get() {
return
}
realPrintln("Error:", err)
realPrintln(a...)
}
func Errorf(f string, a ...any) error {
return fmt.Errorf(f, a...)
}

View File

@ -30,8 +30,6 @@ 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()
@ -58,7 +56,6 @@ type LogFlag struct {
var flags []*LogFlag
var daemonMode bool
var timestamps bool = false
var httpMode http.ResponseWriter
func init() {
@ -144,18 +141,17 @@ func ProcessFlags(callback func(*LogFlag)) {
}
// is the output flag disabled?
// if so, don't print anything!
func (f *LogFlag) Disabled() bool {
// probably a better name than Get()
// switch to this
func (f *LogFlag) Bool() bool {
if !f.Ok() {
return true
return false
}
return !f.b
return f.b
}
// just the opposite of Disabled()
// both are here just for code readability
func (f *LogFlag) Enabled() bool {
// returns the value of the flag
func (f *LogFlag) Get() bool {
if !f.Ok() {
return false
}

10
info.go
View File

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

6
log.go
View File

@ -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. Normal error output. please ignore for now"}, a...)
a = append([]any{"FLAG = NULL"}, a...)
realPrintln(a...)
return
}
if f.Disabled() {
if !f.Get() {
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.Disabled() {
if !f.Get() {
return
}
s = f.short + " " + s

View File

@ -1,7 +1,5 @@
package log
import "io"
/*
import (
"log"
@ -83,14 +81,6 @@ 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...)
}

View File

@ -11,22 +11,10 @@ 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) {
@ -61,9 +49,6 @@ 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 {
@ -74,22 +59,15 @@ 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") // todo: fix GO so Nov 5 1955 works here
}
timestamp := now.Format("2006/01/02 15:04:05") // bummer. other date doesn't work?
s := timestamp + " " + fmt.Sprint(a...)
fmt.Fprintln(httpMode, s)
if flusher != nil {
@ -99,9 +77,6 @@ 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 {
@ -112,21 +87,14 @@ 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")
}
timestamp := now.Format("2006/01/02 15:04:05") // bummer. other date doesn't work?
s := timestamp + " " + fmt.Sprintf(s, a...)
fmt.Fprintln(httpMode, s)
if flusher != nil {
@ -147,14 +115,6 @@ 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...)
}

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
func Verbose(a ...any) {
if VERBOSE.Disabled() {
if !VERBOSE.Ok() {
return
}
if !VERBOSE.b {
return
}
realPrintln(a...)

View File

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