diff --git a/lib/browser/io.ts b/lib/browser/io.ts index cf120279..0d768366 100644 --- a/lib/browser/io.ts +++ b/lib/browser/io.ts @@ -19,8 +19,8 @@ * @mergeTarget */ -export { TlsVersion, SocketType, SocketDomain } from "../common/io"; -import { SocketType, SocketDomain } from "../common/io"; +export { LogLevel, TlsVersion, SocketType, SocketDomain } from "../common/io"; +import { setLogLevel, LogLevel, SocketType, SocketDomain } from "../common/io"; /** * @return false, as ALPN is not configurable from the browser @@ -112,3 +112,7 @@ export class SocketOptions { public keep_alive_max_failed_probes = 0) { } } + +export function enable_logging(level: LogLevel) { + setLogLevel(level); +} diff --git a/lib/common/io.ts b/lib/common/io.ts index d2d104ef..e35bde3c 100644 --- a/lib/common/io.ts +++ b/lib/common/io.ts @@ -63,3 +63,86 @@ export enum SocketDomain { /** UNIX domain socket/Windows named pipes */ LOCAL = 2, } + +/** + * The amount of detail that will be logged + * @category Logging + */ +export enum LogLevel { + /** No logging whatsoever. */ + NONE = 0, + /** Only fatals. In practice, this will not do much, as the process will log and then crash (intentionally) if a fatal condition occurs */ + FATAL = 1, + /** Only errors */ + ERROR = 2, + /** Only warnings and errors */ + WARN = 3, + /** Information about connection/stream creation/destruction events */ + INFO = 4, + /** Enough information to debug the chain of events a given network connection encounters */ + DEBUG = 5, + /** Everything. Only use this if you really need to know EVERY single call */ + TRACE = 6 +} + +let logLevel : LogLevel = LogLevel.NONE; + +export function setLogLevel(level: LogLevel) { + logLevel = level; +} + +export type LogLineGenerator = () => string; + +export function logFatal(subject: string, generator: LogLineGenerator) { + if (logLevel < LogLevel.FATAL) { + return; + } + + let currentTime = new Date().toISOString(); + console.log(`[FATAL] [${currentTime}] [${subject}] - ${generator()}`); +} + +export function logError(subject: string, generator: LogLineGenerator) { + if (logLevel < LogLevel.ERROR) { + return; + } + + let currentTime = new Date().toISOString(); + console.log(`[ERROR] [${currentTime}] [${subject}] - ${generator()}`); +} + +export function logWarn(subject: string, generator: LogLineGenerator) { + if (logLevel < LogLevel.WARN) { + return; + } + + let currentTime = new Date().toISOString(); + console.log(`[WARN] [${currentTime}] [${subject}] - ${generator()}`); +} + +export function logInfo(subject: string, generator: LogLineGenerator) { + if (logLevel < LogLevel.INFO) { + return; + } + + let currentTime = new Date().toISOString(); + console.log(`[INFO] [${currentTime}] [${subject}] - ${generator()}`); +} + +export function logDebug(subject: string, generator: LogLineGenerator) { + if (logLevel < LogLevel.DEBUG) { + return; + } + + let currentTime = new Date().toISOString(); + console.log(`[DEBUG] [${currentTime}] [${subject}] - ${generator()}`); +} + +export function logTrace(subject: string, generator: LogLineGenerator) { + if (logLevel < LogLevel.TRACE) { + return; + } + + let currentTime = new Date().toISOString(); + console.log(`[TRACE] [${currentTime}] [${subject}] - ${generator()}`); +} \ No newline at end of file diff --git a/lib/native/io.ts b/lib/native/io.ts index d77c25af..d9849918 100644 --- a/lib/native/io.ts +++ b/lib/native/io.ts @@ -21,9 +21,9 @@ import crt_native from './binding'; import { NativeResource } from "./native_resource"; -import { TlsVersion, SocketType, SocketDomain } from '../common/io'; +import { setLogLevel, LogLevel, TlsVersion, SocketType, SocketDomain } from '../common/io'; import { Readable } from 'stream'; -export { TlsVersion, SocketType, SocketDomain } from '../common/io'; +export { LogLevel, TlsVersion, SocketType, SocketDomain } from '../common/io'; import { CrtError } from './error'; /** @@ -56,27 +56,6 @@ export function error_code_to_name(error_code: number): string { return crt_native.error_code_to_name(error_code); } -/** - * The amount of detail that will be logged - * @category Logging - */ -export enum LogLevel { - /** No logging whatsoever. Equivalent to never calling {@link enable_logging}. */ - NONE = 0, - /** Only fatals. In practice, this will not do much, as the process will log and then crash (intentionally) if a fatal condition occurs */ - FATAL = 1, - /** Only errors */ - ERROR = 2, - /** Only warnings and errors */ - WARN = 3, - /** Information about connection/stream creation/destruction events */ - INFO = 4, - /** Enough information to debug the chain of events a given network connection encounters */ - DEBUG = 5, - /** Everything. Only use this if you really need to know EVERY single call */ - TRACE = 6 -} - /** * Enables logging of the native AWS CRT libraries. * @param level - The logging level to filter to. It is not possible to log less than WARN. @@ -86,6 +65,7 @@ export enum LogLevel { */ export function enable_logging(level: LogLevel) { crt_native.io_logging_enable(level); + setLogLevel(level); } /**