/
core.js
77 lines (60 loc) · 1.65 KB
/
core.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
'use strict';
/*
* o2.js JavaScript Framework (http://o2js.com - info@o2js.com)
*
* This program is distributed under the terms of the MIT license.
* Please see the LICENSE.md file for details.
*/
var isNode = (typeof module !== 'undefined' && !!module.exports),
isConsoleAvailable = (typeof console !== 'undefined'),
kFs = 'fs',
stream;
function print(label, args) {
var buffer = [],
i, len;
for (i = 0, len = args.length; i < len; i++) {
buffer.push(args[i]);
}
stream.write(label + ': ' + buffer.join(','));
}
if (isNode) {
exports.initialize = function(file) {
// To trick grunt-contrib-jasmine.
var fs = require(kFs);
if (typeof file === 'string') {
stream = fs.createFileStream(
file, {flags: 'a+', encoding: 'utf8'}
);
return;
}
stream = file;
};
exports.log = function() {
if (!stream) {
throw new Error(
'o2.debug: Please call `o2.debug.initialize` first.'
);
}
print('[LOG ]', arguments);
};
exports.warn = function() {
if (!stream) {
throw new Error(
'o2.debug: Please call `o2.debug.initialize` first.'
);
}
print('[WARN]', arguments);
};
} else {
if (isConsoleAvailable) {
exports.log = function() {
console.log.apply(console, arguments);
};
exports.warn = function() {
console.warn.apply(console, arguments);
};
} else {
exports.log = function() {};
exports.warn = function() {};
}
}