Skip to content

Commit 2dee537

Browse files
committed
(#371) Refined mouse logging
1 parent 5cc5efb commit 2dee537

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

lib/mouse.class.ts

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ export class MouseClass {
4646
*/
4747
public async setPosition(target: Point): Promise<MouseClass> {
4848
if (!isPoint(target)) {
49-
throw Error(
49+
const e = new Error(
5050
`setPosition requires a Point, but received ${JSON.stringify(target)}`
5151
);
52+
this.providerRegistry.getLogProvider().error(e);
53+
throw e;
5254
}
5355
this.providerRegistry
5456
.getLogProvider()
@@ -67,8 +69,14 @@ export class MouseClass {
6769
/**
6870
* {@link getPosition} returns a {@link Point} representing the current mouse position
6971
*/
70-
public getPosition(): Promise<Point> {
71-
return this.providerRegistry.getMouse().currentMousePosition();
72+
public async getPosition(): Promise<Point> {
73+
const currentPosition = await this.providerRegistry
74+
.getMouse()
75+
.currentMousePosition();
76+
this.providerRegistry
77+
.getLogProvider()
78+
.debug("Retrieving current mouse position", { currentPosition });
79+
return currentPosition;
7280
}
7381

7482
/**
@@ -83,6 +91,11 @@ export class MouseClass {
8391
return new Promise<MouseClass>(async (resolve, reject) => {
8492
try {
8593
const pathSteps = await path;
94+
this.providerRegistry
95+
.getLogProvider()
96+
.info(
97+
`Moving mouse to target point ${pathSteps[pathSteps.length - 1]}`
98+
);
8699
const timeSteps = calculateMovementTimesteps(
87100
pathSteps.length,
88101
this.config.mouseSpeed,
@@ -96,6 +109,7 @@ export class MouseClass {
96109
}
97110
resolve(this);
98111
} catch (e) {
112+
this.providerRegistry.getLogProvider().error(e as Error);
99113
reject(e);
100114
}
101115
});
@@ -125,8 +139,12 @@ export class MouseClass {
125139
try {
126140
await sleep(this.config.autoDelayMs);
127141
await this.providerRegistry.getMouse().scrollDown(amount);
142+
this.providerRegistry
143+
.getLogProvider()
144+
.info(`Scrolled down ${amount} steps`);
128145
resolve(this);
129146
} catch (e) {
147+
this.providerRegistry.getLogProvider().error(e as Error);
130148
reject(e);
131149
}
132150
});
@@ -142,8 +160,12 @@ export class MouseClass {
142160
try {
143161
await sleep(this.config.autoDelayMs);
144162
await this.providerRegistry.getMouse().scrollUp(amount);
163+
this.providerRegistry
164+
.getLogProvider()
165+
.info(`Scrolled up ${amount} steps`);
145166
resolve(this);
146167
} catch (e) {
168+
this.providerRegistry.getLogProvider().error(e as Error);
147169
reject(e);
148170
}
149171
});
@@ -159,8 +181,12 @@ export class MouseClass {
159181
try {
160182
await sleep(this.config.autoDelayMs);
161183
await this.providerRegistry.getMouse().scrollLeft(amount);
184+
this.providerRegistry
185+
.getLogProvider()
186+
.info(`Scrolled left ${amount} steps`);
162187
resolve(this);
163188
} catch (e) {
189+
this.providerRegistry.getLogProvider().error(e as Error);
164190
reject(e);
165191
}
166192
});
@@ -176,8 +202,12 @@ export class MouseClass {
176202
try {
177203
await sleep(this.config.autoDelayMs);
178204
await this.providerRegistry.getMouse().scrollRight(amount);
205+
this.providerRegistry
206+
.getLogProvider()
207+
.info(`Scrolled right ${amount} steps`);
179208
resolve(this);
180209
} catch (e) {
210+
this.providerRegistry.getLogProvider().error(e as Error);
181211
reject(e);
182212
}
183213
});
@@ -193,10 +223,17 @@ export class MouseClass {
193223
try {
194224
await sleep(this.config.autoDelayMs);
195225
await this.providerRegistry.getMouse().pressButton(Button.LEFT);
226+
this.providerRegistry
227+
.getLogProvider()
228+
.info("Pressed left mouse button");
196229
await this.move(path);
197230
await this.providerRegistry.getMouse().releaseButton(Button.LEFT);
231+
this.providerRegistry
232+
.getLogProvider()
233+
.info("Released left mouse button");
198234
resolve(this);
199235
} catch (e) {
236+
this.providerRegistry.getLogProvider().error(e as Error);
200237
reject(e);
201238
}
202239
});
@@ -211,8 +248,20 @@ export class MouseClass {
211248
try {
212249
await sleep(this.config.autoDelayMs);
213250
await this.providerRegistry.getMouse().pressButton(btn);
251+
this.providerRegistry
252+
.getLogProvider()
253+
.info(
254+
`Pressed mouse button ${
255+
btn === Button.LEFT
256+
? "left"
257+
: btn === Button.MIDDLE
258+
? "middle"
259+
: "right"
260+
}`
261+
);
214262
resolve(this);
215263
} catch (e) {
264+
this.providerRegistry.getLogProvider().error(e as Error);
216265
reject(e);
217266
}
218267
});
@@ -227,8 +276,20 @@ export class MouseClass {
227276
try {
228277
await sleep(this.config.autoDelayMs);
229278
await this.providerRegistry.getMouse().releaseButton(btn);
279+
this.providerRegistry
280+
.getLogProvider()
281+
.info(
282+
`Released mouse button ${
283+
btn === Button.LEFT
284+
? "left"
285+
: btn === Button.MIDDLE
286+
? "middle"
287+
: "right"
288+
}`
289+
);
230290
resolve(this);
231291
} catch (e) {
292+
this.providerRegistry.getLogProvider().error(e as Error);
232293
reject(e);
233294
}
234295
});
@@ -243,8 +304,20 @@ export class MouseClass {
243304
try {
244305
await sleep(this.config.autoDelayMs);
245306
await this.providerRegistry.getMouse().click(btn);
307+
this.providerRegistry
308+
.getLogProvider()
309+
.info(
310+
`Clicked ${
311+
btn === Button.LEFT
312+
? "left"
313+
: btn === Button.MIDDLE
314+
? "middle"
315+
: "right"
316+
} button`
317+
);
246318
resolve(this);
247319
} catch (e) {
320+
this.providerRegistry.getLogProvider().error(e as Error);
248321
reject(e);
249322
}
250323
});
@@ -259,8 +332,20 @@ export class MouseClass {
259332
try {
260333
await sleep(this.config.autoDelayMs);
261334
await this.providerRegistry.getMouse().doubleClick(btn);
335+
this.providerRegistry
336+
.getLogProvider()
337+
.info(
338+
`Double-clicked ${
339+
btn === Button.LEFT
340+
? "left"
341+
: btn === Button.MIDDLE
342+
? "middle"
343+
: "right"
344+
} button`
345+
);
262346
resolve(this);
263347
} catch (e) {
348+
this.providerRegistry.getLogProvider().error(e as Error);
264349
reject(e);
265350
}
266351
});

lib/sleep.function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import providerRegistry from "./provider/provider-registry.class";
22

33
export const sleep = async (ms: number) => {
4-
providerRegistry.getLogProvider().info(`Sleeping for ${ms / 1000} seconds`);
4+
providerRegistry.getLogProvider().debug(`Sleeping for ${ms / 1000} seconds`);
55
return new Promise<void>((resolve) => setTimeout(resolve, ms));
66
};
77

0 commit comments

Comments
 (0)