From 9be3df08283af081668c4cdfdd4297c5d15cbd04 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 2 Mar 2010 17:35:01 -0800 Subject: [PATCH] Add sys.log() --- doc/api.txt | 2 ++ lib/sys.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/doc/api.txt b/doc/api.txt index 73f4f54c2e8..9466fc6e458 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -187,6 +187,8 @@ Like +puts()+ but without the trailing new-line. A synchronous output function. Will block the process and output +string+ immediately to +stdout+. ++log(string)+:: +Output with timestamp. +inspect(object, showHidden, depth)+ :: diff --git a/lib/sys.js b/lib/sys.js index 5ad68e49268..a828e5cef5e 100644 --- a/lib/sys.js +++ b/lib/sys.js @@ -188,6 +188,25 @@ exports.p = function () { } }; +function pad (n) { + return n < 10 ? '0' + n.toString(10) : n.toString(10); +} + +var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; + +// 26 Feb 16:19:34 +function timestamp () { + var d = new Date(); + return [ d.getDate() + , months[d.getMonth()] + , [pad(d.getHours()), pad(d.getMinutes()), pad(d.getSeconds())].join(':') + ].join(' '); +} + +exports.log = function (msg) { + exports.puts(timestamp() + ' - ' + msg.toString()); +} + exports.exec = function (command, callback) { var child = process.createChildProcess("/bin/sh", ["-c", command]); var stdout = "";