diff options
| author | Peter Olsar <polsar@gmail.com> | 2018-12-14 17:14:04 -0800 |
|---|---|---|
| committer | Peter Olsar <polsar@gmail.com> | 2018-12-14 17:14:04 -0800 |
| commit | be2835886bdc8519aac1e917d96a9bc1c513cbf9 (patch) | |
| tree | 27bc2bf058971131006e91846ce8c7405c9b963e /services/data-logger.js | |
| parent | 5877729bdda9ca20f164d9dff634f6bf3aa54204 (diff) | |
| download | tplink-energy-monitor-be2835886bdc8519aac1e917d96a9bc1c513cbf9.tar.xz tplink-energy-monitor-be2835886bdc8519aac1e917d96a9bc1c513cbf9.zip | |
- Added support to specify logger config file on command line.
- Added support to specify log directory path in logger config.
Diffstat (limited to 'services/data-logger.js')
| -rw-r--r-- | services/data-logger.js | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/services/data-logger.js b/services/data-logger.js index 9c23de1..f4f4865 100644 --- a/services/data-logger.js +++ b/services/data-logger.js @@ -1,6 +1,9 @@ const fs = require('fs'); +const path = require('path'); +const shell = require('shelljs'); const dataBroadcaster = require('./data-broadcaster'); +let logDirPath; let logIntervalMs; let maxLogEntries; @@ -8,16 +11,30 @@ loadLogConfig(); function loadLogConfig() { try { - let config = JSON.parse(fs.readFileSync('logger-config.json', 'utf8')); + // Use logger config file specified on command line, otherwise use default one. + if (process.argv.length > 2) { + loggerConfigPath = process.argv[2]; + } else { + loggerConfigPath = 'logger-config.json'; + } + console.log('Logger config file: "' + loggerConfigPath + '"') + + let config = JSON.parse(fs.readFileSync(loggerConfigPath, 'utf8')); + logDirPath = config.logDirPath; logIntervalMs = (config.logIntervalSeconds * 1000); maxLogEntries = config.maxLogEntries; } catch (err) { console.warn('Error reading logger config. Reverting to defaults.', err); + logDirPath = '.' // Current directory logIntervalMs = 60000 // 1 min maxLogEntries = 1440 // 24 hrs at 1/min - } + } + + // Create log directory path if it doesn't already exist. + console.log('Log directory path: "' + logDirPath + '"') + shell.mkdir('-p', logDirPath); } function startLogging(device) { @@ -26,7 +43,7 @@ function startLogging(device) { } function writeLog(filePath, log) { - fs.writeFile(filePath, JSON.stringify(log), { flag: 'w' }, (err) => { + fs.writeFileSync(filePath, JSON.stringify(log), { flag: 'w' }, (err) => { if (err) { console.warn('Error writing log for ' + device.alias + ' [' + device.deviceId + ']', err); } @@ -34,7 +51,7 @@ function writeLog(filePath, log) { } function getLogEntries(filePath, callback) { - + fs.access(filePath, fs.constants.F_OK, (err) => { if(err) { // No log file, init empty one @@ -68,7 +85,7 @@ function log(device) { getLogEntries(filePath, (entries) => { entries.push(logEntry) - + // Remove old entries entries.splice(0, entries.length - maxLogEntries); @@ -80,7 +97,7 @@ function log(device) { } function getLogPath(deviceId) { - return deviceId + '-log.json'; + return path.join(logDirPath, deviceId + '-log.json'); } function getLogEntriesForDevice(deviceId, callback) { @@ -91,4 +108,4 @@ module.exports = { startLogging: startLogging, log: log, getLogEntriesForDevice: getLogEntriesForDevice -}
\ No newline at end of file +} |