All files / src/shared logging.ts

96.36% Statements 53/55
100% Branches 9/9
100% Functions 9/9
96.36% Lines 53/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 9221x 21x 21x   21x 21x 21x 21x 21x     154x     154x       2x     2x     21x 22x 22x 2x 2x   22x     21x 833x         132x 82x   50x 50x   1x       21x 647x 595x   52x 52x 52x 3x 3x   49x     21x 76x 76x 76x 16x 16x   60x     21x 2x 2x 2x 2x 2x         21x 2x 2x 2x 2x 2x        
import * as vscode from "vscode";
import { ConfigManager } from "./config";
import { DEBUG_ENV_VAR, PROVIDER_DISPLAY_NAME } from "./constants";
 
const OUTPUT_CHANNEL_NAME = PROVIDER_DISPLAY_NAME;
const DEBUG_LOG_PREFIX = `[${PROVIDER_DISPLAY_NAME} Debug]`;
const LOG_PREFIX = `[${PROVIDER_DISPLAY_NAME}]`;
const ERROR_LOG_PREFIX = `[${PROVIDER_DISPLAY_NAME} Error]`;
const WARN_LOG_PREFIX = `[${PROVIDER_DISPLAY_NAME} Warning]`;
 
function getGlobalOutputChannel(): vscode.OutputChannel | undefined {
  const globalWindow = globalThis as typeof globalThis & {
    __nvidiaNimOutputChannel?: vscode.OutputChannel;
  };
  return globalWindow.__nvidiaNimOutputChannel;
}
 
function setGlobalOutputChannel(channel: vscode.OutputChannel): void {
  const globalWindow = globalThis as typeof globalThis & {
    __nvidiaNimOutputChannel?: vscode.OutputChannel;
  };
  globalWindow.__nvidiaNimOutputChannel = channel;
}
 
export function getOutputChannel(): vscode.OutputChannel {
  let channel = getGlobalOutputChannel();
  if (!channel) {
    channel = vscode.window.createOutputChannel(OUTPUT_CHANNEL_NAME);
    setGlobalOutputChannel(channel);
  }
  return channel;
}
 
export function debugEnabled(): boolean {
  return process.env[DEBUG_ENV_VAR] === "1" || ConfigManager.getDeveloperConfig().debugLogging;
}
 
/** Serialize a log payload without ever throwing on circular structures. */
function toLogMessage(value: unknown): string {
  if (typeof value === "string") {
    return value;
  }
  try {
    return JSON.stringify(value, null, 2);
  } catch {
    return String(value);
  }
}
 
export function debugLog(label: string, value: unknown): void {
  if (!debugEnabled()) {
    return;
  }
  const message = toLogMessage(value);
  const channel = getGlobalOutputChannel();
  if (channel) {
    channel.appendLine(`${DEBUG_LOG_PREFIX} ${label}: ${message}`);
    return;
  }
  console.log(`${DEBUG_LOG_PREFIX} ${label}:`, value);
}
 
export function outputLog(label: string, value: unknown): void {
  const message = toLogMessage(value);
  const channel = getGlobalOutputChannel();
  if (channel) {
    channel.appendLine(`${LOG_PREFIX} ${label}: ${message}`);
    return;
  }
  console.log(`${LOG_PREFIX} ${label}:`, value);
}
 
export function errorLog(label: string, value: unknown): void {
  const message = toLogMessage(value);
  const channel = getGlobalOutputChannel();
  if (channel) {
    channel.appendLine(`${ERROR_LOG_PREFIX} ${label}: ${message}`);
    return;
  }
  console.error(`${ERROR_LOG_PREFIX} ${label}:`, value);
}
 
export function warnLog(label: string, value: unknown): void {
  const message = toLogMessage(value);
  const channel = getGlobalOutputChannel();
  if (channel) {
    channel.appendLine(`${WARN_LOG_PREFIX} ${label}: ${message}`);
    return;
  }
  console.warn(`${WARN_LOG_PREFIX} ${label}:`, value);
}