1 | /** |
||
2 | * @file The file what opens logs. |
||
3 | * @author willyb321 |
||
4 | * @copyright MIT |
||
5 | */ |
||
6 | /** |
||
7 | * @module Reader |
||
8 | */ |
||
9 | import LineByLineReader from 'line-by-line'; |
||
10 | import {dialog} from 'electron'; |
||
11 | import _ from 'lodash'; |
||
12 | import moment from 'moment'; |
||
13 | import pug from 'pug' |
||
14 | import tableify from 'tableify'; |
||
15 | import {logPath, currentData, win} from '../main/index'; |
||
16 | import path from 'path'; |
||
17 | import Raven from 'raven'; |
||
18 | Raven.config('https://8f7736c757ed4d2882fc24a2846d1ce8:[email protected]/226655', { |
||
19 | release: require('electron').app.getVersion(), |
||
20 | autoBreadcrumbs: true |
||
21 | }).install(); |
||
22 | /** |
||
23 | * Opens dialog that returns the path of a log file. |
||
24 | * @returns {Promise} Array with path. |
||
25 | */ |
||
26 | export function getLogPath() { |
||
27 | return new Promise((resolve, reject) => { |
||
28 | const files = dialog.showOpenDialog({ |
||
29 | defaultPath: logPath, |
||
30 | buttonLabel: 'Load File', |
||
31 | filters: [{ |
||
32 | name: 'Logs and saved HTML/JSON', |
||
33 | extensions: ['log', 'html', 'json'] |
||
34 | }, { |
||
35 | name: 'All files', |
||
36 | extensions: ['*'] |
||
37 | }] |
||
38 | }, { |
||
39 | properties: ['openFile'] |
||
40 | }); |
||
41 | if (files) { |
||
42 | process.loadfile = files; |
||
43 | resolve(files) |
||
44 | } else { |
||
45 | reject(); |
||
46 | } |
||
47 | }) |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Reads log files line by line, then compiles with Pug and loads it. |
||
52 | */ |
||
53 | export function readLog(log, filter) { |
||
54 | let toPug = []; |
||
55 | let tablified = []; |
||
56 | let checkEvents = []; |
||
57 | if (!filter) { |
||
58 | filter = 'All Events'; |
||
59 | } |
||
60 | currentData.currentPath = log; |
||
61 | const lr = new LineByLineReader(log); |
||
62 | lr.on('error', err => { |
||
63 | Raven.captureException(err); |
||
64 | }); |
||
65 | lr.on('line', line => { |
||
66 | Raven.context(function() { |
||
67 | Raven.captureBreadcrumb({ |
||
68 | message: 'Log-process line', |
||
69 | data: { |
||
70 | line: line, |
||
71 | filename: log |
||
72 | } |
||
73 | }); |
||
74 | let parsed; |
||
75 | if (!parsed) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
76 | try { |
||
77 | parsed = JSON.parse(line); |
||
78 | } catch (e) { |
||
79 | Raven.captureException(e); |
||
80 | line = line |
||
81 | .replace(/\u000e/igm, '') |
||
82 | .replace(/\u000f/igm, ''); |
||
83 | parsed = JSON.parse(line); |
||
84 | } |
||
85 | } |
||
86 | if (filter && parsed && parsed.event !== filter && filter !== 'All Events') { |
||
87 | parsed = null; |
||
88 | } |
||
89 | if (parsed) { |
||
90 | checkEvents.push(parsed.event); |
||
91 | parsed.timestamp = moment(parsed.timestamp).format('h:mm a - D/M '); |
||
92 | toPug.push(parsed); |
||
93 | _.each(Object.keys(parsed), elem => { |
||
94 | if (!(elem.endsWith('_Localised') || !parsed[elem].toString().startsWith('$'))) { |
||
95 | delete parsed[elem]; |
||
96 | } |
||
97 | }); |
||
98 | currentData.events.push(parsed.event); |
||
99 | tablified.push(tableify(parsed, undefined, undefined, true)); |
||
100 | } |
||
101 | }) |
||
102 | }); |
||
103 | lr.on('end', err => { |
||
104 | if (err) { |
||
105 | Raven.captureException(err); |
||
106 | } else { |
||
107 | if (currentData.events !== checkEvents) { |
||
108 | currentData.events = checkEvents; |
||
109 | } |
||
110 | currentData.events = _.uniq(currentData.events); |
||
111 | const filterLog = pug.renderFile(path.join(__dirname, '..', 'filter.pug'), { |
||
112 | basedir: path.join(__dirname, '..'), |
||
113 | events: currentData.events, |
||
114 | currentEvent: filter || 'All Events' |
||
115 | }); |
||
116 | const compiledLog = pug.renderFile(path.join(__dirname, '..', 'logload.pug'), { |
||
117 | basedir: path.join(__dirname, '..'), |
||
118 | data: toPug, |
||
119 | tabled: tablified, |
||
120 | filename: log, |
||
121 | events: currentData.events, |
||
122 | filterLog |
||
123 | }); |
||
124 | currentData.log = compiledLog; |
||
125 | win.loadURL('data:text/html,' + compiledLog, {baseURLForDataURL: `file://${path.join(__dirname, '..')}`}); |
||
126 | } |
||
127 | }) |
||
128 | } |
||
129 |