Skip to content

Commit

Permalink
feat(log): Add logCommand for structured output using ololog
Browse files Browse the repository at this point in the history
Replace direct `console.log` and manual padding with `logCommand`,
enhancing readability and simplifying logging.
  • Loading branch information
moodmosaic committed Mar 26, 2024
1 parent 6178cc8 commit 31f0901
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 79 deletions.
85 changes: 85 additions & 0 deletions contrib/core-contract-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contrib/core-contract-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@stacks/transactions": "^6.13.0",
"chokidar-cli": "^3.0.0",
"fast-check": "^3.15.1",
"ololog": "^1.1.175",
"typescript": "^5.4.2",
"vite": "^5.1.6",
"vitest": "^1.3.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PoxCommand, Real, Stub, Wallet } from "./pox_CommandModel.ts";
import { PoxCommand, Real, Stub, Wallet, logCommand } from "./pox_CommandModel.ts";
import { expect } from "vitest";
import { boolCV, Cl, ClarityType, OptionalCV, UIntCV } from "@stacks/transactions";

Expand Down Expand Up @@ -66,21 +66,7 @@ export class AllowContractCallerCommand implements PoxCommand {

// Log to console for debugging purposes. This is not necessary for the
// test to pass but it is useful for debugging and eyeballing the test.
console.info(
`✓ ${
this.wallet.label.padStart(
8,
" ",
)
} ${
"allow-contract-caller".padStart(
34,
" ",
)
} ${this.allowanceTo.label.padStart(12, " ")} ${"until".padStart(53)} ${
optionalCVToString(this.allowUntilBurnHt).padStart(17)
}`,
);
logCommand(`✓ ${this.wallet.label}`, "allow-contract-caller", this.allowanceTo.label, "until", optionalCVToString(this.allowUntilBurnHt));
}

toString() {
Expand Down
13 changes: 13 additions & 0 deletions contrib/core-contract-tests/tests/pox-4/pox_CommandModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fc from "fast-check";
import ololog from "ololog";

import { Simnet } from "@hirosystems/clarinet-sdk";
import { StacksPrivateKey } from "@stacks/transactions";
Expand Down Expand Up @@ -37,3 +38,15 @@ export type Wallet = {
};

export type PoxCommand = fc.Command<Stub, Real>;

export const logCommand = (...items: (string | undefined)[]) => {
// Ensure we only render up to the first 10 items for brevity.
const renderItems = items.slice(0, 10);
const columnWidth = 23;
// Pad each column to the same width.
const prettyPrint = renderItems.map((content) =>
content ? content.padEnd(columnWidth) : "".padEnd(columnWidth)
);

ololog.configure({ locate: false })(prettyPrint.join(""));
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PoxCommand, Real, Stub, Wallet } from "./pox_CommandModel.ts";
import { PoxCommand, Real, Stub, Wallet, logCommand } from "./pox_CommandModel.ts";
import { poxAddressToTuple } from "@stacks/stacking";
import { assert, expect } from "vitest";
import { Cl, ClarityType, isClarityType } from "@stacks/transactions";
Expand Down Expand Up @@ -146,13 +146,7 @@ export class DelegateStackStxCommand implements PoxCommand {

// Log to console for debugging purposes. This is not necessary for the
// test to pass but it is useful for debugging and eyeballing the test.
console.info(
`✓ ${this.operator.label.padStart(8, " ")} Ӿ ${this.stacker.label.padStart(8, " ")} ${
"delegate-stack-stx".padStart(23, " ")
} ${"lock-amount".padStart(12, " ")} ${
this.amountUstx.toString().padStart(15, " ")
} ${"until".padStart(37)} ${this.stacker.unlockHeight.toString().padStart(17)}`,
);
logCommand(`✓ ${this.operator.label} Ӿ ${this.stacker.label}`, "delegate-stack-stx", "lock-amount", this.amountUstx.toString(), "until", this.stacker.unlockHeight.toString());
}

toString() {
Expand Down
20 changes: 2 additions & 18 deletions contrib/core-contract-tests/tests/pox-4/pox_DelegateStxCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PoxCommand, Real, Stub, Wallet } from "./pox_CommandModel.ts";
import { PoxCommand, Real, Stub, Wallet, logCommand } from "./pox_CommandModel.ts";
import { poxAddressToTuple } from "@stacks/stacking";
import { expect } from "vitest";
import { boolCV, Cl } from "@stacks/transactions";
Expand Down Expand Up @@ -88,23 +88,7 @@ export class DelegateStxCommand implements PoxCommand {
delegatedWallet.hasPoolMembers.push(wallet.stxAddress);
// Log to console for debugging purposes. This is not necessary for the
// test to pass but it is useful for debugging and eyeballing the test.
console.info(
`✓ ${this.wallet.label.padStart(8, " ")} ${
"delegate-stx".padStart(
34,
" ",
)
} ${"amount".padStart(12, " ")} ${
amountUstx
.toString()
.padStart(15, " ")
} delegated to ${
this.delegateTo.label.padStart(
42,
" ",
)
}`,
);
logCommand(`✓ ${this.wallet.label}`, "delegate-stx", "amount", amountUstx.toString(), "delegated to", this.delegateTo.label);
}

toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PoxCommand, Real, Stub, Wallet } from "./pox_CommandModel.ts";
import { PoxCommand, Real, Stub, Wallet, logCommand } from "./pox_CommandModel.ts";
import { assert } from "vitest";
import { ClarityType, isClarityType } from "@stacks/transactions";

