Skip to content

Commit

Permalink
Add timestamps to console and file
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerHardiman committed Apr 27, 2017
1 parent ca1220a commit d8f2ed0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
23 changes: 17 additions & 6 deletions read_serial_port.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,15 @@ port.on('open', function(err) {
// Callback - Data
port.on('data', function(buffer) {

var now = dateTime.create();
var nowString = now.format('H:M:S.N');
var msg = nowString + 'Rx' + BufferToHexString(buffer) + '\r\n';

// write to console
if (args.verbose) process.stdout.write(BufferToHexString(buffer));
if (args.verbose) console.log(msg);

// write to log file if 'fd' is not undefined
if (log_fd) {
var msg = 'Rx' + BufferToHexString(buffer) + '\r\n';
fs.write(log_fd,msg,function(err) {
if (err) console.log('Error writing to file');
});
Expand All @@ -158,12 +161,16 @@ port.on('disconnect', function(err) {

// Callback - Dedoded protocol
pelco_d_decoder.on('log', function(message) {

var now = dateTime.create();
var nowString = now.format('H:M:S.N');
var msg = nowString + '=>' + message + '\r\n';

// show on console
console.log(message);
console.log(msg);

// Write to file
if (log_fd) {
var msg = '=>' + message + '\r\n';
fs.write(log_fd,msg,function(err) {
if (err) console.log('Error writing to file');
});
Expand All @@ -173,12 +180,16 @@ pelco_d_decoder.on('log', function(message) {

try{
extra_decoder_1.on('log', function(message) {

var now = dateTime.create();
var nowString = now.format('H:M:S.N');
var msg = nowString + '=>' + message + '\r\n';

// show on console
console.log(message);
console.log(msg);

// Write to file
if (log_fd) {
var msg = '=>' + message + '\r\n';
fs.write(log_fd,msg,function(err) {
if (err) console.log('Error writing to file');
});
Expand Down
28 changes: 17 additions & 11 deletions read_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ if (args.list || (!args.port)) {
return;
}



// Defaults 2400 8-N-1
var port = '9000';

// Log File
var log_fd;

Expand Down Expand Up @@ -86,12 +81,15 @@ var server = new net.createServer(function(sock) {
// Callback - Data
sock.on('data', function(buffer) {

var now = dateTime.create();
var nowString = now.format('H:M:S.N');
var msg = nowString + 'Rx' + BufferToHexString(buffer) + '\r\n';

// write to console
if (args.verbose) process.stdout.write(BufferToHexString(buffer));
if (args.verbose) console.log(msg);

// write to log file if 'fd' is not undefined
if (log_fd) {
var msg = 'Rx' + BufferToHexString(buffer) + '\r\n';
fs.write(log_fd,msg,function(err) {
if (err) console.log('Error writing to file');
});
Expand All @@ -118,12 +116,16 @@ server.listen(port,'127.0.0.1');

// Callback - Dedoded protocol
pelco_d_decoder.on('log', function(message) {

var now = dateTime.create();
var nowString = now.format('H:M:S.N');
var msg = nowString + '=>' + message + '\r\n';

// show on console
console.log(message);
console.log(msg);

// Write to file
if (log_fd) {
var msg = '=>' + message + '\r\n';
fs.write(log_fd,msg,function(err) {
if (err) console.log('Error writing to file');
});
Expand All @@ -133,12 +135,16 @@ pelco_d_decoder.on('log', function(message) {

try{
extra_decoder_1.on('log', function(message) {

var now = dateTime.create();
var nowString = now.format('H:M:S.N');
var msg = nowString + '=>' + message + '\r\n';

// show on console
console.log(message);
console.log(msg);

// Write to file
if (log_fd) {
var msg = '=>' + message + '\r\n';
fs.write(log_fd,msg,function(err) {
if (err) console.log('Error writing to file');
});
Expand Down

0 comments on commit d8f2ed0

Please sign in to comment.