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) { |
| 35 | return console.debug.bind(console); |
| 36 | } else { |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 37 | return noop; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
| 41 | get info() { |
| 42 | if (isDebugMode) { |
| 43 | return console.info.bind(console); |
| 44 | } else { |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 45 | return noop; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | get warn() { |
| 50 | return console.warn.bind(console); |
| 51 | } |
| 52 | |
| 53 | get error() { |
| 54 | return console.error.bind(console); |
| 55 | } |
| 56 | |
| 57 | invokeConsoleMethod(type: string, args?: any): void { |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 58 | const logFn: Function = (console)[type] || console.log || noop; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 59 | logFn.apply(console, [args]); |
| 60 | } |
| 61 | } |