forked from purdrew/incubator-cordova-js
/
console.js
136 lines (116 loc) · 4.02 KB
/
console.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
var exec = require('cordova/exec');
/**
* This class provides access to the debugging console.
* @constructor
*/
var DebugConsole = function() {
this.logLevel = DebugConsole.INFO_LEVEL;
};
// from most verbose, to least verbose
DebugConsole.ALL_LEVEL = 1; // same as first level
DebugConsole.DEBUG_LEVEL = 1;
DebugConsole.LOG_LEVEL = 1;
DebugConsole.INFO_LEVEL = 1;
DebugConsole.WARN_LEVEL = 2;
DebugConsole.ERROR_LEVEL = 4;
DebugConsole.NONE_LEVEL = 8;
DebugConsole.prototype.setLevel = function(level) {
this.logLevel = level;
};
/**
* create a nice string for an object
*/
function stringify(message) {
try {
if (typeof message === "object" && JSON && JSON.stringify) {
try {
return JSON.stringify(message);
}
catch (e) {
return "error JSON.stringify()ing argument: " + e;
}
} else {
return message.toString();
}
} catch (e) {
return e.toString();
}
};
/**
* remember the original console and it's methods
*/
var origConsole = window.console || {}
var origConsole_log = origConsole.log || function(){}
var origConsole_warn = origConsole.warn || function(){}
var origConsole_error = origConsole.error || function(){}
/**
* Print a normal debug message to the console
* @param {Object|String} message Message or object to print to the console
*/
DebugConsole.prototype.debug = function(message) {
origConsole_log.apply(origConsole, arguments)
if (this.logLevel <= DebugConsole.DEBUG_LEVEL) {
exec(null, null, 'Debug Console', 'log', [ stringify(message), { logLevel: 'DEBUG' } ]);
}
};
/**
* Print a normal log message to the console
* @param {Object|String} message Message or object to print to the console
*/
DebugConsole.prototype.log = function(message) {
origConsole_log.apply(origConsole, arguments)
if (this.logLevel <= DebugConsole.LOG_LEVEL) {
exec(null, null, 'Debug Console', 'log', [ stringify(message), { logLevel: 'LOG' } ]);
}
};
/**
* Print a warning message to the console
* @param {Object|String} message Message or object to print to the console
*/
DebugConsole.prototype.info = function(message) {
origConsole_warn.apply(origConsole, arguments)
if (this.logLevel <= DebugConsole.INFO_LEVEL) {
exec(null, null, 'Debug Console', 'log', [ stringify(message), { logLevel: 'INFO' } ]);
}
};
/**
* Print a normal warn message to the console
* @param {Object|String} message Message or object to print to the console
*/
DebugConsole.prototype.warn = function(message) {
origConsole_log.apply(origConsole, arguments)
if (this.logLevel <= DebugConsole.WARN_LEVEL) {
exec(null, null, 'Debug Console', 'log', [ stringify(message), { logLevel: 'WARN' } ]);
}
};
/**
* Print an error message to the console
* @param {Object|String} message Message or object to print to the console
*/
DebugConsole.prototype.error = function(message) {
origConsole_error.apply(origConsole, arguments)
if (this.logLevel <= DebugConsole.ERROR_LEVEL) {
exec(null, null, 'Debug Console', 'log', [ stringify(message), { logLevel: 'ERROR' } ]);
}
};
module.exports = new DebugConsole();