blob: 1ba88f990d247b61779a98e0ff66ab07bec18035 [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
20export let isDebugMode = environment.isDebugMode;
21
22const noop = (): any => undefined;
23
24/**
25 * ONOS GUI -- LogService
26 * Inspired by https://robferguson.org/blog/2017/09/09/a-simple-logging-service-for-angular-4/
27 */
28@Injectable()
29export class ConsoleLoggerService implements Logger {
30
31 get debug() {
32 if (isDebugMode) {
33 return console.debug.bind(console);
34 } else {
35 return noop;
36 }
37 }
38
39 get info() {
40 if (isDebugMode) {
41 return console.info.bind(console);
42 } else {
43 return noop;
44 }
45 }
46
47 get warn() {
48 return console.warn.bind(console);
49 }
50
51 get error() {
52 return console.error.bind(console);
53 }
54
55 invokeConsoleMethod(type: string, args?: any): void {
56 const logFn: Function = (console)[type] || console.log || noop;
57 logFn.apply(console, [args]);
58 }
59}