-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.ahk
78 lines (69 loc) · 1.7 KB
/
Utils.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class Utils {
static _SleepResolution := 15.6
static SystemTime {
get {
static freq := 0
if (freq == 0)
DllCall("QueryPerformanceFrequency", "Int64*", &freq)
DllCall("QueryPerformanceCounter", "Int64*", &tick := 0)
return tick / freq * 1000
}
}
static ThreadSleep(time) {
DllCall("kernel32\Sleep", "UInt", time)
}
static SleepAtLeast(time) {
start := this.SystemTime
Sleep(time)
while (this.SystemTime - start < time)
Sleep(this._SleepResolution)
}
static SleepAtMost(time) {
if (time <= this._SleepResolution) {
this.ThreadSleep(time)
return
}
start := this.SystemTime
Sleep(time - this._SleepResolution)
while (this.SystemTime - start + this._SleepResolution < time)
Sleep(this._SleepResolution)
}
/**
* @param time The amount of time to pause (in milliseconds).
* @param mode Sleep mode.
* - `unset` or `""`: `Sleep`
* - `"thread"`: `Utils.ThreadSleep`
* - `"min"`: `Utils.SleepAtLeast`
* - `"max"`: `Utils.SleepAtMost`
*/
static Sleep(time, mode?) {
if (!IsSet(mode) || mode == "")
Sleep(time)
else if (mode == "thread")
this.ThreadSleep(time)
else if (mode == "min")
this.SleepAtLeast(time)
else if (mode == "max")
this.SleepAtMost(time)
else
throw ValueError("Invalid sleep mode: " mode)
}
}
class Logger {
__New(filePath := "") {
this.FilePath := filePath
this.__File := FileOpen(filePath, FileExist(filePath) ? "a" : "w")
}
__Delete() {
this.__File.Close()
}
Log(message) {
this.__File.Write("[" FormatTime(A_Now, "yyyy-MM-dd HH:mm:ss.") A_MSec "] " message "`n")
handle := this.__File.Handle
}
Clear() {
this.__File.Close()
this.__File := FileOpen(this.FilePath, "w")
this.__File.Write("")
}
}