如何在TypeScript中获取源中的实际行号(用于自定义日志记录) [英] How to get actual line # within source (for custom logging) in TypeScript
问题描述
引用此问题我正在使用以下代码来查找我的自定义日志记录函数的调用方的行号:
Referencing this question I am using this bit of code to find the line number of the caller to my custom logging function:
/**
* eLog - displays calling line number & message & dumps vars as pretty json string
* @param {string} msg - string to display in log message
* @param {any} dispVars - any number of variables (ellipsis , aka Rest parameters) to dump
*/
function eLog(msg:string,...dispVars:any[]){
let caller_line = (new Error).stack.split("\n")[4];
console.log(`eLog->Line#${caller_line}->${msg}->`);
console.log(JSON.stringify((new Error).stack.split("\n"),null,2));
dispVars.forEach(value => {
console.log(JSON.stringify(value,null,2));
});
}
这样称呼:
eLog("eLog Test",this);
尽管这样做确实可以正确转储 .js
文件行号,我需要源代码行号, .ts
行号。我该如何正确生成呢?
And while this does do a proper dump of the .js
file line #, I need the Source line#, the .ts
line number. How can I generate this properly?
推荐答案
我在晚上进行了这项工作,并想出了一个令我满意的功能。感谢您的入门帮助-
I went through this over the evening and came up with a function I am pleased with. Thanks for the help in getting started-
将其分离为 log.ts
require('source-map-support').install({
environment: 'node'
});
/**
* eLog - displays calling line number & message & dumps vars as pretty json string
* @param {string} msg - string to display in log message
* @param {any} dispVars - any number of variables (ellipsis , aka Rest parameters) to dump
* {@link https://github.com/evanw/node-source-map-support usable by typescript node-source-map-support module}
* {@link https://github.com/mozilla/source-map/ Mozilla source-map library & project}
* {@link http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/ good introduction to sourcemaps}
*/
export function eLog(msg:string,...dispVars:any[]){
/**
* go one line back for the caller
* @type {string}
*/
let stackLine = (new Error).stack.split("\n")[2];
/**
* retrieve the file basename & positional data, after the last `/` to the `)`
*/
//
let caller_line = stackLine.slice(stackLine.lastIndexOf('/'),stackLine.lastIndexOf(')'))
/**
* test for no `/` ; if there is no `/` then use filename without a prefixed path
*/
if ( caller_line.length == 0 ) {
caller_line = stackLine.slice(stackLine.lastIndexOf('('),stackLine.lastIndexOf(')'))
}
//
/**
* filename_base - parse out the file basename; remove first `/` char and go to `:`
*/
let filename_base = caller_line.slice(0+1,caller_line.indexOf(':'));
/**
* line_no - parse out the line number ; remove first `:` char and go to 2nd `:`
*/
let line_no = caller_line.slice(caller_line.indexOf(':')+1,caller_line.lastIndexOf(':'));
/**
* line_pos - line positional - from the last `:` to the end of the string
*/
let line_pos = caller_line.slice(caller_line.lastIndexOf(':')+1);
console.log(`eLog called by ${filename_base} on line# ${line_no} @ char# ${line_pos} said:\n${msg}`);
// print out the input variables as pretty JSON strings
dispVars.forEach(value => {
console.log(JSON.stringify(value,null,2));
});
}
可以用以下简单方法调用它:
Which can be called with a simple:
eLog("eLog Test",this);
从任何文件开始,只要函数被加载(例如)
from any file so long as the function is loaded (such as)
import { eLog } from './log'
我希望对其他人有所帮助。
I hope that helps somebody else.
干杯。
-埃里克
Cheers guys. -Eric
这篇关于如何在TypeScript中获取源中的实际行号(用于自定义日志记录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!