Skip to content

Commit abb29da

Browse files
committed
Improved logger to keep caller informations like line numbers.
1 parent af404a2 commit abb29da

File tree

4 files changed

+78
-82
lines changed

4 files changed

+78
-82
lines changed

src/mode/modder.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ cwc.mode.Modder.prototype.setMode = function(mode) {
130130
sidebarInstance.clear();
131131
}
132132

133+
// Reset Render instance
134+
let rendererInstance = this.helper.getInstance('renderer');
135+
if (rendererInstance) {
136+
rendererInstance.setServerMode(false);
137+
}
138+
133139
this.log_.info('Initialize mode and decorate UI for', mode, '…');
134140
this.mode = mode;
135141
this.modder = modeConfig.getMod(this.helper);

src/protocol/low-level/tcp/http_server.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ cwc.protocol.tcp.HTTPServer.prototype.addCustomHandler = function(path,
130130
*/
131131
cwc.protocol.tcp.HTTPServer.prototype.addFile = function(path, content) {
132132
if (!content) {
133-
this.log_.warn('Empty file', path);
134-
return;
133+
this.log_.warn('Empty content', path);
135134
}
136135
if (!path.startsWith('/')) {
137136
path = '/' + path;

src/server/server.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,9 @@ cwc.server.Server.prototype.setPreview = function(content) {
128128
if (!filename.endsWith('.html') && !filename.endsWith('.htm')) {
129129
filename = filename + '.html';
130130
}
131-
this.previewFile = '/' + filename
132-
.replace('_cwc', '') || '/preview.html';
131+
this.previewFile = '/preview/' + filename.replace('_cwc', '');
133132
} else {
134-
this.previewFile = '/preview.html';
133+
this.previewFile = '/preview/';
135134
}
136135
}
137136
this.httpServer.addFile(this.previewFile, content);

src/utils/logger.js

Lines changed: 69 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ cwc.utils.LogLevel = {
5555
* @param {string=} name
5656
* @param {number=} logLevel
5757
* @constructor
58-
* @final
58+
* @export
5959
*/
6060
cwc.utils.Logger = function(name = 'Logger',
6161
logLevel = cwc.utils.LogLevel.NOTICE) {
6262
/** @type {!string} */
6363
this.name = name;
6464

65-
/** @type {!Array} */
66-
this.displayName = this.name ? ['%c' + this.name, 'font-weight: bold;']: [];
65+
/** @type {!string} */
66+
this.displayName = this.name ? '%c' + this.name : '';
6767

6868
/** @type {!number} */
6969
this.logLevel = typeof cwc.config !== 'undefined' ?
@@ -72,107 +72,99 @@ cwc.utils.Logger = function(name = 'Logger',
7272
/** @type {!boolean} */
7373
this.enabled_ = ENABLE_LOGGING;
7474

75-
/**
76-
* Disable logging styles for specific environments like Mocha, Jasmine, ...
77-
*/
75+
/** @type {!Function} */
76+
this.trace = function() {};
77+
78+
/** @type {!Function} */
79+
this.debug = function() {};
80+
81+
/** @type {!Function} */
82+
this.info = function() {};
83+
84+
/** @type {!Function} */
85+
this.notice = function() {};
86+
87+
/** @type {!Function} */
88+
this.warn = function() {};
89+
90+
/** @type {!Function} */
91+
this.error = function() {};
92+
93+
/** @type {!Function} */
94+
this.critical = function() {};
95+
96+
/** @type {!Function} */
97+
this.alert = function() {};
98+
99+
// Disable logging styles for specific environments like Mocha, Jasmine, ...
78100
if ((window['mocha'] || window['jasmine'] || window['__karma__']) &&
79101
this.name) {
80-
this.displayName = ['[' + this.name + ']'];
102+
this.displayName = '[' + this.name + ']';
81103
}
82-
};
83104

84-
85-
/**
86-
* Trace logger.
87-
* @param {...*} args
88-
*/
89-
cwc.utils.Logger.prototype.trace = function(...args) {
90-
if (this.enabled_ && this.logLevel >= cwc.utils.LogLevel.TRACE) {
91-
Function.prototype.apply.apply(
92-
console.log, [console, this.displayName.concat(args)]);
93-
}
105+
this.setLogLevel(this.logLevel);
94106
};
95107

96108

97109
/**
98-
* Debug logger.
99-
* @param {...*} args
110+
* @param {!cwc.utils.LogLevel} logLevel
111+
* @export
100112
*/
101-
cwc.utils.Logger.prototype.debug = function(...args) {
102-
if (this.enabled_ && this.logLevel >= cwc.utils.LogLevel.DEBUG) {
103-
Function.prototype.apply.apply(
104-
console.log, [console, this.displayName.concat(args)]);
105-
}
106-
};
113+
cwc.utils.Logger.prototype.setLogLevel = function(logLevel) {
114+
this.logLevel = logLevel;
107115

116+
// Trace logger
117+
this.setLogger_('trace', cwc.utils.LogLevel.TRACE, console.log);
108118

109-
/**
110-
* Info logger.
111-
* @param {...*} args
112-
*/
113-
cwc.utils.Logger.prototype.info = function(...args) {
114-
if (this.enabled_ && this.logLevel >= cwc.utils.LogLevel.INFO) {
115-
Function.prototype.apply.apply(
116-
console.log, [console, this.displayName.concat(args)]);
117-
}
118-
};
119+
// Debug logger
120+
this.setLogger_('debug', cwc.utils.LogLevel.DEBUG, console.log);
119121

122+
// Info logger
123+
this.setLogger_('info', cwc.utils.LogLevel.INFO, console.log);
120124

121-
/**
122-
* Notice logger.
123-
* @param {...*} args
124-
*/
125-
cwc.utils.Logger.prototype.notice = function(...args) {
126-
if (this.enabled_ && this.logLevel >= cwc.utils.LogLevel.NOTICE) {
127-
Function.prototype.apply.apply(
128-
console.log, [console, this.displayName.concat(args)]);
129-
}
130-
};
125+
// Notice logger
126+
this.setLogger_('notice', cwc.utils.LogLevel.NOTICE, console.log);
131127

128+
// Warn logger
129+
this.setLogger_('warn', cwc.utils.LogLevel.WARN, console.warn);
132130

133-
/**
134-
* Warn logger.
135-
* @param {...*} args
136-
*/
137-
cwc.utils.Logger.prototype.warn = function(...args) {
138-
if (this.enabled_ && this.logLevel >= cwc.utils.LogLevel.WARNING) {
139-
Function.prototype.apply.apply(
140-
console.warn, [console, this.displayName.concat(args)]);
141-
}
142-
};
131+
// Error logger
132+
this.setLogger_('error', cwc.utils.LogLevel.ERROR, console.error);
143133

134+
// Critical logger
135+
this.setLogger_('critical', cwc.utils.LogLevel.CRITICAL, console.error);
144136

145-
/**
146-
* Error logger.
147-
* @param {...*} args
148-
*/
149-
cwc.utils.Logger.prototype.error = function(...args) {
150-
if (this.logLevel >= cwc.utils.LogLevel.ERROR) {
151-
Function.prototype.apply.apply(
152-
console.error, [console, this.displayName.concat(args)]);
153-
}
137+
// Critical logger
138+
this.setLogger_('alert', cwc.utils.LogLevel.ALERT, console.error);
154139
};
155140

156141

157142
/**
158-
* Critical logger.
159-
* @param {...*} args
143+
* @param {!string} name
144+
* @param {!cwc.utils.LogLevel} logLevel
145+
* @param {!Function} logger
146+
* @private
160147
*/
161-
cwc.utils.Logger.prototype.critical = function(...args) {
162-
if (this.logLevel >= cwc.utils.LogLevel.CRITICAL) {
163-
Function.prototype.apply.apply(
164-
console.error, [console, this.displayName.concat(args)]);
148+
cwc.utils.Logger.prototype.setLogger_ = function(name, logLevel, logger) {
149+
// Enable logger for all errors and higher by default.
150+
if ((this.enabled_ || this.logLevel <= 3) && this.logLevel >= logLevel) {
151+
this[name] = this.log_(logger);
152+
} else {
153+
this[name] = function() {};
165154
}
166155
};
167156

168157

169158
/**
170-
* Alert logger.
171-
* @param {...*} args
159+
* @param {!Function} logger
160+
* @return {Function}
161+
* @private
172162
*/
173-
cwc.utils.Logger.prototype.alert = function(...args) {
174-
if (this.logLevel >= cwc.utils.LogLevel.ALERT) {
175-
Function.prototype.apply.apply(
176-
console.error, [console, this.displayName.concat(args)]);
163+
cwc.utils.Logger.prototype.log_ = function(logger) {
164+
if (this.displayName.includes('%c')) {
165+
return Function.prototype.bind.call(logger, console, this.displayName,
166+
'font-weight: bold;');
167+
} else {
168+
return Function.prototype.bind.call(logger, console, this.displayName);
177169
}
178170
};

0 commit comments

Comments
 (0)