-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from atomicsamurai/custom-noise-filter
Custom noise filter
- Loading branch information
Showing
7 changed files
with
76 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import fs from 'fs'; | ||
import os from 'os'; | ||
import { Log } from '@rockcarver/frodo-lib'; | ||
|
||
import { printMessage } from './Console'; | ||
|
||
const { defaultNoiseFilter } = Log; | ||
|
||
export const FRODO_CONFIG_PATH_KEY = 'FRODO_CONFIG_PATH'; | ||
export const FRODO_LOG_NOISEFILTER_FILENAME = 'LoggingNoiseFilter.json'; | ||
|
||
export function getConfigPath(): string { | ||
return process.env[FRODO_CONFIG_PATH_KEY] || `${os.homedir()}/.frodo`; | ||
} | ||
|
||
function getCustomNoiseFilters(): Array<string> { | ||
const filename = `${getConfigPath()}/${FRODO_LOG_NOISEFILTER_FILENAME}`; | ||
let noiseFilter = []; | ||
try { | ||
const data = fs.readFileSync(filename, 'utf8'); | ||
noiseFilter = JSON.parse(data); | ||
} catch (e) { | ||
printMessage(`Error reading ${filename} (${e.message})`, 'error'); | ||
} | ||
return noiseFilter; | ||
} | ||
|
||
export function getNoiseFilters(defaults: boolean): Array<string> { | ||
const filename = `${getConfigPath()}/${FRODO_LOG_NOISEFILTER_FILENAME}`; | ||
if(defaults) { | ||
printMessage(`Using default logging noise filters.`, 'info'); | ||
return defaultNoiseFilter(); | ||
} | ||
let noiseFilter = getCustomNoiseFilters(); | ||
if (noiseFilter.length > 0) { | ||
printMessage(`Using custom noise filters from ${filename}`, 'info'); | ||
} else { | ||
printMessage(`No custom noise filters defined. Using defaults.`, 'info'); | ||
noiseFilter = defaultNoiseFilter(); | ||
try { | ||
fs.writeFileSync(filename, JSON.stringify(noiseFilter, null, 2)); | ||
printMessage( | ||
`The default filters were saved in ${filename}. You can change the filters as needed.`, | ||
'info' | ||
); | ||
} catch (e) { | ||
printMessage(`Error creating noise filter configuration with default values.`, 'error'); | ||
} | ||
} | ||
return noiseFilter; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters