Skip to content

Function caller info #414

@mrousavy

Description

@mrousavy

Problem

I'm using a custom implementation of a Logger which outputs all logs in the following format:

[current time] [function that invoked log()]: [log message]

My log function looks like this:

function formatCaller(caller: Function): string {
  if (!caller) return '{?}:';
  else return `{${caller.name}}: `;
}

export function log(...messages: unknown[]): void {
  let caller = '{?}:';
  try {
    caller = formatCaller(log.caller);
  } catch {}
  const joinedMessage = messages.map((m) => (typeof m === 'string' ? m : JSON.stringify(m))).join(' ');
  console.log(caller + joinedMessage);
}

On JSC (iOS) the caller name is correctly retrieved, but on Hermes log.caller evaluates to null because caller information is not available in Hermes bytecode. This makes it extremely difficult to trace breadcrumbs back when trying to find the source of an error that pops up in your Sentry/Crashlytics/...

Solution

Add [currentFunction].caller information. Since the function's code is stored as bytecode, I'm assuming only name and arguments are available properties, toString() should just return the name instead of the function's code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions