Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use same debug syntax everywhere #433

Merged
merged 9 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/basics/src/file-save.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ export default function FILESave(data, feed) {
this.whenFinish = new Promise((resolve, reject) => {
const output = compress ? this.input.pipe(createWriteStream(filename)) : this.input;
output.once('error', (err) => {
debug('ezs')(`WARNING: ${filename} not saved. ${err}`);
debug('ezs:warn')(`File ${filename} not saved.`, this.ezs.serializeError(err));
reject(err);
});
output.once('close', () => {
debug('ezs')(`${filename} saved.`);
debug('ezs:info')(`${filename} saved.`);
lstat(filename, (err, stat) => {
if (err) {
return reject(err);
Expand Down
3 changes: 2 additions & 1 deletion packages/basics/src/tar-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import micromatch from 'micromatch';
import { createGunzip } from 'zlib';
import getStream from 'get-stream';
import writeTo from 'stream-write';
import debug from 'debug';

/**
* Take the content of a tar file, extract some files.
Expand Down Expand Up @@ -61,7 +62,7 @@ export default function TARExtract(data, feed) {
() => next(),
);
} catch (e) {
console.warn(`WARNING: file was ignored (${header.name})`, e);
debug('ezs:warn')(`File was ignored (${header.name})`, ezs.serializeError(e));
stream.resume();
return next();
}
Expand Down
12 changes: 7 additions & 5 deletions packages/basics/src/url-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default async function URLConnect(data, feed) {
await retry(
async (bail, numberOfTimes) => {
if (numberOfTimes > 1) {
debug('ezs')(`Attempts to reconnect (${numberOfTimes})`);
debug('ezs:debug')(`Attempts to reconnect (${numberOfTimes})`);
}
const controller = new AbortController();
const response = await fetch(url, {
Expand Down Expand Up @@ -108,13 +108,15 @@ export default async function URLConnect(data, feed) {
}
catch (e) {
if (!noerror) {
debug('ezs')(
`Break item #${this.getIndex()} [URLConnect] <${e}>`,
debug('ezs:warn')(
`Break item #${this.getIndex()} [URLConnect]`,
ezs.serializeError(e),
);
feed.stop(e);
} else {
debug('ezs')(
`Ignore item #${this.getIndex()} [URLConnect] <${e}>`,
debug('ezs:info')(
`Ignore item #${this.getIndex()} [URLConnect]`,
ezs.serializeError(e),
);
}
output.end();
Expand Down
4 changes: 2 additions & 2 deletions packages/basics/src/url-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ export default async function URLFetch(data, feed) {
} catch (e) {
controller.abort();
if (noerror) {
debug('ezs')(`Ignore item #${this.getIndex()} [URLFetch] <${e}>`);
debug('ezs:info')(`Ignore item #${this.getIndex()} [URLFetch]`, this.ezs.serializeError(e));
return feed.send(data);
}
debug('ezs')(`Break item #${this.getIndex()} [URLFetch] <${e}>`);
debug('ezs:warn')(`Break item #${this.getIndex()} [URLFetch]`, this.ezs.serializeError(e));
return feed.send(e);
}
}
4 changes: 2 additions & 2 deletions packages/basics/src/url-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ export default async function URLRequest(data, feed) {
const onError = (e) => {
controller.abort();
if (noerror) {
debug('ezs')(`Ignore item #${this.getIndex()} [URLRequest] <${e}>`);
debug('ezs:info')(`Ignore item #${this.getIndex()} [URLRequest]`, this.ezs.serializeError(e));
return feed.send(data);
}
debug('ezs')(`Break item #${this.getIndex()} [URLRequest] <${e}>`);
debug('ezs:warn')(`Break item #${this.getIndex()} [URLRequest]`, this.ezs.serializeError(e));
return feed.send(e);
};
try {
Expand Down
5 changes: 3 additions & 2 deletions packages/basics/src/url-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default async function URLStream(data, feed) {
if (this.isLast()) {
return feed.close();
}
const { ezs } = this;
const url = this.getParam('url');
const path = this.getParam('path', '*');
const retries = Number(this.getParam('retries', 5));
Expand All @@ -103,10 +104,10 @@ export default async function URLStream(data, feed) {
const onError = (e) => {
controller.abort();
if (noerror) {
debug('ezs')(`Ignore item #${this.getIndex()} [URLStream] <${e}>`);
debug('ezs:info')(`Ignore item #${this.getIndex()} [URLStream]`, ezs.serializeError(e));
return feed.send(data);
}
debug('ezs')(`Break item #${this.getIndex()} [URLStream] <${e}>`);
debug('ezs:warn')(`Break item #${this.getIndex()} [URLStream]`, ezs.serializeError(e));
return feed.send(e);
};
try {
Expand Down
1 change: 0 additions & 1 deletion packages/basics/test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ describe('test', () => {
res.push(chunk);
})
.on('end', () => {
console.log(res);
assert.equal(3, res.length);
assert.equal('"a";"b";"c"\r\n', res[0]);
assert.equal('"1";"2";"3"\r\n', res[1]);
Expand Down
4 changes: 4 additions & 0 deletions packages/basics/test/url-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ httpbin
.post('/status/400')
.reply(400);

httpbin
.get('/status/400')
.reply(400);

httpbin
.post('/status/503')
.reply(503);
Expand Down
4 changes: 2 additions & 2 deletions packages/conditor/src/corhal-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default async function CORHALFetch(data, feed) {
};
const onError = (e) => {
controller.abort();
debug('ezs')(`Break item #${this.getIndex()} [CORHALFetch] <${e}>`);
debug('ezs:warn')(`Break item #${this.getIndex()} [CORHALFetch]`, ezs.serializeError(e));
return feed.stop(e);
};
const loop = async (stream, arr, afterKeyToken) => {
Expand All @@ -102,7 +102,7 @@ export default async function CORHALFetch(data, feed) {
const { headers: headersBis, body: noticesBis } = await responseBis.json();
loop(stream, noticesBis, headersBis['after-key-token']);
} catch (e) {
console.error(`Error with ${url}/after/`, e.message, e.body);
debug('ezs:error')(`Error with ${url}/after/`, ezs.serializeError(e));
stream.end();
}
} else {
Expand Down
10 changes: 5 additions & 5 deletions packages/conditor/src/wos-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import writeTo from 'stream-write';
import each from 'async-each-series';

const request = (url, parameters) => async (bail, attempt) => {
debug('ezs')(`Request #${attempt} to ${url}`);
debug('ezs:debug')(`Request #${attempt} to ${url}`);
const response = await fetch(url, parameters);
if (!response.ok) {
const { message } = await response.json();
Expand Down Expand Up @@ -87,7 +87,7 @@ export default async function WOSFetch(data, feed) {
};
const onError = (e) => {
controller.abort();
debug('ezs')(`Break item #${this.getIndex()} [WOSFetch] <${e}>`);
debug('ezs:warn')(`Break item #${this.getIndex()} [WOSFetch]`, ezs.serializeError(e));
return feed.stop(e);
};
const loop = async (stream, Records, reqPerSec, amtPerYear, QueryID, RecordsFound) => {
Expand All @@ -98,7 +98,7 @@ export default async function WOSFetch(data, feed) {
try {
await write(stream, Records);
} catch (e) {
console.error('Write Error', e.message);
debug('ezs:error')('Write Error', ezs.serializeError(e));
throw new Error(e);
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ export default async function WOSFetch(data, feed) {
}
await loop(stream, RecordsBis, reqPerSec, amtPerYear, QueryID, RecordsFound);
} catch (e) {
console.error(`Error with ${cURLBis.href}`, e.message);
debug('ezs:error')(`Error with ${cURLBis.href}`, ezs.serializeError(e));
throw new Error(e);
}
};
Expand All @@ -139,7 +139,7 @@ export default async function WOSFetch(data, feed) {
const amtPerYear = response.headers.get('x-rec-amtperyear-remaining');
const QueryID = get(jsonResponse, 'QueryResult.QueryID');
const RecordsFound = get(jsonResponse, 'QueryResult.RecordsFound');
debug('ezs')(`Query #${QueryID} should download ${RecordsFound} notices (Allowed downloads remaining : ${amtPerYear})`);
debug('ezs:debug')(`Query #${QueryID} should download ${RecordsFound} notices (Allowed downloads remaining : ${amtPerYear})`);
await loop(output, [], reqPerSec, amtPerYear, QueryID, RecordsFound);
await feed.flow(output);
} catch (e) {
Expand Down
26 changes: 18 additions & 8 deletions packages/core/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ import yargs from 'yargs';
import debug from 'debug';
import ezs from '.';
import File from './file';
import { version } from '../package.json';
import { VERSION, VERBOSE } from './constants';
import settings from './settings';

export default function cli(errlog) {
const args = yargs
.env('EZS')
.usage('Usage: $0 [options] [<file>|<directory>] [<file2> <file3> ...]')
.version(version)
.version(VERSION)
.options({
verbose: {
alias: 'v',
default: false,
describe: 'Enable debug mode with DEBUG=ezs',
describe: `Make ezs more talkative, which is equivalent to setting the envar to DEBUG=${VERBOSE}`,
type: 'boolean',
},
logs: {
alias: 'l',
default: false,
describe: `Enable logs mode, which is equivalent to setting the envar to DEBUG_COLORS=0 DEBUG=${VERBOSE}`,
type: 'boolean',
},
tracer: {
Expand Down Expand Up @@ -66,7 +72,11 @@ export default function cli(errlog) {
const { argv } = args;

if (argv.verbose) {
debug.enable('ezs');
debug.enable(VERBOSE);
}
if (argv.logs) {
process.env.DEBUG_COLORS = 0;
debug.enable(VERBOSE);
}
if (argv.tracer) {
settings.tracerEnable = true;
Expand All @@ -85,7 +95,7 @@ export default function cli(errlog) {
errlog(`Error: ${argv.daemon} doesn't exists.`);
process.exit(1);
}
debug('ezs')(`Serving ${serverPath} with ${settings.concurrency} shards`);
debug('ezs:debug')(`Serving ${serverPath} with ${settings.concurrency} shards`);
return ezs.createCluster(settings.port, serverPath);
}
if (argv._.length === 0) {
Expand All @@ -95,21 +105,21 @@ export default function cli(errlog) {

let input;
if (argv.env) {
debug('ezs')('Reading environment variables...');
debug('ezs:info')('Reading environment variables...');
input = new PassThrough(ezs.objectMode());
input.write(process.env);
input.end();
} else if (argv.file) {
try {
const filename = realpathSync(argv.file);
debug('ezs')(`Reading file ${filename} ...`);
debug('ezs:info')(`Reading file ${filename} ...`);
input = createReadStream(filename);
} catch (e) {
errlog(`Error: ${argv.file} doesn't exists.`);
process.exit(1);
}
} else {
debug('ezs')('Reading standard input...');
debug('ezs:info')('Reading standard input...');
input = process.stdin;
input.resume();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const connectServer = (ezs) => (serverOptions, index) => {
const output = new PassThrough(ezs.objectMode());
const handle = http.request(serverOptions, (res) => {
connected = true;
debug('ezs')(`http://${hostname}:${port} send code ${res.statusCode}`);
debug('ezs:info')(`http://${hostname}:${port} send code ${res.statusCode}`);
if (res.statusCode === 200) {
res
.pipe(ezs.uncompress(res.headers))
Expand Down Expand Up @@ -94,7 +94,7 @@ export const connectServer = (ezs) => (serverOptions, index) => {
));
return output.end();
}
debug('ezs')(`http://${hostname}:${port} was stopped properly following ${e}`);
debug('ezs:info')(`http://${hostname}:${port} was stopped properly following`, ezs.serializeError(e));
return 4;
});
handle.setNoDelay(false);
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/compactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ const z = chooseZ();
export function compressStream(ezs, opts = {}) {
const encoding = opts['Content-Encoding'] || opts['content-encoding'] || 'identity';
if (typeof z.createGunzip === 'function' && encoding === 'gzip') {
debug('ezs')('ezs will use zlib to compress stream.');
debug('ezs:debug')('ezs will use zlib to compress stream.');
return z.createGzip();
}
debug('ezs')('ezs will not compress stream.');
debug('ezs:debug')('ezs will not compress stream.');
return new PassThrough(ezs.bytesMode());
}

export function uncompressStream(ezs, opts = {}) {
const encoding = opts['Content-Encoding'] || opts['content-encoding'] || 'identity';
if (typeof z.createGunzip === 'function' && encoding === 'gzip') {
debug('ezs')('ezs will use zlib to uncompress stream.');
debug('ezs:debug')('ezs will use zlib to uncompress stream.');
return z.createGunzip();
}
debug('ezs')('ezs will not uncompress stream.');
debug('ezs:debug')('ezs will not uncompress stream.');
return new PassThrough(ezs.bytesMode());
}
1 change: 1 addition & 0 deletions packages/core/src/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import filenameRegex from 'filename-regex';
import { version } from '../package.json';

export const VERBOSE = 'ezs:*,-ezs:debug,-ezs:trace';
export const VERSION = version;
export const STARTED_AT = Date.now();
export const RX_FILENAME = filenameRegex();
Loading
Loading