내가 내 사용자 지정 로깅 기능에 발신자의 줄 번호를 찾기 위해 코드의이 비트를 사용하고 this question를 참조하기 (사용자 정의 로깅을위한) 소스 내에서 실제 라인 번호를 얻을 수 있습니다 이 : 어떻게 타이프 라이터
eLog("eLog Test",this);
그리고 이것은 .js
파일 호선의 적절한 덤프를하지 않는 동안, 나는 소스 라인 번호가 필요
.ts
행 번호. 어떻게 이것을 올바르게 생성 할 수 있습니까?
내가 내 사용자 지정 로깅 기능에 발신자의 줄 번호를 찾기 위해 코드의이 비트를 사용하고 this question를 참조하기 (사용자 정의 로깅을위한) 소스 내에서 실제 라인 번호를 얻을 수 있습니다 이 : 어떻게 타이프 라이터
eLog("eLog Test",this);
그리고 이것은 .js
파일 호선의 적절한 덤프를하지 않는 동안, 나는 소스 라인 번호가 필요
.ts
행 번호. 어떻게 이것을 올바르게 생성 할 수 있습니까?
내가 저녁 동안이를 통해 가서 내가 가진 기쁘게 생각 함수를 내놓았다. 너무 오래 모든 파일에서
eLog("eLog Test",this);
:
started- 얻기에 도움을 주셔서 감사합니다 간단한 호출 할 수 log.ts
require('source-map-support').install({
\t 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[]){
\t /**
\t * go one line back for the caller
\t * @type {string}
\t */
\t let stackLine = (new Error).stack.split("\n")[2];
\t /**
\t * retrieve the file basename & positional data, after the last `/` to the `)`
\t */
\t //
\t let caller_line = stackLine.slice(stackLine.lastIndexOf('/'),stackLine.lastIndexOf(')'))
\t /**
\t * test for no `/` ; if there is no `/` then use filename without a prefixed path
\t */
\t if (caller_line.length == 0) {
\t \t caller_line = stackLine.slice(stackLine.lastIndexOf('('),stackLine.lastIndexOf(')'))
\t }
\t //
\t /**
\t * filename_base - parse out the file basename; remove first `/` char and go to `:`
\t */
\t let filename_base = caller_line.slice(0+1,caller_line.indexOf(':'));
\t /**
\t * line_no - parse out the line number ; remove first `:` char and go to 2nd `:`
\t */
\t let line_no = caller_line.slice(caller_line.indexOf(':')+1,caller_line.lastIndexOf(':'));
\t /**
\t * line_pos - line positional - from the last `:` to the end of the string
\t */
\t let line_pos = caller_line.slice(caller_line.lastIndexOf(':')+1);
\t console.log(`eLog called by ${filename_base} on line# ${line_no} @ char# ${line_pos} said:\n${msg}`);
\t // print out the input variables as pretty JSON strings
\t dispVars.forEach(value => {
\t \t console.log(JSON.stringify(value,null,2));
\t });
}
으로 그것을 밖으로 분리 함수가로드 될 때 (예 :
)import { eLog } from './log'
나는 다른 사람을 돕기를 바랍니다.
건배! -Eric
어떻게 생성 할 수 있습니다이 제대로
가.) 당신이 코드는 사용자 런타임시에 상당히 의존한다 (예를 들어)가 IE에서 작동하지 않습니다.
b.) js-> ts가 작동하려면 소스 맵이 필요합니다. 쉽게 만드는 요소는 이미 존재하지 않습니다. sourcemaps를 사용하는 사실상 원시 패키지는 다음과 같습니다 https://www.npmjs.com/package/source-map