Skip to content

Commit

Permalink
Merge branch 'main' into 7-logger-min-level
Browse files Browse the repository at this point in the history
  • Loading branch information
km127pl committed Aug 14, 2023
2 parents 9b449e6 + f481f38 commit 1f7f5ca
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ dist
*.js
*.d.ts
.idea

!src/types/*.d.ts
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Config from "./src/Config.js";
import Server from "./src/Server.js";
import LoginSuccessPacket from "./src/packet/server/LoginSuccessPacket.js";
import Connection from "./src/Connection.js";

const config: Config = await Config.fromFile("config.json");

Expand All @@ -9,8 +10,7 @@ server.start();
server.on("listening", (port) => server.logger.info(`Listening on port ${port}`));

server.on("unknownPacket", (packet, conn) => {
server.logger.warn("Unknown packet, disconnecting", packet.dataBuffer);
conn.disconnect().then();
server.logger.debug("Unknown packet", `{state=${Connection.State[conn.state]}}`, packet.dataBuffer);
});
server.on("packet", (packet, _conn) => {
server.logger.debug(packet.constructor.name, packet.data);
Expand Down
14 changes: 11 additions & 3 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ class Logger {
}

/**
* Print object without any log level
* Print object without any log level to STDOUT
* @param obj Object to print
*/
private send(...obj: any[]): void {
private stdout(...obj: any[]): void {
console.log(...obj);
}

/**
* Print object without any log level to STDERR
* @param obj Object to print
*/
private stderr(...obj: any[]): void {
console.error(...obj);
}

/**
* Format string with log level and prefix
* @param level Log level
Expand All @@ -32,7 +40,7 @@ class Logger {
*/
public log(level: Logger.Level, message: string, ...obj: any[]): void {
if (this.shouldLog(level)) {
this.send(this.format(level, message), ...obj);
this[level === Logger.Level.ERROR ? "stderr" : "stdout"](this.format(level, message), ...obj);
}
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
export type EventMap = {
[key: string]: (...args: any[]) => void
}

/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Andy Wermke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* @see https://github.com/andywer/typed-emitter
*/
export default interface TypedEventEmitter<Events extends EventMap> {
addListener<E extends keyof Events> (event: E, listener: Events[E]): this
on<E extends keyof Events> (event: E, listener: Events[E]): this
Expand All @@ -23,3 +43,6 @@ export default interface TypedEventEmitter<Events extends EventMap> {
getMaxListeners (): number
setMaxListeners (maxListeners: number): this
}
export type EventMap = {
[key: string]: (...args: any[]) => void
}
File renamed without changes.

0 comments on commit 1f7f5ca

Please sign in to comment.