239 lines
3.6 KiB
Go
239 lines
3.6 KiB
Go
package logging
|
|
|
|
import (
|
|
`errors`
|
|
)
|
|
|
|
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,
|
|
} {
|
|
if !(1 <= eid <= 1000) {
|
|
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)
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
}
|
|
|
|
func (l *WinLogger) DoDebug(d bool) {
|
|
|
|
l.EnableDebug = d
|
|
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
}
|
|
|
|
func (l *WinLogger) GetPrefix() (prefix string) {
|
|
|
|
prefix = l.Prefix
|
|
|
|
return
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
func (l *WinLogger) Err(s string, v ...interface{}) (err error) {
|
|
|
|
var msg string
|
|
|
|
if v != nil {
|
|
msg = fmt.Sprintf(s, v...)
|
|
} else {
|
|
msg = s
|
|
}
|
|
|
|
err = l.elog.Error(l.eids.Error, msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|