-
Notifications
You must be signed in to change notification settings - Fork 1
/
activity_log.lua
65 lines (50 loc) · 1.63 KB
/
activity_log.lua
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
-- Log screen & power events
local fun = require 'fun'
local M = {}
local LOGDIR = os.getenv("HOME").."/log"
local LOGFILE = LOGDIR.."/activities.log"
M.LOGDIR = LOGDIR
M.LOGFILE = LOGFILE
local caffeinate_watcher = hs.caffeinate.watcher
local function dirExists(filepath)
return hs.fs.attributes(filepath, 'mode') == 'directory'
end
if not dirExists(LOGDIR) then hs.fs.mkdir(LOGDIR) end
local function log_activity(message)
local output_file = assert(io.open(LOGFILE, "a+"))
output_file:write(os.date("%Y-%m-%d %H:%M:%S") ..' '.. M.host ..' activity-log '.. tostring(message) .."\n")
output_file:close()
return true
end
local caffeinate_events = {
"screensaverDidStart", "screensaverDidStop", "screensaverWillStop",
"screensDidLock", "screensDidUnlock",
"screensDidSleep", "screensDidWake",
"sessionDidBecomeActive", "sessionDidResignActive",
"systemWillSleep", "systemWillPowerOff", "systemDidWake",
}
for _,event in pairs(caffeinate_events) do
caffeinate_events[caffeinate_watcher[event]] = event
end
M.watchers = {
caffeinate = caffeinate_watcher.new(function(event)
-- write event to log
log_activity(tostring(caffeinate_events[event]))
end),
}
function M:start()
M.host = hs.host.localizedName()
for _,watcher in pairs(self.watchers) do
watcher:start()
end
log_activity('Start:'..
fun.reduce(function(acc,k) return acc == '' and k or acc..','..k end, '', self.watchers))
end
function M:stop()
for _,watcher in pairs(self.watchers) do
watcher:stop()
end
log_activity('Stop:'..
fun.reduce(function(acc,k) return acc == '' and k or acc..','..k end, '', self.watchers))
end
return M