-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.js
280 lines (127 loc) · 3.86 KB
/
Log.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
137
138
139
140
/**
* TOYOSHIMA-HOUSE Library for JavaScript
*/
/**
* Log prototype
*
* This prototype provide common log interfaces.
* @author Takashi Toyoshima <toyoshim@gmail.com>
*
*/
/**
* Log prototype function. This prototype provide three kinds of Log
* mechanisms. User can specify its type by id argument.
* @param id Log type
* undefined: Use native console.log if it's available.
* null: Eliminate all logs.
* <string>: Output as pre element under DOM object which has <string> id.
* @param reverse logging order
* true: Newer logs will be added to tail.
* false: Newer logs will be added to head.
*/
function Log (id, reverse) {
this.lastLevel = "";
this.reverse = reverse;
// Set default log scheme.
this.print = function (object) { /* Do nothing. */ }
if (id == undefined) {
// Try to use native console.
// node.js doesn't have window, but has console.log.
if ((typeof window === "undefined") ||
(window.console != undefined)) {
this.print = function (object) {
console.log(object);
}
}
} else if (id != null) {
// Try to output under specified DOM object.
this.frameDiv = document.getElementById(id);
if (this.frameDiv == undefined)
return;
this.framePre = document.createElement('pre');
this.frameDiv.appendChild(this.framePre);
this.print = function (object) {
if (window.console != undefined) {
console.log(object);
}
var element;
if (object instanceof Object) {
element = document.createElement('pre');
var text = object.toString();
var textNode = document.createTextNode(text);
element.appendChild(textNode);
var title = "";
for (var item in object) {
title += item + ":" + object[item] + "; \n";
}
element.setAttribute('title', title);
} else {
element = document.createTextNode(object + "\n");
}
if (this.reverse && this.framePre.firstChild)
this.framePre.insertBefore(element, this.framePre.firstChild);
else
this.framePre.appendChild(element);
}
}
}
Log.log = new Log();
/**
* Set default log instance.
* @param newLog Log instance to set
*/
Log.setLog = function (newLog) {
Log.log = newLog;
};
/**
* Get default log instance.
* @return default Log instance
*/
Log.getLog = function () {
return Log.log;
};
/**
* Log fatal message.
* @param message fatal message
*/
Log.prototype.fatal = function (message) {
if (this.LastLevel != "FATAL") {
this.LastLevel = "FATAL";
this.print("*FATAL*");
}
this.print(message);
};
/**
* Log error message.
* @param message error message
*/
Log.prototype.error = function (message) {
if (this.LastLevel != "ERROR") {
this.LastLevel = "ERROR";
this.print("*ERROR*");
}
this.print(message);
};
/**
* Log warning message.
* @param message warning message
*/
Log.prototype.warn = function (message) {
if (this.LastLevel != "WARN") {
this.LastLevel = "WARN";
this.print("*WARN*");
}
this.print(message);
};
/**
* Log information message.
* @param message information message
*/
Log.prototype.info = function (message) {
if (this.LastLevel != "INFO") {
this.LastLevel = "INFO";
this.print("*INFO*");
}
this.print(message);
};
//exports.Log = Log;