Expand Down Expand Up @@ -42,13 +42,7 @@ export class GetStackingMinimumCommand implements PoxCommand {

// Log to console for debugging purposes. This is not necessary for the
// test to pass but it is useful for debugging and eyeballing the test.
console.info(
`✓ ${this.wallet.label.padStart(8, " ")} ${
"get-stacking-minimum".padStart(34, " ")
} ${"pox-4".padStart(12, " ")} ${
stackingMinimum.value.toString().padStart(15, " ")
}`,
);
logCommand(`✓ ${this.wallet.label}`, "get-stacking-minimum", "pox-4", stackingMinimum.value.toString());
}

toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PoxCommand, Real, Stub, Wallet } from "./pox_CommandModel.ts";
import { PoxCommand, Real, Stub, Wallet, logCommand } from "./pox_CommandModel.ts";
import { expect } from "vitest";
import { Cl } from "@stacks/transactions";

Expand Down Expand Up @@ -35,17 +35,7 @@ export class GetStxAccountCommand implements PoxCommand {

// Log to console for debugging purposes. This is not necessary for the
// test to pass but it is useful for debugging and eyeballing the test.
console.info(
`✓ ${this.wallet.label.padStart(8, " ")} ${
"stx-account".padStart(34, " ")
} ${"lock-amount".padStart(12, " ")} ${
actual.amountLocked.toString().padStart(15, " ")
} ${"unlocked-amount".padStart(12, " ")} ${
actual.amountUnlocked.toString().padStart(15, " ")
} ${"unlocked-height".padStart(12, " ")} ${
actual.unlockHeight.toString().padStart(7, " ")
}`,
);
logCommand(`✓ ${this.wallet.label}`, "stx-account", "lock-amount", actual.amountLocked.toString(), "unlocked-amount", actual.amountUnlocked.toString(), "unlocked-height", actual.unlockHeight.toString());
}

toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PoxCommand, Real, Stub, Wallet } from "./pox_CommandModel.ts";
import { PoxCommand, Real, Stub, Wallet, logCommand } from "./pox_CommandModel.ts";
import { poxAddressToTuple } from "@stacks/stacking";
import { expect } from "vitest";
import { Cl, someCV, tupleCV } from "@stacks/transactions";
Expand Down Expand Up @@ -77,14 +77,7 @@ export class RevokeDelegateStxCommand implements PoxCommand {

// Log to console for debugging purposes. This is not necessary for the
// test to pass but it is useful for debugging and eyeballing the test.
console.info(
`✓ ${
this.wallet.label.padStart(
8,
" ",
)
} ${"revoke-delegate-stx".padStart(34, " ")}`,
);
logCommand(`✓ ${this.wallet.label}`, "revoke-delegate-stx");
}

toString() {
Expand Down
10 changes: 2 additions & 8 deletions contrib/core-contract-tests/tests/pox-4/pox_StackStxCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PoxCommand, Real, Stub, Wallet } from "./pox_CommandModel.ts";
import { PoxCommand, Real, Stub, Wallet, logCommand } from "./pox_CommandModel.ts";
import { Pox4SignatureTopic, poxAddressToTuple } from "@stacks/stacking";
import { assert, expect } from "vitest";
import { Cl, ClarityType, isClarityType } from "@stacks/transactions";
Expand Down Expand Up @@ -155,13 +155,7 @@ export class StackStxCommand implements PoxCommand {

// Log to console for debugging purposes. This is not necessary for the
// test to pass but it is useful for debugging and eyeballing the test.
console.info(
`✓ ${this.wallet.label.padStart(8, " ")} ${
"stack-stx".padStart(34, " ")
} ${"lock-amount".padStart(12, " ")} ${
amountUstx.toString().padStart(15, " ")
}`,
);
logCommand(`✓ ${this.wallet.label}`, "stack-stx", "lock-amount", amountUstx.toString());
}

toString() {
Expand Down

0 comments on commit 31f0901

Please sign in to comment.