...
  
  
     1  
     2  
     3  
     4  
     5  
     6  
     7  
     8  package eventlog
     9  
    10  import (
    11  	"errors"
    12  	"syscall"
    13  
    14  	"golang.org/x/sys/windows"
    15  )
    16  
    17  
    18  type Log struct {
    19  	Handle windows.Handle
    20  }
    21  
    22  
    23  func Open(source string) (*Log, error) {
    24  	return OpenRemote("", source)
    25  }
    26  
    27  
    28  func OpenRemote(host, source string) (*Log, error) {
    29  	if source == "" {
    30  		return nil, errors.New("Specify event log source")
    31  	}
    32  	var s *uint16
    33  	if host != "" {
    34  		s = syscall.StringToUTF16Ptr(host)
    35  	}
    36  	h, err := windows.RegisterEventSource(s, syscall.StringToUTF16Ptr(source))
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	return &Log{Handle: h}, nil
    41  }
    42  
    43  
    44  func (l *Log) Close() error {
    45  	return windows.DeregisterEventSource(l.Handle)
    46  }
    47  
    48  func (l *Log) report(etype uint16, eid uint32, msg string) error {
    49  	ss := []*uint16{syscall.StringToUTF16Ptr(msg)}
    50  	return windows.ReportEvent(l.Handle, etype, 0, eid, 0, 1, 0, &ss[0], nil)
    51  }
    52  
    53  
    54  
    55  func (l *Log) Info(eid uint32, msg string) error {
    56  	return l.report(windows.EVENTLOG_INFORMATION_TYPE, eid, msg)
    57  }
    58  
    59  
    60  
    61  func (l *Log) Warning(eid uint32, msg string) error {
    62  	return l.report(windows.EVENTLOG_WARNING_TYPE, eid, msg)
    63  }
    64  
    65  
    66  
    67  func (l *Log) Error(eid uint32, msg string) error {
    68  	return l.report(windows.EVENTLOG_ERROR_TYPE, eid, msg)
    69  }
    70  
View as plain text