Skip to content

Commit

Permalink
Merge pull request #29 from holochain/feat/custom-log-levels
Browse files Browse the repository at this point in the history
Add option to specify rust-log and wasm-log via CLI args
  • Loading branch information
matthme authored Jan 31, 2024
2 parents af43d34 + 79304c5 commit cb2cbc3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
30 changes: 23 additions & 7 deletions src/main/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@ import fs from 'fs';
import type { HolochainVersion } from '../types';
import { DEFAULT_HOLOCHAIN_VERSION } from './binaries';

export interface CliArgs {
export type CliArgs = {
profile?: string;
holochainPath?: string;
adminPort?: number;
lairUrl?: string;
appsDataDir?: string;
bootstrapUrl?: string;
signalingUrl?: string;
}
rustLog?: string;
wasmLog?: string;
};

export type ValidatedCliArgs = {
profile: string | undefined;
holochainVersion: HolochainVersion;
bootstrapUrl: string | undefined;
signalingUrl: string | undefined;
rustLog: string | undefined;
wasmLog: string | undefined;
};

export function validateArgs(
args: CliArgs,
): [string | undefined, HolochainVersion, string | undefined, string | undefined] {
export function validateArgs(args: CliArgs): ValidatedCliArgs {
const allowedProfilePattern = /^[0-9a-zA-Z-]+$/;
if (args.profile && !allowedProfilePattern.test(args.profile)) {
throw new Error(
Expand Down Expand Up @@ -90,7 +99,14 @@ export function validateArgs(
const profile = args.profile ? args.profile : undefined;

const bootstrapUrl = args.bootstrapUrl && !args.adminPort ? args.bootstrapUrl : undefined;
const singalingUrl = args.signalingUrl && !args.adminPort ? args.signalingUrl : undefined;
const signalingUrl = args.signalingUrl && !args.adminPort ? args.signalingUrl : undefined;

return [profile, holochainVersion, bootstrapUrl, singalingUrl];
return {
profile,
holochainVersion,
bootstrapUrl,
signalingUrl,
rustLog: args.rustLog ? args.rustLog : undefined,
wasmLog: args.wasmLog ? args.wasmLog : undefined,
};
}
18 changes: 14 additions & 4 deletions src/main/holochainManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export type AppPort = number;

const DEFAULT_BOOTSTRAP_SERVER = 'https://bootstrap.holo.host';
const DEFAULT_SIGNALING_SERVER = 'wss://signal.holo.host';
const DEFAULT_RUST_LOG =
'warn,' +
// this thrashes on startup
'wasmer_compiler_cranelift=error,' +
// this gives a bunch of warnings about how long db accesses are taking, tmi
'holochain_sqlite::db::access=error,' +
// this gives a lot of "search_and_discover_peer_connect: no peers found, retrying after delay" messages on INFO
'kitsune_p2p::spawn::actor::discover=error';
const DEFAULT_WASM_LOG = 'warn';

export class HolochainManager {
processHandle: childProcess.ChildProcessWithoutNullStreams | undefined;
Expand Down Expand Up @@ -64,6 +73,8 @@ export class HolochainManager {
lairUrl: string,
bootstrapUrl?: string,
signalingUrl?: string,
rustLog?: string,
wasmLog?: string,
nonDefaultPartition?: HolochainPartition, // launch with data from a non-default partition
): Promise<[HolochainManager, HolochainDataRoot]> {
let holochainDataRoot: HolochainDataRoot;
Expand Down Expand Up @@ -170,11 +181,10 @@ export class HolochainManager {
}

const binary = version.type === 'built-in' ? HOLOCHAIN_BINARIES[version.version] : version.path;
console.log('HOLOCHAIN_BINARIES: ', HOLOCHAIN_BINARIES);
console.log('HOLOCHAIN BINARY: ', binary);
const conductorHandle = childProcess.spawn(binary, ['-c', configPath, '-p'], {
env: {
RUST_LOG: 'info',
RUST_LOG: rustLog ? rustLog : DEFAULT_RUST_LOG,
WASM_LOG: wasmLog ? wasmLog : DEFAULT_WASM_LOG,
},
});
conductorHandle.stdin.write(password);
Expand Down Expand Up @@ -256,7 +266,7 @@ export class HolochainManager {
await this.adminWebsocket.enableApp({ installed_app_id: appId });
console.log('Insalled app.');
const installedApps = await this.adminWebsocket.listApps({});
console.log('Installed apps: ', installedApps);
// console.log('Installed apps: ', installedApps);
this.installedApps = installedApps;
this.launcherEmitter.emit(APP_INSTALLED, {
version: this.version,
Expand Down
42 changes: 25 additions & 17 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,17 @@ cli
.option(
'-s, --signaling-url <url>',
'URL of the signaling server to use. Is ignored if an external holochain binary is being used.',
);
)
.option('--rust-log <string>', 'Set the RUST_LOG to be used.')
.option('--wasm-log <string>', 'Set the WASM_LOG to be used.');

cli.parse();

console.log('GOT CLI ARGS: ', cli.opts());

const [PROFILE, HOLOCHAIN_VERSION, BOOTSTRAP_URL, SIGNALING_URL] = validateArgs(cli.opts());
const VALIDATED_CLI_ARGS = validateArgs(cli.opts());

console.log('VALIDATED CLI ARGS: ', PROFILE, HOLOCHAIN_VERSION, BOOTSTRAP_URL, SIGNALING_URL);
console.log('VALIDATED CLI ARGS: ', VALIDATED_CLI_ARGS);

const appName = app.getName();

Expand Down Expand Up @@ -121,7 +123,7 @@ protocol.registerSchemesAsPrivileged([
},
]);

const LAUNCHER_FILE_SYSTEM = LauncherFileSystem.connect(app, PROFILE);
const LAUNCHER_FILE_SYSTEM = LauncherFileSystem.connect(app, VALIDATED_CLI_ARGS.profile);
const LAUNCHER_EMITTER = new LauncherEmitter();

setupLogs(LAUNCHER_EMITTER, LAUNCHER_FILE_SYSTEM);
Expand Down Expand Up @@ -205,7 +207,7 @@ app.on('quit', () => {
async function handleSetupAndLaunch(password: string) {
if (!LAUNCHER_WINDOWS) throw new Error('Main window needs to exist before launching.');

if (HOLOCHAIN_VERSION.type !== 'running-external') {
if (VALIDATED_CLI_ARGS.holochainVersion.type !== 'running-external') {
const lairHandleTemp = childProcess.spawnSync(LAIR_BINARY, ['--version']);
if (!lairHandleTemp.stdout) {
console.error(`Failed to run lair-keystore binary:\n${lairHandleTemp}`);
Expand All @@ -229,10 +231,11 @@ async function handleLaunch(password: string) {
LAUNCHER_EMITTER.emit(LOADING_PROGRESS_UPDATE, 'startingLairKeystore');
let lairUrl: string;

if (HOLOCHAIN_VERSION.type === 'running-external') {
lairUrl = HOLOCHAIN_VERSION.lairUrl;
if (VALIDATED_CLI_ARGS.holochainVersion.type === 'running-external') {
lairUrl = VALIDATED_CLI_ARGS.holochainVersion.lairUrl;
const externalZomeCallSigner = await rustUtils.ZomeCallSigner.connect(lairUrl, password);
CUSTOM_ZOME_CALL_SIGNERS[HOLOCHAIN_VERSION.adminPort] = externalZomeCallSigner;
CUSTOM_ZOME_CALL_SIGNERS[VALIDATED_CLI_ARGS.holochainVersion.adminPort] =
externalZomeCallSigner;
} else {
const [lairHandle, lairUrl2] = await launchLairKeystore(
LAIR_BINARY,
Expand All @@ -253,22 +256,24 @@ async function handleLaunch(password: string) {
if (!LAUNCHER_WINDOWS) throw new Error('Main window needs to exist before launching.');

const nonDefaultPartition: HolochainPartition =
HOLOCHAIN_VERSION.type === 'running-external'
? { type: 'external', name: 'unknown', path: HOLOCHAIN_VERSION.appsDataDir }
: HOLOCHAIN_VERSION.type === 'custom-path'
VALIDATED_CLI_ARGS.holochainVersion.type === 'running-external'
? { type: 'external', name: 'unknown', path: VALIDATED_CLI_ARGS.holochainVersion.appsDataDir }
: VALIDATED_CLI_ARGS.holochainVersion.type === 'custom-path'
? { type: 'custom', name: 'unknown' }
: { type: 'default' };

console.log('HOLOCHAIN_VERSION: ', HOLOCHAIN_VERSION);
console.log('VALIDATED_CLI_ARGS.holochainVersion: ', VALIDATED_CLI_ARGS.holochainVersion);

const [holochainManager, holochainDataRoot] = await HolochainManager.launch(
LAUNCHER_EMITTER,
LAUNCHER_FILE_SYSTEM,
password,
HOLOCHAIN_VERSION,
VALIDATED_CLI_ARGS.holochainVersion,
lairUrl,
BOOTSTRAP_URL,
SIGNALING_URL,
VALIDATED_CLI_ARGS.bootstrapUrl,
VALIDATED_CLI_ARGS.signalingUrl,
VALIDATED_CLI_ARGS.rustLog,
VALIDATED_CLI_ARGS.wasmLog,
nonDefaultPartition,
);
HOLOCHAIN_DATA_ROOT = holochainDataRoot;
Expand Down Expand Up @@ -318,7 +323,9 @@ const router = t.router({
installedAppId: appInfo.installed_app_id,
agentPubKey: appInfo.agent_pub_key,
adminPort:
HOLOCHAIN_VERSION.type === 'running-external' ? HOLOCHAIN_VERSION.adminPort : undefined,
VALIDATED_CLI_ARGS.holochainVersion.type === 'running-external'
? VALIDATED_CLI_ARGS.holochainVersion.adminPort
: undefined,
};
happWindow.on('close', () => {
delete WINDOW_INFO_MAP[happWindow.webContents.id];
Expand Down Expand Up @@ -350,7 +357,8 @@ const router = t.router({
}),
lairSetupRequired: t.procedure.query(() => {
const isInitialized =
LAUNCHER_FILE_SYSTEM.keystoreInitialized() || HOLOCHAIN_VERSION.type === 'running-external';
LAUNCHER_FILE_SYSTEM.keystoreInitialized() ||
VALIDATED_CLI_ARGS.holochainVersion.type === 'running-external';
const isInitializedValidated = validateWithZod({
schema: z.boolean(),
data: isInitialized,
Expand Down

0 comments on commit cb2cbc3

Please sign in to comment.