Skip to content

Commit

Permalink
Make applog interface a little more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
harbaum committed Jul 4, 2017
1 parent 321250d commit 11c3ef4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 54 deletions.
50 changes: 50 additions & 0 deletions board/fischertechnik/TXT/rootfs/var/www/applog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>fischertechnik TXT community firmware</title>
<link rel="stylesheet" href="txt.css">
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
</head><body>
<h1><div class="outline"><font color="red">fischer</font><font color="#046ab4">technik</font>&nbsp;<font color="#fcce04">TXT</font></div></h1>

<h1>Application log</h1>
<div id='file'></div>
<script type="text/javascript" charset="utf-8">
function write_line(l) {
var obj = document.getElementById('file');
obj.innerHTML += l;
obj.scrollTop = obj.scrollHeight;
}

function launch() {
var http = new XMLHttpRequest();
http.open("GET", "./applog.py");
http.onreadystatechange = function() { }
http.send();
}

if ("MozWebSocket" in window) {
WebSocket = MozWebSocket;
}

function ws_start() {
var ws = new WebSocket("ws://"+document.location.hostname+":8999/");
ws.onopen = function() {
};
ws.onmessage = function(evt) {
write_line(evt.data);
};
ws.onclose = function() {
write_line(".");
setTimeout(function(){ ws_start() }, 1000);
};
}

if (WebSocket) {
launch();
ws_start();
} else {
alert("WebSocket not supported;")
}
</script>
</body></html>
76 changes: 22 additions & 54 deletions board/fischertechnik/TXT/rootfs/var/www/applog.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,6 @@
import socket

FILE = "/tmp/app.log"
#FILE = "/var/log/syslog"

def dump_html():
print('Content-Type: text/html')
print('')
print('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">')
print('<html xmlns="http://www.w3.org/1999/xhtml">')
print('<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />')
print('<title>fischertechnik TXT community firmware</title>')
print('<link rel="stylesheet" href="txt.css">')
print('<link rel="icon" href="/favicon.ico" type="image/x-icon" />')
print('</head><body>')
print('<h1><div class="outline"><font color="red">fischer</font><font color="#046ab4">technik</font>&nbsp;<font color="#fcce04">TXT</font></div></h1>')

print('<h1>Application console log</h1>')
print('<div id="file"></div>')
print(' <script type="text/javascript" charset="utf-8">')
print(' var ws = new WebSocket("ws://"+document.location.hostname+":9001/");')
print(' ws.onmessage = function(evt) {')
print(' var objDiv = document.getElementById("file");')
print(' objDiv.innerHTML += evt.data;')
print(' objDiv.scrollTop = objDiv.scrollHeight - div.clientHeight;')
print(' objDiv.animate({scrollTop: objDiv.scrollHeight});')
print(' };')
print(' </script>')
print('</body></html>')
sys.stdout.flush()

def htmlize(s):
s = s.replace("&", "&amp;")
s = s.replace("<", "&lt;")
s = s.replace(">", "&gt;")
s = s.replace("\n", "<br/>")
return s

def notify_launcher(str):
# Create a socket (SOCK_STREAM means a TCP socket)
Expand All @@ -56,6 +22,13 @@ def notify_launcher(str):
finally:
sock.close()

def htmlize(s):
s = s.replace("&", "&amp;")
s = s.replace("<", "&lt;")
s = s.replace(">", "&gt;")
s = s.replace("\n", "<br/>")
return s

@asyncio.coroutine
def handler(websocket, path):
with open(FILE, 'r') as fin:
Expand All @@ -69,32 +42,27 @@ def handler(websocket, path):

asyncio.get_event_loop().stop()


if os.fork():
sys.exit(0)

os.setsid()
time.sleep(1)

if os.fork():
# request log file creation from launcher
notify_launcher("logging-start")
os.setsid()

# this is the server process which reads the file, monitors
# it for new contents and forwards it to the client
loop = asyncio.get_event_loop()
start_server = websockets.serve(handler, "", 9001)
websocket_server = loop.run_until_complete(start_server)
# request log file creation from launcher
notify_launcher("logging-start")

try:
loop.run_forever()
finally:
websocket_server.close()
loop.run_until_complete(websocket_server.wait_closed())
# this is the server process which reads the file, monitors
# it for new contents and forwards it to the client
loop = asyncio.get_event_loop()
start_server = websockets.serve(handler, "", 8999)
websocket_server = loop.run_until_complete(start_server)

notify_launcher("logging-stop")
sys.exit(0)
try:
loop.run_forever()
finally:
websocket_server.close()
loop.run_until_complete(websocket_server.wait_closed())

# some delay to make sure the server is listening
time.sleep(2)
dump_html()
notify_launcher("logging-stop")

0 comments on commit 11c3ef4

Please sign in to comment.