---
source: boltffi_bindgen/src/render/typescript/templates.rs
expression: template.render().unwrap()
---
const _LoggerFinalizer: FinalizationRegistry<number> | null =
  typeof FinalizationRegistry === "undefined"
    ? null
    : new FinalizationRegistry<number>((handle) => {
        (_exports.boltffi_logger_free as Function)(handle);
      });

export class Logger {
  private _handle: number;
  private _disposed: boolean;

  private constructor(handle: number) {
    this._handle = handle;
    this._disposed = false;
    _LoggerFinalizer?.register(this, handle, this);
  }

  static _fromHandle(handle: number): Logger {
    if (handle === 0) {
      throw new Error("Logger received a null handle");
    }
    return new Logger(handle);
  }

  [Symbol.dispose](): void {
    this.dispose();
  }

  dispose(): void {
    if (this._disposed) {
      return;
    }
    this._disposed = true;
    _LoggerFinalizer?.unregister(this);
    (_exports.boltffi_logger_free as Function)(this._handle);
    this._handle = 0;
  }

  private _assertNotDisposed(): void {
    if (this._disposed) {
      throw new Error("Logger has been disposed");
    }
  }

  static new(): Logger {
    const handle = (_exports.boltffi_logger_new as Function)();
    return Logger._fromHandle(handle);
  }

  log(message: string): void {
    this._assertNotDisposed();
    const message_alloc = _module.allocString(message);
    try {
      (_exports.boltffi_logger_log as Function)(this._handle, message_alloc.ptr, message_alloc.len);
    } finally {
      _module.freeAlloc(message_alloc);
    }
  }
}
