Skip to content

Commit

Permalink
Add LogType 'text' in order to provide different way of logging (#1282)
Browse files Browse the repository at this point in the history
  • Loading branch information
wirednkod authored Aug 31, 2023
1 parent f1fc6de commit b96ff22
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Options:
-c, --spawn-concurrency <concurrency> Number of concurrent spawning process to launch, default is 1
-p, --provider <provider> Override provider to use (choices: "podman", "kubernetes", "native")
default: kubernetes
-l, --logType <logType>" Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent")
-d, --dir <path> Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet)
-f, --force Force override all prompt commands
-m, --monitor Start as monitor, do not auto cleanup network
Expand Down
1 change: 1 addition & 0 deletions docs/src/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Options:
-c, --spawn-concurrency <concurrency> Number of concurrent spawning process to launch, default is 1
-p, --provider <provider> Override provider to use (choices: "podman","kubernetes", "native", default: kubernetes)
-d, --dir <path> Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet)
-l, --logType <logType>" Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent")
-f, --force Force override all prompt commands
-m, --monitor Start as monitor, do not auto cleanup network
-h, --help display help for command
Expand Down
1 change: 1 addition & 0 deletions javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Options:
-p, --provider <provider> Override provider to use (choices: "podman", "kubernetes", "native")
default: kubernetes
-d, --dir <path> Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet)
-l, --logType <logType>" Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent")
-f, --force Force override all prompt commands
-m, --monitor Start as monitor, do not auto cleanup network
-h, --help display help for command
Expand Down
1 change: 1 addition & 0 deletions javascript/packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Options:
-p, --provider <provider> Override provider to use (choices: "podman", "kubernetes", "native")
default: kubernetes
-d, --dir <path> Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet)
-l, --logType <logType>" Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent")
-f, --force Force override all prompt commands
-m, --monitor Start as monitor, do not auto cleanup network
-h, --help display help for command
Expand Down
3 changes: 2 additions & 1 deletion javascript/packages/cli/src/actions/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
decorators,
getCredsFilePath,
readNetworkConfig,
getLogType,
} from "@zombienet/utils";
import fs from "fs";
import { resolve } from "path";
Expand Down Expand Up @@ -95,7 +96,7 @@ export async function spawn(
dir,
force,
inCI,
silent: false,
logType: getLogType(opts.logType),
setGlobalNetwork,
};
const network = await start(creds, config, options);
Expand Down
4 changes: 2 additions & 2 deletions javascript/packages/cli/src/actions/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parser from "@zombienet/dsl-parser-wrapper";
import type { TestDefinition } from "@zombienet/orchestrator";
import { run } from "@zombienet/orchestrator";
import { decorators, RelativeLoader } from "@zombienet/utils";
import { decorators, RelativeLoader, getLogType } from "@zombienet/utils";
import fs from "fs";
import { Environment } from "nunjucks";
import path from "path";
Expand Down Expand Up @@ -68,7 +68,7 @@ export async function test(
providerToUse,
inCI,
opts.spawnConcurrency,
false,
getLogType(opts.logType),
runningNetworkSpec,
dir,
);
Expand Down
7 changes: 6 additions & 1 deletion javascript/packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ program
["podman", "kubernetes", "native"],
),
)

