Skip to content

Commit d054fc4

Browse files
authored
Simplify logging utils (#1046)
1 parent 4076595 commit d054fc4

27 files changed

+365
-345
lines changed

extension/data/init.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ import * as TBApi from './tbapi';
3434
import * as TBCore from './tbcore.js';
3535
import {delay} from './tbhelpers.js';
3636
import TBListener from './tblistener.js';
37-
import TBLog from './tblog';
3837
import TBModule from './tbmodule.jsx';
3938
import {getCache, setCache} from './util/cache';
4039
import {documentInteractive} from './util/dom';
40+
import createLogger from './util/logging';
4141
import {isUserLoggedInQuick} from './util/platform';
4242
import {getSettingAsync, setSettingAsync, updateSettings} from './util/settings';
4343
import {reactRenderer} from './util/ui_interop';
@@ -296,13 +296,13 @@ async function doSettingsUpdates () {
296296
}
297297

298298
// Create a logger
299-
const logger = TBLog('Init');
299+
const log = createLogger('Init');
300300

301301
// Ensure that other conditions are met, and return early if not
302302
try {
303303
await checkLoadConditions();
304304
} catch (error) {
305-
logger.error('Load condition not met:', error);
305+
log.error('Load condition not met:', error);
306306
return;
307307
}
308308

@@ -392,7 +392,7 @@ async function doSettingsUpdates () {
392392
OldReddit,
393393
]
394394
) {
395-
logger.debug('Registering module', m);
395+
log.debug('Registering module', m);
396396
TBModule.register_module(m);
397397
}
398398

extension/data/modules/achievements.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import $ from 'jquery';
33
import * as TBCore from '../tbcore.js';
44
import * as TBHelpers from '../tbhelpers.js';
55
import {Module} from '../tbmodule.jsx';
6+
import createLogger from '../util/logging.ts';
67
import {getSettingAsync} from '../util/settings.ts';
78

