Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: generic logging #100

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/api_productions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import dotenv from 'dotenv';
import { v4 as uuidv4 } from 'uuid';
import { ConnectionQueue } from './connection_queue';
import { CoreFunctions } from './api_productions_core_functions';
import { Log } from './log';
dotenv.config();

const productionManager = new ProductionManager();
Expand Down Expand Up @@ -81,6 +82,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
reply.code(400).send({ message: 'Failed to create production' });
}
} catch (err) {
Log().error(err);
reply
.code(500)
.send('Exception thrown when trying to create production: ' + err);
Expand Down Expand Up @@ -111,6 +113,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}))
);
} catch (err) {
Log().error(err);
reply
.code(500)
.send('Exception thrown when trying to get productions: ' + err);
Expand Down Expand Up @@ -146,6 +149,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
};
reply.code(200).send(productionResponse);
} catch (err) {
Log().error(err);
reply
.code(500)
.send('Exception thrown when trying to get productions: ' + err);
Expand Down Expand Up @@ -219,6 +223,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
reply.code(200).send(allLinesResponse);
}
} catch (err) {
Log().error(err);
reply
.code(500)
.send('Unhandled exception thrown when trying to add line: ' + err);
Expand Down Expand Up @@ -264,6 +269,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
reply.code(200).send(lineResponse);
}
} catch (err) {
Log().error(err);
reply
.code(500)
.send('Exception thrown when trying to get line: ' + err);
Expand Down Expand Up @@ -329,6 +335,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
}
} catch (err) {
Log().error(err);
reply
.code(500)
.send('Exception thrown when trying to get line: ' + err);
Expand Down Expand Up @@ -377,6 +384,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
}
} catch (err) {
Log().error(err);
reply
.code(500)
.send('Exception thrown when trying to get line: ' + err);
Expand Down Expand Up @@ -453,6 +461,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
});
}
} catch (err) {
Log().error(err);
reply
.code(500)
.send('Exception thrown when trying to create endpoint: ' + err);
Expand Down Expand Up @@ -517,6 +526,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
);
reply.code(204);
} catch (err) {
Log().error(err);
reply
.code(500)
.send('Exception thrown when trying to configure endpoint: ' + err);
Expand Down Expand Up @@ -550,6 +560,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
reply.code(200).send(`Deleted production ${productionId}`);
} catch (err) {
Log().error(err);
reply
.code(500)
.send('Exception thrown when trying to delete production: ' + err);
Expand Down Expand Up @@ -580,6 +591,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
reply.code(200).send(`Deleted connection ${sessionId}`);
} catch (err) {
Log().error(err);
reply
.code(500)
.send('Exception thrown when trying to delete connection: ' + err);
Expand Down Expand Up @@ -617,6 +629,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
await waitForChange;
reply.code(200).send(participants);
} catch (err) {
Log().error(err);
reply
.code(500)
.send(
Expand Down
5 changes: 3 additions & 2 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SfuEndpointDescription
} from './sfu/interface';
import { MediaStreamsInfo } from './media_streams_info';
import { Log } from './log';

export class Connection {
private resourceId: string;
Expand Down Expand Up @@ -39,11 +40,11 @@ export class Connection {
}

protected log(...args: string[] | Connection[]) {
console.log(`[connection ${this.connectionId}]`, ...args);
Log().info(`[connection ${this.connectionId}]`, ...args);
}

protected error(...args: string[] | Connection[]) {
console.error(`[connection ${this.connectionId}]`, ...args);
Log().error(`[connection ${this.connectionId}]`, ...args);
}

createOffer(): SessionDescription {
Expand Down
105 changes: 105 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import util from 'util';

let logger_: Logger | undefined;

type LevelCode = 'info' | 'error' | 'warn' | 'debug' | 'fatal';
type LevelItem = {
text: string;
method: LevelCode;
};
const LEVELS: { [k in LevelCode]: LevelItem } = {
info: { text: 'info', method: 'info' },
error: { text: 'error', method: 'error' },
warn: { text: 'warn', method: 'info' },
debug: { text: 'debug', method: 'info' },
fatal: { text: 'fatal', method: 'error' }
};

function log(method: LevelCode) {
switch (method) {
case 'info':
return console.info;
case 'error':
return console.error;
default:
throw new Error(`Invalid log method ${method}`);
}
}

function logfmt(levelCode: LevelCode, ...args: any[]) {
const level = LEVELS[levelCode];
if (levelCode == 'debug') {
args = args.map((arg) => {
if (typeof arg == 'object') {
return util.inspect(arg, { depth: null });
}
return arg;
});
log(level.method)(...args);
} else {
const msg = util.format(...args);
log(level.method)(msg);
}
}

export class Logger {
private debugMode = false;

constructor() {
return this;
}

info(...args: any[]) {
logfmt('info', ...args);
return this;
}

warn(...args: any[]) {
logfmt('warn', ...args);
return this;
}

error(...args: any[]) {
logfmt('error', ...args);
return this;
}

debug(...args: any[]) {
if (this.debugMode) logfmt('debug', ...args);
return this;
}

fatal(...args: any[]) {
logfmt('fatal', ...args);
return this;
}

get level(): 'debug' | 'info' {
return this.debugMode ? 'debug' : 'info';
}

set level(value: 'debug' | 'info') {
if (value == 'debug') {
this.debugMode = true;
} else if (value == 'info') {
this.debugMode = false;
} else {
this.warn('level', value, 'not supported');
}
}
}

export function Log(): Logger {
if (logger_) {
return logger_;
}

logger_ = new Logger();
logger_.level = 'info';

if (process.env.DEBUG) {
logger_.level = 'debug';
}

return logger_;
}
3 changes: 2 additions & 1 deletion src/production_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from './models';
import { assert } from './utils';
import dbManager from './db_manager';
import { Log } from './log';

const SESSION_INACTIVE_THRESHOLD = 60_000;
const SESSION_EXPIRED_THRESHOLD = 120_000;
Expand Down Expand Up @@ -173,7 +174,7 @@ export class ProductionManager extends EventEmitter {
isActive: true,
isExpired: false
};
console.log(`Created user session: "${name}": ${sessionId}`);
Log().info(`Created user session: "${name}": ${sessionId}`);
this.emit('users:change');
}

Expand Down
6 changes: 5 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import api from './api';
import { checkUserStatus } from './api_productions';
import { Log } from './log';

const SMB_ADDRESS: string = process.env.SMB_ADDRESS ?? 'http://localhost:8080';

Expand All @@ -26,6 +27,9 @@ const PORT = process.env.PORT ? Number(process.env.PORT) : 8000;
if (err) {
throw err;
}
console.log(`Server listening on ${address}`);
Log().info(`Manager listening on ${address}`);
Log().info(
`Media Bridge at ${SMB_ADDRESS} (${ENDPOINT_IDLE_TIMEOUT_S}s idle timeout)`
);
});
})();
9 changes: 5 additions & 4 deletions src/smb.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Log } from './log';
import { SmbEndpointDescription, DetailedConference } from './models';

interface AllocateConferenceResponse {
Expand Down Expand Up @@ -69,10 +70,10 @@ export class SmbProtocol {
if (idleTimeout) {
request['idleTimeout'] = idleTimeout;
}
console.log(request);
Log().debug(request);

const url = smbUrl + conferenceId + '/' + endpointId;
console.log(url);
Log().debug(url);
const response = await fetch(url, {
method: 'POST',
headers: {
Expand Down Expand Up @@ -113,10 +114,10 @@ export class SmbProtocol {
},
body: JSON.stringify(request)
});
console.log(request);
Log().debug(request);

if (!response.ok) {
console.log(JSON.stringify(request));
Log().debug(JSON.stringify(request));

const contentType = response.headers.get('content-type');

Expand Down
Loading