-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathti.timelogger.js
119 lines (103 loc) · 2.39 KB
/
ti.timelogger.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
/**
* TimeLogger
* Enhanced logger, with information about the current timestamp and the time passed before the previous event.
*
* @author Davide Cassenti <davide.cassenti@gmail.com>
*
*/
(function() {
var loggers = [];
/**
* TimeLogger
* Create a TimeLogger object
*
* @param id The ID of the logger (it will be printed out as well)
*/
function TimeLogger(id) {
if (!id)
id = "default";
if (loggers[id])
return loggers[id];
var logger = {};
logger.timestamp = [];
// keep track of the different timestamps
/**
* log
* Log a message
*
* @param type Can be either info, warn or error
* @param message The message to log
*/
logger.log = function(type, message) {
var now = (new Date()).getTime();
var diff = 0;
if (this.timestamp[type] && this.timestamp[type] > 0) {
diff = now - this.timestamp[type];
}
this.timestamp[type] = now;
// get line number (Android only, iOS does not support the stack apparently)
var lineNumber = "";
try {
throw new Error('');
} catch(e) {
if (e.stack) {
var stack = e.stack.split("\n");
if (stack[3]) {
var e = stack[3];
var start = e.indexOf('(');
var end = e.indexOf(')');
if (start >= 0 && end >= 0 && end > start)
lineNumber = e.substring(start, end + 1);
}
} else if (e.backtrace) {
Ti.API.info(e.backtrace);
var stack = e.backtrace.split("\n");
var e = stack[stack.length-1];
var start = e.lastIndexOf('/');
if (start >= 0) {
lineNumber = e.substring(start+1);
}
}
}
message = "[TimeLogger][" + id + "][" + lineNumber + "][" + now + "][" + diff + "ms]" + ": " + message;
switch (type) {
case "info":
Ti.API.info(message);
break;
case "warn":
Ti.API.warn(message);
break;
case "error":
Ti.API.error(message);
break;
}
}
/**
* info
*
* @param message The information message to print out
*/
logger.info = function(message) {
this.log('info', message);
}
/**
* warn
*
* @param message The warning message to print out
*/
logger.warn = function(message) {
this.log('warn', message);
}
/**
* error
*
* @param message The error message to print out
*/
logger.error = function(message) {
this.log('error', message);
}
loggers[id] = logger;
return logger;
}
module.exports = TimeLogger;
})();