standardize logging

This commit is contained in:
emerson 2023-07-03 16:15:51 +00:00
parent c5740a45fc
commit 2e7582e6c6
5 changed files with 67 additions and 71 deletions

View file

@ -66,7 +66,7 @@ export class Channel {
}).catch(e => { }).catch(e => {
const errcode = e.response?.data?.errcode; const errcode = e.response?.data?.errcode;
if (errcode !== "M_NOT_FOUND") if (errcode !== "M_NOT_FOUND")
console.log(e); this.server.doLog(e);
this.delSyncLock("m.room.create") this.delSyncLock("m.room.create")
}) })
this.addSyncLock("m.room.canonical_alias"); this.addSyncLock("m.room.canonical_alias");
@ -79,7 +79,7 @@ export class Channel {
}).catch(e => { }).catch(e => {
const errcode = e.response?.data?.errcode; const errcode = e.response?.data?.errcode;
if (errcode !== "M_NOT_FOUND") if (errcode !== "M_NOT_FOUND")
console.log(e); this.server.doLog(e);
this.delSyncLock("m.room.canonical_alias") this.delSyncLock("m.room.canonical_alias")
}) })
this.addSyncLock("m.room.topic"); this.addSyncLock("m.room.topic");
@ -94,7 +94,7 @@ export class Channel {
}).catch(e => { }).catch(e => {
const errcode = e.response?.data?.errcode; const errcode = e.response?.data?.errcode;
if (errcode !== "M_NOT_FOUND") if (errcode !== "M_NOT_FOUND")
console.log(this.roomId, e); this.server.doLog(`${this.roomId}: ${e}`);
this.delSyncLock("m.room.topic") this.delSyncLock("m.room.topic")
}) })
this.addSyncLock("m.room.members"); this.addSyncLock("m.room.members");
@ -113,7 +113,7 @@ export class Channel {
}).catch(e => { }).catch(e => {
const errcode = e.response?.data?.errcode; const errcode = e.response?.data?.errcode;
if (errcode !== "M_NOT_FOUND") if (errcode !== "M_NOT_FOUND")
console.log(e); this.server.doLog(e);
this.delSyncLock("m.room.members") this.delSyncLock("m.room.members")
}) })
this.addSyncLock("m.room.power_levels") this.addSyncLock("m.room.power_levels")
@ -132,7 +132,7 @@ export class Channel {
}).catch(e => { }).catch(e => {
const errcode = e.response?.data?.errcode; const errcode = e.response?.data?.errcode;
if (errcode !== "M_NOT_FOUND") if (errcode !== "M_NOT_FOUND")
console.log(e); this.server.doLog(e);
this.delSyncLock("m.room.power_levels") this.delSyncLock("m.room.power_levels")
}) })
} }

View file

