@@ -26,35 +26,50 @@ export type WindowCallback = (target: Window) => void | Promise<void>;
2626export type MatchResultCallback = ( target : MatchResult ) => void | Promise < void > ;
2727export type FindHookCallback = WindowCallback | MatchResultCallback ;
2828
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 } ` ) ;
3037 if (
3138 search . left < 0 ||
3239 search . top < 0 ||
3340 search . width < 0 ||
3441 search . height < 0
3542 ) {
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 ;
3746 }
3847 if (
3948 isNaN ( search . left ) ||
4049 isNaN ( search . top ) ||
4150 isNaN ( search . width ) ||
4251 isNaN ( search . height )
4352 ) {
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 ;
4556 }
4657 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.`
4960 ) ;
61+ providerRegistry . getLogProvider ( ) . error ( e , { region : search } ) ;
62+ throw e ;
5063 }
5164 if (
5265 search . left + search . width > screen . width ||
5366 search . top + search . height > screen . height
5467 ) {
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 } )`
5770 ) ;
71+ providerRegistry . getLogProvider ( ) . error ( e , { region : search , screen } ) ;
72+ throw e ;
5873 }
5974}
6075
@@ -159,17 +174,21 @@ export class ScreenClass {
159174 params ?: OptionalSearchParameters < PROVIDER_DATA_TYPE >
160175 ) : Promise < FindResult > {
161176 const needle = await searchInput ;
177+ this . providerRegistry . getLogProvider ( ) . info ( `Searching for ${ needle } ` ) ;
162178
163179 if ( ! isImage ( needle ) && ! isTextQuery ( needle ) && ! isWindowQuery ( needle ) ) {
164- throw Error (
180+ const e = Error (
165181 `find requires an Image, a text query or a window query, but received ${ JSON . stringify (
166182 needle
167183 ) } `
168184 ) ;
185+ this . providerRegistry . getLogProvider ( ) . error ( e , { needle } ) ;
186+ throw e ;
169187 }
170188
171189 try {
172190 if ( isWindowQuery ( needle ) ) {
191+ this . providerRegistry . getLogProvider ( ) . debug ( `Running a window search` ) ;
173192 const windowHandle = await this . providerRegistry
174193 . getWindowFinder ( )
175194 . findMatch ( needle ) ;
@@ -184,6 +203,7 @@ export class ScreenClass {
184203 }
185204 return window ;
186205 } else {
206+ this . logNeedleType ( needle ) ;
187207 const { minMatch, screenSize, searchRegion, screenImage } =
188208 await this . getFindParameters ( params ) ;
189209
@@ -196,7 +216,7 @@ export class ScreenClass {
196216 params
197217 ) ;
198218
199- validateSearchRegion ( searchRegion , screenSize ) ;
219+ validateSearchRegion ( searchRegion , screenSize , this . providerRegistry ) ;
200220 this . providerRegistry . getLogProvider ( ) . debug ( `Search region is valid` ) ;
201221 const matchResult = await getMatchResult (
202222 this . providerRegistry ,
@@ -232,7 +252,11 @@ export class ScreenClass {
232252 }
233253 }
234254 } 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 ;
236260 }
237261 }
238262
@@ -254,17 +278,21 @@ export class ScreenClass {
254278 params ?: OptionalSearchParameters < PROVIDER_DATA_TYPE >
255279 ) : Promise < FindResult [ ] > {
256280 const needle = await searchInput ;
281+ this . providerRegistry . getLogProvider ( ) . info ( `Searching for ${ needle } ` ) ;
257282
258283 if ( ! isImage ( needle ) && ! isTextQuery ( needle ) && ! isWindowQuery ( needle ) ) {
259- throw Error (
284+ const e = Error (
260285 `findAll requires an Image, a text query or a window query, but received ${ JSON . stringify (
261286 needle
262287 ) } `
263288 ) ;
289+ this . providerRegistry . getLogProvider ( ) . error ( e , { needle } ) ;
290+ throw e ;
264291 }
265292
266293 try {
267294 if ( isWindowQuery ( needle ) ) {
295+ this . providerRegistry . getLogProvider ( ) . debug ( `Running a window search` ) ;
268296 const matches = await this . providerRegistry
269297 . getWindowFinder ( )
270298 . findMatches ( needle ) ;
@@ -286,6 +314,7 @@ export class ScreenClass {
286314 }
287315 return windows ;
288316 } else {
317+ this . logNeedleType ( needle ) ;
289318 const { minMatch, screenSize, searchRegion, screenImage } =
290319 await this . getFindParameters ( params ) ;
291320
@@ -298,7 +327,7 @@ export class ScreenClass {
298327 params
299328 ) ;
300329
301- validateSearchRegion ( searchRegion , screenSize ) ;
330+ validateSearchRegion ( searchRegion , screenSize , this . providerRegistry ) ;
302331 this . providerRegistry . getLogProvider ( ) . debug ( `Search region is valid` ) ;
303332 const matchResults = await getMatchResults (
304333 this . providerRegistry ,
@@ -339,7 +368,11 @@ export class ScreenClass {
339368 }
340369 }
341370 } 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 ;
343376 }
344377 }
345378
@@ -352,11 +385,13 @@ export class ScreenClass {
352385 ) : Promise < Region > {
353386 const highlightRegion = await regionToHighlight ;
354387 if ( ! isRegion ( highlightRegion ) ) {
355- throw Error (
388+ const e = Error (
356389 `highlight requires an Region, but received ${ JSON . stringify (
357390 highlightRegion
358391 ) } `
359392 ) ;
393+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
394+ throw e ;
360395 }
361396 this . providerRegistry
362397 . getLogProvider ( )
@@ -406,11 +441,13 @@ export class ScreenClass {
406441 const updateIntervalValue = updateInterval ?? 500 ;
407442
408443 if ( ! isImage ( needle ) && ! isTextQuery ( needle ) && ! isWindowQuery ( needle ) ) {
409- throw Error (
444+ const e = Error (
410445 `waitFor requires an Image, a text query or a window query, but received ${ JSON . stringify (
411446 searchInput
412447 ) } `
413448 ) ;
449+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
450+ throw e ;
414451 }
415452 this . providerRegistry
416453 . getLogProvider ( )
@@ -447,11 +484,13 @@ export class ScreenClass {
447484 ! isTextQuery ( searchInput ) &&
448485 ! isWindowQuery ( searchInput )
449486 ) {
450- throw Error (
487+ const e = new Error (
451488 `on requires an Image, a text query or a window query, but received ${ JSON . stringify (
452489 searchInput
453490 ) } `
454491 ) ;
492+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
493+ throw e ;
455494 }
456495 const existingHooks = this . findHooks . get ( searchInput ) || [ ] ;
457496 this . findHooks . set ( searchInput , [ ...existingHooks , callback ] ) ;
@@ -481,15 +520,17 @@ export class ScreenClass {
481520 ) : Promise < string > {
482521 const currentScreen = await this . providerRegistry . getScreen ( ) . grabScreen ( ) ;
483522 if ( ! isImage ( currentScreen ) ) {
484- throw Error (
523+ const e = new Error (
485524 `capture requires an Image, but received ${ JSON . stringify (
486525 currentScreen
487526 ) } `
488527 ) ;
528+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
529+ throw e ;
489530 }
490531 this . providerRegistry
491532 . getLogProvider ( )
492- . debug (
533+ . info (
493534 `Capturing whole screen (0, 0, ${ currentScreen . width } , ${ currentScreen . height } )`
494535 ) ;
495536 return this . saveImage (
@@ -506,7 +547,13 @@ export class ScreenClass {
506547 * {@link grab } grabs screen content of a systems main display
507548 */
508549 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 ;
510557 }
511558
512559 /**
@@ -528,24 +575,28 @@ export class ScreenClass {
528575 ) : Promise < string > {
529576 const targetRegion = await regionToCapture ;
530577 if ( ! isRegion ( targetRegion ) ) {
531- throw Error (
578+ const e = new Error (
532579 `captureRegion requires an Region, but received ${ JSON . stringify (
533580 targetRegion
534581 ) } `
535582 ) ;
583+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
584+ throw e ;
536585 }
537586 this . providerRegistry
538587 . getLogProvider ( )
539- . debug ( `Capturing screen region ${ targetRegion . toString ( ) } ` ) ;
588+ . info ( `Capturing screen region ${ targetRegion . toString ( ) } ` ) ;
540589 const regionImage = await this . providerRegistry
541590 . getScreen ( )
542591 . grabScreenRegion ( targetRegion ) ;
543592 if ( ! isImage ( regionImage ) ) {
544- throw Error (
593+ const e = new Error (
545594 `captureRegion requires an Image, but received ${ JSON . stringify (
546595 regionImage
547596 ) } `
548597 ) ;
598+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
599+ throw e ;
549600 }
550601 return this . saveImage (
551602 regionImage ,
@@ -566,16 +617,21 @@ export class ScreenClass {
566617 ) : Promise < Image > {
567618 const targetRegion = await regionToGrab ;
568619 if ( ! isRegion ( targetRegion ) ) {
569- throw Error (
620+ const e = new Error (
570621 `grabRegion requires an Region, but received ${ JSON . stringify (
571622 targetRegion
572623 ) } `
573624 ) ;
625+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
626+ throw e ;
574627 }
628+ const screenContent = await this . providerRegistry
629+ . getScreen ( )
630+ . grabScreenRegion ( targetRegion ) ;
575631 this . providerRegistry
576632 . 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 ;
579635 }
580636
581637 /**
@@ -586,9 +642,11 @@ export class ScreenClass {
586642 const screenContent = await this . providerRegistry . getScreen ( ) . grabScreen ( ) ;
587643 const inputPoint = await point ;
588644 if ( ! isPoint ( inputPoint ) ) {
589- throw Error (
645+ const e = new Error (
590646 `colorAt requires a Point, but received ${ JSON . stringify ( inputPoint ) } `
591647 ) ;
648+ this . providerRegistry . getLogProvider ( ) . error ( e ) ;
649+ throw e ;
592650 }
593651 const scaledPoint = new Point (
594652 inputPoint . x * screenContent . pixelDensity . scaleX ,
@@ -601,9 +659,13 @@ export class ScreenClass {
601659 screenContent . pixelDensity . scaleX
602660 } , ${ screenContent . pixelDensity . scaleY } ) into ${ scaledPoint . toString ( ) } `
603661 ) ;
604- return this . providerRegistry
662+ const color = await this . providerRegistry
605663 . getImageProcessor ( )
606664 . colorAt ( screenContent , scaledPoint ) ;
665+ this . providerRegistry
666+ . getLogProvider ( )
667+ . info ( `Color at ${ inputPoint . toString ( ) } is ${ color . toString ( ) } ` ) ;
668+ return color ;
607669 }
608670
609671 private async saveImage (
@@ -668,4 +730,12 @@ export class ScreenClass {
668730 }
669731 return [ ] ;
670732 }
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+ }
671741}
0 commit comments