@@ -9,48 +9,45 @@ import (
99// <product_name>-<component_name>
1010const providername = "ACN-Monitoring"
1111
12- type ETWCore struct {
12+ type Core struct {
1313 provider * etw.Provider
1414 eventName string
1515 encoder zapcore.Encoder
16- fields []zapcore.Field
1716 zapcore.LevelEnabler
1817}
1918
20- func NewETWCore ( eventName string , encoder zapcore.Encoder , levelEnabler zapcore.LevelEnabler ) (* ETWCore , error ) {
21- provider , err := etw .NewProviderWithOptions (providername )
19+ func New ( providerName , eventName string , encoder zapcore.Encoder , levelEnabler zapcore.LevelEnabler ) (zapcore. Core , func () , error ) {
20+ provider , err := etw .NewProviderWithOptions (providerName )
2221 if err != nil {
23- return nil , errors .Wrap (err , "failed to create ETW provider" )
22+ return nil , func () { _ = provider . Close () }, errors .Wrap (err , "failed to create ETW provider" )
2423 }
25- return & ETWCore {
24+ return & Core {
2625 provider : provider ,
2726 eventName : eventName ,
2827 encoder : encoder ,
2928 LevelEnabler : levelEnabler ,
30- }, nil
29+ }, func () { _ = provider . Close () }, nil
3130}
3231
33- func (core * ETWCore ) With (fields []zapcore.Field ) zapcore.Core {
34- return & ETWCore {
35- provider : core .provider ,
36- eventName : core .eventName ,
37- encoder : core .encoder ,
38- LevelEnabler : core .LevelEnabler ,
39- fields : append (core .fields , fields ... ),
32+ func (core * Core ) With (fields []zapcore.Field ) zapcore.Core {
33+ clone := core .clone ()
34+ for i := range fields {
35+ fields [i ].AddTo (clone .encoder )
4036 }
37+ return clone
4138}
4239
4340// Check is an implementation of the zapcore.Core interface's Check method.
4441// Check determines whether the logger core is enabled at the supplied zapcore.Entry's Level.
4542// If enabled, it adds the core to the CheckedEntry and returns it, otherwise returns the CheckedEntry unchanged.
46- func (core * ETWCore ) Check (entry zapcore.Entry , checkedEntry * zapcore.CheckedEntry ) * zapcore.CheckedEntry {
43+ func (core * Core ) Check (entry zapcore.Entry , checkedEntry * zapcore.CheckedEntry ) * zapcore.CheckedEntry {
4744 if core .Enabled (entry .Level ) {
4845 return checkedEntry .AddCore (entry , core )
4946 }
5047 return checkedEntry
5148}
5249
53- func (core * ETWCore ) Write (entry zapcore.Entry , fields []zapcore.Field ) error {
50+ func (core * Core ) Write (entry zapcore.Entry , fields []zapcore.Field ) error {
5451 etwLevel := zapLevelToETWLevel (entry .Level )
5552
5653 buffer , err := core .encoder .EncodeEntry (entry , fields )
@@ -70,10 +67,19 @@ func (core *ETWCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
7067 return nil
7168}
7269
73- func (core * ETWCore ) Sync () error {
70+ func (core * Core ) Sync () error {
7471 return nil
7572}
7673
74+ func (core * Core ) clone () * Core {
75+ return & Core {
76+ provider : core .provider ,
77+ eventName : core .eventName ,
78+ encoder : core .encoder .Clone (),
79+ LevelEnabler : core .LevelEnabler ,
80+ }
81+ }
82+
7783func zapLevelToETWLevel (level zapcore.Level ) etw.Level {
7884 switch level {
7985 case zapcore .DebugLevel :
0 commit comments