Skip to content

Commit

Permalink
eclipse-theiaGH-2028: Moved the bunyan logger to its own extension.
Browse files Browse the repository at this point in the history
Closes: eclipse-theia#2026.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
  • Loading branch information
kittaakos committed Jun 27, 2018
1 parent b216840 commit 808d070
Show file tree
Hide file tree
Showing 21 changed files with 286 additions and 114 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cache:
- examples/browser/node_modules
- examples/electron/node_modules
- node_modules
- packages/bunyan/node_modules
- packages/callhierarchy/node_modules
- packages/core/node_modules
- packages/cpp/node_modules
Expand Down
25 changes: 10 additions & 15 deletions dev-packages/application-manager/src/application-package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import * as path from 'path';
import * as fs from 'fs-extra';
import * as cp from 'child_process';
import { ApplicationPackage, ApplicationPackageOptions } from "@theia/application-package";
import { WebpackGenerator, FrontendGenerator, BackendGenerator } from "./generator";
import { ApplicationPackage, ApplicationPackageOptions } from '@theia/application-package';
import { WebpackGenerator, FrontendGenerator, BackendGenerator } from './generator';
import { ApplicationProcess } from './application-process';

export class ApplicationPackageManager {
Expand All @@ -34,15 +35,15 @@ export class ApplicationPackageManager {
constructor(options: ApplicationPackageOptions) {
this.pck = new ApplicationPackage(options);
this.process = new ApplicationProcess(this.pck, options.projectPath);
this.__process = new ApplicationProcess(this.pck, `${__dirname}/..`);
this.__process = new ApplicationProcess(this.pck, path.join(__dirname, '..'));
this.webpack = new WebpackGenerator(this.pck);
this.backend = new BackendGenerator(this.pck);
this.frontend = new FrontendGenerator(this.pck);
}

protected async remove(path: string): Promise<void> {
if (await fs.pathExists(path)) {
await fs.remove(path);
protected async remove(fsPath: string): Promise<void> {
if (await fs.pathExists(fsPath)) {
await fs.remove(fsPath);
}
}

Expand Down Expand Up @@ -77,16 +78,12 @@ export class ApplicationPackageManager {
}

async startElectron(args: string[]): Promise<void> {
return this.__process.bunyan(
this.__process.spawnBin('electron', [this.pck.frontend('electron-main.js'), ...args], {
stdio: [0, 'pipe', 'pipe']
})
);
this.__process.spawnBin('electron', [this.pck.frontend('electron-main.js'), ...args]);
}

async startBrowser(args: string[]): Promise<void> {
const options: cp.ForkOptions = {
stdio: [0, 'pipe', 'pipe', 'ipc'],
stdio: [0, 1, 2, 'ipc'],
env: {
...process.env,
THEIA_PARENT_PID: String(process.pid)
Expand All @@ -98,9 +95,7 @@ export class ApplicationPackageManager {
const inspectArg = mainArgs.splice(inspectIndex, 1)[0];
options.execArgv = ['--nolazy', inspectArg];
}
return this.__process.bunyan(
this.__process.fork(this.pck.backend('main.js'), mainArgs, options)
);
this.__process.fork(this.pck.backend('main.js'), mainArgs, options);
}

}
19 changes: 10 additions & 9 deletions dev-packages/application-manager/src/application-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,44 @@ export class ApplicationProcess {
spawn(command: string, args?: string[], options?: cp.SpawnOptions): cp.ChildProcess {
return cp.spawn(command, args, Object.assign({}, this.defaultOptions, options));
}

fork(modulePath: string, args?: string[], options?: cp.ForkOptions): cp.ChildProcess {
return cp.fork(modulePath, args, Object.assign({}, this.defaultOptions, options));
}

canRun(command: string): boolean {
return fs.existsSync(this.resolveBin(command));
}

run(command: string, args: string[], options?: cp.SpawnOptions): Promise<void> {
const commandProcess = this.spawnBin(command, args, options);
return this.promisify(command, commandProcess);
}

spawnBin(command: string, args: string[], options?: cp.SpawnOptions): cp.ChildProcess {
const binPath = this.resolveBin(command);
return this.spawn(binPath, args, options);
}

protected resolveBin(command: string): string {
const commandPath = path.resolve(this.binProjectPath, 'node_modules', '.bin', command);
return process.platform === 'win32' ? commandPath + '.cmd' : commandPath;
}

bunyan(childProcess: cp.ChildProcess): Promise<void> {
const bunyanProcess = this.spawnBin('bunyan', []);
childProcess.stdout.pipe(bunyanProcess.stdin);
childProcess.stderr.pipe(bunyanProcess.stdin);
return this.promisify('bunyan', bunyanProcess);
}

protected promisify(command: string, p: cp.ChildProcess): Promise<void> {
return new Promise((resolve, reject) => {
p.stdout.on('data', data => this.pck.log(data.toString()));
p.stderr.on('data', data => this.pck.error(data.toString()));
p.on('error', reject);
p.on('close', code => {
p.on('close', (code, signal) => {
if (signal) {
reject(new Error(`${command} exited with an unexpected signal: ${signal}.`));
return;
}
if (code === 0) {
resolve();
} else {
reject(new Error(command + ' failed with the exit code ' + code));
reject(new Error(`${command} exited with an unexpected code: ${code}.`));
}
});
});
Expand Down
9 changes: 9 additions & 0 deletions packages/bunyan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Theia bunyan Logger Extension

This extension provides the [`bunyan`](https://www.npmjs.com/package/bunyan)-based logger implementation for Theia.

See [here](https://github.com/theia-ide/theia) for a detailed documentation.

## License
- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
11 changes: 11 additions & 0 deletions packages/bunyan/compile.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../configs/base.tsconfig",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"baseUrl": "."
},
"include": [
"src"
]
}
48 changes: 48 additions & 0 deletions packages/bunyan/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@theia/bunyan",
"version": "0.3.11",
"description": "Theia - bunyan Logger Extension",
"dependencies": {
"@theia/core": "^0.3.11",
"@types/bunyan": "^1.8.0",
"bunyan": "^1.8.10"
},
"publishConfig": {
"access": "public"
},
"theiaExtensions": [
{
"backend": "lib/node/bunyan-backend-module"
}
],
"keywords": [
"theia-extension"
],
"license": "EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0",
"repository": {
"type": "git",
"url": "https://github.com/theia-ide/theia.git"
},
"bugs": {
"url": "https://github.com/theia-ide/theia/issues"
},
"homepage": "https://github.com/theia-ide/theia",
"files": [
"lib",
"src"
],
"scripts": {
"prepare": "yarn run clean && yarn run build",
"clean": "theiaext clean",
"build": "theiaext build",
"watch": "theiaext watch",
"test": "theiaext test",
"docs": "theiaext docs"
},
"devDependencies": {
"@theia/ext-scripts": "^0.3.11"
},
"nyc": {
"extends": "../../configs/nyc.json"
}
}
23 changes: 23 additions & 0 deletions packages/bunyan/src/node/bunyan-backend-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { interfaces, ContainerModule } from 'inversify';
import { ILoggerServer } from '@theia/core/lib/common/logger-protocol';
import { BunyanLoggerServer } from './bunyan-logger-server';

export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind) => {
rebind(ILoggerServer).to(BunyanLoggerServer).inSingletonScope();
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

import * as bunyan from 'bunyan';
import { inject, injectable, postConstruct } from 'inversify';
import { LogLevel, rootLoggerName } from '../common/logger';
import { ILoggerServer, ILoggerClient, ILogLevelChangedEvent } from '../common/logger-protocol';
import { LoggerWatcher } from '../common/logger-watcher';
import { LogLevelCliContribution } from './logger-cli-contribution';
import { LoggerWatcher } from '@theia/core/lib/common/logger-watcher';
import { LogLevel, rootLoggerName } from '@theia/core/lib/common/logger';
import { LogLevelCliContribution } from '@theia/core/lib/node/logger-cli-contribution';
import { ILoggerServer, ILoggerClient, ILogLevelChangedEvent } from '@theia/core/lib/common/logger-protocol';

@injectable()
export class BunyanLoggerServer implements ILoggerServer {
Expand Down Expand Up @@ -48,19 +48,17 @@ export class BunyanLoggerServer implements ILoggerServer {
}

protected updateLogLevels() {
for (const [loggerName, _] of this.loggers) {
for (const loggerName of this.loggers.keys()) {
const newLevel = this.cli.logLevelFor(loggerName);
this.setLogLevel(loggerName, newLevel);
}
}

protected makeLoggerOptions(name: string) {
const opts = {
return {
logger: name,
level: this.toBunyanLevel(this.cli.logLevelFor(name)),
};

return opts;
}

dispose(): void {
Expand Down
21 changes: 21 additions & 0 deletions packages/bunyan/src/package.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

describe('bunyan package', () => {

it('support code coverage statistics', () => true);

});
6 changes: 3 additions & 3 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ existing loggers. However, each log message specifies from which logger it
comes from, which can give an idea, without having to read the code:

```
[2018-05-10T20:01:45.608Z] INFO: Theia/7045 on elxacz23q12: (logger=root)
^^^^ ^^^^
log level logger name
root INFO [nsfw-watcher: 10734] Started watching: /Users/captain.future/git/theia/CONTRIBUTING.md
^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^
```
Where `root` is the name of the logger and `INFO` is the log level. These are optionally followed by the name of a child process and the process ID.

## License
- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@types/react-dom": "^16.0.6",
"ajv": "^5.2.2",
"body-parser": "^1.17.2",
"bunyan": "^1.8.10",
"electron": "1.8.2-beta.5",
"es6-promise": "^4.2.4",
"express": "^4.16.3",
Expand Down
40 changes: 0 additions & 40 deletions packages/core/src/common/console-logger-server.ts

This file was deleted.

Loading

0 comments on commit 808d070

Please sign in to comment.