@ -167,7 +167,7 @@ export class Client {
break; break;
default: default:
this.sendMessage(this.server.name, "421", [this.user.nick, message.command, 'Unknown command'], message.tags); this.sendMessage(this.server.name, "421", [this.user.nick, message.command, 'Unknown command'], message.tags);
console.log(`unknown command ${message.command}`); this.server.doLog(`unknown command ${message.command}`);
break; break;
} }
} }
@ -206,14 +206,14 @@ export class Client {
} else { } else {
this.sendMessage(this.server.name, "306", [this.user.nick, "You have been marked as being away"], message.tags); this.sendMessage(this.server.name, "306", [this.user.nick, "You have been marked as being away"], message.tags);
} }
}).catch(function (error) { }).catch((error) => {
if (error.response) { if (error.response) {
console.log(error.response.data); this.server.doLog(error.response.data);
} else if (error.request) { } else if (error.request) {
console.log(error.request); this.server.doLog(error.request);
} else { } else {
console.log('Error', error.message); this.server.doLog(`Error: ${error.message}`);
console.log(error.config); this.server.doLog(error.config);
} }
}) })
} }
@ -294,14 +294,14 @@ export class Client {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel) this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
} }
} }
}).catch(function (error) { }).catch((error) => {
if (error.response) { if (error.response) {
console.log(error.response.data); this.server.doLog(error.response.data);
} else if (error.request) { } else if (error.request) {
console.log(error.request); this.server.doLog(error.request);
} else { } else {
console.log('Error', error.message); this.server.doLog(`Error: ${error.message}`);
console.log(error.config); this.server.doLog(error.config);
} }
}) })
} }
@ -331,14 +331,14 @@ export class Client {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel) this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
} }
} }
}).catch(function (error) { }).catch((error) => {
if (error.response) { if (error.response) {
console.log(error.response.data); this.server.doLog(error.response.data);
} else if (error.request) { } else if (error.request) {
console.log(error.request); this.server.doLog(error.request);
} else { } else {
console.log('Error', error.message); this.server.doLog(`Error: ${error.message}`);
console.log(error.config); this.server.doLog(error.config);
} }
}) })
} }
@ -373,14 +373,14 @@ export class Client {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel) this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
} }
} }
}).catch(function (error) { }).catch((error) => {
if (error.response) { if (error.response) {
console.log(error.response.data); this.server.doLog(error.response.data);
} else if (error.request) { } else if (error.request) {
console.log(error.request); this.server.doLog(error.request);
} else { } else {
console.log('Error', error.message); this.server.doLog(`Error: ${error.message}`);
console.log(error.config); this.server.doLog(error.config);
} }
}) })
} }
@ -420,10 +420,8 @@ export class Client {
return; return;
} }
for (const msg of batch.messages) { for (const msg of batch.messages) {
console.log(firstMessage.params[1], msg.params[1]);
const separator = (msg.tags.has('draft/multiline-concat') || firstMessage.params[1] === msg.params[1]) ? '' : '\n'; const separator = (msg.tags.has('draft/multiline-concat') || firstMessage.params[1] === msg.params[1]) ? '' : '\n';
fullMessage = `${fullMessage}${separator}${msg.params[1]}`; fullMessage = `${fullMessage}${separator}${msg.params[1]}`;
console.log(fullMessage);
} }
const highlightFilteredMsg = fullMessage.split(" ").map(w => { const highlightFilteredMsg = fullMessage.split(" ").map(w => {
if (!w.startsWith('@')) return w; if (!w.startsWith('@')) return w;
@ -456,14 +454,14 @@ export class Client {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel) this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
} }
} }
}).catch(function (error) { }).catch((error) => {
if (error.response) { if (error.response) {
console.log(error.response.data); this.server.doLog(error.response.data);
} else if (error.request) { } else if (error.request) {
console.log(error.request); this.server.doLog(error.request);
} else { } else {
console.log('Error', error.message); this.server.doLog(`Error: ${error.message}`);
console.log(error.config); this.server.doLog(error.config);
} }
}); });
} }
@ -517,14 +515,14 @@ export class Client {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel) this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
} }
} }
}).catch(function (error) { }).catch((error) => {
if (error.response) { if (error.response) {
console.log(error.response.data); this.server.doLog(error.response.data);
} else if (error.request) { } else if (error.request) {
console.log(error.request); this.server.doLog(error.request);
} else { } else {
console.log('Error', error.message); this.server.doLog(`Error: ${error.message}`);
console.log(error.config); this.server.doLog(error.config);
} }
}); });
} }
@ -597,14 +595,14 @@ export class Client {
//@ts-ignore //@ts-ignore
this.sendMessage(this.server.name, "NOTICE", [this.user.nick, JSON.stringify(response.data)], message.tags); this.sendMessage(this.server.name, "NOTICE", [this.user.nick, JSON.stringify(response.data)], message.tags);
} }
}).catch(function (error) { }).catch((error) => {
if (error.response) { if (error.response) {
console.log(error.response.data); this.server.doLog(error.response.data);
} else if (error.request) { } else if (error.request) {
console.log(error.request); this.server.doLog(error.request);
} else { } else {
console.log('Error', error.message); this.server.doLog(`Error: ${error.message}`);
console.log(error.config); this.server.doLog(error.config);
} }
}) })
} }
@ -633,14 +631,14 @@ export class Client {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel) this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
} }
} }
}).catch(function (error) { }).catch((error) => {
if (error.response) { if (error.response) {
console.log(error.response.data); this.server.doLog(error.response.data);
} else if (error.request) { } else if (error.request) {
console.log(error.request); this.server.doLog(error.request);
} else { } else {
console.log('Error', error.message); this.server.doLog(`Error: ${error.message}`);
console.log(error.config); this.server.doLog(error.config);
} }
}); });
} }
@ -653,14 +651,14 @@ export class Client {
} }
this.apiCall.put(`/rooms/${targetChannel.roomId}/typing/${this.user.mxid}`, data).then(r => { this.apiCall.put(`/rooms/${targetChannel.roomId}/typing/${this.user.mxid}`, data).then(r => {
// No response body for successful request // No response body for successful request
}).catch(function (error) { }).catch((error) => {
if (error.response) { if (error.response) {
console.log(error.response.data); this.server.doLog(error.response.data);
} else if (error.request) { } else if (error.request) {
console.log(error.request); this.server.doLog(error.request);
} else { } else {
console.log('Error', error.message); this.server.doLog(`Error: ${error.message}`);
console.log(error.config); this.server.doLog(error.config);
} }
}); });
} }
@ -712,14 +710,14 @@ export class Client {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel) this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
} }
} }
}).catch(function (error) { }).catch((error) => {
if (error.response) { if (error.response) {
console.log(error.response.data); this.server.doLog(error.response.data);
} else if (error.request) { } else if (error.request) {
console.log(error.request); this.server.doLog(error.request);
} else { } else {
console.log('Error', error.message); this.server.doLog(`Error: ${error.message}`);
console.log(error.config); this.server.doLog(error.config);
} }
}) })
} }

