Skip to content

Commit

Permalink
Merge pull request #182 from electron/electron-logging
Browse files Browse the repository at this point in the history
feat: add an execution toggle to enable advanced electron logging
  • Loading branch information
felixrieseberg authored Mar 27, 2019
2 parents 94da511 + 00bb15a commit b3e25cf
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/less/root.less
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

// Variables, also imported in each file
@import "variables.less";

@import "main.less";
@import "container.less";
@import "mosaic-vendor.less";
Expand Down
45 changes: 36 additions & 9 deletions src/renderer/components/settings-execution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class ExecutionSettings extends React.Component<ExecutionSettingsProps, {
super(props);

this.handleDeleteDataChange = this.handleDeleteDataChange.bind(this);
this.handleElectronLoggingChange = this.handleElectronLoggingChange.bind(this);
}

/**
Expand All @@ -35,14 +36,30 @@ export class ExecutionSettings extends React.Component<ExecutionSettingsProps, {
this.props.appState.isKeepingUserDataDirs = checked;
}

/**
* Handles a change on whether or not electron should log more things
*
* @param {React.ChangeEvent<HTMLInputElement>} event
*/
public handleElectronLoggingChange(
event: React.FormEvent<HTMLInputElement>
) {
const { checked } = event.currentTarget;
this.props.appState.isEnablingElectronLogging = checked;
}

public render() {
const { isKeepingUserDataDirs } = this.props.appState;
const { isKeepingUserDataDirs, isEnablingElectronLogging } = this.props.appState;

const deleteUserDirLabel = `
Whenever Electron runs, it creates a user data directory for cookies, the cache,
and various other things that it needs to keep around. Since fiddles are usually
just run once, we delete this directory after your fiddle exits. Enable this
setting to keep the user data directories around.
`.trim();
const electronLoggingLabel = `
There are some flags that Electron uses to log extra information both internally
and through Chromium. Enable this option to make Fiddle produce those logs.`.trim();

return (
<div>
Expand All @@ -52,14 +69,24 @@ export class ExecutionSettings extends React.Component<ExecutionSettingsProps, {
</Callout>
<br />
<Callout>
<FormGroup label={deleteUserDirLabel}>
<Checkbox
checked={isKeepingUserDataDirs}
label='Do not delete user data directories.'
onChange={this.handleDeleteDataChange}
/>
</FormGroup>
</Callout>
<FormGroup label={deleteUserDirLabel}>
<Checkbox
checked={isKeepingUserDataDirs}
label='Do not delete user data directories.'
onChange={this.handleDeleteDataChange}
/>
</FormGroup>
</Callout>
<br />
<Callout>
<FormGroup label={electronLoggingLabel}>
<Checkbox
checked={isEnablingElectronLogging}
label='Enable advanced Electron logging.'
onChange={this.handleElectronLoggingChange}
/>
</FormGroup>
</Callout>
</div>
);
}
Expand Down
14 changes: 13 additions & 1 deletion src/renderer/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,19 @@ export class Runner {
const binaryPath = binaryManager.getElectronBinaryPath(version, localPath);
console.log(`Runner: Binary ${binaryPath} ready, launching`);

this.child = spawn(binaryPath, [ dir, '--inspect' ]);
const env = { ...process.env };
if (this.appState.isEnablingElectronLogging) {
env.ELECTRON_ENABLE_LOGGING = 'true';
env.ELECTRON_ENABLE_STACK_DUMPING = 'true';
} else {
delete env.ELECTRON_ENABLE_LOGGING;
delete env.ELECTRON_ENABLE_STACK_DUMPING;
}

this.child = spawn(binaryPath, [ dir, '--inspect' ], {
cwd: dir,
env,
});
this.appState.isRunning = true;
pushOutput(`Electron v${version} started.`);

Expand Down
2 changes: 2 additions & 0 deletions src/renderer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class AppState {
this.retrieve('statesToShow') as Array<ElectronVersionState>
|| [ ElectronVersionState.downloading, ElectronVersionState.ready, ElectronVersionState.unknown ];
@observable public isKeepingUserDataDirs: boolean = !!this.retrieve('isKeepingUserDataDirs');
@observable public isEnablingElectronLogging: boolean = !!this.retrieve('isEnablingElectronLogging');

@observable public binaryManager: BinaryManager = new BinaryManager();

Expand Down Expand Up @@ -117,6 +118,7 @@ export class AppState {
autorun(() => this.save('gitHubToken', this.gitHubToken));
autorun(() => this.save('gitHubPublishAsPublic', this.gitHubPublishAsPublic));
autorun(() => this.save('isKeepingUserDataDirs', this.isKeepingUserDataDirs));
autorun(() => this.save('isEnablingElectronLogging', this.isEnablingElectronLogging));
autorun(() => this.save('version', this.version));
autorun(() => this.save('versionsToShow', this.versionsToShow));
autorun(() => this.save('statesToShow', this.statesToShow));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,17 @@ exports[`ExecutionSettings component renders 1`] = `
/>
</Blueprint3.FormGroup>
</Blueprint3.Callout>
<br />
<Blueprint3.Callout>
<Blueprint3.FormGroup
label="There are some flags that Electron uses to log extra information both internally
and through Chromium. Enable this option to make Fiddle produce those logs."
>
<Blueprint3.Checkbox
label="Enable advanced Electron logging."
onChange={[Function]}
/>
</Blueprint3.FormGroup>
</Blueprint3.Callout>
</div>
`;
20 changes: 20 additions & 0 deletions tests/renderer/components/settings-execution-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,24 @@ describe('ExecutionSettings component', () => {
expect(store.isKeepingUserDataDirs).toBe(true);
});
});

describe('handleElectronLoggingChange()', () => {
it('handles a new selection', async () => {
const wrapper = shallow(
<ExecutionSettings appState={store} />
);
const instance = wrapper.instance() as any;
await instance.handleElectronLoggingChange({
currentTarget: { checked: false }
});

expect(store.isEnablingElectronLogging).toBe(false);

await instance.handleElectronLoggingChange({
currentTarget: { checked: true }
});

expect(store.isEnablingElectronLogging).toBe(true);
});
});
});
16 changes: 16 additions & 0 deletions tests/renderer/runner-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ describe('Runner component', () => {
expect(store.isRunning).toBe(true);
});

it('runs with logging when enabled', async () => {
store.isEnablingElectronLogging = true;
(findModulesInEditors as any).mockReturnValueOnce([ 'fake-module' ]);
(spawn as jest.Mock).mockImplementationOnce((_, __, opts) => {
expect(opts.env).toHaveProperty('ELECTRON_ENABLE_LOGGING');
expect(opts.env).toHaveProperty('ELECTRON_ENABLE_STACK_DUMPING');
return mockChild;
});

expect(await instance.run()).toBe(true);
expect(store.binaryManager.getIsDownloaded).toHaveBeenCalled();
expect(window.ElectronFiddle.app.fileManager.saveToTemp).toHaveBeenCalled();
expect(installModules).toHaveBeenCalled();
expect(store.isRunning).toBe(true);
});

it('emits output', async () => {
(findModulesInEditors as any).mockReturnValueOnce([ 'fake-module' ]);
(spawn as any).mockReturnValueOnce(mockChild);
Expand Down

0 comments on commit b3e25cf

Please sign in to comment.