Skip to content

Commit

Permalink
Merge pull request #181 from Moonsong-Labs/save-logs
Browse files Browse the repository at this point in the history
Save logs
  • Loading branch information
timbrinded authored Jun 29, 2023
2 parents 1a03fc5 + a33c150 commit 4de8bae
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 74 deletions.
8 changes: 8 additions & 0 deletions .changeset/unlucky-bulldogs-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@moonwall/types": patch
"@moonwall/util": patch
"@moonwall/cli": patch
---

Added log saving
- [#175](https://github.com/Moonsong-Labs/moonwall/issues/175)
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ node_modules/
dist/
html/*
*.sqlite
**/*.log
downloads/*
*.wasm
moonbeam
Expand Down
1 change: 1 addition & 0 deletions moonwall.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@
"name": "moonbeam",
"running": false,
"binPath": "tmp/moonbeam",
"retainAllLogs": true,
"ports": { "p2pPort": 30333, "wsPort": 9944, "rpcPort": 9933 },
"options": [
"--dev",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"changeset": "changeset",
"changeset:release": "changeset publish",
"changeset:version": "changeset version",
"clean-all":"rimraf node_modules && pnpm -r --filter='./packages/**' run clean",
"start": "pnpm exec moonwall",
"display-reports": "pnpm exec vite preview --base __vitest__ --outDir html",
"test": "pnpm exec moonwall test 'basic chopsticks dev_seq chop_state_test'",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"*.cjs"
],
"scripts": {
"clean":"pnpm rimraf dist && pnpm rimraf node_modules",
"build": "pnpm exec rimraf dist && tsup src --format cjs,esm --config tsup.config.ts && pnpm generate-types",
"generate-types": "tsup src --format cjs,esm --dts --config tsup.config.ts",
"watch": "tsup src --format cjs,esm --dts --watch",
Expand Down
15 changes: 9 additions & 6 deletions packages/cli/src/cmds/runNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { parse } from "yaml";
import { importJsonConfig, loadEnvVars } from "../lib/configReader.js";
import { MoonwallContext, runNetworkOnly } from "../lib/globalContext.js";
import { executeTests } from "./runTests.js";
import { clearNodeLogs, reportLogLocation } from "src/internal/cmdFunctions/tempLogs.js";

inquirer.registerPrompt("press-to-continue", PressToContinuePrompt);

Expand All @@ -30,11 +31,8 @@ export async function runNetwork(args) {
}
await loadEnvVars();

const testFileDirs = globalConfig.environments.find(
({ name }) => name == args.envName
)!.testFileDir;
const foundation = globalConfig.environments.find(({ name }) => name == args.envName)!.foundation
.type;
const testFileDirs = env.testFileDir;
const foundation = env.foundation.type;

const questions = [
{
Expand Down Expand Up @@ -121,10 +119,14 @@ export async function runNetwork(args) {
},
];

if (env.foundation.type == "dev" && !env.foundation.launchSpec[0].retainAllLogs) {
clearNodeLogs();
}

await runNetworkOnly(globalConfig);
clear();
const portsList = await reportServicePorts();

reportLogLocation();
portsList.forEach(({ port }) =>
console.log(` 🖥️ https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A${port}`)
);
Expand Down Expand Up @@ -308,6 +310,7 @@ const resolveInfoChoice = async (env: Environment) => {
console.log(chalk.bgWhite.blackBright("Launch Spec in Config File:"));
console.dir(env, { depth: null });
const portsList = await reportServicePorts();
reportLogLocation();
portsList.forEach(({ port }) =>
console.log(` 🖥️ https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A${port}`)
);
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/cmds/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import fs from "node:fs";
import path from "path";
import chalk from "chalk";
import { execSync } from "node:child_process";
import { filterCallsSome } from "@polkadot/types/metadata/decorate/index.js";
import { clearNodeLogs } from "src/internal/cmdFunctions/tempLogs.js";

export async function testCmd(envName: string, additionalArgs?: {}) {
const globalConfig = await importJsonConfig();
const env = globalConfig.environments.find(({ name }) => name === envName)!;
process.env.MOON_TEST_ENV = envName;
// process.env.MOON_RUN_SCRIPTS = "true";

if (!!!env) {
const envList = globalConfig.environments.map((env) => env.name);
throw new Error(
Expand Down Expand Up @@ -63,7 +63,9 @@ export async function testCmd(envName: string, additionalArgs?: {}) {
}
}
}

if (env.foundation.type == "dev" && !env.foundation.launchSpec[0].retainAllLogs) {
clearNodeLogs();
}
const vitest = await executeTests(env, additionalArgs);
const failed = vitest!.state.getFiles().filter((file) => file.result!.state === "fail");

Expand Down
34 changes: 34 additions & 0 deletions packages/cli/src/internal/cmdFunctions/tempLogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import path from "path";
import fs from "fs";

export function clearNodeLogs() {
const dirPath = path.join(process.cwd(), "tmp", "node_logs");
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}

// Check for existing log files and delete
const files = fs.readdirSync(dirPath);
for (const file of files) {
if (file.endsWith(".log")) {
fs.unlinkSync(path.join(dirPath, file));
}
}
}

export function reportLogLocation() {
const dirPath = path.join(process.cwd(), "tmp", "node_logs");
const result = fs.readdirSync(dirPath);
let filePath = "";
try {
filePath = ` 🪵 Log location: ${path.join(
dirPath,
result.find((file) => path.extname(file) == ".log")!
)}`;
} catch (e) {
console.error(e);
}

console.log(filePath);
return filePath;
}
24 changes: 24 additions & 0 deletions packages/cli/src/internal/localNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ChildProcess, spawn } from "child_process";
import chalk from "chalk";
import Debug from "debug";
import { checkAccess, checkExists } from "./fileCheckers.js";
import fs from "fs";
import path from "path";
const debugNode = Debug("global:node");

export async function launchNode(cmd: string, args: string[], name: string): Promise<ChildProcess> {
Expand All @@ -12,6 +14,8 @@ export async function launchNode(cmd: string, args: string[], name: string): Pro

let runningNode: ChildProcess;

const dirPath = path.join(process.cwd(), "tmp", "node_logs");

const onProcessExit = () => {
runningNode && runningNode.kill();
};
Expand All @@ -21,11 +25,22 @@ export async function launchNode(cmd: string, args: string[], name: string): Pro

process.once("exit", onProcessExit);
process.once("SIGINT", onProcessInterrupt);

runningNode = spawn(cmd, args);

const fsStream = fs.createWriteStream(
path.join(
dirPath,
`${path.basename(cmd)}_node_${args.find((a) => a.includes("port"))?.split("=")[1]}_${
runningNode.pid
}.log`
)
);

runningNode.once("exit", () => {
process.removeListener("exit", onProcessExit);
process.removeListener("SIGINT", onProcessInterrupt);
fsStream.end(); // This line ensures that the writable stream is properly closed
debugNode(`Exiting dev node: ${name}`);
});

Expand All @@ -40,6 +55,15 @@ export async function launchNode(cmd: string, args: string[], name: string): Pro
process.exit(1);
});

const writeLogToFile = (chunk: any) => {
fsStream.write(chunk, (err) => {
if (err) console.error(err);
else fsStream.emit("drain");
});
};
runningNode.stderr?.on("data", writeLogToFile);
runningNode.stdout?.on("data", writeLogToFile);

const binaryLogs: any[] = [];
await new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => {
Expand Down
Loading

0 comments on commit 4de8bae

Please sign in to comment.