9+
const log = createLogger('Achievements');
10+
811
const self = new Module({
912
name: 'Achievements',
1013
id: 'Achievements',
@@ -65,12 +68,12 @@ function Manager () {
6568
const title = titles[i];
6669
const maxValue = maxValues[i];
6770

68-
self.log('Registering Achievement');
71+
log.debug('Registering Achievement');
6972
if (debugMode) {
70-
self.log(` name=${title}`);
73+
log.debug(` name=${title}`);
7174
} // spoilers
72-
self.log(` maxValue=${maxValue}`);
73-
self.log(` saveIndex=${saveIndex}`);
75+
log.debug(` maxValue=${maxValue}`);
76+
log.debug(` saveIndex=${saveIndex}`);
7477

7578
achievementsBlock.push({
7679
title,
@@ -89,19 +92,19 @@ function Manager () {
8992
if (value === undefined) {
9093
value = 1;
9194
}
92-
self.log(`Unlocking achievement block: index=${saveIndex}, value=${value}`);
95+
log.debug(`Unlocking achievement block: index=${saveIndex}, value=${value}`);
9396

9497
const old = saves[saveIndex];
95-
self.log(` Old value: ${saves[saveIndex]}`);
98+
log.debug(` Old value: ${saves[saveIndex]}`);
9699
saves[saveIndex] += value;
97-
self.log(` New value: ${saves[saveIndex]}`);
100+
log.debug(` New value: ${saves[saveIndex]}`);
98101

99102
const achievementsBlock = achievements[saveIndex];
100103
let achievement;
101104
for (let index = 0; index < achievementsBlock.length; index++) {
102-
self.log(` Checking achievement ${index}`);
105+
log.debug(` Checking achievement ${index}`);
103106
achievement = achievementsBlock[index];
104-
self.log(` Comparing to max value: ${achievement.maxValue}`);
107+
log.debug(` Comparing to max value: ${achievement.maxValue}`);
105108
if (saves[saveIndex] >= achievement.maxValue && old < achievement.maxValue) {
106109
let title = achievement.title;
107110

@@ -111,10 +114,10 @@ function Manager () {
111114
try {
112115
title = $(achievement.title).text() || achievement.title;
113116
} catch (e) {
114-
self.log(`error: ${e}`);
117+
log.debug(`error: ${e}`);
115118
}
116119

117-
self.log(`${title} Unlocked!`);
120+
log.debug(`${title} Unlocked!`);
118121
TBCore.notification(
119122
'Mod achievement unlocked!',
120123
title,
@@ -206,7 +209,7 @@ function init ({lastSeen}) {
206209
const $body = $('body');
207210

208211
// Achievement definitions
209-
self.log('Registering achievements');
212+
log.debug('Registering achievements');
210213

211214
// Random awesome
212215
self.manager.register(
@@ -216,7 +219,7 @@ function init ({lastSeen}) {
216219
const awesome = 7;
217220
const chanceOfBeingAwesome = TBHelpers.getRandomNumber(10000);
218221

219-
self.log(`You rolled a: ${chanceOfBeingAwesome}`);
222+
log.debug(`You rolled a: ${chanceOfBeingAwesome}`);
220223
if (awesome === chanceOfBeingAwesome) {
221224
self.manager.unlock(saveIndex);
222225
}
@@ -233,10 +236,10 @@ function init ({lastSeen}) {
233236
const now = TBHelpers.getTime();
234237
const timeSince = now - lastSeen;
235238
const daysSince = TBHelpers.millisecondsToDays(timeSince);
236-
self.log(`daysSince: ${daysSince}`);
239+
log.debug(`daysSince: ${daysSince}`);
237240

238241
if (daysSince >= 7) {
239-
// self.log("you've got an award!");
242+
// log.debug("you've got an award!");
240243
self.manager.unlock(saveIndex);
241244
}
242245

@@ -278,7 +281,7 @@ function init ({lastSeen}) {
278281
}
279282
// TODO: wait for 'yes' click.
280283
// $body.on('click', '.yes', function(){
281-
// self.log('yes clicked');
284+
// log.debug('yes clicked');
282285
// });
283286
});
284287
});

extension/data/modules/betterbuttons.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import * as TBApi from '../tbapi.ts';
44
import * as TBCore from '../tbcore.js';
55
import {Module} from '../tbmodule.jsx';
66
import {actionButton} from '../tbui.js';
7+
import createLogger from '../util/logging.ts';
78
import {getSettingAsync} from '../util/settings.ts';
89

9-
const self = new Module({
10+
const log = createLogger('BButtons');
11+
12+
export default new Module({
1013
name: 'Better Buttons',
1114
id: 'BButtons',
1215
enabledByDefault: true,
@@ -106,7 +109,6 @@ const self = new Module({
106109
initCommentLock();
107110
}
108111
});
109-
export default self;
110112

111113
// Bread and buttons
112114
const $body = $('body');
@@ -116,7 +118,7 @@ function initModSave () {
116118
if (TBCore.isModmail) {
117119
return;
118120
}
119-
self.log('Adding mod save buttons');
121+
log.debug('Adding mod save buttons');
120122

121123
// Watches for changes in the DOM
122124
let shouldSticky = false;
@@ -127,7 +129,7 @@ function initModSave () {
127129
const $item = $(mutation.addedNodes[i]);
128130
// Check if the added element is a comment
129131
if ($item.is('div.comment')) {
130-
self.log('Clicking distinguish button');
132+
log.debug('Clicking distinguish button');
131133
// Distinguish the comment, stickying if we need to
132134
const things = $item.find('form[action="/post/distinguish"] > .option > a');
133135
if (shouldSticky) {
@@ -163,7 +165,7 @@ function initModSave () {
163165

164166
// Add actions to the mod save buttons
165167
$('body').on('click', 'button.save-mod', function () {
166-
self.log('Mod save clicked!');
168+
log.debug('Mod save clicked!');
167169
commentObserver.observe(document.body, {
168170
childList: true,
169171
subtree: true,
@@ -173,7 +175,7 @@ function initModSave () {
173175
$(this).closest('.usertext-buttons').find('button.save').click();
174176
});
175177
$('body').on('click', 'button.save-sticky', function () {
176-
self.log('Mod save + sticky clicked!');
178+
log.debug('Mod save + sticky clicked!');
177179
commentObserver.observe(document.body, {
178180
childList: true,
179181
subtree: true,
@@ -231,7 +233,7 @@ function initDistinguishToggle () {
231233
// User initiated click, this is the distinguish toggle on a top level comment
232234

233235
if (Object.prototype.hasOwnProperty.call(e, 'originalEvent')) {
234-
self.log('Top level comment distinguish has been clicked and it is the real deal!');
236+
log.debug('Top level comment distinguish has been clicked and it is the real deal!');
235237

236238
// Comment is already distinguished or stickied. So we'll simply undistinguish
237239
if (distinguished) {
@@ -256,7 +258,7 @@ function initDistinguishToggle () {
256258
// Otherwise the event is missing the origionalEvent property, meaning it was a code induced click.
257259
// In this case we want to sticky (or unsticky)
258260
} else {
259-
self.log('Top level comment distinguish has been clicked by a robot!');
261+
log.debug('Top level comment distinguish has been clicked by a robot!');
260262
// Really simple, only possible when nothing is distinguished or stickied.
261263
if (secondDistinguishButton) {
262264
secondDistinguishButton.click();
@@ -286,7 +288,7 @@ function initDistinguishToggle () {
286288
$siblingDistinguish.find('form[action="/post/distinguish"]').click();
287289
}
288290

289-
self.log('Adding distinguish toggle events');
291+
log.debug('Adding distinguish toggle events');
290292

291293
// Add distinguish button listeners
292294
$body.on('click', 'form[action="/post/distinguish"]', distinguishClicked);
@@ -317,7 +319,7 @@ function initDistinguishToggle () {
317319
}
318320

319321
function initRemoveConfirmation () {
320-
self.log('Adding one-click remove events');
322+
log.debug('Adding one-click remove events');
321323

322324
// Approve
323325
$body.on('click', '.flat-list .approve-button .togglebutton', function () {
@@ -348,10 +350,10 @@ function initRemoveConfirmation () {
348350
}
349351

350352
function initAutoApprove () {
351-
self.log('Adding ignore reports toggle events');
353+
log.debug('Adding ignore reports toggle events');
352354

353355
$body.on('click', '.big-mod-buttons > .pretty-button.neutral', function () {
354-
self.log('Ignore reports pressed');
356+
log.debug('Ignore reports pressed');
355357
const $button = $(this).parent().find('> span > .positive');
356358
const button = $button[0];
357359
if (!$button.hasClass('pressed')) {
@@ -363,7 +365,7 @@ function initAutoApprove () {
363365
}
364366

365367
function initAutoIgnoreReports () {
366-
self.log('Adding approve toggle events');
368+
log.debug('Adding approve toggle events');
367369

368370
$body.on('click', '.big-mod-buttons > span > .pretty-button.positive', function () {
369371
const $button = $(this).closest('.big-mod-buttons').find('> .neutral');
@@ -536,7 +538,7 @@ function initCommentLock () {
536538
$lockButton.attr('tb-action', newAction);
537539
$lockButton.text(newAction);
538540
} catch (error) {
539-
self.error('Error toggling lock on comment:\n', error);
541+
log.error('Error toggling lock on comment:\n', error);
540542
}
541543
});
542544

extension/data/modules/comment.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import TBListener from '../tblistener.js';
77
import {Module} from '../tbmodule.jsx';
88
import * as TBui from '../tbui.js';
99
import {icons} from '../util/icons.ts';
10+
import createLogger from '../util/logging.ts';
1011
import {purifyObject} from '../util/purify.js';
1112

1213
import {modbarExists} from './modbar.js';
1314

15+
const log = createLogger('Comments');
16+
1417
const self = new Module({
1518
name: 'Comments',
1619
id: 'Comments',
@@ -96,7 +99,7 @@ self.initOldReddit = async function ({hideRemoved, approveComments, spamRemoved,
9699
removedCounter += 1;
97100
});
98101

99-
self.log(removedCounter);
102+
log.debug(removedCounter);
100103

101104
if ($('#tb-bottombar').find('#tb-toggle-removed').length) {
102105
const $tbToggle = $('#tb-bottombar').find('#tb-toggle-removed');
@@ -201,15 +204,15 @@ self.initOldReddit = async function ({hideRemoved, approveComments, spamRemoved,
201204
if (TBCore.isUserPage) {
202205
const $modActions = $('.moderator, [data-subreddit="spam"]');
203206
if ($modActions.length > 0) {
204-
self.log('found mod actions');
207+
log.debug('found mod actions');
205208

206209
if ($('.tb-hide-mod-comments').length < 1) {
207210
$('.menuarea').append(
208211
'&nbsp;&nbsp;<a href="javascript:;" name="hideModComments" class="tb-hide-mod-comments tb-general-button">hide mod actions</a>',
209212
);
210213

211214
$body.on('click', '.tb-hide-mod-comments', function () {
212-
self.log('hiding mod actions');
215+
log.debug('hiding mod actions');
213216
hidden = true;
214217
$modActions.closest('.thing').hide();
215218
$(this).hide();
@@ -224,7 +227,7 @@ self.initOldReddit = async function ({hideRemoved, approveComments, spamRemoved,
224227
window.addEventListener('TBNewThings', () => {
225228
addHideModButton();
226229
if (hidden) {
227-
self.log('hiding mod actions');
230+
log.debug('hiding mod actions');
228231
$('.moderator, [data-subreddit="spam"]').closest('.thing').hide();
229232
}
230233
});
@@ -239,7 +242,7 @@ self.initOldReddit = async function ({hideRemoved, approveComments, spamRemoved,
239242
);
240243

241244
$body.on('click', '.tb-hide-old', () => {
242-
self.log('hiding old comments');
245+
log.debug('hiding old comments');
243246
$('.entry').show(); // reset before hiding.
244247
$('.old-expand').removeClass('old-expand'); // new old expands
245248

@@ -403,12 +406,12 @@ function init ({
403406
const $htmlCommentView = $body.find('#tb-sitetable'); // This will contain the new html we will add to the page.
404407

405408
$body.find('.tb-flatview-search-input').keyup(() => {
406-
self.log('typing');
409+
log.debug('typing');
407410
const FlatViewSearchName = $body.find('#tb-flatview-search-name').val();
408411
const FlatViewSearchContent = $body.find('#tb-flatview-search-content').val();
409412

410-
self.log(FlatViewSearchName);
411-
self.log(FlatViewSearchContent);
413+
log.debug(FlatViewSearchName);
414+
log.debug(FlatViewSearchContent);
412415

413416
$htmlCommentView.find('.tb-comment').each(function () {
414417
const $this = $(this);
@@ -486,10 +489,10 @@ function init ({
486489
});
487490
}
488491

489-
self.log('openContextInPopup enabled.');
492+
log.debug('openContextInPopup enabled.');
490493

491494
$body.on('click', '.tb-comment-context-popup', function (event) {
492-
self.log('Context button clicked.');
495+
log.debug('Context button clicked.');
493496

494497
const $this = $(this);
495498

0 commit comments

Comments
 (0)