cjdnslog 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env node
  2. /* -*- Mode:Js */
  3. /* vim: set expandtab ts=4 sw=4: */
  4. /*
  5. * You may redistribute this program and/or modify it under the terms of
  6. * the GNU General Public License as published by the Free Software Foundation,
  7. * either version 3 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. var Cjdns = require('../contrib/nodejs/cjdnsadmin/cjdnsadmin');
  18. var usage = function () {
  19. console.log("Usage: cjdnslog.js [-v <verbosity>] [-f <filename>] [-l <lineNum>]");
  20. console.log("cjdnslog.js -v INFO <-- log all INFO and higher (WARN, ERROR, or CRITICAL) messages.");
  21. console.log("cjdnslog.js <-- log everything");
  22. console.log("cjdnslog.js -f CryptoAuth.c <-- log everything in CryptoAuth.c");
  23. console.log("cjdnslog.js -v INFO -f CryptoAuth.c <-- log INFO and higher in CryptoAuth.c");
  24. console.log("cjdnslog.js -f CryptoAuth.c -l 747 <-- print messages from log statement on line 747 of CryptoAuth.c");
  25. console.log("cjdnslog.js -l 747 <-- print messages from log statements on line 747 of any file at all.");
  26. };
  27. if (process.argv[process.argv.length-1] === '--help') {
  28. usage();
  29. process.exit(0);
  30. }
  31. var printMsg = function (data) {
  32. console.log(data['time'] + ' ' + data['level'] + ' ' + data['file'] + ':' + data['line'] + ' ' + data['message']);
  33. }
  34. Cjdns.connectWithAdminInfo(function (cjdns) {
  35. var n;
  36. var verbosity;
  37. var file;
  38. var line;
  39. if ((n = process.argv.indexOf('-v')) !== -1) { verbosity = process.argv[n+1]; }
  40. if ((n = process.argv.indexOf('-f')) !== -1) { file = process.argv[n+1]; }
  41. if ((n = process.argv.indexOf('-l')) !== -1) { line = process.argv[n+1]; }
  42. cjdns.setDefaultHandler(function (err, msg) {
  43. if (err) { throw err; }
  44. printMsg(msg);
  45. });
  46. cjdns.AdminLog_subscribe(line, file, verbosity, function (err, ret) {
  47. if (err) { throw err; }
  48. // make sure cjdns doesn't think we've gone missing!
  49. setInterval(function () {
  50. cjdns.ping(function (err, ret) {
  51. if (err) { throw err; }
  52. });
  53. }, 10000);
  54. });
  55. });