'use strict';
+// netdata
+// real-time performance and health monitoring, done right!
+// (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
+// GPL v3+
+
var url = require('url');
var http = require('http');
var util = require('util');
});
req.on('error', function(e) {
- service.error('Failed to make request: ' + netdata.stringify(service.request) + ', message: ' + e.message);
+ if(netdata.options.DEBUG === true) netdata.debug('Failed to make request: ' + netdata.stringify(service.request) + ', message: ' + e.message);
+ service.error('Failed to make request, message: ' + e.message);
callback(null);
});
return util.inspect(obj, {depth: 10});
},
+ zeropad2: function(s) {
+ if(typeof s !== 'string')
+ s = s.toString();
+
+ switch(s.length) {
+ case 0: return '00';
+ case 1: return '0' + s;
+ default: return s;
+ }
+ },
+
+ logdate: function(d) {
+ if(typeof d === 'undefined') d = new Date();
+ return this.zeropad2(d.getFullYear()) + '-' + this.zeropad2(d.getMonth()) + '-' + this.zeropad2(d.getDay())
+ + ' ' + this.zeropad2(d.getHours()) + ':' + this.zeropad2(d.getMinutes()) + ':' + this.zeropad2(d.getSeconds());
+ },
+
// show debug info, if debug is enabled
debug: function(msg) {
if(this.options.DEBUG === true) {
- var now = new Date();
- console.error(now.toString() + ': ' + netdata.options.filename + ': DEBUG: ' + ((typeof(msg) === 'object')?netdata.stringify(msg):msg).toString());
+ console.error(this.logdate() + ': ' + netdata.options.filename + ': DEBUG: ' + ((typeof(msg) === 'object')?netdata.stringify(msg):msg).toString());
}
},
// log an error
error: function(msg) {
- var now = new Date();
- console.error(now.toString() + ': ' + netdata.options.filename + ': ERROR: ' + ((typeof(msg) === 'object')?netdata.stringify(msg):msg).toString());
+ console.error(this.logdate() + ': ' + netdata.options.filename + ': ERROR: ' + ((typeof(msg) === 'object')?netdata.stringify(msg):msg).toString());
},
// send data to netdata
void log_date(FILE *out)
{
- char outstr[24];
+ char outstr[26];
time_t t;
struct tm *tmp, tmbuf;
tmp = localtime_r(&t, &tmbuf);
if (tmp == NULL) return;
- if (unlikely(strftime(outstr, sizeof(outstr), "%y-%m-%d %H:%M:%S", tmp) == 0)) return;
+ if (unlikely(strftime(outstr, sizeof(outstr), "%Y-%m-%d %H:%M:%S", tmp) == 0)) return;
fprintf(out, "%s: ", outstr);
}
log_date(stdout);
va_start( args, fmt );
- printf("DEBUG (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
+ printf("%s: DEBUG (%04lu@%-10.10s:%-15.15s): ", program_name, line, file, function);
vprintf(fmt, args);
va_end( args );
putchar('\n');
log_date(stderr);
va_start( args, fmt );
- if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
- else fprintf(stderr, "INFO: %s: ", program_name);
+ if(debug_flags) fprintf(stderr, "%s: INFO: (%04lu@%-10.10s:%-15.15s):", program_name, line, file, function);
+ else fprintf(stderr, "%s: INFO: ", program_name);
vfprintf( stderr, fmt, args );
va_end( args );
log_date(stderr);
va_start( args, fmt );
- if(debug_flags) fprintf(stderr, "%s (%04lu@%-10.10s:%-15.15s): %s: ", prefix, line, file, function, program_name);
- else fprintf(stderr, "%s: %s: ", prefix, program_name);
+ if(debug_flags) fprintf(stderr, "%s: %s: (%04lu@%-10.10s:%-15.15s): ", program_name, prefix, line, file, function);
+ else fprintf(stderr, "%s: %s: ", program_name, prefix);
vfprintf( stderr, fmt, args );
va_end( args );
log_date(stderr);
va_start( args, fmt );
- if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
- else fprintf(stderr, "FATAL: %s: ", program_name);
+ if(debug_flags) fprintf(stderr, "%s: FATAL: (%04lu@%-10.10s:%-15.15s): ", program_name, line, file, function);
+ else fprintf(stderr, "%s: FATAL: ", program_name);
vfprintf( stderr, fmt, args );
va_end( args );