Skip to content

Commit 272ef05

Browse files
authored
Merge pull request #5541 from NomicFoundation/debug-logs
Basic debug logging
2 parents 5ce208a + a3e153b commit 272ef05

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed

pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v-next/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
"devDependencies": {
5656
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
5757
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
58-
"@nomicfoundation/hardhat-test-utils": "workspace:^",
5958
"@microsoft/api-extractor": "^7.43.4",
59+
"@nomicfoundation/hardhat-test-utils": "workspace:^",
6060
"@types/node": "^20.14.9",
6161
"@types/semver": "^7.5.8",
6262
"@typescript-eslint/eslint-plugin": "^7.7.1",

v-next/hardhat-utils/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"./common-errors": "./dist/src/common-errors.js",
1919
"./crypto": "./dist/src/crypto.js",
2020
"./date": "./dist/src/date.js",
21+
"./debug": "./dist/src/debug.js",
2122
"./error": "./dist/src/error.js",
2223
"./eth": "./dist/src/eth.js",
2324
"./fs": "./dist/src/fs.js",
@@ -57,6 +58,7 @@
5758
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
5859
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
5960
"@types/bn.js": "^5.1.5",
61+
"@types/debug": "^4.1.4",
6062
"@types/keccak": "^3.0.4",
6163
"@types/node": "^20.14.9",
6264
"@typescript-eslint/eslint-plugin": "^7.7.1",
@@ -74,6 +76,7 @@
7476
"typescript-eslint": "7.7.1"
7577
},
7678
"dependencies": {
79+
"debug": "^4.1.1",
7780
"fast-equals": "^5.0.1",
7881
"keccak": "^3.0.4",
7982
"rfdc": "^1.3.1",

v-next/hardhat-utils/src/debug.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import debugLib from "debug";
2+
3+
/**
4+
* A simple decorator that adds debug logging for when a method is entered and exited.
5+
*
6+
* This decorator is meant to be used for debugging purposes only. It should not be committed in runtime code.
7+
*
8+
* Example usage:
9+
*
10+
* ```
11+
* class MyClass {
12+
* @withDebugLogs("MyClass:exampleClassMethod")
13+
* public function exampleClassMethod(...)
14+
* }
15+
* ```
16+
*/
17+
export function withDebugLogs<This, Args extends any[], Return>(
18+
tag: string = "",
19+
) {
20+
return function actualDecorator(
21+
originalMethod: (this: This, ...args: Args) => Return,
22+
_context: ClassMethodDecoratorContext<
23+
This,
24+
(this: This, ...args: Args) => Return
25+
>,
26+
): (this: This, ...args: Args) => Return {
27+
const log = debugLib(`hardhat:dev:core${tag === "" ? "" : `:${tag}`}`);
28+
29+
function replacementMethod(this: This, ...args: Args): Return {
30+
log(`Entering method with args:`, args);
31+
const result = originalMethod.call(this, ...args);
32+
log(`Exiting method.`);
33+
return result;
34+
}
35+
36+
return replacementMethod;
37+
};
38+
}

v-next/hardhat/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
5959
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
6060
"@nomicfoundation/hardhat-test-utils": "workspace:^",
61+
"@types/debug": "^4.1.4",
6162
"@types/node": "^20.14.9",
6263
"@typescript-eslint/eslint-plugin": "^7.7.1",
6364
"@typescript-eslint/parser": "^7.7.1",
@@ -78,6 +79,7 @@
7879
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2",
7980
"@ignored/hardhat-vnext-zod-utils": "workspace:^3.0.0-next.2",
8081
"chalk": "^5.3.0",
82+
"debug": "^4.1.1",
8183
"enquirer": "^2.3.0",
8284
"tsx": "^4.11.0",
8385
"zod": "^3.23.8"

v-next/hardhat/src/internal/cli/main.ts

+19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
import { isCi } from "@ignored/hardhat-vnext-utils/ci";
2929
import { getRealPath } from "@ignored/hardhat-vnext-utils/fs";
3030
import { kebabToCamelCase } from "@ignored/hardhat-vnext-utils/string";
31+
import debug from "debug";
3132

3233
import { resolveHardhatConfigPath } from "../../config.js";
3334
import { createHardhatRuntimeEnvironment } from "../../hre.js";
@@ -47,8 +48,12 @@ export async function main(
4748
cliArguments: string[],
4849
print: (message: string) => void = console.log,
4950
): Promise<void> {
51+
const log = debug("hardhat:core:cli:main");
52+
5053
let builtinGlobalOptions;
5154

55+
log("Hardhat CLI started");
56+
5257
try {
5358
const usedCliArguments: boolean[] = new Array(cliArguments.length).fill(
5459
false,
@@ -59,6 +64,8 @@ export async function main(
5964
usedCliArguments,
6065
);
6166

67+
log("Parsed builtin global options");
68+
6269
if (builtinGlobalOptions.version) {
6370
return await printVersionMessage(print);
6471
}
@@ -69,8 +76,12 @@ export async function main(
6976

7077
await ensureTelemetryConsent();
7178

79+
log("Retrieved telemetry consent");
80+
7281
if (builtinGlobalOptions.configPath === undefined) {
7382
builtinGlobalOptions.configPath = await resolveHardhatConfigPath();
83+
84+
log("Resolved config path");
7485
}
7586

7687
const projectRoot = await resolveProjectRoot(
@@ -79,12 +90,16 @@ export async function main(
7990

8091
const userConfig = await importUserConfig(builtinGlobalOptions.configPath);
8192

93+
log("User config imported");
94+
8295
const configPlugins = Array.isArray(userConfig.plugins)
8396
? userConfig.plugins
8497
: [];
8598
const plugins = [...builtinPlugins, ...configPlugins];
8699
const resolvedPlugins = await resolvePluginList(projectRoot, plugins);
87100

101+
log("Resolved plugins");
102+
88103
const pluginGlobalOptionDefinitions =
89104
buildGlobalOptionDefinitions(resolvedPlugins);
90105
const globalOptionDefinitions = new Map([
@@ -97,6 +112,8 @@ export async function main(
97112
usedCliArguments,
98113
);
99114

115+
log("Creating Hardhat Runtime Environment");
116+
100117
const hre = await createHardhatRuntimeEnvironment(
101118
userConfig,
102119
{ ...builtinGlobalOptions, ...userProvidedGlobalOptions },
@@ -141,6 +158,8 @@ export async function main(
141158
task,
142159
);
143160

161+
log(`Running task "${task.id.join(" ")}"`);
162+
144163
await task.run(taskArguments);
145164
} catch (error) {
146165
printErrorMessages(error, builtinGlobalOptions?.showStackTraces);

0 commit comments

Comments
 (0)