github.com/go-xe2/third@v1.0.3/golang.org/x/sys/windows/svc/eventlog/install.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 8 9 import ( 10 "errors" 11 12 "github.com/go-xe2/third/golang.org/x/sys/windows" 13 "github.com/go-xe2/third/golang.org/x/sys/windows/registry" 14 ) 15 16 const ( 17 // Log levels. 18 Info = windows.EVENTLOG_INFORMATION_TYPE 19 Warning = windows.EVENTLOG_WARNING_TYPE 20 Error = windows.EVENTLOG_ERROR_TYPE 21 ) 22 23 const addKeyName = `SYSTEM\CurrentControlSet\Services\EventLog\Application` 24 25 // Install modifies PC registry to allow logging with an event source src. 26 // It adds all required keys and values to the event log registry key. 27 // Install uses msgFile as the event message file. If useExpandKey is true, 28 // the event message file is installed as REG_EXPAND_SZ value, 29 // otherwise as REG_SZ. Use bitwise of log.Error, log.Warning and 30 // log.Info to specify events supported by the new event source. 31 func Install(src, msgFile string, useExpandKey bool, eventsSupported uint32) error { 32 appkey, err := registry.OpenKey(registry.LOCAL_MACHINE, addKeyName, registry.CREATE_SUB_KEY) 33 if err != nil { 34 return err 35 } 36 defer appkey.Close() 37 38 sk, alreadyExist, err := registry.CreateKey(appkey, src, registry.SET_VALUE) 39 if err != nil { 40 return err 41 } 42 defer sk.Close() 43 if alreadyExist { 44 return errors.New(addKeyName + `\` + src + " registry key already exists") 45 } 46 47 err = sk.SetDWordValue("CustomSource", 1) 48 if err != nil { 49 return err 50 } 51 if useExpandKey { 52 err = sk.SetExpandStringValue("EventMessageFile", msgFile) 53 } else { 54 err = sk.SetStringValue("EventMessageFile", msgFile) 55 } 56 if err != nil { 57 return err 58 } 59 err = sk.SetDWordValue("TypesSupported", eventsSupported) 60 if err != nil { 61 return err 62 } 63 return nil 64 } 65 66 // InstallAsEventCreate is the same as Install, but uses 67 // %SystemRoot%\System32\EventCreate.exe as the event message file. 68 func InstallAsEventCreate(src string, eventsSupported uint32) error { 69 return Install(src, "%SystemRoot%\\System32\\EventCreate.exe", true, eventsSupported) 70 } 71 72 // Remove deletes all registry elements installed by the correspondent Install. 73 func Remove(src string) error { 74 appkey, err := registry.OpenKey(registry.LOCAL_MACHINE, addKeyName, registry.SET_VALUE) 75 if err != nil { 76 return err 77 } 78 defer appkey.Close() 79 return registry.DeleteKey(appkey, src) 80 }