func installService(name, desc string) error { exepath, err := exePath() if err != nil { return err } m, err := mgr.Connect() if err != nil { return err } defer m.Disconnect() s, err := m.OpenService(name) if err == nil { s.Close() return fmt.Errorf("service %s already exists", name) } s, err = m.CreateService(name, exepath, mgr.Config{DisplayName: desc}, "is", "auto-started") if err != nil { return err } defer s.Close() err = eventlog.InstallAsEventCreate(name, eventlog.Error|eventlog.Warning|eventlog.Info) if err != nil { s.Delete() return fmt.Errorf("SetupEventLogSource() failed: %s", err) } return nil }
func (ws *windowsService) Install() error { exepath, err := ws.execPath() if err != nil { return err } m, err := mgr.Connect() if err != nil { return err } defer m.Disconnect() s, err := m.OpenService(ws.Name) if err == nil { s.Close() return fmt.Errorf("service %s already exists", ws.Name) } s, err = m.CreateService(ws.Name, exepath, mgr.Config{ DisplayName: ws.DisplayName, Description: ws.Description, StartType: mgr.StartAutomatic, ServiceStartName: ws.UserName, Password: ws.Option.string("Password", ""), Dependencies: ws.Dependencies, }, ws.Arguments...) if err != nil { return err } defer s.Close() err = eventlog.InstallAsEventCreate(ws.Name, eventlog.Error|eventlog.Warning|eventlog.Info) if err != nil { s.Delete() return fmt.Errorf("InstallAsEventCreate() failed: %s", err) } return nil }
func TestLog(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode - it modifies system logs") } const name = "mylog" const supports = eventlog.Error | eventlog.Warning | eventlog.Info err := eventlog.InstallAsEventCreate(name, supports) if err != nil { t.Fatalf("Install failed: %s", err) } defer func() { err = eventlog.Remove(name) if err != nil { t.Fatalf("Remove failed: %s", err) } }() l, err := eventlog.Open(name) if err != nil { t.Fatalf("Open failed: %s", err) } defer l.Close() err = l.Info(1, "info") if err != nil { t.Fatalf("Info failed: %s", err) } err = l.Warning(2, "warning") if err != nil { t.Fatalf("Warning failed: %s", err) } err = l.Error(3, "error") if err != nil { t.Fatalf("Error failed: %s", err) } }