Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | import { Injectable } from '@angular/core'; |
| 17 | import { environment } from '../environments/environment'; |
| 18 | import { Logger } from './log.service'; |
| 19 | |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 20 | export let isDebugMode: boolean = !environment.production; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 21 | |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 22 | const noop = (): any => undefined; |
| 23 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 24 | /** |
| 25 | * ONOS GUI -- LogService |
| 26 | * Inspired by https://robferguson.org/blog/2017/09/09/a-simple-logging-service-for-angular-4/ |
| 27 | */ |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 28 | @Injectable({ |
| 29 | providedIn: 'root', |
| 30 | }) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 31 | export class ConsoleLoggerService implements Logger { |
| 32 | |
| 33 | get debug() { |
| 34 | if (isDebugMode) { |
Sean Condon | dfc6dba | 2019-11-09 11:50:23 +0000 | [diff] [blame] | 35 | // tslint:disable-next-line:no-console |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 36 | return console.debug.bind(console); |
| 37 | } else { |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 38 | return noop; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | get info() { |
| 43 | if (isDebugMode) { |
Sean Condon | dfc6dba | 2019-11-09 11:50:23 +0000 | [diff] [blame] | 44 | // tslint:disable-next-line:no-console |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 45 | return console.info.bind(console); |
| 46 | } else { |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 47 | return noop; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 48 | } |
| 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 Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 60 | const logFn: Function = (console)[type] || console.log || noop; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 61 | logFn.apply(console, [args]); |
| 62 | } |
| 63 | } |