aboutsummaryrefslogtreecommitdiff
path: root/src/logging.ts
blob: 788a7df557e85e6ebc66f359fba05a596787230e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 This file is part of TALER
 (C) 2016 Inria

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

/**
 * Configurable logging.
 *
 * @author Florian Dold
 */

import {Store, QueryRoot, openPromise} from "./query";

export type Level = "error" | "debug" | "info" | "warn";

function makeInfo() {
  return console.info.bind(console, "%o");
}

function makeWarn() {
  return console.warn.bind(console, "%o");
}

function makeError() {
  return console.error.bind(console, "%o");
}

function makeDebug() {
  return console.log.bind(console, "%o");
}

export async function log(msg: string, level: Level = "info"): Promise<void> {
  let ci = getCallInfo(2);
  return record(level, msg, undefined, ci.file, ci.line, ci.column);
}

function getCallInfo(level: number) {
  // see https://github.com/v8/v8/wiki/Stack-Trace-API
  let stack = Error().stack;
  if (!stack) {
    return unknownFrame;
  }
  let lines = stack.split("\n");
  return parseStackLine(lines[level + 1]);
}

interface Frame {
  file?: string;
  method?: string;
  column?: number;
  line?: number;
}

const unknownFrame: Frame = {
  file: "(unknown)",
  method: "(unknown)",
  line: 0,
  column: 0
};

/**
 * Adapted from https://github.com/errwischt/stacktrace-parser.
 */
function parseStackLine(stackLine: string): Frame {
  const chrome = /^\s*at (?:(?:(?:Anonymous function)?|((?:\[object object\])?\S+(?: \[as \S+\])?)) )?\(?((?:file|http|https):.*?):(\d+)(?::(\d+))?\)?\s*$/i;
  const gecko = /^(?:\s*([^@]*)(?:\((.*?)\))?@)?(\S.*?):(\d+)(?::(\d+))?\s*$/i;
  const node  = /^\s*at (?:((?:\[object object\])?\S+(?: \[as \S+\])?) )?\(?(.*?):(\d+)(?::(\d+))?\)?\s*$/i;
  let parts;

  if ((parts = gecko.exec(stackLine))) {
    let f: Frame = {
        file: parts[3],
        method: parts[1] || "(unknown)",
        line: +parts[4],
        column: parts[5] ? +parts[5] : undefined,
    };
    return f;
  } else if ((parts = chrome.exec(stackLine))) {
    let f: Frame = {
        file: parts[2],
        method: parts[1] || "(unknown)",
        line: +parts[3],
        column: parts[4] ? +parts[4] : undefined,
    };
    return f;
  } else if ((parts = node.exec(stackLine))) {
    let f: Frame = {
        file: parts[2],
        method: parts[1] || "(unknown)",
        line: +parts[3],
        column: parts[4] ? +parts[4] : undefined,
    };
    return f;
  }
  return unknownFrame;
}


let db: IDBDatabase|undefined = undefined;

export interface LogEntry {
  timestamp: number;
  level: string;
  msg: string;
  detail?: string;
  source?: string;
  col?: number;
  line?: number;
  id?: number;
}

export async function getLogs(): Promise<LogEntry[]> {
  if (!db) {
    db = await openLoggingDb();
  }
  return await new QueryRoot(db).iter(logsStore).toArray();
}

/**
 * The barrier ensures that only one DB write is scheduled against the log db
 * at the same time, so that the DB can stay responsive.  This is a bit of a
 * design problem with IndexedDB, it doesn't guarantee fairness.
 */
let barrier: any;

export async function recordException(msg: string, e: any): Promise<void> {
  let stack: string|undefined;
  let frame: Frame|undefined;
  try {
    stack = e.stack;
    if (stack) {
      let lines = stack.split("\n");
      frame = parseStackLine(lines[1]);
    }
  } catch (e) {
    // ignore
  }
  if (!frame) {
    frame = unknownFrame;
  }
  return record("error", e.toString(), stack, frame.file, frame.line, frame.column);
}

export async function record(level: Level, msg: string, detail?: string, source?: string, line?: number, col?: number): Promise<void> {
  if (typeof indexedDB === "undefined") {
    return;
  }

  let myBarrier: any;

  if (barrier) {
    let p = barrier.promise;
    myBarrier = barrier = openPromise();
    await p;
  } else {
    myBarrier = barrier = openPromise();
  }

  try {
    if (!db) {
      db = await openLoggingDb();
    }

    let count = await new QueryRoot(db).count(logsStore);

    if (count > 1000) {
      await new QueryRoot(db).deleteIf(logsStore, (e, i) => (i < 200));
    }

    let entry: LogEntry = {
      timestamp: new Date().getTime(),
      level,
      msg,
      source,
      line,
      col,
      detail,
    };
    await new QueryRoot(db).put(logsStore, entry);
  } finally {
    await Promise.resolve().then(() => myBarrier.resolve());
  }
}

const loggingDbVersion = 1;

const logsStore: Store<LogEntry> = new Store<LogEntry>("logs");

export function openLoggingDb(): Promise<IDBDatabase> {
  return new Promise((resolve, reject) => {
    const req = indexedDB.open("taler-logging", loggingDbVersion);
    req.onerror = (e) => {
      reject(e);
    };
    req.onsuccess = (e) => {
      resolve(req.result);
    };
    req.onupgradeneeded = (e) => {
      const db = req.result;
      if (e.oldVersion != 0) {
        try {
          db.deleteObjectStore("logs");
        } catch (e) {
          console.error(e);
        }
      }
      db.createObjectStore("logs", {keyPath: "id", autoIncrement: true});
    };
  });
}

export const info = makeInfo();
export const debug = makeDebug();
export const warn = makeWarn();
export const error = makeError();