043eb31d5eebc751d96a839429bdfc08173a6745
[platform/upstream/iotjs.git] / tools / common_js / logger.js
1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 var fs = require('fs');
17
18 function Logger(path) {
19   this.text_colors = {
20     red: "\033[1;31m",
21     yellow: "\033[1;33m",
22     green: "\033[1;32m",
23     blue: "\033[1;34m",
24     empty: "\033[0m",
25   };
26   this.status = {
27     pass: "pass",
28     skip: "skip",
29     fail: "fail",
30     timeout: "timeout",
31     summary: "summary"
32   }
33   this.path = path;
34
35   return this;
36 }
37
38 Logger.prototype.message = function (msg, status) {
39   if (this.path) {
40     // FIXME : After fs.appendFile is implemented, it should be replaced.
41     var data = fs.readFileSync(this.path);
42     var newData = data + msg + "\n";
43     fs.writeFileSync(this.path, new Buffer(newData));
44   }
45   if (status == this.status.pass) {
46     console.log(this.text_colors.green + msg + this.text_colors.empty);
47   } else if (status == this.status.skip) {
48     console.log(this.text_colors.yellow + msg + this.text_colors.empty);
49   } else if (status == this.status.fail || status == this.status.timeout){
50     console.log(this.text_colors.red + msg + this.text_colors.empty);
51   } else if (status == this.status.summary){
52     console.log(this.text_colors.blue + msg + this.text_colors.empty);
53   } else {
54     console.log(msg);
55   }
56 }
57
58 module.exports.Logger = Logger;