try allowing switching to writing to http

This commit is contained in:
Jeff Carr 2024-11-07 01:23:43 -06:00
parent 2b11a4e334
commit ef61eb494b
14 changed files with 155 additions and 93 deletions

View File

@ -9,6 +9,9 @@ redomod:
GO111MODULE= go mod init
GO111MODULE= go mod tidy
goimport:
goimports -w *.go
github:
git push origin master
git push origin devel

View File

@ -5,6 +5,8 @@
package log
func Bool(b bool, a ...any) {
if ! b {return}
if !b {
return
}
realPrintln(a...)
}

1
doc.go
View File

@ -3,6 +3,7 @@
// license that can be found in the LICENSE file.
package log
//
// version v1.5
//

View File

@ -5,7 +5,9 @@
package log
func Error(err error, a ...any) {
if ! ERROR.Get() { return }
if !ERROR.Get() {
return
}
realPrintln("Error:", err)
realPrintln(a...)
}

View File

@ -25,9 +25,8 @@ package log
*/
import (
"net/http"
"sync"
)
@ -57,7 +56,7 @@ type LogFlag struct {
var flags []*LogFlag
var daemonMode bool
var httpMode http.ResponseWriter
func init() {
full := "go.wit.com/log"
@ -84,7 +83,9 @@ func init() {
// restores flag to it's default value
func (f *LogFlag) SetDefault() {
if ! f.Ok() {return}
if !f.Ok() {
return
}
f.b = f.orig
}
@ -99,7 +100,9 @@ func SetDefaults() {
// simply protects against panic() by making sure it exists.
func (f *LogFlag) Ok() bool {
if f == nil {return false}
if f == nil {
return false
}
return true
}
@ -141,13 +144,17 @@ func ProcessFlags(callback func(*LogFlag)) {
// probably a better name than Get()
// switch to this
func (f *LogFlag) Bool() bool {
if ! f.Ok() {return false}
if !f.Ok() {
return false
}
return f.b
}
// returns the value of the flag
func (f *LogFlag) Get() bool {
if ! f.Ok() {return false}
if !f.Ok() {
return false
}
return f.b
}
@ -160,25 +167,33 @@ func (f *LogFlag) Set(b bool) {
// returns the name of the flag
func (f *LogFlag) GetName() string {
if ! f.Ok() {return ""}
if !f.Ok() {
return ""
}
return f.name
}
// returns the subsystem of the flag
func (f *LogFlag) GetSubsystem() string {
if ! f.Ok() {return ""}
if !f.Ok() {
return ""
}
return f.subsystem
}
// returns the description of the flag
func (f *LogFlag) GetDesc() string {
if ! f.Ok() {return ""}
if !f.Ok() {
return ""
}
return f.desc
}
// returns the description of the flag
func (f *LogFlag) GetDefault() bool {
if ! f.Ok() {return false}
if !f.Ok() {
return false
}
return f.orig
}

16
info.go
View File

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

8
log.go
View File

@ -28,13 +28,17 @@ func Log(f *LogFlag, a ...any) {
realPrintln(a...)
return
}
if ! f.Get() { return }
if !f.Get() {
return
}
a = append([]any{f.short}, a...)
realPrintln(a...)
}
func Logf(f *LogFlag, s string, a ...any) {
if ! f.Get() { return }
if !f.Get() {
return
}
s = f.short + " " + s
realPrintf(s, a...)
}

View File

@ -50,33 +50,35 @@ package log
*/
func Println(a ...any) {
if ! PRINTLN.Ok() { return }
if ! PRINTLN.b { return }
if !PRINTLN.Ok() {
return
}
if !PRINTLN.b {
return
}
realPrintln(a...)
}
func Printf(s string, a ...any) {
if ! PRINTLN.Ok() { return }
if ! PRINTLN.b { return }
if !PRINTLN.Ok() {
return
}
if !PRINTLN.b {
return
}
realPrintf(s, a...)
}
func Sprint(a ...any) string {
if ! PRINTLN.Ok() { return ""}
if ! PRINTLN.b { return ""}
return realSprint(a...)
}
func Sprintf(s string, a ...any) string {
if ! PRINTLN.Ok() { return ""}
if ! PRINTLN.b { return ""}
return realSprintf(s, a...)
}
func Sprintln(s string, a ...any) string {
if ! PRINTLN.Ok() { return ""}
if ! PRINTLN.b { return ""}
return realSprintln(s, a...)
func Sprintln(a ...any) string {
return realSprintln(a...)
}
func Fatalln(a ...any) {

View File

@ -5,8 +5,8 @@
package log
import (
"os"
golanglog "log"
"os"
)
// start writing all the logging to a tmp file

View File

@ -6,12 +6,17 @@ package log
import (
"fmt"
reallog "log"
"net/http"
)
func DaemonMode(b bool) {
daemonMode = b
}
func HttpMode(w http.ResponseWriter) {
httpMode = w
}
func DaemonShow() {
if daemonMode {
fmt.Println("daemonMode=true")
@ -21,19 +26,27 @@ func DaemonShow() {
}
func realPrintln(a ...any) {
if httpMode == nil {
if daemonMode {
fmt.Println(a...)
} else {
reallog.Println(a...)
}
} else {
fmt.Fprintln(httpMode, a...)
}
}
func realPrintf(s string, a ...any) {
if httpMode == nil {
if daemonMode {
fmt.Printf(s, a...)
} else {
reallog.Printf(s, a...)
}
} else {
fmt.Fprintln(httpMode, fmt.Sprintf(s, a...))
}
}
func realSprint(a ...any) string {
@ -44,8 +57,8 @@ func realSprintf(s string, a ...any) string {
return fmt.Sprintf(s, a...)
}
func realSprintln(s string, a ...any) string {
return fmt.Sprintf(s, a...) + "\n"
func realSprintln(a ...any) string {
return fmt.Sprintln(a...)
}
func realFatalln(a ...any) {

View File

@ -7,10 +7,10 @@ package log
// a shortcut for sleep so you don't have to always change the import lines when debugging
import (
"os"
"time"
"errors"
"os"
"reflect"
"time"
)
/*
@ -18,7 +18,7 @@ import (
sleep(.1) # you know what this does? yes, it sleeps for 1/10th of a second
*/
func Sleep(a ...any) {
if (a == nil) {
if a == nil {
time.Sleep(time.Second)
return
}

View File

@ -9,8 +9,12 @@ import (
)
func Spew(b any, a ...any) {
if ! SPEW.Ok() { return }
if ! SPEW.b { return }
if !SPEW.Ok() {
return
}
if !SPEW.b {
return
}
switch b.(type) {
case bool:

View File

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

View File

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