View file

@ -11,7 +11,6 @@ export class IRCClient extends Client {
} }
writeMessage(message: string) { writeMessage(message: string) {
//console.log(`SENT: ${msgToSend}`);
this.socket.write(`${message}\r\n`); this.socket.write(`${message}\r\n`);
} }

View file

@ -75,7 +75,6 @@ function addToTags(key: string): boolean {
} }
export function parseIRCMessage(rawLine: string) { export function parseIRCMessage(rawLine: string) {
//console.log(`RAW: ${rawLine}`);
let restOfMessage = rawLine; let restOfMessage = rawLine;
let parsedTags: Map<string, string> = new Map(); let parsedTags: Map<string, string> = new Map();
let prefix = ''; let prefix = '';
@ -119,6 +118,6 @@ export function parseIRCMessage(rawLine: string) {
if (lastParam !== '') { if (lastParam !== '') {
params.push(lastParam); params.push(lastParam);
} }
//console.log(parsedTags, prefix, command, params);
return new IRCMessage(parsedTags, prefix, command, params); return new IRCMessage(parsedTags, prefix, command, params);
} }

View file

@ -65,7 +65,7 @@ export class IRCServer {
this.getDirectMessages(); this.getDirectMessages();
this.doSync(); this.doSync();
}).catch(e => { }).catch(e => {
console.log(e); this.doLog(e);
}) })
} }
@ -158,7 +158,7 @@ export class IRCServer {
}) })
this.listener.listen(this.config["port"], () => { this.listener.listen(this.config["port"], () => {
console.log(`Listening on port ${this.config["port"]}`); this.doLog(`Listening on port ${this.config["port"]}`);
}) })
} }
} }
@ -308,8 +308,8 @@ export class IRCServer {
case 'org.matrix.msc3381.poll.start': case 'org.matrix.msc3381.poll.start':
break; break;
default: default:
console.log(`${targetChannel.name}:`); this.doLog(`${targetChannel.name}:`);
console.log(nextEvent); this.doLog(nextEvent);
break; break;
} }
} }
@ -398,7 +398,7 @@ export class IRCServer {
handleMatrixGuestAccess(event: any, targetChannel: Channel) { handleMatrixGuestAccess(event: any, targetChannel: Channel) {
const rule = event["content"]?.["guest_access"]; const rule = event["content"]?.["guest_access"];
if (!rule) { if (!rule) {
console.log(`Warning: Guest access not found in ${event}`); this.doLog(`Warning: Guest access not found in ${event}`);
return; return;
} }
targetChannel.guestAccess = rule; targetChannel.guestAccess = rule;
@ -407,7 +407,7 @@ export class IRCServer {
handleMatrixHistoryVisibility(event: any, targetChannel: Channel) { handleMatrixHistoryVisibility(event: any, targetChannel: Channel) {
const rule = event["content"]?.["history_visibility"]; const rule = event["content"]?.["history_visibility"];
if (!rule) { if (!rule) {
console.log(`Warning: history visibility not found in ${event}`); this.doLog(`Warning: history visibility not found in ${event}`);
return; return;
} }
targetChannel.historyVisibility = rule; targetChannel.historyVisibility = rule;
@ -416,7 +416,7 @@ export class IRCServer {
handleMatrixJoinRule(event: any, targetChannel: Channel) { handleMatrixJoinRule(event: any, targetChannel: Channel) {
const rule = event["content"]?.["join_rule"]; const rule = event["content"]?.["join_rule"];
if (!rule) { if (!rule) {
console.log(`Warning: join rule not found in ${event}`); this.doLog(`Warning: join rule not found in ${event}`);
return; return;
} }
targetChannel.joinRules = rule; targetChannel.joinRules = rule;
@ -467,7 +467,7 @@ export class IRCServer {
targetChannel.matrixUsers.delete(targetUser.nick) targetChannel.matrixUsers.delete(targetUser.nick)
} }
else { else {
console.log(`Got unknown m.room.member event: ${event}`); this.doLog(`Got unknown m.room.member event: ${event}`);
} }
} }
@ -498,7 +498,7 @@ export class IRCServer {
return; return;
const mxcregex = uri.match(/mxc:\/\/(?<servername>[^\/]+)\/(?<mediaid>.+)/) const mxcregex = uri.match(/mxc:\/\/(?<servername>[^\/]+)\/(?<mediaid>.+)/)
if (!mxcregex || !mxcregex.groups) if (!mxcregex || !mxcregex.groups)
console.log(`Failed to parse MXC URI: ${uri}`); this.doLog(`Failed to parse MXC URI: ${uri}`);
else else
uri = `${this.homeserver}/_matrix/media/v3/download/${mxcregex.groups.servername}/${mxcregex.groups.mediaid}`; uri = `${this.homeserver}/_matrix/media/v3/download/${mxcregex.groups.servername}/${mxcregex.groups.mediaid}`;
messageContent = `\x01ACTION shared ${messageContent}: ${uri}\x01`; messageContent = `\x01ACTION shared ${messageContent}: ${uri}\x01`;
@ -641,7 +641,7 @@ export class IRCServer {
const mxcregex = imgMxc.match(/mxc:\/\/(?<servername>[^\/]+)\/(?<mediaid>.+)/) const mxcregex = imgMxc.match(/mxc:\/\/(?<servername>[^\/]+)\/(?<mediaid>.+)/)
let uri = imgMxc; let uri = imgMxc;
if (!mxcregex || !mxcregex.groups) { if (!mxcregex || !mxcregex.groups) {
console.log(`Failed to parse MXC URI: ${imgMxc}`); this.doLog(`Failed to parse MXC URI: ${imgMxc}`);
} else { } else {
uri = `${this.homeserver}/_matrix/media/v3/download/${mxcregex.groups.servername}/${mxcregex.groups.mediaid}`; uri = `${this.homeserver}/_matrix/media/v3/download/${mxcregex.groups.servername}/${mxcregex.groups.mediaid}`;
} }