2022-01-05 05:15:38 -05:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
`os`
|
|
|
|
|
|
|
|
sysd `github.com/coreos/go-systemd/journal`
|
|
|
|
`github.com/google/uuid`
|
|
|
|
`r00t2.io/sysutils/paths`
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
AddDefaultLogger adds a default Logger (as would be determined by GetLogger) to a MultiLogger.
|
|
|
|
|
|
|
|
identifier is a string to use to identify the added Logger in MultiLogger.Loggers.
|
|
|
|
If empty, one will be automatically generated.
|
2022-01-16 02:05:42 -05:00
|
|
|
|
|
|
|
See the documentation for GetLogger for details on other arguments.
|
2022-01-05 05:15:38 -05:00
|
|
|
*/
|
2022-01-16 02:05:42 -05:00
|
|
|
func (m *MultiLogger) AddDefaultLogger(identifier string, logFlags int, logPaths ...string) (err error) {
|
2022-01-05 05:15:38 -05:00
|
|
|
|
|
|
|
var l Logger
|
|
|
|
var exists bool
|
|
|
|
|
|
|
|
if identifier == "" {
|
|
|
|
identifier = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists = m.Loggers[identifier]; exists {
|
|
|
|
err = ErrExistingLogger
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
if l, err = GetLogger(m.EnableDebug, m.Prefix, logFlags, logPaths...); err != nil {
|
2022-01-05 05:15:38 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Loggers[identifier] = l
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
AddSysdLogger adds a SystemDLogger to a MultiLogger.
|
|
|
|
|
|
|
|
identifier is a string to use to identify the added SystemDLogger in MultiLogger.Loggers.
|
|
|
|
If empty, one will be automatically generated.
|
|
|
|
*/
|
|
|
|
func (m *MultiLogger) AddSysdLogger(identifier string) (err error) {
|
|
|
|
|
|
|
|
var exists bool
|
2022-01-16 02:05:42 -05:00
|
|
|
var prefix string
|
2022-01-05 05:15:38 -05:00
|
|
|
|
|
|
|
if identifier == "" {
|
|
|
|
identifier = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists = m.Loggers[identifier]; exists {
|
|
|
|
err = ErrExistingLogger
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sysd.Enabled() {
|
|
|
|
err = ErrNoSysD
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Loggers[identifier] = &SystemDLogger{
|
|
|
|
EnableDebug: m.EnableDebug,
|
|
|
|
Prefix: m.Prefix,
|
|
|
|
}
|
2022-01-16 02:05:42 -05:00
|
|
|
if err = m.Loggers[identifier].Setup(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if prefix, err = m.Loggers[identifier].GetPrefix(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-01-05 05:15:38 -05:00
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
m.Loggers[identifier].Debug("logger initialized of type %T with prefix %v", m.Loggers[identifier], prefix)
|
2022-01-05 05:15:38 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
AddSyslogLogger adds a SyslogLogger to a MultiLogger.
|
|
|
|
|
|
|
|
identifier is a string to use to identify the added SyslogLogger in MultiLogger.Loggers.
|
|
|
|
If empty, one will be automatically generated.
|
|
|
|
*/
|
|
|
|
func (m *MultiLogger) AddSyslogLogger(identifier string) (err error) {
|
|
|
|
|
|
|
|
var exists bool
|
|
|
|
var hasSyslog bool
|
|
|
|
var stat os.FileInfo
|
|
|
|
var devlogPath string = devlog
|
2022-01-16 02:05:42 -05:00
|
|
|
var prefix string
|
2022-01-05 05:15:38 -05:00
|
|
|
|
|
|
|
if identifier == "" {
|
|
|
|
identifier = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists = m.Loggers[identifier]; exists {
|
|
|
|
err = ErrExistingLogger
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasSyslog, stat, err = paths.RealPathExistsStat(&devlogPath); hasSyslog && err != nil {
|
|
|
|
return
|
|
|
|
} else if !hasSyslog {
|
|
|
|
err = ErrNoSyslog
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if stat.Mode().IsRegular() {
|
|
|
|
err = ErrInvalidDevLog
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Loggers[identifier] = &SyslogLogger{
|
|
|
|
EnableDebug: m.EnableDebug,
|
|
|
|
Prefix: m.Prefix,
|
|
|
|
}
|
2022-01-16 02:05:42 -05:00
|
|
|
if err = m.Loggers[identifier].Setup(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if prefix, err = m.Loggers[identifier].GetPrefix(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-01-05 05:15:38 -05:00
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
m.Loggers[identifier].Debug("logger initialized of type %T with prefix %v", m.Loggers[identifier], prefix)
|
2022-01-05 05:15:38 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|