1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // Package eventlog implements access to Windows event log.
15 "golang.org/x/sys/windows"
18 // Log provides access to the system log.
23 // Open retrieves a handle to the specified event log.
24 func Open(source string) (*Log, error) {
25 return OpenRemote("", source)
28 // OpenRemote does the same as Open, but on different computer host.
29 func OpenRemote(host, source string) (*Log, error) {
31 return nil, errors.New("Specify event log source")
35 s = syscall.StringToUTF16Ptr(host)
37 h, err := windows.RegisterEventSource(s, syscall.StringToUTF16Ptr(source))
41 return &Log{Handle: h}, nil
44 // Close closes event log l.
45 func (l *Log) Close() error {
46 return windows.DeregisterEventSource(l.Handle)
49 func (l *Log) report(etype uint16, eid uint32, msg string) error {
50 ss := []*uint16{syscall.StringToUTF16Ptr(msg)}
51 return windows.ReportEvent(l.Handle, etype, 0, eid, 0, 1, 0, &ss[0], nil)
54 // Info writes an information event msg with event id eid to the end of event log l.
55 // When EventCreate.exe is used, eid must be between 1 and 1000.
56 func (l *Log) Info(eid uint32, msg string) error {
57 return l.report(windows.EVENTLOG_INFORMATION_TYPE, eid, msg)
60 // Warning writes an warning event msg with event id eid to the end of event log l.
61 // When EventCreate.exe is used, eid must be between 1 and 1000.
62 func (l *Log) Warning(eid uint32, msg string) error {
63 return l.report(windows.EVENTLOG_WARNING_TYPE, eid, msg)
66 // Error writes an error event msg with event id eid to the end of event log l.
67 // When EventCreate.exe is used, eid must be between 1 and 1000.
68 func (l *Log) Error(eid uint32, msg string) error {
69 return l.report(windows.EVENTLOG_ERROR_TYPE, eid, msg)