From ee0eb97a5e04cb6f0c809b8031d4f5b9c49a539e Mon Sep 17 00:00:00 2001 From: Stephen Palmer Date: Tue, 23 Apr 2019 15:00:44 +0200 Subject: [PATCH] Backing out logging changes --- import.js | 2 +- lib/cache/cache_fs.js | 2 +- lib/cache/cache_ram.js | 8 ++++---- lib/cache/reliability_manager.js | 8 ++++---- lib/helpers.js | 25 ++++--------------------- lib/server/command_processor.js | 10 +++++----- lib/unity_cache_server.js | 4 ++-- 7 files changed, 21 insertions(+), 38 deletions(-) diff --git a/import.js b/import.js index 57d250d..64c9ebb 100644 --- a/import.js +++ b/import.js @@ -74,7 +74,7 @@ async function importTransactionFile(filePath, addressString, defaultPort) { } try { - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Begin transaction for ${helpers.GUIDBufferToString(guid)}-${hash.toString('hex')}`); + helpers.log(consts.LOG_DBG, `Begin transaction for ${helpers.GUIDBufferToString(guid)}-${hash.toString('hex')}`); await client.beginTransaction(guid, hash); } catch (err) { diff --git a/lib/cache/cache_fs.js b/lib/cache/cache_fs.js index 5b4db1a..c16fc77 100644 --- a/lib/cache/cache_fs.js +++ b/lib/cache/cache_fs.js @@ -107,7 +107,7 @@ class CacheFS extends CacheBase { const promises = transaction.files.map((file) => self._writeFileToCache(file.type, transaction.guid, transaction.hash, file.file) - .then(filePath => helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Added file to cache: ${file.size} ${filePath}`))); + .then(filePath => helpers.log(consts.LOG_TEST, `Added file to cache: ${file.size} ${filePath}`))); return Promise.all(promises); } diff --git a/lib/cache/cache_ram.js b/lib/cache/cache_ram.js index 6d4815d..3e51508 100644 --- a/lib/cache/cache_ram.js +++ b/lib/cache/cache_ram.js @@ -110,10 +110,10 @@ class CacheRAM extends CacheBase { } if(freeBlock.fileId) { - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Allocated existing block of size ${freeBlock.size} for ${key}, last accessed ${freeBlock.lastAccessTime}`); + helpers.log(consts.LOG_DBG, `Allocated existing block of size ${freeBlock.size} for ${key}, last accessed ${freeBlock.lastAccessTime}`); } else { - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Allocated free block of size ${freeBlock.size} for key ${key}`); + helpers.log(consts.LOG_DBG, `Allocated free block of size ${freeBlock.size} for key ${key}`); } // Clone the free block, then set it's file id and size @@ -173,7 +173,7 @@ class CacheRAM extends CacheBase { const entry = this._reserveBlock(key, buffer.length); - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Saving file key: ${key} pageIndex: ${entry.pageIndex} pageOffset: ${entry.pageOffset} size: ${entry.size}`); + helpers.log(consts.LOG_TEST, `Saving file key: ${key} pageIndex: ${entry.pageIndex} pageOffset: ${entry.pageOffset} size: ${entry.size}`); buffer.copy(this._pages[entry.pageIndex], entry.pageOffset, 0, buffer.length); @@ -215,7 +215,7 @@ class CacheRAM extends CacheBase { const promises = pages.map(async page => { const file = path.join(cachePath, page.index); - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Loading page file at ${file}`); + helpers.log(consts.LOG_DBG, `Loading page file at ${file}`); const stats = await fs.stat(file); if(stats.size !== page.size) throw new Error(`Unrecognized/invalid page file '${file}'`); diff --git a/lib/cache/reliability_manager.js b/lib/cache/reliability_manager.js index 93111d8..74d413f 100644 --- a/lib/cache/reliability_manager.js +++ b/lib/cache/reliability_manager.js @@ -67,7 +67,7 @@ class ReliabilityManager { } if(this._options.multiClient && params.clientId === entry.clientId) { - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Ignoring duplicate transaction for GUID: ${params.guidStr} Hash: ${params.hashStr} from previous client (multiClient = true)`); + helpers.log(consts.LOG_DBG, `Ignoring duplicate transaction for GUID: ${params.guidStr} Hash: ${params.hashStr} from previous client (multiClient = true)`); return entry; } @@ -75,7 +75,7 @@ class ReliabilityManager { if(entry.versionHash === params.versionHashStr) { entry.factor += 1; - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `GUID: ${params.guidStr} Hash: ${params.hashStr} ReliabilityFactor: ${entry.factor}`); + helpers.log(consts.LOG_DBG, `GUID: ${params.guidStr} Hash: ${params.hashStr} ReliabilityFactor: ${entry.factor}`); } else { entry.state = ReliabilityManager.reliabilityStates.Unreliable; @@ -116,11 +116,11 @@ class ReliabilityManager { if(info.state === ReliabilityManager.reliabilityStates.Unreliable && this._options.saveUnreliableVersionArtifacts) { const unreliableFilePath = path.join(this._cachePath, kUnreliableRootDir, params.guidStr, params.hashStr); await trx.writeFilesToPath(unreliableFilePath); - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Unreliable version artifacts saved to ${unreliableFilePath}`); + helpers.log(consts.LOG_DBG, `Unreliable version artifacts saved to ${unreliableFilePath}`); } if(info.state !== ReliabilityManager.reliabilityStates.ReliableNew) { - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Invalidating transaction from client at ${trx.clientAddress} for GUID: ${params.guidStr} Hash: ${params.hashStr} ReliabilityState: ${info.state.toString()}`); + helpers.log(consts.LOG_DBG, `Invalidating transaction from client at ${trx.clientAddress} for GUID: ${params.guidStr} Hash: ${params.hashStr} ReliabilityState: ${info.state.toString()}`); await trx.invalidate(); } } diff --git a/lib/helpers.js b/lib/helpers.js index c75ea14..78e064c 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -136,8 +136,6 @@ async function readDir(dir, fileCallback) { exports.readDir = readDir; -function shouldLog(lvl) { return lvl <= logLevel; } -exports.shouldLog = shouldLog; /** * @@ -145,35 +143,20 @@ exports.shouldLog = shouldLog; * @param {String} msg */ exports.log = exports.defaultLogger = (lvl, msg) => { - if(!shouldLog(lvl)) return; + if(lvl > logLevel) return; console.log(msg); }; - -const kMasterLogPrefix = "[Cluster:M] "; -const workerLogPrefix = {}; - /** * * @param {Number} lvl * @param {String} msg */ exports.defaultClusterLogger = (lvl, msg) => { - if (!shouldLog(lvl)) return; - - if(cluster.isMaster) { - process.stdout.write(kMasterLogPrefix); - } else { - const id = cluster.worker.id; - - if(!workerLogPrefix.hasOwnProperty(id)) { - workerLogPrefix[id] = `[Cluster:${id}] `; - } - - process.stdout.write(workerLogPrefix[id]); + if (lvl <= logLevel) { + const prefix = cluster.isMaster ? "[Cluster:M] " : `[Cluster:${cluster.worker.id}] `; + console.log(`${prefix}${msg}`); } - - console.log(msg); }; /** diff --git a/lib/server/command_processor.js b/lib/server/command_processor.js index 4c3943d..a674b35 100644 --- a/lib/server/command_processor.js +++ b/lib/server/command_processor.js @@ -327,7 +327,7 @@ class CommandProcessor extends Duplex { item.exists = true; item.size = info.size; this._sendFileQueueCount++; - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Adding file to send queue, size ${info.size}`); + helpers.log(consts.LOG_DBG, `Adding file to send queue, size ${info.size}`); } catch(err) { // Ignore error @@ -349,13 +349,13 @@ class CommandProcessor extends Duplex { */ async _onTransactionStart(guid, hash) { if(this._trx !== null) { - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, "Cancel previous transaction"); + helpers.log(consts.LOG_DBG, "Cancel previous transaction"); this._trx = null; } this._trx = await this[kCache].createPutTransaction(guid, hash); this._trx.clientAddress = this[kSource].clientAddress; - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `Start transaction for GUID: ${helpers.GUIDBufferToString(guid)} Hash: ${hash.toString('hex')}`); + helpers.log(consts.LOG_DBG, `Start transaction for GUID: ${helpers.GUIDBufferToString(guid)} Hash: ${hash.toString('hex')}`); } /** @@ -370,7 +370,7 @@ class CommandProcessor extends Duplex { await this[kCache].endPutTransaction(this._trx); this.emit('onTransactionEnd', this._trx); - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `End transaction for GUID: ${helpers.GUIDBufferToString(this._trx.guid)} Hash: ${this._trx.hash.toString('hex')}`); + helpers.log(consts.LOG_DBG, `End transaction for GUID: ${helpers.GUIDBufferToString(this._trx.guid)} Hash: ${this._trx.hash.toString('hex')}`); this._trx = null; } @@ -395,7 +395,7 @@ class CommandProcessor extends Duplex { } }); - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, `PUT rejected from non-whitelisted IP: ${this._trx.clientAddress}`); + helpers.log(consts.LOG_DBG, `PUT rejected from non-whitelisted IP: ${this._trx.clientAddress}`); } this._putStream.promiseWrite = promisify(this._putStream.write).bind(this._putStream); diff --git a/lib/unity_cache_server.js b/lib/unity_cache_server.js index ae0b437..45031c2 100644 --- a/lib/unity_cache_server.js +++ b/lib/unity_cache_server.js @@ -232,7 +232,7 @@ class UnityCacheServer { } cache.on('cleanup_delete_item', item => { - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, item); + helpers.log(consts.LOG_DBG, item); }); cache.on('cleanup_delete_finish', data => { @@ -280,7 +280,7 @@ class UnityCacheServer { class FakeSpinner { set text(msg) { - helpers.shouldLog(consts.LOG_DBG) && helpers.log(consts.LOG_DBG, msg); + helpers.log(consts.LOG_DBG, msg); } start(msg) { this.text = msg; };