Skip to content

Commit

Permalink
Simple logging API; defaults to console
Browse files Browse the repository at this point in the history
  • Loading branch information
Bret Ambrose committed Jul 22, 2024
1 parent 32ac472 commit 19bcd02
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 25 deletions.
8 changes: 6 additions & 2 deletions lib/browser/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,3 +112,7 @@ export class SocketOptions {
public keep_alive_max_failed_probes = 0) {
}
}

export function enable_logging(level: LogLevel) {
setLogLevel(level);
}
83 changes: 83 additions & 0 deletions lib/common/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`);
}
26 changes: 3 additions & 23 deletions lib/native/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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.
Expand All @@ -86,6 +65,7 @@ export enum LogLevel {
*/
export function enable_logging(level: LogLevel) {
crt_native.io_logging_enable(level);
setLogLevel(level);
}

/**
Expand Down

0 comments on commit 19bcd02

Please sign in to comment.