Add new code new version
[sdc.git] / dox-sequence-diagram-ui / src / main / webapp / lib / ecomp / asdc / sequencer / common / Logger.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16
17 /* eslint-disable no-console */
18
19 import Common from './Common';
20
21 /**
22  * Logger, to allow calls to console.log during development, but
23  * disable them for production.
24  */
25 export default class Logger {
26
27   // ///////////////////////////////////////////////////////////////////////////////////////////////
28
29   /**
30    * No-op call so that we can leave imports in place,
31    * even when there's no debugging.
32    */
33   static noop() {
34     // Nothing.
35   }
36
37   // ///////////////////////////////////////////////////////////////////////////////////////////////
38
39   /**
40    * Set debug level.
41    * @param level threshold.
42    */
43   static setLevel(level) {
44     this.level = Logger.OFF;
45     if (Common.getType(level) === 'Number') {
46       this.level = level;
47     } else {
48       this.level = Logger[level];
49     }
50   }
51
52   // ///////////////////////////////////////////////////////////////////////////////////////////////
53
54   /**
55    * Get debug level.
56    * @returns {number|*}
57    */
58   static getLevel() {
59     return this.level;
60   }
61
62   // ///////////////////////////////////////////////////////////////////////////////////////////////
63
64   /**
65    * Write DEBUG-level log.
66    * @param msg message or tokens.
67    */
68   static debug(...msg) {
69     if (this.level >= Logger.DEBUG) {
70       const out = this.serialize(msg);
71       console.info(`ASDCS [DEBUG] ${out}`);
72     }
73   }
74
75   // ///////////////////////////////////////////////////////////////////////////////////////////////
76
77   /**
78    * Write INFO-level log.
79    * @param msg message or tokens.
80    */
81   static info(...msg) {
82     if (this.level >= Logger.INFO) {
83       const out = this.serialize(msg);
84       console.info(`ASDCS [INFO] ${out}`);
85     }
86   }
87
88   // ///////////////////////////////////////////////////////////////////////////////////////////////
89
90   /**
91    * Write debug.
92    * @param msg message or tokens.
93    */
94   static warn(msg) {
95     if (this.level >= Logger.WARN) {
96       const out = this.serialize(msg);
97       console.warn(`ASDCS [WARN] ${out}`);
98     }
99   }
100
101   // ///////////////////////////////////////////////////////////////////////////////////////////////
102
103   /**
104    * Write error.
105    * @param msg message or tokens.
106    */
107   static error(...msg) {
108     if (this.level >= Logger.ERROR) {
109       const out = this.serialize(msg);
110       console.error(`ASDCS [ERROR] ${out}`);
111     }
112   }
113
114   // ///////////////////////////////////////////////////////////////////////////////////////////////
115
116   /**
117    * Serialize msg.
118    * @param msg message or tokens.
119    * @returns {string}
120    */
121   static serialize(...msg) {
122     let out = '';
123     msg.forEach((token) => {
124       out = `${out}${token}`;
125     });
126     return out;
127   }
128 }
129
130 // /////////////////////////////////////////////////////////////////////////////////////////////////
131
132 Logger.OFF = 0;
133 Logger.ERROR = 1;
134 Logger.WARN = 2;
135 Logger.INFO = 3;
136 Logger.DEBUG = 4;
137 Logger.level = Logger.OFF;