From 811b34668cba228ba966dfda7ae1b319af99d8fe Mon Sep 17 00:00:00 2001 From: Kevin Parsons Date: Tue, 19 Mar 2019 00:09:35 -0700 Subject: [PATCH] Provide a hook constructor which accepts an ETW provider Previously, the only constructor for the etwlogrus hook accepted a name, and created a new ETW provider with that name. With this change, there is another constructor which takes an already created ETW provider. This is to allow the use of the ETW provider for other things, such as if the application wants to support ETW capture state. --- pkg/etwlogrus/hook.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/pkg/etwlogrus/hook.go b/pkg/etwlogrus/hook.go index 0a067df..715214e 100644 --- a/pkg/etwlogrus/hook.go +++ b/pkg/etwlogrus/hook.go @@ -10,20 +10,25 @@ import ( // Hook is a Logrus hook which logs received events to ETW. type Hook struct { - provider *etw.Provider + provider *etw.Provider + closeProvider bool } -// NewHook registers a new ETW provider and returns a hook to log from it. +// NewHook registers a new ETW provider and returns a hook to log from it. The +// provider will be closed when the hook is closed. func NewHook(providerName string) (*Hook, error) { - hook := Hook{} - provider, err := etw.NewProvider(providerName, nil) if err != nil { return nil, err } - hook.provider = provider - return &hook, nil + return &Hook{provider, true}, nil +} + +// NewHookFromProvider creates a new hook based on an existing ETW provider. The +// provider will not be closed when the hook is closed. +func NewHookFromProvider(provider *etw.Provider) (*Hook, error) { + return &Hook{provider, false}, nil } // Levels returns the set of levels that this hook wants to receive log entries @@ -186,7 +191,12 @@ func getFieldOpt(k string, v interface{}) etw.FieldOpt { return etw.StringField(k, fmt.Sprintf("(Unsupported: %T) %v", v, v)) } -// Close cleans up the hook and closes the ETW provider. +// Close cleans up the hook and closes the ETW provider. If the provder was +// registered by etwlogrus, it will be closed as part of `Close`. If the +// provider was passed in, it will not be closed. func (h *Hook) Close() error { - return h.provider.Close() + if h.closeProvider { + return h.provider.Close() + } + return nil }