/
init.lua
142 lines (123 loc) · 4.05 KB
/
init.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
-- _ _ _ __
-- | | | | (_)/ _|
-- | | ___ __ _ _ __ ___ | |_ _| |_ _ _
-- | |/ _ \ / _` | '_ \ / _ \| __| | _| | | |
-- | | (_) | (_| | | | | (_) | |_| | | | |_| |
-- |_|\___/ \__, |_| |_|\___/ \__|_|_| \__, |
-- __/ | __/ |
-- |___/ |___/
--
-- log watcher for awesome wm
-- show a naughty popup each time something
-- changes in one of the predefined log files.
--
-- based on work of koniu <gkusnierz <at> gmail.com>
-- (see https://awesome.naquadah.org/wiki/Naughty_log_watcher)
--
-- Copyright (c) 2011, Jörg Thalheim <jthalheim@gmail.com>
--
-- This program is free software. It comes without any warranty, to
-- the extent permitted by applicable law. You can redistribute it
-- and/or modify it under the terms of the Do What The Fuck You Want
-- To Public License, Version 2, as published by Sam Hocevar. See
-- http://sam.zoy.org/wtfpl/COPYING for more details.
-- {{{ Grab enviroment
-- standart library
local io = io and { open = io.open } or require("io")
local ipairs = ipairs
local pairs = pairs
local print = print
local setmetatable = setmetatable
local timer = timer
local type = type
-- external
local socket = require("socket")
local inotify = require("inotify")
local escape = awful and awful.util.escape or require("awful.util").escape
local naughty = naughty or require("naughty")
-- }}}
module("lognotify")
LOGNOTIFY = {}
LOGNOTIFY_mt = { __index = LOGNOTIFY }
function new(settings)
local watcher = {}
if type(settings) ~= "table" then settings = {} end
watcher.logs = settings.logs or {}
watcher.naughty_timeout = settings.naughty_timeout or 0
watcher.timer = timer({ timeout = settings.interval or 1})
setmetatable(watcher, LOGNOTIFY_mt)
return watcher
end
function LOGNOTIFY:start()
local errno, errstr
self.inotify, errno, errstr = inotify.init()
self.sd = { getfd = function () return self.inotify:fileno() end }
for logname, log in pairs(self.logs) do
self:read_log(logname)
log.wd, errno, errstr = self.inotify:addwatch(log.file, inotify.IN_MODIFY)
end
if self.timer.add_signal then
self.timer:add_signal("timeout", function() self:watch() end)
else
self.timer:connect_signal("timeout", function() self:watch() end)
end
self.timer:start()
end
function LOGNOTIFY:stop()
self.timer:stop()
self.inotify:close()
end
function LOGNOTIFY:watch()
if #socket.select({self.sd}, nil, 0) > 0 then
local events, nread, errno, errstr = self.inotify:read()
if events then
for i, event in ipairs(events) do
for logname, log in pairs(self.logs) do
if event.wd == log.wd then
local diff = self:read_log(logname)
if diff then
self:notify(logname, log.file, diff)
end
end
end
end
end
end
end
function LOGNOTIFY:read_log(logname)
local log = self.logs[logname]
-- read log file
local f, errno = io.open(log.file)
if not f then
print("[lognotify] Can't read: "..errno)
return
end
-- log was visited earlier
if not log.len then
log.len = f:seek("end")
return
end
f:seek("set", log.len)
-- remove trailing newline
local diff = f:read("*a"):gsub("\n$", "")
-- set last length
log.len = f:seek("end")
f:close()
-- check if ignored
local ignored = false
for i, phr in ipairs(log.ignore or {}) do
if diff:find(phr) then ignored = true; break end
end
if not ignored then
return diff
end
end
function LOGNOTIFY:notify(name,file,diff)
naughty.notify{
title = '<span color="white">'..name.."</span>: "..file,
text = escape(diff),
hover_timeout = 0.2, timeout = self.naughty_timeout
}
end
setmetatable(_M, { __call = function(_, ...) return new(...) end })
-- vim:filetype=lua:tabstop=8:shiftwidth=4:expandtab: