/******************************************************************************** * Copyright (C) 2017 Ericsson 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 { Container, ContainerModule } from 'inversify'; import { ConsoleLogger, LoggerServer, loggerPath, Logger } from '../common'; import { LoggerImpl, LoggerFactory, LoggerName, rootLoggerName } from '../common'; import { LoggerWatcher } from '../common/logger-watcher'; import { IConnectionProvider } from '@gedit/connection'; import { FrontendApplicationContribution } from '@gedit/application-common/lib/browser'; export default new ContainerModule(bind => { bind(FrontendApplicationContribution).toDynamicValue(ctx => ({ initialize(): void { // TODO 先注解掉 // setRootLogger(ctx.container.get(Logger)) } })); bind(LoggerName).toConstantValue(rootLoggerName); bind(Logger).to(LoggerImpl).inSingletonScope().whenTargetIsDefault(); bind(LoggerWatcher).toSelf().inSingletonScope(); bind(LoggerServer).toDynamicValue(ctx => { const loggerWatcher = ctx.container.get(LoggerWatcher); const connection = ctx.container.get(IConnectionProvider); // @ts-ignore const target = connection.createProxy(loggerPath, loggerWatcher.getLoggerClient()); function get(_: LoggerServer, property: K): LoggerServer[K] | LoggerServer['log'] { if (property === 'log') { return (name, logLevel, message, params) => { ConsoleLogger.log(name, logLevel, message, params); return target.log(name, logLevel, message, params); }; } return target[property]; } return new Proxy(target, {get}); }).inSingletonScope(); bind(LoggerFactory).toFactory(ctx => (name: string) => { const child = new Container({defaultScope: 'Singleton'}); child.parent = ctx.container; child.bind(Logger).to(LoggerImpl).inTransientScope(); child.bind(LoggerName).toConstantValue(name); return child.get(Logger); } ); });