Fixes in RCoreLog and add log chat test in shlr/www/log.html

This commit is contained in:
pancake 2012-12-23 15:19:11 +01:00
parent 2d0dd302be
commit 802d9ffd48
4 changed files with 114 additions and 2 deletions

View File

@ -74,12 +74,16 @@ static int cmd_log(void *data, const char *input) {
int n = atoi (input2);
int n2 = arg? atoi (arg+1): 0;
switch (*input) {
case 'l':
r_cons_printf ("%d\n", core->log->last-1);
break;
case '-':
r_core_log_del (core, n);
break;
case '?':
r_cons_printf ("Usage: l[-][ num|msg]\n"
" l new comment 0x80480\n"
" ll get last log message id\n"
" l- delete all logs\n"
" l-123 delete logs before 123\n"
" l 123 list log from 123 \n"

View File

@ -12,7 +12,7 @@ R_API int r_core_log_list(RCore *core, int n, int nth, char fmt) {
for (i=idx=0; str && *str; i++, id++) {
if ((n&&n<=id)||!n) {
switch (fmt) {
case 'j':r_cons_printf ("%s{%d,\"%s\"}",
case 'j':r_cons_printf ("%s[%d,\"%s\"]",
printed?",":"",id, str); break;
case '*':r_cons_printf ("\"l %s\"\n", str); break;
default: r_cons_printf ("%d %s\n", id, str); break;
@ -57,8 +57,11 @@ R_API void r_core_log_del(RCore *core, int n) {
core->log->first = core->log->last;
r_strpool_empty (core->log->sp);
} else {
int idx;
char *s;
int idx = n-core->log->first;
if (n > core->log->last)
n = core->log->last;
idx = n-core->log->first;
if (idx<0) return;
core->log->first += idx+1;
s = r_strpool_get_i (core->log->sp, idx);

78
shlr/www/js/r2.js Normal file
View File

@ -0,0 +1,78 @@
var r2 = {};
r2.root = ""; // prefix path
function Ajax (method, uri, body, fn) {
var x = new XMLHttpRequest ();
x.open (method, uri, false);
x.setRequestHeader ('Accept', 'text/plain');
x.onreadystatechange = function (y) {
if (x.status == 200) {
if (fn) fn (x.responseText);
} else alert ("ajax "+x.status)
}
x.send (body);
}
r2.cmd = function (c, cb) {
Ajax ('GET', r2.root+"/cmd/"+c, '', function (x) {
if (cb) cb (x);
});
}
r2.getLogger = function (obj) {
if (typeof (obj) != "object")
obj = {};
obj.last = 0;
obj.events = {};
obj.interval = null;
r2.cmd ("ll", function (x) {
obj.last = +x;
});
obj.load = function (cb) {
r2.cmd ("lj "+(obj.last+1), function (ret) {
var json = JSON.parse (ret);
if (cb) cb (json);
});
}
obj.clear = function (cb) {
// XXX: fix l-N
r2.cmd ("l-", cb); //+obj.last, cb);
}
obj.send = function (msg, cb) {
r2.cmd ("l "+msg, cb);
}
obj.refresh = function (cb) {
obj.load (function (ret) {
//obj.last = 0;
for (var i = 0; i< ret.length; i++) {
var message = ret[i];
obj.events["message"] ({
"id": message[0],
"text": message[1]
});
if (message[0] > obj.last)
obj.last = message[0];
}
if (cb) cb ();
});
}
obj.autorefresh = function (n) {
if (!n) {
if (obj.interval)
obj.interval.stop ();
return;
}
obj.interval = setInterval (function () {
obj.refresh (function () {
//obj.clear ();
});
return true;
}, n*1000);
}
obj.on = function (ev, cb) {
obj.events[ev] = cb;
return obj;
}
return obj;
}

27
shlr/www/log.html Normal file
View File

@ -0,0 +1,27 @@
<html>
<head>
<title>r2 log chat</title>
<script src="js/r2.js"></script>
<script>
var logger = null;
function init () {
logger = r2.getLogger ().on ("message", function (msg) {
var out = document.getElementById ('out');
out.innerHTML += "<br />"+msg.text;
});
// logger.send ("hello world");
logger.autorefresh (3);
}
function sendmsg() {
var msg = document.getElementById ("input").value;
if (logger) logger.send (msg);
}
</script>
</head>
<body onload="init()">
<input id="input" >
<input type="button" value="send" onclick="sendmsg()">
<div id="out" style="background-color:#d0d0d0; height:150px;overflow:scroll"> </div>
</body>
</html>