Skip to content

Instantly share code, notes, and snippets.

@a2ikm
Last active March 17, 2017 14:08
Show Gist options
  • Save a2ikm/10537b76ba6073203275e8d9b4835b73 to your computer and use it in GitHub Desktop.
Save a2ikm/10537b76ba6073203275e8d9b4835b73 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def parse_query(query)
lines = query.split("\n")
[lines.shift, lines.join("\n")]
end
require "shellwords"
path = ARGV[0] or abort "Usage: tail_slow_log.rb <path_to_slow_log>"
notifier = nil
if system("type terminal-notifier >/dev/null 2>&1")
notifier = ->(header, query) {
timestamp, query = parse_query(query)
puts query
escaped = Shellwords.escape(query)
system(%Q{terminal-notifier -title "SLOW LOG" -message #{escaped} >/dev/null 2>&1})
}
else
notifier = ->(header, query) {
timestamp, query = parse_query(query)
puts query
}
warn <<-WARNING
terminal-notifier is not installed, So queries are only printed on this screen.
WARNING
end
f = File.open(path)
f.seek(0, IO::SEEK_END)
header = String.new
query = String.new
loop do
begin
line = f.readline
if line.start_with?("#")
if query.empty?
header += line
else
notifier.call(header, query)
header = String.new
query = String.new
header += line
end
else
if header.empty?
# do nothing
else
query += line
end
end
rescue EOFError
if header.empty?
else
notifier.call(header, query)
header = String.new
query = String.new
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment