Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
94145fb4c7
|
|||
|
81a2d308f0
|
|||
|
c4b3c6441a
|
@@ -1,5 +1,12 @@
|
|||||||
package bitmask
|
package bitmask
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"encoding/binary"
|
||||||
|
"math/bits"
|
||||||
|
)
|
||||||
|
|
||||||
// MaskBit is a flag container.
|
// MaskBit is a flag container.
|
||||||
type MaskBit uint
|
type MaskBit uint
|
||||||
|
|
||||||
@@ -62,6 +69,46 @@ func (m *MaskBit) ToggleFlag(flag MaskBit) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Bytes returns the current value of a MasBit as a byte slice (big-endian).
|
||||||
|
|
||||||
|
If trim is false, b will (probably) be 4 bytes long if you're on a 32-bit size system,
|
||||||
|
and b will (probably) be 8 bytes long if you're on a 64-bit size system. You can determine
|
||||||
|
the size of the resulting slice via (math/)bits.UintSize / 8.
|
||||||
|
|
||||||
|
If trim is true, it will trim leading null bytes (if any). This will lead to an unpredictable
|
||||||
|
byte slice length in b, but is most likely preferred for byte operations.
|
||||||
|
|
||||||
|
*/
|
||||||
|
func (m *MaskBit) Bytes(trim bool) (b []byte) {
|
||||||
|
|
||||||
|
var b2 []byte
|
||||||
|
var size int = bits.UintSize / 8
|
||||||
|
var err error
|
||||||
|
|
||||||
|
b2 = make([]byte, size)
|
||||||
|
|
||||||
|
switch s := bits.UintSize; s {
|
||||||
|
case 32:
|
||||||
|
binary.BigEndian.PutUint32(b2[:], uint32(*m))
|
||||||
|
case 64:
|
||||||
|
binary.BigEndian.PutUint64(b2[:], uint64(*m))
|
||||||
|
default:
|
||||||
|
err = errors.New("unsupported Uint/system bit size")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if trim {
|
||||||
|
b = bytes.TrimLeft(b2, "\x00")
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
b = b2
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Value returns the current raw uint value of a MaskBit.
|
// Value returns the current raw uint value of a MaskBit.
|
||||||
func (m *MaskBit) Value() (v uint) {
|
func (m *MaskBit) Value() (v uint) {
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
`path`
|
"path"
|
||||||
|
|
||||||
`github.com/google/uuid`
|
"github.com/google/uuid"
|
||||||
`r00t2.io/sysutils/paths`
|
"r00t2.io/sysutils/paths"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -145,6 +145,40 @@ func (m *MultiLogger) AddFileLogger(identifier string, logFlags int, logfilePath
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
AddNullLogger adds a NullLogger to a MultiLogger.
|
||||||
|
|
||||||
|
identifier is a string to use to identify the added NullLogger in MultiLogger.Loggers.
|
||||||
|
If empty, one will be automatically generated.
|
||||||
|
*/
|
||||||
|
func (m *MultiLogger) AddNullLogger(identifier string) (err error) {
|
||||||
|
|
||||||
|
var exists bool
|
||||||
|
var prefix string
|
||||||
|
|
||||||
|
if identifier == "" {
|
||||||
|
identifier = uuid.New().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, exists = m.Loggers[identifier]; exists {
|
||||||
|
err = ErrExistingLogger
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Loggers[identifier] = &NullLogger{}
|
||||||
|
if err = m.Loggers[identifier].Setup(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if prefix, err = m.Loggers[identifier].GetPrefix(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Loggers[identifier].Debug("logger initialized of type %T with prefix %v", m.Loggers[identifier], prefix)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveLogger will let you remove a Logger from MultiLogger.Loggers.
|
// RemoveLogger will let you remove a Logger from MultiLogger.Loggers.
|
||||||
func (m *MultiLogger) RemoveLogger(identifier string) (err error) {
|
func (m *MultiLogger) RemoveLogger(identifier string) (err error) {
|
||||||
|
|
||||||
|
|||||||
66
logging/funcs_nulllogger.go
Normal file
66
logging/funcs_nulllogger.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package logging
|
||||||
|
|
||||||
|
// Setup does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) Setup() (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// DoDebug does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) DoDebug(d bool) (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPrefix does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) SetPrefix(p string) (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPrefix does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) GetPrefix() (p string, err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shutdown does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) Shutdown() (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Alert does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) Alert(s string, v ...interface{}) (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crit does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) Crit(s string, v ...interface{}) (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debug does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) Debug(s string, v ...interface{}) (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emerg does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) Emerg(s string, v ...interface{}) (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Err does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) Err(s string, v ...interface{}) (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) Info(s string, v ...interface{}) (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notice does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) Notice(s string, v ...interface{}) (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warning does nothing at all; it's here for interface compat. 🙃
|
||||||
|
func (l *NullLogger) Warning(s string, v ...interface{}) (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ package logging
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
`os`
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -86,6 +86,9 @@ type FileLogger struct {
|
|||||||
writer *os.File
|
writer *os.File
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NullLogger is used mainly for test implementations, mockup code, etc. It does absolutely nothing with all messages sent to it.
|
||||||
|
type NullLogger struct{}
|
||||||
|
|
||||||
// MultiLogger is used to contain one or more Loggers and present them all as a single Logger.
|
// MultiLogger is used to contain one or more Loggers and present them all as a single Logger.
|
||||||
type MultiLogger struct {
|
type MultiLogger struct {
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user