.addOption(
new Option(
"-l, --logType <logType>",
"Type of logging - defaults to 'table'",
).choices(["table", "text", "silent"]),
)
.addOption(
new Option(
"-d, --dir <path>",
Expand Down
1 change: 1 addition & 0 deletions javascript/packages/orchestrator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Options:
-p, --provider <provider> Override provider to use (choices: "podman", "kubernetes", "native")
default: kubernetes
-d, --dir <path> Directory path for placing the network files instead of random temp one (e.g. -d /home/user/my-zombienet)
-l, --logType <logType>" Type of logging on the console - defaults to 'table'" (choices: "table", "text", "silent")
-f, --force Force override all prompt commands
-m, --monitor Start as monitor, do not auto cleanup network
-h, --help display help for command
Expand Down
16 changes: 11 additions & 5 deletions javascript/packages/orchestrator/src/orchestrator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LogType } from "@zombienet/utils";
import {
CreateLogTable,
PARACHAIN_NOT_FOUND,
Expand All @@ -11,7 +12,7 @@ import {
loadTypeDef,
makeDir,
series,
setSilent,
setLogType,
sleep,
} from "@zombienet/utils";
import fs from "fs";
Expand Down Expand Up @@ -64,7 +65,7 @@ export interface OrcOptionsInterface {
inCI?: boolean;
dir?: string;
force?: boolean;
silent?: boolean; // Mute logging output
logType?: LogType; // Set the logging output
setGlobalNetwork?: (network: Network) => void;
}

Expand All @@ -74,11 +75,16 @@ export async function start(
options?: OrcOptionsInterface,
) {
const opts = {
...{ monitor: false, spawnConcurrency: 1, inCI: false, silent: true },
...{
monitor: false,
spawnConcurrency: 1,
inCI: false,
logType: "silent" as LogType,
},
...options,
};

setSilent(opts.silent);
setLogType(opts.logType as LogType);
let network: Network | undefined;
let cronInterval = undefined;

Expand Down Expand Up @@ -362,7 +368,7 @@ export async function start(
}

const spawnOpts = {
silent: opts.silent,
logType: opts.logType as LogType,
inCI: opts.inCI,
monitorIsAvailable,
userDefinedTypes,
Expand Down
5 changes: 3 additions & 2 deletions javascript/packages/orchestrator/src/spawner.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LogType } from "@zombienet/utils";
import {
CreateLogTable,
decorators,
Expand Down Expand Up @@ -30,7 +31,7 @@ export const spawnNode = async (
bootnodes: string[],
filesToCopy: fileMap[],
opts: {
silent: boolean;
logType: LogType;
inCI: boolean;
monitorIsAvailable: boolean;
userDefinedTypes?: any;
Expand Down Expand Up @@ -231,7 +232,7 @@ export const spawnNode = async (
]);
logTable.print();

if (!opts.silent)
if (opts.logType !== "silent")
console.log(client.getLogsCommand(podDef.metadata.name) + "\n\n");
}

Expand Down
9 changes: 5 additions & 4 deletions javascript/packages/orchestrator/src/test-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {
decorators,
getLokiUrl,
readNetworkConfig,
setSilent,
setLogType,
sleep,
LogType,
} from "@zombienet/utils";
import fs from "fs";
import Mocha from "mocha";
Expand Down Expand Up @@ -34,11 +35,11 @@ export async function run(
provider: string,
inCI = false,
concurrency = 1,
silent = false,
logType: LogType = "table",
runningNetworkSpecPath: string | undefined,
dir: string | undefined,
) {
setSilent(silent);
logType && setLogType(logType);
let network: Network;
const backchannelMap: BackchannelMap = {};

Expand Down Expand Up @@ -92,7 +93,7 @@ export async function run(
network = await start(creds!, config, {
spawnConcurrency: concurrency,
inCI,
silent,
logType,
dir,
});
} else {
Expand Down
49 changes: 43 additions & 6 deletions javascript/packages/utils/src/tableCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Table, {
HorizontalTableRow,
VerticalTableRow,
} from "cli-table3";
import { decorators } from "./colors";

type CharsObj = {
[key in
Expand Down Expand Up @@ -54,15 +55,37 @@ interface TableCreationProps {
wordWrap?: boolean;
}

export type LogType = "text" | "table" | "silent";

// Module level config.
let silent = true;
export function setSilent(value: boolean) {
silent = value;
}
let logType: LogType = "table";
const logTypeValues = ["text", "table", "silent"];

export const getLogType = (logType: LogType): LogType => {
if (logTypeValues.includes(logType)) {
return logType;
} else {
logType &&
console.error(
`${decorators.red(`
Argument 'logType' provided ('${logType}') is not one of the accepted params; Falling back to 'table'.
Possible values: ${logTypeValues.join(
", ",
)} - Defaults to 'table'.\n\n`)}`,
);
return "table";
}
};

export const setLogType = (value: LogType) => {
logType = value;
};

export class CreateLogTable {
table: CreatedTable | undefined;
colWidths: number[];
wordWrap: boolean;
text: string[];

constructor({ head, colWidths, doubleBorder, wordWrap }: TableCreationProps) {
this.wordWrap = wordWrap || false;
Expand All @@ -74,6 +97,7 @@ export class CreateLogTable {
if (doubleBorder) {
params.chars = chars;
}
this.text = [];
this.table = new Table(params);
}

Expand All @@ -96,12 +120,25 @@ export class CreateLogTable {
input[index] = inp;
}
});
this.table!.push(input);
if (logType === "text") {
if (input[0] === "\x1B[36mCommand\x1B[0m") {
input[1] = input[1].replace(/\n/g, " ");
}
// if input has a JSON - that means a merged cell
if (input[0]?.content) {
input[0] = input[0]?.content;
}
console.log(input.join(" : "));
} else if (logType === "silent") {
return;
} else if (logType === "table") {
this.table!.push(input);
}
});
};

print = () => {
if (!silent) console.log(this.table!.toString());
if (logType === "table") console.log(this.table!.toString());
};

// This function makes the process of creating a table, pushing data and printing it faster
Expand Down

0 comments on commit b96ff22

Please sign in to comment.