@@ -11,42 +11,51 @@ import (
1111 "time"
1212
1313 "github.com/cockroachdb/errors"
14- "github.com/google/uuid"
1514 "github.com/imroc/req/v3"
1615 "github.com/rs/zerolog"
1716 "github.com/tidwall/gjson"
1817 "meow.tf/streamdeck/sdk"
1918)
2019
2120type Instance struct {
22- cfg * config
23- contextApp string
24- lg zerolog.Logger
25- executor ScriptExecutor
26- ctx context.Context
27- ctxCancel context.CancelFunc
28- mut sync.Mutex
21+ cfg * config
22+ contextApp string
23+ currentLogger zerolog.Logger
24+ executor ScriptExecutor
25+ ctx context.Context
26+ ctxCancel context.CancelFunc
27+ mut sync.Mutex
2928}
3029
3130func NewInstance (
3231 contextApp string ,
3332 executor ScriptExecutor ,
33+ logger zerolog.Logger ,
3434) * Instance {
3535 return & Instance {
36- contextApp : contextApp ,
37- lg : lg .With ().Str ("context_id" , contextApp ).Logger (),
38- executor : executor ,
39- mut : sync.Mutex {},
36+ contextApp : contextApp ,
37+ currentLogger : logger .With ().Str ("context_id" , contextApp ).Logger (),
38+ executor : executor ,
39+ mut : sync.Mutex {},
4040 }
4141}
4242
43+ func (i * Instance ) GetLogger () zerolog.Logger {
44+ return i .currentLogger
45+ }
46+
4347func (i * Instance ) SetConfig (ctxId string , cfg * config ) {
4448 if i .contextApp != ctxId {
4549 return
4650 }
4751
4852 i .cfg = cfg
49- lg .Debug ().Msg ("set config" )
53+
54+ if i .cfg .MinLogLevel != nil {
55+ i .currentLogger = i .currentLogger .Level (* i .cfg .MinLogLevel )
56+ } else {
57+ i .currentLogger = i .currentLogger .Level (zerolog .WarnLevel )
58+ }
5059}
5160
5261func (i * Instance ) ShowAlert () {
@@ -55,7 +64,7 @@ func (i *Instance) ShowAlert() {
5564
5665func (i * Instance ) KeyPressed () {
5766 if i .cfg == nil {
58- lg .Error ().Msg ("global config not set" )
67+ i . currentLogger .Error ().Msg ("global config not set" )
5968 sdk .ShowAlert (i .contextApp )
6069 return
6170 }
@@ -65,12 +74,12 @@ func (i *Instance) KeyPressed() {
6574 targetUrl = i .cfg .ApiUrl
6675 }
6776
68- targetUrl = runTemplate (targetUrl , i .cfg )
77+ targetUrl = runTemplate (targetUrl , i .cfg , i . currentLogger )
6978
7079 if err := exec .Command ("rundll32" ,
7180 "url.dll,FileProtocolHandler" , targetUrl ).Start (); err != nil {
7281
73- lg .Error ().Msg ("global config not set" )
82+ i . currentLogger .Error ().Msg ("global config not set" )
7483 sdk .ShowAlert (i .contextApp )
7584 return
7685 }
@@ -108,15 +117,14 @@ func (i *Instance) run() {
108117 interval = i .cfg .IntervalSeconds
109118 }
110119
111- newLogger := lg .With ().Str ("id" , uuid .NewString ()).Logger ()
112120 innerCtx , innerCancel := context .WithCancel (ctx )
113- innerCtx = newLogger .WithContext (innerCtx )
121+ innerCtx = i . currentLogger .WithContext (innerCtx )
114122
115123 processErr := i .sendAndProcess (innerCtx )
116124 innerCancel ()
117125
118126 if processErr != nil {
119- lg .Err (errors .Wrap (processErr , "error processing response" )).Send ()
127+ i . currentLogger .Err (errors .Wrap (processErr , "error processing response" )).Send ()
120128 i .ShowAlert ()
121129 } else {
122130 if i .cfg .ShowSuccessNotification {
@@ -129,12 +137,12 @@ func (i *Instance) run() {
129137}
130138
131139func (i * Instance ) sendAndProcess (ctx context.Context ) error {
132- apiUrl := runTemplate (i .cfg .ApiUrl , i .cfg )
140+ apiUrl := runTemplate (i .cfg .ApiUrl , i .cfg , i . currentLogger )
133141 httpReq := req .C ().NewRequest ()
134142 httpReq = httpReq .SetContext (ctx )
135143
136144 for k , v := range i .cfg .Headers {
137- httpReq .SetHeader (k , runTemplate (v , i .cfg ))
145+ httpReq .SetHeader (k , runTemplate (v , i .cfg , i . currentLogger ))
138146 }
139147
140148 zerolog .Ctx (ctx ).Trace ().Str ("url" , apiUrl ).Msg ("sending request" )
@@ -144,7 +152,7 @@ func (i *Instance) sendAndProcess(ctx context.Context) error {
144152 }
145153
146154 value := resp .String ()
147- zerolog .Ctx (ctx ).Debug ().Str ("response" , value ).Msg ("got raw response" )
155+ zerolog .Ctx (ctx ).Trace ().Str ("response" , value ).Msg ("got raw response" )
148156
149157 if strings .TrimSpace (i .cfg .BodyScript ) != "" {
150158 zerolog .Ctx (ctx ).Trace ().Str ("script" , i .cfg .BodyScript ).Msg ("executing script" )
@@ -185,14 +193,14 @@ func (i *Instance) sendAndProcess(ctx context.Context) error {
185193 }
186194 }
187195
188- zerolog .Ctx (ctx ).Debug ().Str ("final_result" , value ).Msgf ("final" )
196+ zerolog .Ctx (ctx ).Info ().Str ("final_result" , value ).Msgf ("final" )
189197
190198 return i .handleResponse (ctx , value )
191199}
192200
193201func (i * Instance ) handleResponse (_ context.Context , response string ) error {
194202 var sb strings.Builder
195- prefix := runTemplate (i .cfg .TitlePrefix , i .cfg )
203+ prefix := runTemplate (i .cfg .TitlePrefix , i .cfg , i . currentLogger )
196204 if prefix != "" {
197205 sb .WriteString (strings .ReplaceAll (prefix , "\\ n" , "\n " ) + "\n " )
198206 }
@@ -217,7 +225,7 @@ func (i *Instance) handleResponse(_ context.Context, response string) error {
217225 mapped = def
218226 } else if ! ok && ! defaultOk {
219227 sb .WriteString ("!! NO !!\n !! MAPPING !!" )
220- lg .Error ().Msgf ("response mapper not found for value - %v" , response )
228+ i . currentLogger .Error ().Msgf ("response mapper not found for value - %v" , response )
221229
222230 sdk .SetTitle (i .contextApp , sb .String (), 0 )
223231 sdk .SetImage (i .contextApp , "" , 0 )
0 commit comments