Commit 085c7e94 authored by Phyks's avatar Phyks
Browse files

Jarvis has logging support

parent 18c11618
......@@ -27,6 +27,7 @@ Jarvis can be easily installed on a Raspberry Pi, starting from an up-to-date Ra
* This folder contains some example arduino scripts to use an Arduino and a regular PC instead of the Raspberry Pi GPIO pins. They are just here for demo purpose and are no longer used. Thus, they may be unstable.
* `aziz.py` is our moderation script, to inform us of new emails waiting for moderation. It's basically a Jarvis plugin.
* `data` folder contains the scripts, aliases etc you put into Jarvis.
* `irclog` folder contains the script to convert the Jarvis log as a nice HTML page.
* `jarvis.all.log` (created by Jarvis) is the complete log of the watched channel.
* `jarvis.py` is the main script, which serves the bot.
* `jarvis_cmd.py` is a collection of functions to translate Jarvis actions to GPIO actions etc.
......
......@@ -26,7 +26,10 @@ class Log(Rule):
if self.log_save_buffer_count > save_buffer_size:
self.flush_buffer()
self.log_cache.appendleft((datetime.now().hour,
self.log_cache.appendleft((datetime.now().day,
datetime.now().month,
datetime.now().year,
datetime.now().hour,
datetime.now().minute,
author,
msg))
......@@ -34,7 +37,7 @@ class Log(Rule):
def cache_to_buffer(self):
"""Pop a line from log cache and append it to save buffer"""
t = self.log_cache.pop()
self.log_save_buffer += "%02d:%02d <%s> %s\n" % t
self.log_save_buffer += "%02d/%02d/%04d %02d:%02d <%s> %s\n" % t
self.log_save_buffer_count += 1
def flush_buffer(self):
......@@ -60,7 +63,7 @@ class Log(Rule):
end = args[3]
found_end = False
found_start = False
for (h, m, auth, msg) in self.log_cache:
for (d, m, y, h, m, auth, msg) in self.log_cache:
end_index = msg.rfind(end)
if not found_end and end_index >= 0:
msg = msg[:end_index + len(end)]
......
<!DOCTYPE html>
<?php
date_default_timezone_set('Europe/Paris');
$min_time = empty($_GET['from']) ? strtotime("midnight", time()) : intval($_GET['from']);
$max_time = empty($_GET['to']) ? strtotime("tomorrow", $min_time) - 1 : intval($_GET['to']);
$today = $min_time;
$one_day = 86400;
$show_all = false;
if (isset($_GET['all'])) {
$min_time = 0;
$max_time = 9999999999999999999999;
$show_all= true;
}
?>
<html>
<head>
<title>Log of #hackens IRC channel</title>
<meta charset="utf-8"/>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header>
<h1>Log of #hackens IRC channel</h1>
<?php if (!$show_all) { ?>
<h2>Day <?=date('Y-m-d', $today)?></h2>
<p>
<a href="?from=<?=$today-$one_day?>&to=<?=$today?>">Previous day</a> <a href="?from=<?=$today+$one_day?>&to=<?=$today+2*$one_day?>">Next day</a>
</p>
<?php } ?>
</header>
<table>
<tbody>
</tbody>
</table>
</body>
</html>
#!/usr/bin/env python3
import cgi
import datetime
import os
import re
import sys
import time
nb_colors = 37
if len(sys.argv) < 3:
print("Usage: "+sys.argv[0]+" LOGFILE OUTPUT")
sys.exit(1)
logfile = sys.argv[1]
output = sys.argv[2]
msg = re.compile('^(\d\d)/(\d\d)/(\d\d\d\d) (\d\d):(\d\d) <(.*?)> (.*)$',)
script_path = os.path.dirname(os.path.realpath(sys.argv[0]))+'/'
def tuple_of_hue(hue):
t = hue * 6.0
if t < 1.0:
return (1.0, t, 0.0)
elif t < 2.0:
return (2.0 - t, 1.0, 0.0)
elif t < 3.0:
return (0.0, 1.0, t - 2.0)
elif t < 4.0:
return (0.0, 4.0 - t, 1.0)
elif t < 5.0:
return (t - 4.0, 0.0, 1.0)
else:
return (1.0, 0.0, 6.0 - t)
def color_of_hue(hue):
c = tuple_of_hue(hue)
return int(((c[0] * 256 + c[1]) * 256 + c[2]) * 255)
def hex_of_color(c):
return '#' + hex(c)[2:].zfill(6)
def hash_color(s, offset=0):
return hex_of_color(color_of_hue(float((sum(s.encode("utf-8")) + offset) % nb_colors) / nb_colors))
colorize_table = {}
def colorize(pseudo):
if pseudo not in colorize_table:
c = hash_color(pseudo)
# vals = list(colorize_table.values())
colorize_table[pseudo] = c
return '<span style="color:%s">%s</span>' % (colorize_table[pseudo], pseudo)
def format_time(t):
short_time = t.strftime('%H:%M')
long_time = t.strftime('%Y-%m-%d %H:%M')
return '<div>%s</div><div class="longtime">%s</div>' % (short_time, long_time)
def format_msg(msg):
msg = cgi.escape(msg)
msg = re.sub('(https?://[^ ]*)', '<a href="\\1">\\1</a>', msg)
return msg
if __name__ == "__main__":
write_output = ""
with open(script_path+'begin.php', 'r') as begin:
write_output = begin.read()+"\n"
with open(logfile, 'r') as f:
for l in f:
m = msg.search(l)
if m is not None:
t = datetime.datetime(int(m.group(3)),
int(m.group(2)),
int(m.group(1)),
int(m.group(4)),
int(m.group(5)))
timestamp = time.mktime(t.timetuple())
write_output += '<?php if (%d < $max_time && %d >= $min_time) { ?>' % (timestamp, timestamp)
write_output += "\n"
write_output += '<tr><td>%s</td><td>&lt;%s&gt;</td><td>%s</td></tr>' % (format_time(t), colorize(m.group(6)), format_msg(m.group(7)))
write_output += "\n"
write_output += '<?php } ?>\n'
with open(script_path+'end.php', 'r') as end:
write_output += end.read()+"\n"
with open(output, 'w') as fh:
fh.write(write_output)
html, body {
margin: 0;
padding: 0;
}
header {
background-color: #ddd;
padding: 0.1em 1em;
margin: 0;
color: #000;
font-family: sans-serif;
}
header h2 {
font-style: italic;
color: #444;
font-weight: normal;
}
td:nth-child(1) {
color: #888;
}
td:nth-child(2) {
font-weight: bold;
color: #888;
padding: 0 1em;
white-space: nowrap;
}
tr:nth-child(2n) {
background-color: #f8f8f8;
}
.longtime {
display: none;
}
td:hover .longtime {
display: block;
color: #eee;
background: #444;
position: absolute;
padding: 0.5em;
border-radius: 0.5em;
}
table {
width: 100%;
font-family: monospace;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment