Skip to content

Commit e14b694

Browse files
committed
Fix double-formatting client-side
This is an alternative attempt (after #22) at fixing the "double formatting" situation brought by canalplus/rx-player#1469 that may be made much more frequent by canalplus/rx-player#1625. This solution fixes it client-side instead, which could be seen as arguably less ugly. The idea is to rely on the fact that the RxPlayer does full formatting by calling log functions with at least three arguments: 1. The timestamp in a string format with always numbers after a comma 2. A "namespace" (e.g. "[warn]") 3-n. The log message's arguments By considering this, we can very easily detect client-side a probable case of full formatting.
1 parent c405d9a commit e14b694

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

client/src/client.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ function init(currentScriptSrc, playerClass) {
6868
logQueue.push(log);
6969
};
7070

71-
function formatAndSendLog(namespace, log) {
71+
function sendNetworkLog(namespace, log) {
7272
const time = performance.now().toFixed(2);
73-
const logText = `${time} [${namespace}] ${log}`;
74-
sendLog(logText);
73+
const logText = `${time} [Network] ${log}`;
74+
return sendLog(logText);
7575
}
7676

7777
function processArg(arg) {
@@ -117,9 +117,22 @@ function init(currentScriptSrc, playerClass) {
117117

118118
const spyRemovers = ["log", "error", "info", "warn", "debug"].map((meth) => {
119119
const oldConsoleFn = console[meth];
120+
const namespace = `[${meth}]`;
120121
console[meth] = function (...args) {
121122
const argStr = args.map(processArg).join(" ");
122-
formatAndSendLog(meth, argStr);
123+
124+
// The RxPlayer might already have set the timestamp + namespace format
125+
if (
126+
args.length >= 3 &&
127+
args[1] === namespace &&
128+
/^\d+\.\d+$/.test(args[0])
129+
) {
130+
sendLog(argStr);
131+
} else {
132+
// Else, add it now
133+
const time = performance.now().toFixed(2);
134+
sendLog(`${time} ${namespace} ${argStr}`);
135+
}
123136
return oldConsoleFn.apply(this, args);
124137
};
125138
return function () {
@@ -136,20 +149,19 @@ function init(currentScriptSrc, playerClass) {
136149
return originalXhrOpen.apply(this, arguments);
137150
}
138151
this.addEventListener("load", function () {
139-
formatAndSendLog(
140-
"Network",
152+
sendNetworkLog(
141153
`Loaded ${method} XHR from: ${url} ` + `(status: ${this.status})`,
142154
);
143155
});
144156
this.addEventListener("error", function () {
145-
formatAndSendLog("Network", `Errored ${method} XHR from: ${url}`);
157+
sendNetworkLog(`Errored ${method} XHR from: ${url}`);
146158
});
147159
this.abort = function () {
148-
formatAndSendLog("Network", `Aborted ${method} XHR from: ${url}`);
160+
sendNetworkLog(`Aborted ${method} XHR from: ${url}`);
149161
return XMLHttpRequest.prototype.abort.apply(this, arguments);
150162
};
151163
this.send = function () {
152-
formatAndSendLog("Network", `Sending ${method} XHR to: ${url}`);
164+
sendNetworkLog(`Sending ${method} XHR to: ${url}`);
153165
return XMLHttpRequest.prototype.send.apply(this, arguments);
154166
};
155167
return originalXhrOpen.apply(this, arguments);
@@ -187,21 +199,17 @@ function init(currentScriptSrc, playerClass) {
187199
} else {
188200
method = "GET";
189201
}
190-
formatAndSendLog("Network", `Sending ${method} fetch to: ${url}`);
202+
sendNetworkLog(`Sending ${method} fetch to: ${url}`);
191203
const realFetch = originalFetch.apply(this, arguments);
192204
return realFetch.then(
193205
(res) => {
194-
formatAndSendLog(
195-
"Network",
206+
sendNetworkLog(
196207
`Loaded ${method} fetch from: ${url} ` + `(status: ${res.status})`,
197208
);
198209
return res;
199210
},
200211
(err) => {
201-
formatAndSendLog(
202-
"Network",
203-
`Errored/Aborted ${method} fetch from: ${url}`,
204-
);
212+
sendNetworkLog(`Errored/Aborted ${method} fetch from: ${url}`);
205213
throw err;
206214
},
207215
);

0 commit comments

Comments
 (0)