Function caller info
- Dominant language
- JavaScript
- Stars
- 11.3k
- Forks
- 859
- Avg merge
- 1h 30m
- Merged PRs (30d)
- 3
Description
## 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:
```ts
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.
Contributor guide
Assessment
This issue has not been assessed yet.