Skip to content

Instantly share code, notes, and snippets.

@shiv19
Created September 25, 2019 02:19
Show Gist options
  • Save shiv19/a6700e8c2222363a102d1ae2c628db2d to your computer and use it in GitHub Desktop.
Save shiv19/a6700e8c2222363a102d1ae2c628db2d to your computer and use it in GitHub Desktop.
NativeScript trace writer setup
// in app.ts before application.start
import * as trace from "tns-core-modules/trace";
trace.setCategories(trace.categories.concat(
// trace.categories.Layout, // add these two for detailed
// trace.categories.ViewHierarchy, // add these two for detailed
trace.categories.NativeLifecycle, // these two are for brief
trace.categories.Navigation // these two are for brief
));
// trace.setCategories(trace.categories.All); // this option is extremely detailed
import { setupTimestampConsoleWriter } from "./shared/traceWriter";
setupTimestampConsoleWriter();
trace.enable();
"use strict";
exports.__esModule = true;
var trace = require("tns-core-modules/trace");
var StampedWriter = /** @class */ (function () {
function StampedWriter() {
}
StampedWriter.prototype.write = function (message, category, type) {
if (!console)
return;
var newTime = new Date();
if (!this.startTime || (newTime.getTime() - this.lastCalledAt.getTime()) > 3000) {
this.startTime = newTime;
}
this.lastCalledAt = newTime;
var timeDiff = newTime.getTime() - this.startTime.getTime();
var msgType = type || trace.messageType.log;
var traceMessage = "time +" + timeDiff + " " + category + ": " + message;
switch (msgType) {
case trace.messageType.log:
console.log(traceMessage);
break;
case trace.messageType.info:
console.info(traceMessage);
break;
case trace.messageType.warn:
console.warn(traceMessage);
break;
case trace.messageType.error:
console.error(traceMessage);
break;
}
};
return StampedWriter;
}());
function setupTimestampConsoleWriter() {
trace.clearWriters();
trace.addWriter(new StampedWriter);
}
exports.setupTimestampConsoleWriter = setupTimestampConsoleWriter;
import * as trace from "tns-core-modules/trace";
class StampedWriter implements trace.TraceWriter {
private startTime: Date;
private lastCalledAt: Date;
write(message: any, category: string, type?: number) {
if (!console) return;
let newTime = new Date();
if (!this.startTime || (newTime.getTime()- this.lastCalledAt.getTime())>3000) {
this.startTime = newTime;
}
this.lastCalledAt = newTime;
let timeDiff = newTime.getTime()- this.startTime.getTime();
var msgType = type || trace.messageType.log;
var traceMessage = "time +" + timeDiff+ " " + category + ": " + message;
switch (msgType) {
case trace.messageType.log:
console.log(traceMessage);
break;
case trace.messageType.info:
console.info(traceMessage);
break;
case trace.messageType.warn:
console.warn(traceMessage);
break;
case trace.messageType.error:
console.error(traceMessage);
break;
}
}
}
export function setupTimestampConsoleWriter() {
trace.clearWriters();
trace.addWriter(new StampedWriter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment