Last active
August 29, 2015 13:56
-
-
Save roman-neuhauser/8937424 to your computer and use it in GitHub Desktop.
logdemux in javascript :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vim: sw=2 sts=2 ts=2 fdm=marker cms=\ //\ %s | |
function expand_sink(prefix, today) // {{{ | |
{ | |
return function (expando) | |
{ | |
var sexpando = expando.str(); | |
if (sexpando == "%D") return to_iso_extended_string(today); | |
if (sexpando == "%P") return prefix; | |
return "LOGDEMUX-BUG"; | |
} | |
}; // }}} | |
function rule(prefix, match, sink, final, diag) // {{{ | |
{ | |
var os = new OStream; | |
this.handle = function (today, line) // {{{ | |
{ | |
if (!regex_search(line, pat)) | |
return false; | |
if (opened_on != today) | |
reopen(today); | |
os.write(line, endl); | |
return final; | |
} // }}} | |
function reopen(today) // {{{ | |
{ | |
if (os.is_open()) os.close(); | |
os.clear(); | |
var fname = expand(sink, today); | |
os.open(fname.c_str(), ios.app | ios.binary); | |
if (os.fail()) | |
diag.write("failed to open ", fname, endl); | |
opened_on = today; | |
} // }}} | |
function expand(fmt, d) // {{{ | |
{ | |
return regex_replace( | |
fmt | |
, regex("%[DP]\\>") | |
, cref(expand_sink(prefix, d)) | |
); | |
} // }}} | |
} // }}} | |
function ruleset(ini, prefix, diag) // {{{ | |
{ | |
var rules = []; | |
var order = iniphile.get(ini, "rules.order", []); | |
if (order.empty()) | |
throw missing_rules_order(); | |
order.forEach(function (rname) { | |
rules.push_back(new rule( | |
prefix | |
, iniphile.get(ini, rname + ".match", string("")) | |
, iniphile.get(ini, rname + ".sink", string("")) | |
, iniphile.get(ini, rname + ".final", false) | |
, diag | |
)); | |
}) | |
this.handle(now, line) // {{{ | |
{ | |
for (var i = 0; i < rules.length; ++i) | |
if (rules[i].handle(now, line)) | |
break; | |
} // }}} | |
}; // }}} | |
function today() // {{{ | |
{ | |
return day_clock.local_day(); | |
} // }}} | |
function complain(exitcode, msg) // {{{ | |
{ | |
cerr.write(msg, endl); | |
return exitcode; | |
} // }}} | |
function basename(path) // {{{ | |
{ | |
return regex_replace( | |
path | |
, regex("^(.*/)?([^/]+)(.exe)?$", regex.perl) | |
, "$2" | |
); | |
} // }}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment