[client-app] use debug library for DB

Summary:
Use the wonderful `debug` library instead of all our random flags.
You can now do things like: `DEBUG="app:*,sync:*" npm start` to print out
everything.

Test Plan: manual

Reviewers: mark, halla, spang, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D4297
This commit is contained in:
Evan Morikawa 2017-03-30 17:26:33 -07:00
parent f5f5b60140
commit f9c0a93d1c
4 changed files with 30 additions and 30 deletions

View file

@ -30,7 +30,7 @@
"coffee-script": "1.10.0",
"coffeestack": "^1.1",
"color": "^0.7.3",
"debug": "2.4.5",
"debug": "github:emorikawa/debug#nylas",
"electron": "1.4.15",
"electron-spellchecker": "1.0.1",
"emissary": "^1.3.1",

View file

@ -1,6 +1,7 @@
/* eslint global-require: 0 */
import path from 'path';
import fs from 'fs';
import createDebug from 'debug'
import childProcess from 'child_process';
import PromiseQueue from 'promise-queue';
import {remote, ipcRenderer} from 'electron';
@ -16,6 +17,9 @@ import DatabaseTransaction from './database-transaction';
import DatabaseSetupQueryBuilder from './database-setup-query-builder';
import {setupDatabase, databasePath} from '../../database-helpers'
const debug = createDebug('app:RxDB')
const debugVerbose = createDebug('app:RxDB:all')
const DatabaseVersion = "23";
const DatabasePhase = {
Setup: 'setup',
@ -23,7 +27,6 @@ const DatabasePhase = {
Close: 'close',
}
const DEBUG_TO_LOG = false;
const DEBUG_QUERY_PLANS = NylasEnv.inDevMode();
const BASE_RETRY_LOCK_DELAY = 50;
@ -198,7 +201,7 @@ class DatabaseStore extends NylasStore {
try {
for (const query of builder.setupQueries()) {
console.debug(DEBUG_TO_LOG, `DatabaseStore: ${query}`);
debug(`DatabaseStore: ${query}`);
this._db.prepare(query).run();
}
} catch (err) {
@ -227,7 +230,7 @@ class DatabaseStore extends NylasStore {
const step = () => {
const query = queries.shift();
if (query) {
console.debug(DEBUG_TO_LOG, `DatabaseStore: ${query}`);
debug(`DatabaseStore: ${query}`);
this._executeInBackground(query, []).then(step);
} else {
const msec = Date.now() - start
@ -333,11 +336,12 @@ class DatabaseStore extends NylasStore {
}
this._executeInBackground(query, values).then(({results, backgroundTime}) => {
const msec = Date.now() - start;
if (process.env.ENABLE_RXDB_DEBUG_LOGGING) {
const q = `🔶 N1: (${msec}ms) Background: ${query}`;
StringUtils.logTrim(q)
if (debugVerbose.enabled) {
const q = `🔶 (${msec}ms) Background: ${query}`;
debugVerbose(StringUtils.trimTo(q))
}
if (msec > 100 || DEBUG_TO_LOG) {
if (msec > 100) {
const msgPrefix = msec > 100 ? 'DatabaseStore._executeInBackground took more than 100ms - ' : ''
this._prettyConsoleLog(`${msgPrefix}${msec}msec (${backgroundTime}msec in background): ${query}`);
}
@ -386,14 +390,13 @@ class DatabaseStore extends NylasStore {
const start = Date.now();
results = stmt[fn](values);
const msec = Date.now() - start
if (process.env.ENABLE_RXDB_DEBUG_LOGGING) {
const q = `🔶 N1: (${msec}ms) ${query}`;
StringUtils.logTrim(q)
const msec = Date.now() - start;
if (debugVerbose.enabled) {
const q = `(${msec}ms) ${query}`;
debugVerbose(StringUtils.trimTo(q))
}
if (msec > 100 || DEBUG_TO_LOG) {
if (msec > 100) {
const msgPrefix = msec > 100 ? 'DatabaseStore: query took more than 100ms - ' : ''
if (query.startsWith(`SELECT `) && DEBUG_QUERY_PLANS) {
const plan = this._db.prepare(`EXPLAIN QUERY PLAN ${query}`).all(values);
@ -443,11 +446,11 @@ class DatabaseStore extends NylasStore {
console.error(data.toString())
);
this._agent.on('close', (code) => {
console.debug(DEBUG_TO_LOG, `Query Agent: exited with code ${code}`);
debug(`Query Agent: exited with code ${code}`);
this._agent = null;
});
this._agent.on('error', (err) => {
console.error(DEBUG_TO_LOG, `Query Agent: failed to start or receive message: ${err.toString()}`);
console.error(`Query Agent: failed to start or receive message: ${err.toString()}`);
this._agent.kill('SIGTERM');
this._agent = null;
});

View file

@ -1,3 +1,4 @@
const createDebug = require('debug');
const Sequelize = require('sequelize');
const fs = require('fs');
const path = require('path');
@ -5,8 +6,7 @@ const {StringUtils, loadModels, HookIncrementVersionOnSave, HookTransactionLog}
const TransactionConnector = require('./transaction-connector')
require('./database-extensions'); // Extends Sequelize on require
const ENABLE_SEQUELIZE_DEBUG_LOGGING = process.env.ENABLE_SEQUELIZE_DEBUG_LOGGING;
const debugVerbose = createDebug("sync:K2DB:all");
class LocalDatabaseConnector {
constructor() {
@ -16,13 +16,13 @@ class LocalDatabaseConnector {
_sequelizePoolForDatabase(dbname) {
const storage = NylasEnv.inSpecMode() ? ':memory:' : path.join(process.env.NYLAS_HOME, `${dbname}.sqlite`);
const dbLog = (q, time) => {
StringUtils.logTrim(`🔷 K2: (${time}ms) ${q}`)
debugVerbose(StringUtils.trimTo(`🔷 (${time}ms) ${q}`))
}
return new Sequelize(dbname, '', '', {
storage: storage,
dialect: "sqlite",
benchmark: !!ENABLE_SEQUELIZE_DEBUG_LOGGING,
logging: ENABLE_SEQUELIZE_DEBUG_LOGGING ? dbLog : false,
benchmark: debugVerbose.enabled,
logging: debugVerbose.enabled ? dbLog : false,
})
}

View file

@ -1,12 +1,9 @@
export function logTrim(...args) {
export function trimTo(str, size) {
const g = window || global || {}
const LOG_TRIM_SIZE = process.env.LOG_TRIM_SIZE || g.LOG_TRIM_SIZE || 256;
for (let i = 0; i < args.length; i++) {
let str = args[i];
if (str.length >= LOG_TRIM_SIZE) {
str = `${str.slice(0, LOG_TRIM_SIZE / 2)}…${str.slice(str.length - LOG_TRIM_SIZE / 2, str.length)}`
}
args[i] = str;
const TRIM_SIZE = size || process.env.TRIM_SIZE || g.TRIM_SIZE || 256;
let trimed = str;
if (str.length >= TRIM_SIZE) {
trimed = `${str.slice(0, TRIM_SIZE / 2)}…${str.slice(str.length - TRIM_SIZE / 2, str.length)}`
}
console.log(...args);
return trimed
}