OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / sys / windows / svc / eventlog / log.go
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.
4
5 // +build windows
6
7 // Package eventlog implements access to Windows event log.
8 //
9 package eventlog
10
11 import (
12         "errors"
13         "syscall"
14
15         "golang.org/x/sys/windows"
16 )
17
18 // Log provides access to the system log.
19 type Log struct {
20         Handle windows.Handle
21 }
22
23 // Open retrieves a handle to the specified event log.
24 func Open(source string) (*Log, error) {
25         return OpenRemote("", source)
26 }
27
28 // OpenRemote does the same as Open, but on different computer host.
29 func OpenRemote(host, source string) (*Log, error) {
30         if source == "" {
31                 return nil, errors.New("Specify event log source")
32         }
33         var s *uint16
34         if host != "" {
35                 s = syscall.StringToUTF16Ptr(host)
36         }
37         h, err := windows.RegisterEventSource(s, syscall.StringToUTF16Ptr(source))
38         if err != nil {
39                 return nil, err
40         }
41         return &Log{Handle: h}, nil
42 }
43
44 // Close closes event log l.
45 func (l *Log) Close() error {
46         return windows.DeregisterEventSource(l.Handle)
47 }
48
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)
52 }
53
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)
58 }
59
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)
64 }
65
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)
70 }