Skip to content

Commit 0e43f98

Browse files
committed
save
1 parent e62b05c commit 0e43f98

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

Service/system-service_windows.go

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
loggingC "github.com/codemodify/SystemKit/Logging/Contracts"
1919
)
2020

21+
var logTag = "SYSTEM-SERVICE"
22+
2123
// WindowsService - Represents Windows service
2224
type WindowsService struct {
2325
command ServiceCommand
@@ -34,7 +36,7 @@ func New(command ServiceCommand) SystemService {
3436
func (thisRef *WindowsService) Run() error {
3537
logging.Instance().LogDebugWithFields(loggingC.Fields{
3638
"method": helpersReflect.GetThisFuncName(),
37-
"message": fmt.Sprintf("attempting to run: %s", thisRef.command.Name),
39+
"message": fmt.Sprintf("%s: attempting to run: %s", logTag, thisRef.command.Name),
3840
})
3941

4042
wg := sync.WaitGroup{}
@@ -48,20 +50,20 @@ func (thisRef *WindowsService) Run() error {
4850

4951
logging.Instance().LogDebugWithFields(loggingC.Fields{
5052
"method": helpersReflect.GetThisFuncName(),
51-
"message": fmt.Sprintf("running: %s", thisRef.command.Name),
53+
"message": fmt.Sprintf("%s: running: %s", logTag, thisRef.command.Name),
5254
})
5355
wg.Wait()
5456

5557
if err != nil {
5658
logging.Instance().LogErrorWithFields(loggingC.Fields{
5759
"method": helpersReflect.GetThisFuncName(),
58-
"message": fmt.Sprintf("failed to run: %s, %v", thisRef.command.Name, err),
60+
"message": fmt.Sprintf("%s: failed to run: %s, %v", logTag, thisRef.command.Name, err),
5961
})
6062
}
6163

6264
logging.Instance().LogDebugWithFields(loggingC.Fields{
6365
"method": helpersReflect.GetThisFuncName(),
64-
"message": fmt.Sprintf("stopped: %s", thisRef.command.Name),
66+
"message": fmt.Sprintf("%s: stopped: %s", logTag, thisRef.command.Name),
6567
})
6668

6769
return nil
@@ -71,13 +73,13 @@ func (thisRef *WindowsService) Run() error {
7173
func (thisRef *WindowsService) Install(start bool) error {
7274
logging.Instance().LogDebugWithFields(loggingC.Fields{
7375
"method": helpersReflect.GetThisFuncName(),
74-
"message": fmt.Sprintf("attempting to install: %s", thisRef.command.Name),
76+
"message": fmt.Sprintf("%s: attempting to install: %s", logTag, thisRef.command.Name),
7577
})
7678

7779
// check if service exists
7880
logging.Instance().LogDebugWithFields(loggingC.Fields{
7981
"method": helpersReflect.GetThisFuncName(),
80-
"message": fmt.Sprintf("check if exists: %s", thisRef.command.Name),
82+
"message": fmt.Sprintf("%s: check if exists: %s", logTag, thisRef.command.Name),
8183
})
8284

8385
winServiceManager, winService, err := connectAndOpenService(thisRef.command.Name)
@@ -91,7 +93,7 @@ func (thisRef *WindowsService) Install(start bool) error {
9193

9294
logging.Instance().LogDebugWithFields(loggingC.Fields{
9395
"method": helpersReflect.GetThisFuncName(),
94-
"message": fmt.Sprintf("does not exist: %s", thisRef.command.Name),
96+
"message": fmt.Sprintf("%s: does not exist: %s", logTag, thisRef.command.Name),
9597
})
9698
} else {
9799
if winService != nil {
@@ -103,7 +105,7 @@ func (thisRef *WindowsService) Install(start bool) error {
103105

104106
logging.Instance().LogErrorWithFields(loggingC.Fields{
105107
"method": helpersReflect.GetThisFuncName(),
106-
"message": fmt.Sprintf("service '%s' already exists: ", thisRef.command.Name),
108+
"message": fmt.Sprintf("%s: service '%s' already exists: ", logTag, thisRef.command.Name),
107109
})
108110

109111
return fmt.Errorf("service '%s' already exists: ", thisRef.command.Name)
@@ -121,7 +123,7 @@ func (thisRef *WindowsService) Install(start bool) error {
121123
// Create the system service
122124
logging.Instance().LogDebugWithFields(loggingC.Fields{
123125
"method": helpersReflect.GetThisFuncName(),
124-
"message": fmt.Sprintf("creating: '%s', binary: '%s', args: '%s'", thisRef.command.Name, thisRef.command.Executable, thisRef.command.Args),
126+
"message": fmt.Sprintf("%s: creating: '%s', binary: '%s', args: '%s'", logTag, thisRef.command.Name, thisRef.command.Executable, thisRef.command.Args),
125127
})
126128

127129
if winService != nil {
@@ -148,7 +150,7 @@ func (thisRef *WindowsService) Install(start bool) error {
148150

149151
logging.Instance().LogErrorWithFields(loggingC.Fields{
150152
"method": helpersReflect.GetThisFuncName(),
151-
"message": fmt.Sprintf("error creating: %s, details: %v ", thisRef.command.Name, err1),
153+
"message": fmt.Sprintf("%s: error creating: %s, details: %v ", logTag, thisRef.command.Name, err1),
152154
})
153155

154156
return err1
@@ -159,7 +161,7 @@ func (thisRef *WindowsService) Install(start bool) error {
159161

160162
logging.Instance().LogDebugWithFields(loggingC.Fields{
161163
"method": helpersReflect.GetThisFuncName(),
162-
"message": fmt.Sprintf("created: '%s', binary: '%s', args: '%s'", thisRef.command.Name, thisRef.command.Executable, thisRef.command.Args),
164+
"message": fmt.Sprintf("%s: created: '%s', binary: '%s', args: '%s'", logTag, thisRef.command.Name, thisRef.command.Executable, thisRef.command.Args),
163165
})
164166

165167
if start {
@@ -175,7 +177,7 @@ func (thisRef *WindowsService) Install(start bool) error {
175177
func (thisRef *WindowsService) Start() error {
176178
logging.Instance().LogDebugWithFields(loggingC.Fields{
177179
"method": helpersReflect.GetThisFuncName(),
178-
"message": fmt.Sprint("attempting to start: ", thisRef.command.Name),
180+
"message": fmt.Sprint("%s: attempting to start: ", logTag, thisRef.command.Name),
179181
})
180182

181183
winServiceManager, winService, err := connectAndOpenService(thisRef.command.Name)
@@ -189,15 +191,15 @@ func (thisRef *WindowsService) Start() error {
189191
if err1 != nil {
190192
logging.Instance().LogErrorWithFields(loggingC.Fields{
191193
"method": helpersReflect.GetThisFuncName(),
192-
"message": fmt.Sprintf("error starting: %s, %v", thisRef.command.Name, err1),
194+
"message": fmt.Sprintf("%s: error starting: %s, %v", logTag, thisRef.command.Name, err1),
193195
})
194196

195197
return fmt.Errorf("error starting: %s, %v", thisRef.command.Name, err1)
196198
}
197199

198200
logging.Instance().LogDebugWithFields(loggingC.Fields{
199201
"method": helpersReflect.GetThisFuncName(),
200-
"message": fmt.Sprintf("started: %s", thisRef.command.Name),
202+
"message": fmt.Sprintf("%s: started: %s", logTag, thisRef.command.Name),
201203
})
202204

203205
return nil
@@ -220,7 +222,7 @@ func (thisRef *WindowsService) Restart() error {
220222
func (thisRef *WindowsService) Stop() error {
221223
logging.Instance().LogDebugWithFields(loggingC.Fields{
222224
"method": helpersReflect.GetThisFuncName(),
223-
"message": fmt.Sprint("attempting to stop: ", thisRef.command.Name),
225+
"message": fmt.Sprint("%s: attempting to stop: ", logTag, thisRef.command.Name),
224226
})
225227

226228
err := thisRef.control(svc.Stop, svc.Stopped)
@@ -241,7 +243,7 @@ func (thisRef *WindowsService) Stop() error {
241243

242244
logging.Instance().LogDebugWithFields(loggingC.Fields{
243245
"method": helpersReflect.GetThisFuncName(),
244-
"message": fmt.Sprint("waiting for service to stop"),
246+
"message": fmt.Sprint("%s: waiting for service to stop", logTag),
245247
})
246248

247249
// Wait a few seconds before retrying
@@ -270,7 +272,7 @@ func (thisRef *WindowsService) Stop() error {
270272
func (thisRef *WindowsService) Uninstall() error {
271273
logging.Instance().LogDebugWithFields(loggingC.Fields{
272274
"method": helpersReflect.GetThisFuncName(),
273-
"message": fmt.Sprintf("attempting to uninstall: %s", thisRef.command.Name),
275+
"message": fmt.Sprintf("%s: attempting to uninstall: %s", logTag, thisRef.command.Name),
274276
})
275277

276278
winServiceManager, winService, err := connectAndOpenService(thisRef.command.Name)
@@ -284,15 +286,15 @@ func (thisRef *WindowsService) Uninstall() error {
284286
if err1 != nil {
285287
logging.Instance().LogErrorWithFields(loggingC.Fields{
286288
"method": helpersReflect.GetThisFuncName(),
287-
"message": fmt.Sprintf("failed to uninstall: %s, %v", thisRef.command.Name, err1),
289+
"message": fmt.Sprintf("%s: failed to uninstall: %s, %v", logTag, thisRef.command.Name, err1),
288290
})
289291

290292
return err1
291293
}
292294

293295
logging.Instance().LogDebugWithFields(loggingC.Fields{
294296
"method": helpersReflect.GetThisFuncName(),
295-
"message": fmt.Sprintf("uninstalled: %s", thisRef.command.Name),
297+
"message": fmt.Sprintf("%s: uninstalled: %s", logTag, thisRef.command.Name),
296298
})
297299

298300
return nil
@@ -302,7 +304,7 @@ func (thisRef *WindowsService) Uninstall() error {
302304
func (thisRef *WindowsService) Status() (ServiceStatus, error) {
303305
logging.Instance().LogDebugWithFields(loggingC.Fields{
304306
"method": helpersReflect.GetThisFuncName(),
305-
"message": fmt.Sprintf("querying status: %s", thisRef.command.Name),
307+
"message": fmt.Sprintf("%s: querying status: %s", logTag, thisRef.command.Name),
306308
})
307309

308310
winServiceManager, winService, err := connectAndOpenService(thisRef.command.Name)
@@ -316,15 +318,15 @@ func (thisRef *WindowsService) Status() (ServiceStatus, error) {
316318
if err1 != nil {
317319
logging.Instance().LogErrorWithFields(loggingC.Fields{
318320
"method": helpersReflect.GetThisFuncName(),
319-
"message": fmt.Sprint("error getting service status: ", err1),
321+
"message": fmt.Sprint("%s: error getting service status: ", logTag, err1),
320322
})
321323

322324
return ServiceStatus{}, fmt.Errorf("error getting service status: %v", err1)
323325
}
324326

325327
logging.Instance().LogDebugWithFields(loggingC.Fields{
326328
"method": helpersReflect.GetThisFuncName(),
327-
"message": fmt.Sprintf("service status: %#v", stat),
329+
"message": fmt.Sprintf("%s: service status: %#v", logTag, stat),
328330
})
329331

330332
return ServiceStatus{
@@ -337,22 +339,22 @@ func (thisRef *WindowsService) Status() (ServiceStatus, error) {
337339
func (thisRef *WindowsService) Exists() bool {
338340
logging.Instance().LogDebugWithFields(loggingC.Fields{
339341
"method": helpersReflect.GetThisFuncName(),
340-
"message": fmt.Sprintf("checking existance: %s", thisRef.command.Name),
342+
"message": fmt.Sprintf("%s: checking existance: %s", logTag, thisRef.command.Name),
341343
})
342344

343345
args := []string{"queryex", fmt.Sprintf("\"%s\"", thisRef.command.Name)}
344346

345347
// https://www.computerhope.com/sc-command.htm
346348
logging.Instance().LogDebugWithFields(loggingC.Fields{
347349
"method": helpersReflect.GetThisFuncName(),
348-
"message": fmt.Sprintf("running: 'sc %s'", strings.Join(args, " ")),
350+
"message": fmt.Sprintf("%s: running: 'sc %s'", logTag, strings.Join(args, " ")),
349351
})
350352

351353
_, err := helpersExec.ExecWithArgs("sc", args...)
352354
if err != nil {
353355
logging.Instance().LogErrorWithFields(loggingC.Fields{
354356
"method": helpersReflect.GetThisFuncName(),
355-
"message": fmt.Sprintf("error when checking %s: ", err),
357+
"message": fmt.Sprintf("%s: error when checking %s: ", logTag, err),
356358
})
357359

358360
return false
@@ -375,7 +377,7 @@ func (thisRef *WindowsService) FileContent() ([]byte, error) {
375377
func (thisRef *WindowsService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
376378
logging.Instance().LogDebugWithFields(loggingC.Fields{
377379
"method": helpersReflect.GetThisFuncName(),
378-
"message": fmt.Sprint("WINDOWS SERVICE EXECUTE"),
380+
"message": fmt.Sprintf("%s: WINDOWS SERVICE EXECUTE", logTag),
379381
})
380382

381383
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
@@ -404,7 +406,7 @@ loop:
404406
testOutput += fmt.Sprintf("-%d", c.Context)
405407
logging.Instance().LogDebugWithFields(loggingC.Fields{
406408
"method": helpersReflect.GetThisFuncName(),
407-
"message": fmt.Sprint(testOutput),
409+
"message": fmt.Sprintf("%s: %", logTag, testOutput),
408410
})
409411

410412
break loop
@@ -418,7 +420,7 @@ loop:
418420
default:
419421
logging.Instance().LogWarningWithFields(loggingC.Fields{
420422
"method": helpersReflect.GetThisFuncName(),
421-
"message": fmt.Sprintf("unexpected control request #%d", c),
423+
"message": fmt.Sprintf("%s: unexpected control request #%d", logTag, c),
422424
})
423425
}
424426
}
@@ -431,7 +433,7 @@ loop:
431433
func (thisRef *WindowsService) control(command svc.Cmd, state svc.State) error {
432434
logging.Instance().LogDebugWithFields(loggingC.Fields{
433435
"method": helpersReflect.GetThisFuncName(),
434-
"message": fmt.Sprintf("attempting to control: %s", thisRef.command.Name),
436+
"message": fmt.Sprintf("%s: attempting to control: %s, cmd: %v", logTag, thisRef.command.Name, command),
435437
})
436438

437439
winServiceManager, winService, err := connectAndOpenService(thisRef.command.Name)
@@ -445,7 +447,7 @@ func (thisRef *WindowsService) control(command svc.Cmd, state svc.State) error {
445447
if err1 != nil {
446448
logging.Instance().LogErrorWithFields(loggingC.Fields{
447449
"method": helpersReflect.GetThisFuncName(),
448-
"message": fmt.Sprintf("could not send control: %d, to: %s, details: %v", command, thisRef.command.Name, err1),
450+
"message": fmt.Sprintf("%s: could not send control: %d, to: %s, details: %v", logTag, command, thisRef.command.Name, err1),
449451
})
450452

451453
return fmt.Errorf("could not send control: %d, to: %s, details: %v", command, thisRef.command.Name, err1)
@@ -457,7 +459,7 @@ func (thisRef *WindowsService) control(command svc.Cmd, state svc.State) error {
457459
if timeout.Before(time.Now()) {
458460
logging.Instance().LogErrorWithFields(loggingC.Fields{
459461
"method": helpersReflect.GetThisFuncName(),
460-
"message": fmt.Sprintf("timeout waiting for service to go to state=%d", state),
462+
"message": fmt.Sprintf("%s: timeout waiting for service to go to state=%d", logTag, state),
461463
})
462464

463465
return fmt.Errorf("timeout waiting for service to go to state=%d", state)
@@ -470,7 +472,7 @@ func (thisRef *WindowsService) control(command svc.Cmd, state svc.State) error {
470472
if err1 != nil {
471473
logging.Instance().LogErrorWithFields(loggingC.Fields{
472474
"method": helpersReflect.GetThisFuncName(),
473-
"message": fmt.Sprintf("could not retrieve service status: %v", err1),
475+
"message": fmt.Sprintf("%s: could not retrieve service status: %v", logTag, err1),
474476
})
475477

476478
return fmt.Errorf("could not retrieve service status: %v", err1)
@@ -484,29 +486,29 @@ func connectAndOpenService(serviceName string) (*mgr.Mgr, *mgr.Service, ServiceE
484486
// connect to Windows Service Manager
485487
logging.Instance().LogDebugWithFields(loggingC.Fields{
486488
"method": helpersReflect.GetThisFuncName(),
487-
"message": fmt.Sprint("connecting to Windows Service Manager"),
489+
"message": fmt.Sprintf("%s: connecting to Windows Service Manager", logTag),
488490
})
489491

490492
winServiceManager, err := mgr.Connect()
491493
if err != nil {
492494
logging.Instance().LogErrorWithFields(loggingC.Fields{
493495
"method": helpersReflect.GetThisFuncName(),
494-
"message": fmt.Sprintf("error connecting to Windows Service Manager: %v", err),
496+
"message": fmt.Sprintf("%s: error connecting to Windows Service Manager: %v", logTag, err),
495497
})
496498
return nil, nil, ServiceError{Type: ServiceErrorOther, Details: err}
497499
}
498500

499501
// open service to manage it
500502
logging.Instance().LogDebugWithFields(loggingC.Fields{
501503
"method": helpersReflect.GetThisFuncName(),
502-
"message": fmt.Sprintf("opening service: %s", serviceName),
504+
"message": fmt.Sprintf("%s: opening service: %s", logTag, serviceName),
503505
})
504506

505507
winService, err := winServiceManager.OpenService(serviceName)
506508
if err != nil {
507509
logging.Instance().LogErrorWithFields(loggingC.Fields{
508510
"method": helpersReflect.GetThisFuncName(),
509-
"message": fmt.Sprintf("error opening service: %s, %v", serviceName, err),
511+
"message": fmt.Sprintf("%s: error opening service: %s, %v", logTag, serviceName, err),
510512
})
511513

512514
return winServiceManager, nil, ServiceError{Type: ServiceErrorDoesNotExist, Details: err}

0 commit comments

Comments
 (0)