Skip to content

Commit 8599ba3

Browse files
committed
Merge branch 'feat/cleanup-logging' into develop
2 parents e64095c + 912a3d5 commit 8599ba3

File tree

24 files changed

+444
-380
lines changed

24 files changed

+444
-380
lines changed

package-lock.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config/appConfig.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import fs from 'node:fs';
1818
import { attemptToReadTOMLData } from './utils';
1919
import type { AppConfigData } from './Config';
2020
import Debug from 'debug';
21-
const log = Debug('CID:appConfig');
21+
const log = Debug('cid::engine::config::appConfig');
22+
23+
// TODO: This type of configuration should probably be included as part of the electron application, not the backend engine.
2224

2325
const APP_CONFIG_ENCODING: fs.EncodingOption = 'utf-8';
2426

@@ -38,14 +40,14 @@ export const DEFAULT_APP_CONFIG: AppConfigData = {
3840
};
3941

4042
export function loadAppConfig(configPath: string) {
41-
log('Loading Application config from', configPath);
43+
log(`[INFO] Loading Application config from ${configPath}`);
4244

4345
// attempt to read the file
4446
const configData = attemptToReadTOMLData<AppConfigData>(configPath, APP_CONFIG_ENCODING);
4547

4648
// if cannot be read we assume default application configuration
4749
if (!configData) {
48-
log('Cannot find Application config file -- using the default');
50+
log('[WARN] Cannot find Application config file, using the default configuration');
4951
return DEFAULT_APP_CONFIG;
5052
}
5153

@@ -56,7 +58,7 @@ export function loadAppConfig(configPath: string) {
5658
typeof configData.window.width !== 'number' ||
5759
typeof configData.window.height !== 'number'
5860
) {
59-
log('Application config file is not valid -- using the default');
61+
log('[ERROR] Application config file is not valid, using the default configuration');
6062
return DEFAULT_APP_CONFIG;
6163
}
6264

@@ -65,7 +67,7 @@ export function loadAppConfig(configPath: string) {
6567

6668
export function saveAppConfig(configData: AppConfigData, outputPath: string) {
6769
// update the config hash on import to account for the
68-
const outputData = JSON.stringify(configData, null, ' ');
70+
const outputData = JSON.stringify(configData, null, 4);
6971
fs.writeFileSync(outputPath, outputData, APP_CONFIG_ENCODING);
70-
log('Written Application config data to ', outputPath);
72+
log(`[INFO] Written Application config data to '${outputPath}'`);
7173
}

src/config/configStore.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
import { dirname } from 'node:path';
1818
import fs from 'node:fs';
19+
1920
import Debug from 'debug';
20-
const log = Debug('CID:ConfigStore');
21+
const log = Debug('cid::engine::config::store');
2122

2223
import { loadConfig, CONFIG_FILE_ENCODING } from './loadConfig';
2324
import { loadAppConfig, saveAppConfig, DEFAULT_APP_CONFIG } from './appConfig';
@@ -27,7 +28,7 @@ import type { AppConfigData, Config } from './Config';
2728
// Ensure the application's config file directory exists
2829
function ensureAppDirectoryExists(appDir: string) {
2930
if (!fs.existsSync(appDir)) {
30-
log("Application directory '", appDir, "' does not exits -- creating it");
31+
log(`[WARN] Application directory '${appDir}' does not exits, creating it`);
3132
fs.mkdirSync(appDir /*, { recursive: true } */);
3233
}
3334
}
@@ -40,7 +41,7 @@ function saveConfig(configData: Config.FileConfiguration, outputPath: string) {
4041
// update the config hash on import to account for the
4142
const outputData = JSON.stringify(configData, null, ' ');
4243
fs.writeFileSync(outputPath, outputData, CONFIG_FILE_ENCODING);
43-
log('Written config data to ', outputPath);
44+
log(`[INFO] Written config data to ${outputPath}`);
4445
}
4546

4647
interface ConfigStorePaths {
@@ -96,12 +97,12 @@ export class ConfigStore {
9697
// if the load succesds we have a valid config -- use it as a
9798
// user-provided one
9899
if (userConfigLoad.success) {
99-
log('User config validation success - using it as config');
100+
log('[INFO] User config validation success - using it as config');
100101
this.useUserConfig(userConfigLoad.config, userConfigLoad.lastUpdated);
101102
return;
102103
}
103104

104-
log('User config validation not successful - attempting to load backup config');
105+
log('[WARN] User config validation unsuccessful - attempting to load backup config');
105106
// if the default config load failed use the backup default
106107
// from the app distribution
107108
const backupConfigLoad = loadConfig({
@@ -114,7 +115,7 @@ export class ConfigStore {
114115
// if the load succesds we have a valid config -- use it as
115116
// a config-from-backup
116117
if (backupConfigLoad.success) {
117-
log('Backup config validation success - using it as config');
118+
log('[INFO] Backup config validation success - using it as config');
118119
backupConfigLoad.config.isBackup = true;
119120
this.useBackupConfig(backupConfigLoad.config);
120121
return;
@@ -124,10 +125,7 @@ export class ConfigStore {
124125
this.loadError = backupConfigLoad.error;
125126

126127
// if the backup config fails to load we are screwed
127-
log(
128-
'Backup config load failed - the application should alert the user: ',
129-
backupConfigLoad.error,
130-
);
128+
log(`[ERROR] Backup config load failed - the application should alert the user: ${backupConfigLoad.error}`);
131129

132130
// if there is a salt file error store the config, but act like it's invalid
133131
// (this is needed to pick up error messages from the config file)
@@ -175,16 +173,16 @@ export class ConfigStore {
175173
// This method does not save the backup as the user config, only deletes the user config file
176174
removeUserConfig() {
177175
// attempt to load the backup config
178-
log('[removeUserConfig] Attempting to remove user configuration and replace with backup.');
176+
log('[INFO] Attempting to remove user configuration and replace with backup.');
179177

180178
// if the current config is already a backup config don't do anything
181179
if (this.isCurrentConfigBackup()) {
182-
log('[removeUserConfig] Already using a backup config -- bailing');
180+
log('[INFO] Already using a backup config -- bailing');
183181
// this is not an error - we're already using the backup
184182
return;
185183
}
186184

187-
log('[removeUserConfig] Trying to load backup config file');
185+
log('[INFO] Trying to load backup config file');
188186
const backupConfigLoad = loadConfig({
189187
configPath: this.getBackupConfigFilePath(),
190188
algorithmId: this.getAlgorithmId(),
@@ -194,18 +192,15 @@ export class ConfigStore {
194192

195193
// if failed return the error message (do not delete the user config yet)
196194
if (!backupConfigLoad.success) {
197-
log(
198-
'Backup config validation failed -- returning error and keeping existing user config:',
199-
backupConfigLoad.error,
200-
);
195+
log(`[ERROR] Backup config validation failed -- returning error and keeping existing user config: ${backupConfigLoad.error}`)
201196
// save the error
202197
this.loadError = backupConfigLoad.error;
203198
// and return it
204199
return backupConfigLoad.error;
205200
}
206201

207202
// if successful use the loaded backup configuration
208-
log('[removeUserConfig] Backup config validation success - using it as config');
203+
log('[INFO] Backup config validation success - using it as config');
209204
backupConfigLoad.config.isBackup = true;
210205
this.useBackupConfig(backupConfigLoad.config);
211206

@@ -246,7 +241,7 @@ export class ConfigStore {
246241
// deletes the user configuration file
247242
_deleteUserConfigFile() {
248243
const configPath = this.getConfigFilePath();
249-
log('Deleting config file: ', configPath);
244+
log(`[INFO] Deleting config file: ${configPath}`);
250245
fs.unlinkSync(configPath);
251246
}
252247

src/config/loadConfig.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import { generateConfigHash } from './utils';
2222

2323
import { attemptToReadTOMLData } from './utils';
2424
import type { Config } from './Config';
25+
2526
import Debug from 'debug';
26-
const log = Debug('CID:loadConfig');
27+
const log = Debug('cid::engine::config::loadConfig');
2728

2829
// The encoding used by the config file
2930
export const CONFIG_FILE_ENCODING: fs.EncodingOption = 'utf-8';
@@ -43,14 +44,14 @@ type LoadConfigInput = {
4344
}
4445

4546
export function loadConfig({ configPath, algorithmId, embeddedSalt, usingUI=false, validateConfig=true }: LoadConfigInput): LoadConfigResult {
46-
log('[INFO] Loading config from', configPath);
47+
log(`[INFO] Loading config from '${configPath}'`);
4748
const configData = attemptToReadTOMLData<Config.FileConfiguration>(configPath, CONFIG_FILE_ENCODING);
4849

4950
if (!configData) {
50-
log('[ERROR] Unable to read config file', configPath);
51+
log(`[ERROR] Unable to read config file from '${configPath}'`);
5152
return {
5253
success: false,
53-
error: `Unable to read config file '${configPath}'`,
54+
error: `Unable to read config file from '${configPath}'`,
5455
isSaltFileError: false
5556
};
5657
}
@@ -59,6 +60,7 @@ export function loadConfig({ configPath, algorithmId, embeddedSalt, usingUI=fals
5960
const lastUpdateDate = new Date(fs.statSync(configPath).mtime);
6061

6162
if (!!validateConfig) {
63+
log('[INFO] Validating config');
6264
// validate the config
6365
const validationResult = validateConfigFile(configData, algorithmId, usingUI);
6466

@@ -76,6 +78,7 @@ export function loadConfig({ configPath, algorithmId, embeddedSalt, usingUI=fals
7678

7779
// fail if the signature is not OK
7880
if (configHash !== configData.meta.signature) {
81+
log(`[ERROR] Configuration file signature mismatch; config has '${configData.meta.signature}', generated '${configHash}'`);
7982
return {
8083
success: false,
8184
error: `Configuration file signature mismatch -- required signature is '${configData.meta.signature}' but user configuration has '${configHash}' `,
@@ -86,6 +89,7 @@ export function loadConfig({ configPath, algorithmId, embeddedSalt, usingUI=fals
8689

8790
// alphabetically sort the process, static, and reference fields to standardise and prevent
8891
// different ordering in config producing different results in output.
92+
log('[DEBUG] Standardising column order in configuration');
8993
configData.algorithm.columns.process = configData.algorithm.columns.process.sort();
9094
configData.algorithm.columns.reference = configData.algorithm.columns.reference.sort();
9195
configData.algorithm.columns.static = configData.algorithm.columns.static.sort();
@@ -95,22 +99,26 @@ export function loadConfig({ configPath, algorithmId, embeddedSalt, usingUI=fals
9599
// the config fields are optional. The programme should fail if no salt is provided.
96100

97101
if (configData.algorithm.salt && configData.algorithm.salt.source == "STRING") {
102+
log('[DEBUG] Using string-based salt from configuration file');
98103
return { success: true, lastUpdated: lastUpdateDate, config: configData };
99104
}
100105

101106
if (configData.algorithm.salt && configData.algorithm.salt.source == "FILE") {
107+
log('[DEBUG] Using file-based salt from configuration file');
102108
// load the file, convert to a string value, update the config to be of type: "STRING"
103109
const saltFilePath = configData.algorithm.salt.value;
104110
const validatorRegexp = configData.algorithm.salt.validator_regex ? new RegExp(configData.algorithm.salt.validator_regex) : undefined;
105111
return tryLoadSaltFile({ saltFilePath, validatorRegexp, configData, lastUpdateDate, label: "salt" });
106112
}
107113

108114
if (embeddedSalt && embeddedSalt.source == "STRING") {
115+
log('[DEBUG] Using string-based salt from embedded configuration');
109116
configData.algorithm.salt = { source: "STRING", value: embeddedSalt.value }
110117
return { success: true, lastUpdated: lastUpdateDate, config: configData };
111118
}
112-
119+
113120
if (embeddedSalt && embeddedSalt.source == "FILE") {
121+
log('[DEBUG] Using file-based salt from embedded configuration');
114122
const saltFilePath = embeddedSalt.value;
115123
const validatorRegexp = embeddedSalt.validator_regex ? new RegExp(embeddedSalt.validator_regex) : undefined;
116124
return tryLoadSaltFile({ saltFilePath, validatorRegexp, configData, lastUpdateDate, label: "embedded salt" });
@@ -128,7 +136,7 @@ interface TryLoadSaltFileInput {
128136
}
129137

130138
function tryLoadSaltFile({ saltFilePath, validatorRegexp, configData, lastUpdateDate }: TryLoadSaltFileInput): LoadConfigResult {
131-
log('[INFO] Loading salt from', saltFilePath);
139+
log(`[INFO] Loading salt from ${saltFilePath}`);
132140

133141
const loadSaltResponse = loadSaltFile({ saltFilePath, validatorRegexp });
134142
if (!loadSaltResponse.success) {
@@ -140,8 +148,11 @@ function tryLoadSaltFile({ saltFilePath, validatorRegexp, configData, lastUpdate
140148
if (loadSaltResponse.message) log(loadSaltResponse.message);
141149

142150
// update the config to be of salt type: "STRING" with loaded file data
151+
log(`[DEBUG] Updating configuration with string-based salt of ${loadSaltResponse.data.length} characters`);
143152
configData.algorithm.salt = { source: "STRING", value: loadSaltResponse.data }
153+
144154
// update the signature since we have changed the salt configuration
145155
configData.meta.signature = generateConfigHash(configData);
156+
log(`[DEBUG] Updated configuration signature to ${configData.meta.signature}`);
146157
return { success: true, lastUpdated: lastUpdateDate, config: configData };
147158
}

src/config/loadSaltFile.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
import fs from 'node:fs';
17-
import path from 'node:path';
18-
1917
import { attemptToReadFileData } from './utils';
2018

2119
// the encoding used for the salt file

src/decoding/base.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
import Debug from 'debug';
18-
const log = Debug('CID:Decoder');
19-
2017
import type { Config } from '../config/Config';
2118
import type { CidDocument } from '../document';
2219
import type { RawData, MappedData } from '../document';
@@ -59,7 +56,6 @@ export abstract class DecoderBase {
5956
return transformed;
6057
});
6158

62-
log('AFTER:', objectRows[0]);
6359
return objectRows;
6460
}
6561

src/decoding/csv.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ import fs from 'node:fs';
1818
import { parse as csv_parse } from 'csv-parse/sync';
1919
import type { Options as CsvOptions } from 'csv-parse/sync';
2020

21+
import Debug from 'debug';
22+
const log = Debug('cid::engine::decoding::csv');
23+
2124
import { DecoderBase } from './base';
2225
import type { Config } from '../config/Config';
2326

24-
// A decoder for CSVs
2527
class CsvDecoder extends DecoderBase {
2628
csvOptions: CsvOptions = {};
2729

@@ -31,9 +33,16 @@ class CsvDecoder extends DecoderBase {
3133
}
3234

3335
decodeFile(path: string, fileEncoding: fs.EncodingOption = 'utf-8') {
34-
let data = fs.readFileSync(path, fileEncoding);
35-
let parsed = csv_parse(data, this.csvOptions);
36-
return this.documentFromRawData(path, parsed);
36+
log(`[INFO] Reading CSV file from ${path} with encoding '${fileEncoding}'`);
37+
const data = fs.readFileSync(path, fileEncoding);
38+
const parsed = csv_parse(data, this.csvOptions);
39+
log(`[INFO] Parsed ${parsed.length} rows from CSV file '${path}'`);
40+
41+
log(`[DEBUG] Found ${parsed[0].length} columns: [${parsed[0].join(', ')}]`);
42+
const document = this.documentFromRawData(path, parsed);
43+
log(`[DEBUG] Renamed columns to aliases, columns: [${Object.keys(document.data[0]).join(', ')}]`);
44+
45+
return document;
3746
}
3847
}
3948

src/decoding/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function fileTypeOf(filePath: string) {
2727
if (filePath.endsWith('.csv')) {
2828
return SUPPORTED_FILE_TYPES.CSV;
2929
}
30-
throw new Error('Unknown file type');
30+
throw new Error(`Unknown file type for file at '${filePath}'`);
3131
}
3232

3333
// Returns an appropriate decoder for a file

0 commit comments

Comments
 (0)