Skip to content

Commit 434a537

Browse files
committed
(#371) Started adding log output to window.function.ts, sleep.function.ts and screen.class.ts
1 parent 9fc4504 commit 434a537

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Diff for: lib/screen.class.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export class ScreenClass {
7878
* Screens with higher pixel density (e.g. retina displays in MacBooks) might have a higher width in in actual pixels
7979
*/
8080
public width() {
81+
this.providerRegistry.getLogProvider().debug(`Fetching screen width`);
8182
return this.providerRegistry.getScreen().screenWidth();
8283
}
8384

@@ -87,6 +88,7 @@ export class ScreenClass {
8788
* Screens with higher pixel density (e.g. retina displays in MacBooks) might have a higher height in in actual pixels
8889
*/
8990
public height() {
91+
this.providerRegistry.getLogProvider().debug(`Fetching screen height`);
9092
return this.providerRegistry.getScreen().screenHeight();
9193
}
9294

@@ -108,6 +110,7 @@ export class ScreenClass {
108110
} = await this.getFindParameters(params);
109111

110112
const needle = await ScreenClass.getNeedle(template);
113+
this.providerRegistry.getLogProvider().info(`Searching for image ${needle.id} in region ${searchRegion.toString()} ${searchMultipleScales ? 'over multiple scales' : 'without scaling'}. Required confidence: ${minMatch}`);
111114

112115
const matchRequest = new MatchRequest(
113116
screenImage,
@@ -119,9 +122,12 @@ export class ScreenClass {
119122
return new Promise<Region>(async (resolve, reject) => {
120123
try {
121124
validateSearchRegion(searchRegion, screenSize);
125+
this.providerRegistry.getLogProvider().debug(`Search region is valid`);
122126
const matchResult = await this.providerRegistry.getImageFinder().findMatch(matchRequest);
123127
const possibleHooks = this.findHooks.get(needle) || [];
128+
this.providerRegistry.getLogProvider().debug(`${possibleHooks.length} hooks triggered for match`);
124129
for (const hook of possibleHooks) {
130+
this.providerRegistry.getLogProvider().debug(`Executing hook`);
125131
await hook(matchResult);
126132
}
127133
const resultRegion = new Region(
@@ -130,7 +136,9 @@ export class ScreenClass {
130136
matchResult.location.width,
131137
matchResult.location.height
132138
)
139+
this.providerRegistry.getLogProvider().info(`Match is located at ${resultRegion.toString()}`);
133140
if (this.config.autoHighlight) {
141+
this.providerRegistry.getLogProvider().debug(`Autohighlight is enabled`);
134142
resolve(this.highlight(resultRegion));
135143
} else {
136144
resolve(resultRegion);
@@ -161,6 +169,7 @@ export class ScreenClass {
161169
} = await this.getFindParameters(params);
162170

163171
const needle = await ScreenClass.getNeedle(template);
172+
this.providerRegistry.getLogProvider().info(`Searching for image ${needle.id} in region ${searchRegion.toString()} ${searchMultipleScales ? 'over multiple scales' : 'without scaling'}. Required confidence: ${minMatch}`);
164173

165174
const matchRequest = new MatchRequest(
166175
screenImage,
@@ -172,22 +181,28 @@ export class ScreenClass {
172181
return new Promise<Region[]>(async (resolve, reject) => {
173182
try {
174183
validateSearchRegion(searchRegion, screenSize);
184+
this.providerRegistry.getLogProvider().debug(`Search region is valid`);
175185
const matchResults = await this.providerRegistry.getImageFinder().findMatches(matchRequest);
176186
const possibleHooks = this.findHooks.get(needle) || [];
187+
this.providerRegistry.getLogProvider().debug(`${possibleHooks.length} hooks triggered for ${matchResults.length} matches`);
177188
for (const hook of possibleHooks) {
178189
for (const matchResult of matchResults) {
190+
this.providerRegistry.getLogProvider().debug(`Executing hook`);
179191
await hook(matchResult);
180192
}
181193
}
182194
const resultRegions = matchResults.map(matchResult => {
183-
return new Region(
195+
const resultRegion = new Region(
184196
searchRegion.left + matchResult.location.left,
185197
searchRegion.top + matchResult.location.top,
186198
matchResult.location.width,
187199
matchResult.location.height
188200
)
201+
this.providerRegistry.getLogProvider().info(`Match is located at ${resultRegion.toString()}`);
202+
return resultRegion;
189203
})
190204
if (this.config.autoHighlight) {
205+
this.providerRegistry.getLogProvider().debug(`Autohighlight is enabled`);
191206
resultRegions.forEach(region => this.highlight(region));
192207
resolve(resultRegions);
193208
} else {
@@ -207,6 +222,7 @@ export class ScreenClass {
207222
*/
208223
public async highlight(regionToHighlight: Region | Promise<Region>): Promise<Region> {
209224
const highlightRegion = await regionToHighlight;
225+
this.providerRegistry.getLogProvider().info(`Highlighting ${highlightRegion.toString()} for ${this.config.highlightDurationMs / 1000} with ${this.config.highlightOpacity * 100}% opacity`);
210226
await this.providerRegistry.getScreen().highlightScreenRegion(highlightRegion, this.config.highlightDurationMs, this.config.highlightOpacity);
211227
return highlightRegion;
212228
}
@@ -229,6 +245,7 @@ export class ScreenClass {
229245
if (!isImage(needle)) {
230246
throw Error(`waitFor requires an Image, but received ${JSON.stringify(templateImage)}`)
231247
}
248+
this.providerRegistry.getLogProvider().info(`Waiting for image ${needle.id} to appear on screen. Timeout: ${timeoutMs / 1000} seconds, interval: ${updateInterval} ms`);
232249
return timeout(updateInterval, timeoutMs, () => this.find(needle, params), {signal: params?.abort});
233250
}
234251

@@ -243,6 +260,7 @@ export class ScreenClass {
243260
}
244261
const existingHooks = this.findHooks.get(templateImage) || [];
245262
this.findHooks.set(templateImage, [...existingHooks, callback]);
263+
this.providerRegistry.getLogProvider().info(`Registered callback for image ${templateImage.id}. There are currently ${existingHooks.length + 1} hooks registered`);
246264
}
247265

248266
/**

Diff for: lib/sleep.function.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import providerRegistry from "./provider/provider-registry.class";
2+
13
export const sleep = async (ms: number) => {
4+
providerRegistry.getLogProvider().info(`Sleeping for ${ms / 1000} seconds`);
25
return new Promise<void>(resolve => setTimeout(resolve, ms));
36
};
47

Diff for: lib/window.function.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ export const createWindowApi = (providerRegistry: ProviderRegistry): WindowApi =
66
return ({
77
async getActiveWindow(): Promise<Window> {
88
const windowHandle = await providerRegistry.getWindow().getActiveWindow();
9+
providerRegistry.getLogProvider().info('Active window handle', {windowHandle});
910
return new Window(providerRegistry, windowHandle);
1011
},
1112
async getWindows(): Promise<Window[]> {
1213
const windowHandles = await providerRegistry.getWindow().getWindows();
14+
providerRegistry.getLogProvider().info(`Retrieved ${windowHandles.length} window handles`);
1315
return windowHandles.map((handle: number) => {
1416
return new Window(providerRegistry, handle);
1517
});

0 commit comments

Comments
 (0)