cjdnslog 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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, verbosity, file, function (err, ret) {
  47. if (err) { throw err; }
  48. if (ret.error !== 'none') { throw new Error(ret.error); }
  49. // make sure cjdns doesn't think we've gone missing!
  50. setInterval(function () {
  51. cjdns.ping(function (err, ret) {
  52. if (err) { throw err; }
  53. });
  54. }, 10000);
  55. var sigint = false;
  56. process.on('SIGINT', function () {
  57. if (sigint) { process.exit(100); }
  58. console.error('Disconnecting...');
  59. cjdns.AdminLog_unsubscribe(ret.streamId, function (err, ret) {
  60. if (err) { throw err; }
  61. console.error('done');
  62. cjdns.disconnect();
  63. process.exit(0);
  64. });
  65. });
  66. });
  67. });