func (ws *windowsService) SystemLogger(errs chan<- error) (Logger, error) {
	el, err := eventlog.Open(ws.Name)
	if err != nil {
		return nil, err
	}
	return WindowsLogger{el, errs}, nil
}
Ejemplo n.º 2
0
func TestLog(t *testing.T) {
	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)
	}
	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)
	}
	err = eventlog.Remove(name)
	if err != nil {
		t.Fatalf("Remove failed: %s", err)
	}
}
Ejemplo n.º 3
0
func (this *Service) RemoveService() error {
	l, err := eventlog.Open(this.name)
	if err != nil {
		return fmt.Errorf("Cannot open eventlog")
	}

	m, err := mgr.Connect()
	if err != nil {
		l.Error(3, fmt.Sprintf("remove service: cannot connect to windows service manager.", err))
		return err
	}
	defer m.Disconnect()

	s, err := m.OpenService(this.name)
	if err != nil {
		l.Error(3, fmt.Sprintf("remove service: service %s doesn't exists", this.name))
		return fmt.Errorf("service %s doesn't exists", this.name)
	}

	err = s.Delete()
	if err != nil {
		return err
	}

	l.Info(1, "Service Removed. removing event log")
	l.Close()

	eventlog.Remove(this.name)
	return nil
}
Ejemplo n.º 4
0
func (this *Service) InstallService() error {
	// open logger
	elog, err := eventlog.Open(this.name)
	if err != nil {
		// install logger
		err = eventlog.InstallAsEventCreate(this.name, eventlog.Error|eventlog.Warning|eventlog.Info)
		if err != nil {
			return fmt.Errorf("SetupEventLogSource() failed: %s", err)
		}

		// open
		elog, err = eventlog.Open(this.name)
		if err != nil {
			return fmt.Errorf("Cannot open eventlog")
		}

	}
	defer elog.Close()

	m, err := mgr.Connect()
	if err != nil {
		elog.Error(3, fmt.Sprintf("install service: cannot connect to windows service manager.", err))
		return err
	}
	defer m.Disconnect()

	s, err := m.OpenService(this.name)
	if err == nil {
		s.Close()
		elog.Error(3, fmt.Sprintf("install service: service %s already exists", this.name))
		return fmt.Errorf("service %s already exists", this.name)
	}

	s, err = m.CreateService(this.name, this.path, mgr.Config{DisplayName: this.desc})
	if err != nil {
		elog.Error(3, fmt.Sprintf("install service: Failed to create service: %v", err))
		return err
	}
	defer s.Close()

	err = this.ConfigServiceAutoStart()
	if err != nil {
		elog.Error(3, fmt.Sprintf("install service: cannot set service to auto start: %v", err))
		return err
	}
	return nil
}
Ejemplo n.º 5
0
func (ws *windowsService) Run(onStart, onStop func() error) error {
	elog, err := eventlog.Open(ws.name)
	if err != nil {
		return err
	}
	defer elog.Close()

	ws.logger = elog

	ws.onStart = onStart
	ws.onStop = onStop
	return svc.Run(ws.name, ws)
}
Ejemplo n.º 6
0
func main() {
	elog, err := eventlog.Open(name)
	if err != nil {
		log.Fatal(err.Error())
	}
	defer elog.Close()

	// `svc.Run` blocks until windows service will stopped.
	// ref. https://msdn.microsoft.com/library/cc429362.aspx
	err = svc.Run(name, &handler{elog: elog})
	if err != nil {
		log.Fatal(err.Error())
	}
}
Ejemplo n.º 7
0
// initEventlog creates a connection to event logs and stores the handler in ctx
func initEventlog(orig_logctx Logging, progname string) (logctx Logging, err error) {
	logctx = orig_logctx
	defer func() {
		if e := recover(); e != nil {
			err = fmt.Errorf("mig.initEventlog() -> %v", e)
		}
	}()
	const name = "mylog"
	const supports = eventlog.Error | eventlog.Warning | eventlog.Info
	err = eventlog.InstallAsEventCreate(name, supports)
	if err != nil {
		panic(err)
	}
	logctx.fd, err = eventlog.Open(progname)
	if err != nil {
		panic(err)
	}
	return
}
Ejemplo n.º 8
0
func queryService(name string, fn func(s *mgr.Service, l *eventlog.Log) (State, error)) (State, error) {
	l, err := eventlog.Open(name)
	if err != nil {
		return State(Unknown), fmt.Errorf("Cannot open eventlog")
	}
	defer l.Close()

	m, err := mgr.Connect()
	if err != nil {
		l.Error(3, fmt.Sprintf("cannot connect to mgr: %v", err))
		return State(Unknown), err
	}
	defer m.Disconnect()

	s, err := m.OpenService(name)
	if err != nil {
		l.Error(3, fmt.Sprintf("cannot open service: %v", err))
		return State(Unknown), fmt.Errorf("service %s is not installed", name)
	}
	defer s.Close()

	return fn(s, l)
}
Ejemplo n.º 9
0
func runService(name string, isDebug bool) {
	var err error
	if isDebug {
		elog = debug.New(name)
	} else {
		elog, err = eventlog.Open(name)
		if err != nil {
			return
		}
	}
	defer elog.Close()

	elog.Info(1, fmt.Sprintf("starting %s service", name))
	run := svc.Run
	if isDebug {
		run = debug.Run
	}
	err = run(name, &myservice{})
	if err != nil {
		elog.Error(1, fmt.Sprintf("%s service failed: %v", name, err))
		return
	}
	elog.Info(1, fmt.Sprintf("%s service stopped", name))
}