github.com/etecs-ru/go-sys-wineventlog@v0.0.0-20210227233244-4c3abb794018/windows/svc/eventlog/log.go (about)

     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  // Report write an event message with event type etype and event ID eid to the
    50  // end of event log l.
    51  // etype should be one of Info, Success, Warning, Error, AuditSuccess, or AuditFailure.
    52  // When EventCreate.exe is used, eid must be between 1 and 1000.
    53  func (l *Log) Report(etype uint16, eid uint32, msgs []string) error {
    54  	var msgPtrs []*uint16
    55  	for _, msg := range msgs {
    56  		msgPtrs = append(msgPtrs, syscall.StringToUTF16Ptr(msg))
    57  	}
    58  	var ptr **uint16
    59  	if len(msgPtrs) > 0 {
    60  		ptr = &msgPtrs[0]
    61  	}
    62  	return windows.ReportEvent(l.Handle, etype, 0, eid, 0, uint16(len(msgPtrs)), 0, ptr, nil)
    63  }
    64  
    65  // Success writes a success event msg with event id eid to the end of event log l.
    66  // When EventCreate.exe is used, eid must be between 1 and 1000.
    67  func (l *Log) Success(eid uint32, msg string) error {
    68  	return l.Report(Success, eid, []string{msg})
    69  }
    70  
    71  // Info writes an information event msg with event id eid to the end of event log l.
    72  // When EventCreate.exe is used, eid must be between 1 and 1000.
    73  func (l *Log) Info(eid uint32, msg string) error {
    74  	return l.Report(Info, eid, []string{msg})
    75  }
    76  
    77  // Warning writes an warning event msg with event id eid to the end of event log l.
    78  // When EventCreate.exe is used, eid must be between 1 and 1000.
    79  func (l *Log) Warning(eid uint32, msg string) error {
    80  	return l.Report(Warning, eid, []string{msg})
    81  }
    82  
    83  // Error writes an error event msg with event id eid to the end of event log l.
    84  // When EventCreate.exe is used, eid must be between 1 and 1000.
    85  func (l *Log) Error(eid uint32, msg string) error {
    86  	return l.Report(Error, eid, []string{msg})
    87  }
    88  
    89  // AuditSuccess writes an audit event msg with event id eid to the end of event log l.
    90  // When EventCreate.exe is used, eid must be between 1 and 1000.
    91  func (l *Log) AuditSuccess(eid uint32, msg string) error {
    92  	return l.Report(AuditSuccess, eid, []string{msg})
    93  }
    94  
    95  // AuditFailure writes an audit event msg with event id eid to the end of event log l.
    96  // When EventCreate.exe is used, eid must be between 1 and 1000.
    97  func (l *Log) AuditFailure(eid uint32, msg string) error {
    98  	return l.Report(AuditFailure, eid, []string{msg})
    99  }