go_goutils/logging/funcs_winlogger_windows.go

258 lines
4.5 KiB
Go
Raw Normal View History

package logging
import (
`errors`
2022-01-06 04:16:44 -05:00
`fmt`
2022-01-05 05:15:38 -05:00
`golang.org/x/sys/windows/svc/eventlog`
)
2022-01-05 05:15:38 -05:00
// Setup sets up/configures a WinLogger and prepares it for use.
func (l *WinLogger) Setup() {
var err error
/*
First a sanity check on the EventIDs.
Since we use eventcreate, all Event IDs must be 1 <= eid <= 1000.
*/
for _, eid := range []uint32{
l.eids.Alert,
l.eids.Crit,
l.eids.Debug,
l.eids.Emerg,
l.eids.Err,
l.eids.Info,
l.eids.Notice,
l.eids.Warning,
} {
2022-01-06 04:16:44 -05:00
if !((eid <= 1000) && (1 <= eid)) {
err = errors.New("event IDs must be between 1 and 1000 inclusive")
panic(err)
}
}
if err = eventlog.InstallAsEventCreate(l.Prefix, eventlog.Error|eventlog.Warning|eventlog.Info); err != nil {
if idx := ptrnSourceExists.FindStringIndex(err.Error()); idx == nil {
// It's an error we want to panic on.
panic(err)
} else {
// It already exists, so ignore the error.
err = nil
}
}
if l.elog, err = eventlog.Open(l.Prefix); err != nil {
panic(err)
}
}
2022-01-05 05:15:38 -05:00
// Shutdown cleanly shuts down a WinLogger.
func (l *WinLogger) Shutdown() {
var err error
if err = l.elog.Close(); err != nil {
panic(err)
}
if err = eventlog.Remove(l.Prefix); err != nil {
panic(err)
}
}
2022-01-05 05:15:38 -05:00
// GetPrefix returns the prefix used by this WinLogger.
func (l *WinLogger) GetPrefix() (prefix string) {
prefix = l.Prefix
return
}
/*
DoDebug sets the debug state of this WinLogger.
Note that this merely acts as a *safety filter* for debug messages to avoid sensitive information being written to the log.
*/
func (l *WinLogger) DoDebug(d bool) {
l.EnableDebug = d
}
2022-01-05 05:15:38 -05:00
// SetPrefix sets the prefix for this WinLogger.
func (l *WinLogger) SetPrefix(prefix string) {
var err error
l.Prefix = prefix
// To properly change the prefix, we need to tear down the old event log and create a new one.
if err = l.elog.Close(); err != nil {
panic(err)
}
if err = eventlog.Remove(l.Prefix); err != nil {
panic(err)
}
if err = eventlog.InstallAsEventCreate(l.Prefix, eventlog.Error|eventlog.Warning|eventlog.Info); err != nil {
if idx := ptrnSourceExists.FindStringIndex(err.Error()); idx == nil {
// It's an error we want to panic on.
panic(err)
} else {
// It already exists, so ignore the error.
err = nil
}
}
if l.elog, err = eventlog.Open(l.Prefix); err != nil {
panic(err)
}
}
2022-01-05 05:15:38 -05:00
// Alert writes an ALERT-level message to this WinLogger.
func (l *WinLogger) Alert(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
// Treat ALERT as Warning
err = l.elog.Warning(l.eids.Alert, msg)
return
}
2022-01-05 05:15:38 -05:00
// Crit writes an CRITICAL-level message to this WinLogger.
func (l *WinLogger) Crit(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
// Treat CRIT as Error
err = l.elog.Error(l.eids.Crit, msg)
return
}
2022-01-05 05:15:38 -05:00
// Debug writes a DEBUG-level message to this WinLogger.
func (l *WinLogger) Debug(s string, v ...interface{}) (err error) {
if !l.EnableDebug {
return
}
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
// Treat DEBUG as Info
err = l.elog.Info(l.eids.Debug, msg)
return
}
2022-01-05 05:15:38 -05:00
// Emerg writes an EMERGENCY-level message to this WinLogger.
func (l *WinLogger) Emerg(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
// Treat EMERG as Error
err = l.elog.Error(l.eids.Emerg, msg)
return
}
2022-01-05 05:15:38 -05:00
// Err writes an ERROR-level message to this WinLogger.
func (l *WinLogger) Err(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
2022-01-06 04:16:44 -05:00
err = l.elog.Error(l.eids.Err, msg)
return
}
2022-01-05 05:15:38 -05:00
// Info writes an INFO-level message to this WinLogger.
func (l *WinLogger) Info(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
err = l.elog.Info(l.eids.Info, msg)
return
}
2022-01-05 05:15:38 -05:00
// Notice writes a NOTICE-level message to this WinLogger.
func (l *WinLogger) Notice(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
// Treat NOTICE as Info
err = l.elog.Info(l.eids.Notice, msg)
return
}
2022-01-05 05:15:38 -05:00
// Warning writes a WARNING/WARN-level message to this WinLogger.
func (l *WinLogger) Warning(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
err = l.elog.Warning(l.eids.Warning, msg)
return
}