From 4030843d8d2157394a4dbd6532f16281048b4a37 Mon Sep 17 00:00:00 2001 From: Sahil Hussain Date: Thu, 28 Feb 2019 12:04:33 +0530 Subject: [PATCH 1/5] Update index.js --- index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index f4ffb20..6df22ed 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,17 @@ export { default as LogView } from "./debug-view"; -import deviceLog from "./debug-service"; export { default as InMemoryAdapter } from "./adapters/in-memory"; export { default as StorageServerHocWriter, -} from "./data-writers/storage-server-hoc-writer"; -export default deviceLog; +} +from "./data-writers/storage-server-hoc-writer"; +export { default as deviceLog } from "./debug-service"; +export { default as debugListView } from "./debug-list-view"; export { default as StringifyDataWriter, -} from "./data-writers/stringify-data-writer"; +} +from "./data-writers/stringify-data-writer"; export { default as RealmDataWriter, RealmBaseScheme, -} from "./data-writers/realm-data-writer"; +} +from "./data-writers/realm-data-writer"; From aafc798972e22e02023ee76e4a80fb2f8e021f51 Mon Sep 17 00:00:00 2001 From: Sahil Hussain Date: Fri, 1 Mar 2019 16:57:25 +0530 Subject: [PATCH 2/5] Update index.js Indentation Changed --- index.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 6df22ed..156aaf1 100644 --- a/index.js +++ b/index.js @@ -2,16 +2,13 @@ export { default as LogView } from "./debug-view"; export { default as InMemoryAdapter } from "./adapters/in-memory"; export { default as StorageServerHocWriter, -} -from "./data-writers/storage-server-hoc-writer"; +} from "./data-writers/storage-server-hoc-writer"; export { default as deviceLog } from "./debug-service"; export { default as debugListView } from "./debug-list-view"; export { default as StringifyDataWriter, -} -from "./data-writers/stringify-data-writer"; +} from "./data-writers/stringify-data-writer"; export { default as RealmDataWriter, RealmBaseScheme, -} -from "./data-writers/realm-data-writer"; +} from "./data-writers/realm-data-writer"; From 6971af9d799f3eeaf64043f6badcfa77acb4bd78 Mon Sep 17 00:00:00 2001 From: Sahil Hussain Date: Mon, 11 Nov 2019 16:08:55 +0530 Subject: [PATCH 3/5] Update debug-list-view.js --- debug-list-view.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/debug-list-view.js b/debug-list-view.js index 4bbf238..516fcfa 100644 --- a/debug-list-view.js +++ b/debug-list-view.js @@ -26,18 +26,9 @@ const LISTVIEW_REF = "listview"; export default class Debug extends React.Component { constructor() { super(); - let ds = new ListView.DataSource({ - rowHasChanged: (r1, r2) => { - let rowHasChanged = r1.id !== r2.id; - if (r1.expanded !== r2.expanded) { - return true; - } - return rowHasChanged; - }, - }); this.preparedRows = { blob: {} }; this.state = { - dataSource: ds.cloneWithRows([]), + dataSource: [], paused: false, rows: [], }; From 09ec585d96012f58a8fe2efa85156aa23b7ef2da Mon Sep 17 00:00:00 2001 From: Sahil Hussain Date: Mon, 11 Nov 2019 16:13:38 +0530 Subject: [PATCH 4/5] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5a4d3fe..059ad7c 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "main": "index.js", "repository": { "type": "git", - "url": "git+https://github.com/olofd/react-native-device-log.git" + "url": "git+https://github.com/sahildhussain/react-native-device-log.git" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" From 9d53dd767f9712833419dbaf54a6f77d5135a28b Mon Sep 17 00:00:00 2001 From: Sahil Hussain Date: Tue, 18 Feb 2020 19:35:28 +0530 Subject: [PATCH 5/5] Update debug-service.js --- debug-service.js | 671 +++++++++++++++++++++++------------------------ 1 file changed, 331 insertions(+), 340 deletions(-) diff --git a/debug-service.js b/debug-service.js index 49474d8..4ec44a9 100644 --- a/debug-service.js +++ b/debug-service.js @@ -1,385 +1,376 @@ -import { AsyncStorage, AppState, NetInfo } from "react-native"; -import moment from "moment"; -import InMemory from "./adapters/in-memory"; -import timers from "./timers"; -import debounce from "debounce"; -import stacktraceParser from "stacktrace-parser"; -import stringify from "json-stringify-safe"; -import StringifyDataWriter from "./data-writers/stringify-data-writer"; -import guid from "./guid"; -import colors from "./colors"; +import { AppState } from 'react-native' +import NetInfo from '@react-native-community/netinfo' +import { AsyncStorage } from '@react-native-community/async-storage' +import moment from 'moment' +import InMemory from './adapters/in-memory' +import timers from './timers' +import debounce from 'debounce' +import stacktraceParser from 'stacktrace-parser' +import stringify from 'json-stringify-safe' +import StringifyDataWriter from './data-writers/stringify-data-writer' +import guid from './guid' +import colors from './colors' const APP_START_LOG_MESSAGE = { - id: guid(), - lengthAtInsertion: 0, - level: "seperator", - message: "APP START", - timeStamp: moment(), - color: "#FFF", -}; + id: guid(), + lengthAtInsertion: 0, + level: 'seperator', + message: 'APP START', + timeStamp: moment(), + color: '#FFF' +} class DebugService { - constructor(options) { - options = options || {}; - options.colors = options.colors || {}; - options.colors = { ...colors, ...options.colors }; - - this.logRows = []; - this.store = new StringifyDataWriter(new InMemory()); - this.listners = []; - this.options = { - logToConsole: false, - logToConsoleMethod : 'match', - logToConsoleFunc : undefined, - logRNErrors: false, - maxNumberToRender: 0, - maxNumberToPersist: 0, - rowInsertDebounceMs: 200, - logAppState: true, - logConnection: true, - ...options, - }; + constructor(options) { + options = options || {} + options.colors = options.colors || {} + options.colors = { ...colors, ...options.colors } + + this.logRows = [] + this.store = new StringifyDataWriter(new InMemory()) + this.listners = [] + this.options = { + logToConsole: false, + logToConsoleMethod: 'match', + logToConsoleFunc: undefined, + logRNErrors: false, + maxNumberToRender: 0, + maxNumberToPersist: 0, + rowInsertDebounceMs: 200, + logAppState: true, + logConnection: true, + ...options } - - _handleConnectivityTypeChange(connectionInfo) { - let { type, effectiveType } = connectionInfo; - if (type === "none") { - this.hasBeenDisconnected = true; - this.seperator(`DISCONNECTED FROM INTERNET`); + } + + _handleConnectivityTypeChange(connectionInfo) { + let { type, effectiveType } = connectionInfo + if (type === 'none') { + this.hasBeenDisconnected = true + this.seperator(`DISCONNECTED FROM INTERNET`) + } else { + const buildConnectionString = () => { + return `${type.toUpperCase()}${ + effectiveType === 'unknown' ? '' : ` - ${effectiveType}` + }` + } + if (this.hasBeenDisconnected) { + this.seperator(`[NETINFO] RECONNECTED TO ${buildConnectionString()}`) + } else { + if (this.connectionHasBeenEstablished) { + this.seperator(`[NETINFO] CHANGED TO ${buildConnectionString()}`) } else { - const buildConnectionString = () => { - return `${type.toUpperCase()}${effectiveType === "unknown" - ? "" - : ` - ${effectiveType}`}`; - }; - if (this.hasBeenDisconnected) { - this.seperator( - `[NETINFO] RECONNECTED TO ${buildConnectionString()}` - ); - } else { - if (this.connectionHasBeenEstablished) { - this.seperator( - `[NETINFO] CHANGED TO ${buildConnectionString()}` - ); - } else { - this.seperator( - `[NETINFO] CONNECTION TO ${buildConnectionString()}` - ); - } - } - } - this.connectionHasBeenEstablished = true; - } - - _handleAppStateChange(currentAppState) { - this.seperator(`APP STATE: ${currentAppState.toUpperCase()}`); - } - - setupRNErrorLogging() { - if (ErrorUtils) { - const defaultHandler = - ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler(); - if (defaultHandler) { - const parseErrorStack = error => { - if (!error || !error.stack) { - return []; - } - return Array.isArray(error.stack) - ? error.stack - : stacktraceParser.parse(error.stack); - }; - - ErrorUtils.setGlobalHandler((error, isFatal) => { - const stack = parseErrorStack(error); - this.rnerror(isFatal, error.message, stack); - defaultHandler && defaultHandler(error, isFatal); - }); - } + this.seperator(`[NETINFO] CONNECTION TO ${buildConnectionString()}`) } + } } - - async init(storageAdapter, options = {}) { - options.colors = options.colors || {}; - options.colors = { ...colors, ...options.colors }; - - this.options = { - ...this.options, - ...options, - }; - - if (this.options.customDataWriter) { - this.store = this.options.customDataWriter; - } else { - this.store = new StringifyDataWriter( - storageAdapter || new InMemory() - ); - } - - if (this.options.logAppState) { - AppState.addEventListener( - "change", - this._handleAppStateChange.bind(this) - ); - } - if (this.options.logConnection) { - NetInfo.addEventListener( - "connectionChange", - this._handleConnectivityTypeChange.bind(this) - ); + this.connectionHasBeenEstablished = true + } + + _handleAppStateChange(currentAppState) { + this.seperator(`APP STATE: ${currentAppState.toUpperCase()}`) + } + + setupRNErrorLogging() { + if (ErrorUtils) { + const defaultHandler = + ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler() + if (defaultHandler) { + const parseErrorStack = error => { + if (!error || !error.stack) { + return [] + } + return Array.isArray(error.stack) + ? error.stack + : stacktraceParser.parse(error.stack) } - if (this.options.logRNErrors) { - this.setupRNErrorLogging(); - } - - this._initalGet(); - return this.insertAppStartMessage(); - } - - async insertAppStartMessage() { - if (!this.appStartRendered) { - await this._appendToLog([APP_START_LOG_MESSAGE]); - this.appStartRendered = true; - } - } - - async insertStoreRows(rows) { - if (this.store.readOnly) { - return; - } - this.rowsToInsert = this.rowsToInsert || []; - this.rowsToInsert = this.rowsToInsert.concat(rows); - if (!this.debouncedInsert) { - this.debouncedInsert = debounce(() => { - if (this.store && this.store.insertRows) { - const insertArray = this.rowsToInsert; - this.rowsToInsert = []; - return this.store.insertRows( - insertArray, - this.getEmittableData(this.logRows) - ); - } - }, this.options.rowInsertDebounceMs); - } - this.debouncedInsert(); - } - - async _initalGet() { - this.initPromise = this.store.getRows(); - const rows = await this.initPromise; - this.store.setReadOnly(false); - const newRows = this.logRows; - this.logRows = this.logRows.concat(rows); - this.sortLogRows(); - await this.insertStoreRows(newRows); - if (this.store.initalDataRead) { - await this.store.initalDataRead(this.logRows); - } - return this.emitDebugRowsChanged(this.logRows); - } - sortLogRows() { - this.logRows.sort((left, right) => { - let dateDiff = moment - .utc(right.timeStamp) - .diff(moment.utc(left.timeStamp)); - if (dateDiff === 0) { - return right.lengthAtInsertion - left.lengthAtInsertion; - } - return dateDiff; - }); + ErrorUtils.setGlobalHandler((error, isFatal) => { + const stack = parseErrorStack(error) + this.rnerror(isFatal, error.message, stack) + defaultHandler && defaultHandler(error, isFatal) + }) + } } + } - async _getAndEmit() { - const rows = await this.store.getRows(); - return this.emitDebugRowsChanged(rows); - } + async init(storageAdapter, options = {}) { + debugger + options.colors = options.colors || {} + options.colors = { ...colors, ...options.colors } - stopTimer(name) { - timers.stop(name); - timers.remove(name); + this.options = { + ...this.options, + ...options } - async startTimer(name) { - let timer = timers.start(name); - return await this._log("start-timer", undefined, name); + if (this.options.customDataWriter) { + this.store = this.options.customDataWriter + } else { + this.store = new StringifyDataWriter(storageAdapter || new InMemory()) } - async logTime(name) { - let timer = timers.stop(name); - return await this._log( - "end-timer", - undefined, - `${name}-timer delta: ${timer.delta}ms` - ); + if (this.options.logAppState) { + AppState.addEventListener('change', this._handleAppStateChange.bind(this)) } - - async clear() { - this.logRows = []; - await this.store.clear(); - return await this._getAndEmit(); + if (this.options.logConnection) { + NetInfo.addEventListener( + 'connectionChange', + this._handleConnectivityTypeChange.bind(this) + ) } - - log(...logRows) { - return this._log("log", undefined, ...logRows); + if (this.options.logRNErrors) { + this.setupRNErrorLogging() } - debug(...logRows) { - return this._log("debug", undefined, ...logRows); - } + this._initalGet() + return this.insertAppStartMessage() + } - info(...logRows) { - return this._log("info", undefined, ...logRows); + async insertAppStartMessage() { + if (!this.appStartRendered) { + await this._appendToLog([APP_START_LOG_MESSAGE]) + this.appStartRendered = true } + } - error(...logRows) { - return this._log("error", undefined, ...logRows); + async insertStoreRows(rows) { + if (this.store.readOnly) { + return } - - fatal(...logRows) { - return this._log("fatal", undefined, ...logRows); + this.rowsToInsert = this.rowsToInsert || [] + this.rowsToInsert = this.rowsToInsert.concat(rows) + if (!this.debouncedInsert) { + this.debouncedInsert = debounce(() => { + if (this.store && this.store.insertRows) { + const insertArray = this.rowsToInsert + this.rowsToInsert = [] + return this.store.insertRows( + insertArray, + this.getEmittableData(this.logRows) + ) + } + }, this.options.rowInsertDebounceMs) } - - success(...logRows) { - return this._log("success", undefined, ...logRows); + this.debouncedInsert() + } + + async _initalGet() { + this.initPromise = this.store.getRows() + const rows = await this.initPromise + this.store.setReadOnly(false) + const newRows = this.logRows + this.logRows = this.logRows.concat(rows) + this.sortLogRows() + await this.insertStoreRows(newRows) + if (this.store.initalDataRead) { + await this.store.initalDataRead(this.logRows) } - - rnerror(fatal, message, stackTrace) { - let errorString = `ERROR: ${message} \nSTACKSTRACE:\n`; - if (stackTrace && Array.isArray(stackTrace)) { - const stackStrings = stackTrace.map(stackTraceItem => { - let methodName = "-"; - let lineNumber = "-"; - let column = "-"; - if (stackTraceItem.methodName) { - methodName = - stackTraceItem.methodName === "" - ? "-" - : stackTraceItem.methodName; - } - if (stackTraceItem.lineNumber !== undefined) { - lineNumber = stackTraceItem.lineNumber.toString(); - } - if (stackTraceItem.column !== undefined) { - column = stackTraceItem.column.toString(); - } - return `Method: ${methodName}, LineNumber: ${lineNumber}, Column: ${column}\n`; - }); - errorString += stackStrings.join("\n"); + return this.emitDebugRowsChanged(this.logRows) + } + + sortLogRows() { + this.logRows.sort((left, right) => { + let dateDiff = moment + .utc(right.timeStamp) + .diff(moment.utc(left.timeStamp)) + if (dateDiff === 0) { + return right.lengthAtInsertion - left.lengthAtInsertion + } + return dateDiff + }) + } + + async _getAndEmit() { + const rows = await this.store.getRows() + return this.emitDebugRowsChanged(rows) + } + + stopTimer(name) { + timers.stop(name) + timers.remove(name) + } + + async startTimer(name) { + let timer = timers.start(name) + return await this._log('start-timer', undefined, name) + } + + async logTime(name) { + let timer = timers.stop(name) + return await this._log( + 'end-timer', + undefined, + `${name}-timer delta: ${timer.delta}ms` + ) + } + + async clear() { + this.logRows = [] + await this.store.clear() + return await this._getAndEmit() + } + + log(...logRows) { + return this._log('log', undefined, ...logRows) + } + + debug(...logRows) { + return this._log('debug', undefined, ...logRows) + } + + info(...logRows) { + return this._log('info', undefined, ...logRows) + } + + error(...logRows) { + return this._log('error', undefined, ...logRows) + } + + fatal(...logRows) { + return this._log('fatal', undefined, ...logRows) + } + + success(...logRows) { + return this._log('success', undefined, ...logRows) + } + + rnerror(fatal, message, stackTrace) { + let errorString = `ERROR: ${message} \nSTACKSTRACE:\n` + if (stackTrace && Array.isArray(stackTrace)) { + const stackStrings = stackTrace.map(stackTraceItem => { + let methodName = '-' + let lineNumber = '-' + let column = '-' + if (stackTraceItem.methodName) { + methodName = + stackTraceItem.methodName === '' + ? '-' + : stackTraceItem.methodName } - if (fatal) { - return this._log("RNFatal", undefined, errorString); - } else { - return this._log("RNError", undefined, errorString); + if (stackTraceItem.lineNumber !== undefined) { + lineNumber = stackTraceItem.lineNumber.toString() } + if (stackTraceItem.column !== undefined) { + column = stackTraceItem.column.toString() + } + return `Method: ${methodName}, LineNumber: ${lineNumber}, Column: ${column}\n` + }) + errorString += stackStrings.join('\n') } - - seperator(name) { - return this._log("seperator", undefined, name); + if (fatal) { + return this._log('RNFatal', undefined, errorString) + } else { + return this._log('RNError', undefined, errorString) } - - getColorForLogLevel(level) { - return this.options.colors[level] || "#fff"; + } + + seperator(name) { + return this._log('seperator', undefined, name) + } + + getColorForLogLevel(level) { + return this.options.colors[level] || '#fff' + } + + async _log(level, options, ...logRows) { + let color = this.getColorForLogLevel(level) + if (options) { + if (options.color) { + color = options.color + } } - - async _log(level, options, ...logRows) { - let color = this.getColorForLogLevel(level); - if (options) { - if (options.color) { - color = options.color; - } - } - this.logToConsole(level, color, ...logRows); - let formattedRows = logRows.map((logRow, idx) => ({ - id: guid(), - lengthAtInsertion: this.logRows.length + idx, - level, - message: this._parseDataToString(logRow), - timeStamp: moment(), - color, - })); - if (this.options.appendToLogRow) { - formattedRows = formattedRows.map(logRow => - this.options.appendToLogRow(logRow) - ); - } - - if (!this.logRows.length) { - await this.insertAppStartMessage(); - } - if (this.store.logRowCreated) { - formattedRows.forEach(logRow => this.store.logRowCreated(logRow)); - } - await this._appendToLog(formattedRows); - if (!this.initPromise) { - await this._initalGet(); - } + this.logToConsole(level, color, ...logRows) + let formattedRows = logRows.map((logRow, idx) => ({ + id: guid(), + lengthAtInsertion: this.logRows.length + idx, + level, + message: this._parseDataToString(logRow), + timeStamp: moment(), + color + })) + if (this.options.appendToLogRow) { + formattedRows = formattedRows.map(logRow => + this.options.appendToLogRow(logRow) + ) } - logToConsole(level, color, ...logRows) { - if ( - this.options.logToConsole && - (!this.options.disableLevelToConsole || - !this.options.disableLevelToConsole.some( - disabledLevel => disabledLevel === level - )) - ) { - if(this.options.logToConsoleFunc) { - this.options.logToConsoleFunc(level, color, logRows) - }else { - if(this.options.logToConsoleMethod === 'match') { - let avaliableConsoleLogs = ["log", "info", "debug", "error"]; - let consoleLogFunc = - avaliableConsoleLogs.find(avCL => avCL === level) || "log"; - console[consoleLogFunc](...logRows); - }else { - console[this.options.logToConsoleMethod](...logRows); - } - } - - } + if (!this.logRows.length) { + await this.insertAppStartMessage() } - - _parseDataToString(data) { - if (typeof data === "string" || data instanceof String) { - return data; + if (this.store.logRowCreated) { + formattedRows.forEach(logRow => this.store.logRowCreated(logRow)) + } + await this._appendToLog(formattedRows) + if (!this.initPromise) { + await this._initalGet() + } + } + + logToConsole(level, color, ...logRows) { + if ( + this.options.logToConsole && + (!this.options.disableLevelToConsole || + !this.options.disableLevelToConsole.some( + disabledLevel => disabledLevel === level + )) + ) { + if (this.options.logToConsoleFunc) { + this.options.logToConsoleFunc(level, color, logRows) + } else { + if (this.options.logToConsoleMethod === 'match') { + let avaliableConsoleLogs = ['log', 'info', 'debug', 'error'] + let consoleLogFunc = + avaliableConsoleLogs.find(avCL => avCL === level) || 'log' + console[consoleLogFunc](...logRows) } else { - let dataAsString = stringify(data, null, " "); //FYI: spaces > tabs - if (dataAsString && dataAsString.length > 12000) { - dataAsString = - dataAsString.substring(0, 12000) + - "...(Cannot display more RN-device-log)"; - } - return dataAsString; + console[this.options.logToConsoleMethod](...logRows) } + } } - - _appendToLog(logRows) { - this.logRows = logRows.concat(this.logRows); - this.insertStoreRows(logRows); - this.emitDebugRowsChanged(this.logRows); + } + + _parseDataToString(data) { + if (typeof data === 'string' || data instanceof String) { + return data + } else { + let dataAsString = stringify(data, null, ' ') //FYI: spaces > tabs + if (dataAsString && dataAsString.length > 12000) { + dataAsString = + dataAsString.substring(0, 12000) + + '...(Cannot display more RN-device-log)' + } + return dataAsString } - - onDebugRowsChanged(cb) { - this.listners.push(cb); - cb(this.getEmittableData(this.logRows)); - return () => { - var i = this.listners.indexOf(cb); - if (i !== -1) { - this.listners.splice(i, 1); - } - }; + } + + _appendToLog(logRows) { + this.logRows = logRows.concat(this.logRows) + this.insertStoreRows(logRows) + this.emitDebugRowsChanged(this.logRows) + } + + onDebugRowsChanged(cb) { + this.listners.push(cb) + cb(this.getEmittableData(this.logRows)) + return () => { + var i = this.listners.indexOf(cb) + if (i !== -1) { + this.listners.splice(i, 1) + } } + } - emitDebugRowsChanged(data) { - this.listners.forEach(cb => cb(this.getEmittableData(data))); - } + emitDebugRowsChanged(data) { + this.listners.forEach(cb => cb(this.getEmittableData(data))) + } - getEmittableData(rows) { - if (this.options.maxNumberToRender !== 0) { - return rows.slice(0, this.options.maxNumberToRender); - } - return rows; + getEmittableData(rows) { + if (this.options.maxNumberToRender !== 0) { + return rows.slice(0, this.options.maxNumberToRender) } + return rows + } } -export default new DebugService(); +export default new DebugService()