@@ -26,35 +26,50 @@ export type WindowCallback = (target: Window) => void | Promise<void>;
26
26
export type MatchResultCallback = ( target : MatchResult ) => void | Promise < void > ;
27
27
export type FindHookCallback = WindowCallback | MatchResultCallback ;
28
28
29
- function validateSearchRegion ( search : Region , screen : Region ) {
29
+ function validateSearchRegion (
30
+ search : Region ,
31
+ screen : Region ,
32
+ providerRegistry : ProviderRegistry
33
+ ) {
34
+ providerRegistry
35
+ . getLogProvider ( )
36
+ . debug ( `Validating search region: ${ search } ` ) ;
30
37
if (
31
38
search . left < 0 ||
32
39
search . top < 0 ||
33
40
search . width < 0 ||
34
41
search . height < 0
35
42
) {
36
- throw new Error ( `Negative values in search region ${ search } ` ) ;
43
+ const e = new Error ( `Negative values in search region` ) ;
44
+ providerRegistry . getLogProvider ( ) . error ( e , { region : search } ) ;
45
+ throw e ;
37
46
}
38
47
if (
39
48
isNaN ( search . left ) ||
40
49
isNaN ( search . top ) ||
41
50
isNaN ( search . width ) ||
42
51
isNaN ( search . height )
43
52
) {
44
- throw new Error ( `NaN values in search region ${ search } ` ) ;
53
+ const e = new Error ( `NaN values in search region` ) ;
54
+ providerRegistry . getLogProvider ( ) . error ( e , { region : search } ) ;
55
+ throw e ;
45
56
}
46
57
if ( search . width < 2 || search . height < 2 ) {
47
- throw new Error (
48
- `Search region ${ search } is not large enough. Must be at least two pixels in both width and height.`
58
+ const e = new Error (
59
+ `Search region is not large enough. Must be at least two pixels in both width and height.`
49
60
) ;
61
+ providerRegistry . getLogProvider ( ) . error ( e , { region : search } ) ;
62
+ throw e ;
50
63
}
51
64
if (
52
65
search . left + search . width > screen . width ||
53
66
search . top + search . height > screen . height
54
67
) {
55
- throw new Error (
56
- `Search region ${ search } extends beyond screen boundaries (${ screen . width } x${ screen . height } )`
68
+ const e = new Error (
69
+ `Search region extends beyond screen boundaries (${ screen . width } x${ screen . height } )`
57
70
) ;
71
+ providerRegistry . getLogProvider ( ) . error ( e , { region : search , screen } ) ;
72
+ throw e ;
58
73
}
59
74
}
60
75
@@ -159,17 +174,21 @@ export class ScreenClass {
159
174
params ?: OptionalSearchParameters < PROVIDER_DATA_TYPE >
160
175
) : Promise < FindResult > {
161
176
const needle = await searchInput ;
177
+ this . providerRegistry . getLogProvider ( ) . info ( `Searching for ${ needle } ` ) ;
162
178
163
179
if ( ! isImage ( needle ) && ! isTextQuery ( needle ) && ! isWindowQuery ( needle ) ) {
164
- throw Error (
180
+ const e = Error (
165
181
`find requires an Image, a text query or a window query, but received ${ JSON . stringify (
166
182
needle
167
183
) } `
168
184
) ;
185
+ this . providerRegistry . getLogProvider ( ) . error ( e , { needle } ) ;
186
+ throw e ;
169
187
}
170
188
171
189
try {
172
190
if ( isWindowQuery ( needle ) ) {
191
+ this . providerRegistry . getLogProvider ( ) . debug ( `Running a window search` ) ;
173
192
const windowHandle = await this . providerRegistry
174
193
. getWindowFinder ( )
175
194
. findMatch ( needle ) ;
@@ -184,6 +203,7 @@ export class ScreenClass {
184
203
}
185
204
return window ;
186
205
} else {
206
+ this . logNeedleType ( needle ) ;
187
207
const { minMatch, screenSize, searchRegion, screenImage } =
188
208
await this . getFindParameters ( params ) ;
189
209
@@ -196,7 +216,7 @@ export class ScreenClass {
196
216
params
197
217
) ;
198
218
199
- validateSearchRegion ( searchRegion , screenSize ) ;
219
+ validateSearchRegion ( searchRegion , screenSize , this . providerRegistry ) ;
200
220
this . providerRegistry . getLogProvider ( ) . debug ( `Search region is valid` ) ;
201
221
const matchResult = await getMatchResult (
202
222
this . providerRegistry ,
@@ -232,7 +252,11 @@ export class ScreenClass {
232
252
}
233
253
}
234
254
} catch ( e ) {
235
- throw new Error ( `Searching for ${ needle . id } failed. Reason: '${ e } '` ) ;
255
+ const error = new Error (
256
+ `Searching for ${ needle . id } failed. Reason: '${ e } '`
257
+ ) ;
258
+ this . providerRegistry . getLogProvider ( ) . error ( error ) ;
259
+ throw error ;
236
260
}
237
261
}
238
262
@@ -254,17 +278,21 @@ export class ScreenClass {
254
278
params ?: OptionalSearchParameters < PROVIDER_DATA_TYPE >
255
279
) : Promise < FindResult [ ] > {
256
280
const needle = await searchInput ;
281
+ this . providerRegistry . getLogProvider ( ) . info ( `Searching for ${ needle } ` ) ;
257
282
258
283
if ( ! isImage ( needle ) && ! isTextQuery ( needle ) && ! isWindowQuery ( needle ) ) {
259
- throw Error (
284
+ const e = Error (
260
285
`findAll requires an Image, a text query or a window query, but received ${ JSON . stringify (
261
286
needle
262
287
) } `
263
288
) ;
289
+ this . providerRegistry . getLogProvider ( ) . error ( e , { needle } ) ;
290
+ throw e ;
264
291
}
265
292
266
293
try {
267
294
if ( isWindowQuery ( needle ) ) {
295
+ this . providerRegistry . getLogProvider ( ) . debug ( `Running a window search` ) ;
268
296
const matches = await this . providerRegistry
269
297
. getWindowFinder ( )
270
298
. findMatches ( needle ) ;
@@ -286,6 +314,7 @@ export class ScreenClass {
286
314
}
287
315
return windows ;
288
316
} else {
317
+ this . logNeedleType ( needle ) ;
289
318
const { minMatch, screenSize, searchRegion, screenImage } =
290
319
await this . getFindParameters ( params ) ;
291
320
@@ -298,7 +327,7 @@ export class ScreenClass {
298
327
params
299
328
) ;
300
329
301
- validateSearchRegion ( searchRegion , screenSize ) ;
330
+ validateSearchRegion ( searchRegion , screenSize , this . providerRegistry ) ;
302
331
this . providerRegistry . getLogProvider ( ) . debug ( `Search region is valid` ) ;
303
332
const matchResults = await getMatchResults (
304
333
this . providerRegistry ,
@@ -339,7 +368,11 @@ export class ScreenClass {
339
368
}
340
369
}
341
370
} catch ( e ) {
342
- throw new Error ( `Searching for ${ needle . id } failed. Reason: '${ e } '` ) ;
371
+ const error = new Error (
372
+ `Searching for ${ needle . id } failed. Reason: '${ e } '`
373
+ ) ;
374
+ this . providerRegistry . getLogProvider ( ) . error ( error ) ;
375
+ throw error ;
343
376
}
344
377
}
345
378
@@ -352,11 +385,13 @@ export class ScreenClass {
352
385
) : Promise < Region > {
353
386
const highlightRegion = await regionToHighlight ;
354
387
if ( ! isRegion ( highlightRegion ) ) {
355
- throw Error (
388
+ const e = Error (
356
389
`highlight requires an Region, but received ${ JSON . stringify (
357
390
highlightRegion
358
391
) } `
359
392
) ;
393
+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
394
+ throw e ;
360
395
}
361
396
this . providerRegistry
362
397
. getLogProvider ( )
@@ -406,11 +441,13 @@ export class ScreenClass {
406
441
const updateIntervalValue = updateInterval ?? 500 ;
407
442
408
443
if ( ! isImage ( needle ) && ! isTextQuery ( needle ) && ! isWindowQuery ( needle ) ) {
409
- throw Error (
444
+ const e = Error (
410
445
`waitFor requires an Image, a text query or a window query, but received ${ JSON . stringify (
411
446
searchInput
412
447
) } `
413
448
) ;
449
+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
450
+ throw e ;
414
451
}
415
452
this . providerRegistry
416
453
. getLogProvider ( )
@@ -447,11 +484,13 @@ export class ScreenClass {
447
484
! isTextQuery ( searchInput ) &&
448
485
! isWindowQuery ( searchInput )
449
486
) {
450
- throw Error (
487
+ const e = new Error (
451
488
`on requires an Image, a text query or a window query, but received ${ JSON . stringify (
452
489
searchInput
453
490
) } `
454
491
) ;
492
+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
493
+ throw e ;
455
494
}
456
495
const existingHooks = this . findHooks . get ( searchInput ) || [ ] ;
457
496
this . findHooks . set ( searchInput , [ ...existingHooks , callback ] ) ;
@@ -481,15 +520,17 @@ export class ScreenClass {
481
520
) : Promise < string > {
482
521
const currentScreen = await this . providerRegistry . getScreen ( ) . grabScreen ( ) ;
483
522
if ( ! isImage ( currentScreen ) ) {
484
- throw Error (
523
+ const e = new Error (
485
524
`capture requires an Image, but received ${ JSON . stringify (
486
525
currentScreen
487
526
) } `
488
527
) ;
528
+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
529
+ throw e ;
489
530
}
490
531
this . providerRegistry
491
532
. getLogProvider ( )
492
- . debug (
533
+ . info (
493
534
`Capturing whole screen (0, 0, ${ currentScreen . width } , ${ currentScreen . height } )`
494
535
) ;
495
536
return this . saveImage (
@@ -506,7 +547,13 @@ export class ScreenClass {
506
547
* {@link grab } grabs screen content of a systems main display
507
548
*/
508
549
public async grab ( ) : Promise < Image > {
509
- return this . providerRegistry . getScreen ( ) . grabScreen ( ) ;
550
+ const currentScreen = await this . providerRegistry . getScreen ( ) . grabScreen ( ) ;
551
+ this . providerRegistry
552
+ . getLogProvider ( )
553
+ . info (
554
+ `Grabbed whole screen (0, 0, ${ currentScreen . width } , ${ currentScreen . height } )`
555
+ ) ;
556
+ return currentScreen ;
510
557
}
511
558
512
559
/**
@@ -528,24 +575,28 @@ export class ScreenClass {
528
575
) : Promise < string > {
529
576
const targetRegion = await regionToCapture ;
530
577
if ( ! isRegion ( targetRegion ) ) {
531
- throw Error (
578
+ const e = new Error (
532
579
`captureRegion requires an Region, but received ${ JSON . stringify (
533
580
targetRegion
534
581
) } `
535
582
) ;
583
+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
584
+ throw e ;
536
585
}
537
586
this . providerRegistry
538
587
. getLogProvider ( )
539
- . debug ( `Capturing screen region ${ targetRegion . toString ( ) } ` ) ;
588
+ . info ( `Capturing screen region ${ targetRegion . toString ( ) } ` ) ;
540
589
const regionImage = await this . providerRegistry
541
590
. getScreen ( )
542
591
. grabScreenRegion ( targetRegion ) ;
543
592
if ( ! isImage ( regionImage ) ) {
544
- throw Error (
593
+ const e = new Error (
545
594
`captureRegion requires an Image, but received ${ JSON . stringify (
546
595
regionImage
547
596
) } `
548
597
) ;
598
+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
599
+ throw e ;
549
600
}
550
601
return this . saveImage (
551
602
regionImage ,
@@ -566,16 +617,21 @@ export class ScreenClass {
566
617
) : Promise < Image > {
567
618
const targetRegion = await regionToGrab ;
568
619
if ( ! isRegion ( targetRegion ) ) {
569
- throw Error (
620
+ const e = new Error (
570
621
`grabRegion requires an Region, but received ${ JSON . stringify (
571
622
targetRegion
572
623
) } `
573
624
) ;
625
+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
626
+ throw e ;
574
627
}
628
+ const screenContent = await this . providerRegistry
629
+ . getScreen ( )
630
+ . grabScreenRegion ( targetRegion ) ;
575
631
this . providerRegistry
576
632
. getLogProvider ( )
577
- . debug ( `Grabbing screen region ${ targetRegion . toString ( ) } `) ;
578
- return this . providerRegistry . getScreen ( ) . grabScreenRegion ( targetRegion ) ;
633
+ . info ( `Grabbed screen region ${ targetRegion . toString ( ) } `) ;
634
+ return screenContent ;
579
635
}
580
636
581
637
/**
@@ -586,9 +642,11 @@ export class ScreenClass {
586
642
const screenContent = await this . providerRegistry . getScreen ( ) . grabScreen ( ) ;
587
643
const inputPoint = await point ;
588
644
if ( ! isPoint ( inputPoint ) ) {
589
- throw Error (
645
+ const e = new Error (
590
646
`colorAt requires a Point, but received ${ JSON . stringify ( inputPoint ) } `
591
647
) ;
648
+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
649
+ throw e ;
592
650
}
593
651
const scaledPoint = new Point (
594
652
inputPoint . x * screenContent . pixelDensity . scaleX ,
@@ -601,9 +659,13 @@ export class ScreenClass {
601
659
screenContent . pixelDensity . scaleX
602
660
} , ${ screenContent . pixelDensity . scaleY } ) into ${ scaledPoint . toString ( ) } `
603
661
) ;
604
- return this . providerRegistry
662
+ const color = await this . providerRegistry
605
663
. getImageProcessor ( )
606
664
. colorAt ( screenContent , scaledPoint ) ;
665
+ this . providerRegistry
666
+ . getLogProvider ( )
667
+ . info ( `Color at ${ inputPoint . toString ( ) } is ${ color . toString ( ) } ` ) ;
668
+ return color ;
607
669
}
608
670
609
671
private async saveImage (
@@ -668,4 +730,12 @@ export class ScreenClass {
668
730
}
669
731
return [ ] ;
670
732
}
733
+
734
+ private logNeedleType ( needle : Image | WordQuery | LineQuery ) {
735
+ if ( isImage ( needle ) ) {
736
+ this . providerRegistry . getLogProvider ( ) . debug ( `Running an image search` ) ;
737
+ } else if ( isTextQuery ( needle ) ) {
738
+ this . providerRegistry . getLogProvider ( ) . debug ( `Running a text search` ) ;
739
+ }
740
+ }
671
741
}
0 commit comments