blob: 627359c4d850455494bd685e2fab18c4c92a4cc2 [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { Injectable } from '@angular/core';
17import { environment } from '../environments/environment';
18import { Logger } from './log.service';
19
Sean Condon2bd11b72018-06-15 08:00:48 +010020export let isDebugMode: boolean = !environment.production;
Sean Condon83fc39f2018-04-19 18:56:13 +010021
Sean Condona00bf382018-06-23 07:54:01 +010022const noop = (): any => undefined;
23
Sean Condon83fc39f2018-04-19 18:56:13 +010024/**
25 * ONOS GUI -- LogService
26 * Inspired by https://robferguson.org/blog/2017/09/09/a-simple-logging-service-for-angular-4/
27 */
Sean Condona00bf382018-06-23 07:54:01 +010028@Injectable({
29 providedIn: 'root',
30})
Sean Condon83fc39f2018-04-19 18:56:13 +010031export class ConsoleLoggerService implements Logger {
32
33 get debug() {
34 if (isDebugMode) {
Sean Condondfc6dba2019-11-09 11:50:23 +000035 // tslint:disable-next-line:no-console
Sean Condon83fc39f2018-04-19 18:56:13 +010036 return console.debug.bind(console);
37 } else {
Sean Condona00bf382018-06-23 07:54:01 +010038 return noop;
Sean Condon83fc39f2018-04-19 18:56:13 +010039 }
40 }
41
42 get info() {
43 if (isDebugMode) {
Sean Condondfc6dba2019-11-09 11:50:23 +000044 // tslint:disable-next-line:no-console
Sean Condon83fc39f2018-04-19 18:56:13 +010045 return console.info.bind(console);
46 } else {
Sean Condona00bf382018-06-23 07:54:01 +010047 return noop;
Sean Condon83fc39f2018-04-19 18:56:13 +010048 }
49 }
50
51 get warn() {
52 return console.warn.bind(console);
53 }
54
55 get error() {
56 return console.error.bind(console);
57 }
58
59 invokeConsoleMethod(type: string, args?: any): void {
Sean Condona00bf382018-06-23 07:54:01 +010060 const logFn: Function = (console)[type] || console.log || noop;
Sean Condon83fc39f2018-04-19 18:56:13 +010061 logFn.apply(console, [args]);
62 }
63}