package logging import ( `errors` `fmt` `golang.org/x/sys/windows/svc/eventlog` ) // 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, } { 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) } } // 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) } } // 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 } // 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) } } // 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 } // 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 } // 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 } // 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 } // 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 } err = l.elog.Error(l.eids.Err, msg) return } // 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 } // 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 } // 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 }