Skip to content

Commit

Permalink
Simplify logging utils (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
eritbh authored Feb 3, 2025
1 parent 4076595 commit d054fc4
Show file tree
Hide file tree
Showing 27 changed files with 365 additions and 345 deletions.
8 changes: 4 additions & 4 deletions extension/data/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ import * as TBApi from './tbapi';
import * as TBCore from './tbcore.js';
import {delay} from './tbhelpers.js';
import TBListener from './tblistener.js';
import TBLog from './tblog';
import TBModule from './tbmodule.jsx';
import {getCache, setCache} from './util/cache';
import {documentInteractive} from './util/dom';
import createLogger from './util/logging';
import {isUserLoggedInQuick} from './util/platform';
import {getSettingAsync, setSettingAsync, updateSettings} from './util/settings';
import {reactRenderer} from './util/ui_interop';
Expand Down Expand Up @@ -296,13 +296,13 @@ async function doSettingsUpdates () {
}

// Create a logger
const logger = TBLog('Init');
const log = createLogger('Init');

// Ensure that other conditions are met, and return early if not
try {
await checkLoadConditions();
} catch (error) {
logger.error('Load condition not met:', error);
log.error('Load condition not met:', error);
return;
}

Expand Down Expand Up @@ -392,7 +392,7 @@ async function doSettingsUpdates () {
OldReddit,
]
) {
logger.debug('Registering module', m);
log.debug('Registering module', m);
TBModule.register_module(m);
}

Expand Down
35 changes: 19 additions & 16 deletions extension/data/modules/achievements.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import $ from 'jquery';
import * as TBCore from '../tbcore.js';
import * as TBHelpers from '../tbhelpers.js';
import {Module} from '../tbmodule.jsx';
import createLogger from '../util/logging.ts';
import {getSettingAsync} from '../util/settings.ts';

const log = createLogger('Achievements');

const self = new Module({
name: 'Achievements',
id: 'Achievements',
Expand Down Expand Up @@ -65,12 +68,12 @@ function Manager () {
const title = titles[i];
const maxValue = maxValues[i];

self.log('Registering Achievement');
log.debug('Registering Achievement');
if (debugMode) {
self.log(` name=${title}`);
log.debug(` name=${title}`);
} // spoilers
self.log(` maxValue=${maxValue}`);
self.log(` saveIndex=${saveIndex}`);
log.debug(` maxValue=${maxValue}`);
log.debug(` saveIndex=${saveIndex}`);

achievementsBlock.push({
title,
Expand All @@ -89,19 +92,19 @@ function Manager () {
if (value === undefined) {
value = 1;
}
self.log(`Unlocking achievement block: index=${saveIndex}, value=${value}`);
log.debug(`Unlocking achievement block: index=${saveIndex}, value=${value}`);

const old = saves[saveIndex];
self.log(` Old value: ${saves[saveIndex]}`);
log.debug(` Old value: ${saves[saveIndex]}`);
saves[saveIndex] += value;
self.log(` New value: ${saves[saveIndex]}`);
log.debug(` New value: ${saves[saveIndex]}`);

const achievementsBlock = achievements[saveIndex];
let achievement;
for (let index = 0; index < achievementsBlock.length; index++) {
self.log(` Checking achievement ${index}`);
log.debug(` Checking achievement ${index}`);
achievement = achievementsBlock[index];
self.log(` Comparing to max value: ${achievement.maxValue}`);
log.debug(` Comparing to max value: ${achievement.maxValue}`);
if (saves[saveIndex] >= achievement.maxValue && old < achievement.maxValue) {
let title = achievement.title;

Expand All @@ -111,10 +114,10 @@ function Manager () {
try {
title = $(achievement.title).text() || achievement.title;
} catch (e) {
self.log(`error: ${e}`);
log.debug(`error: ${e}`);
}

self.log(`${title} Unlocked!`);
log.debug(`${title} Unlocked!`);
TBCore.notification(
'Mod achievement unlocked!',
title,
Expand Down Expand Up @@ -206,7 +209,7 @@ function init ({lastSeen}) {
const $body = $('body');

// Achievement definitions
self.log('Registering achievements');
log.debug('Registering achievements');

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

self.log(`You rolled a: ${chanceOfBeingAwesome}`);
log.debug(`You rolled a: ${chanceOfBeingAwesome}`);
if (awesome === chanceOfBeingAwesome) {
self.manager.unlock(saveIndex);
}
Expand All @@ -233,10 +236,10 @@ function init ({lastSeen}) {
const now = TBHelpers.getTime();
const timeSince = now - lastSeen;
const daysSince = TBHelpers.millisecondsToDays(timeSince);
self.log(`daysSince: ${daysSince}`);
log.debug(`daysSince: ${daysSince}`);

if (daysSince >= 7) {
// self.log("you've got an award!");
// log.debug("you've got an award!");
self.manager.unlock(saveIndex);
}

Expand Down Expand Up @@ -278,7 +281,7 @@ function init ({lastSeen}) {
}
// TODO: wait for 'yes' click.
// $body.on('click', '.yes', function(){
// self.log('yes clicked');
// log.debug('yes clicked');
// });
});
});
Expand Down
30 changes: 16 additions & 14 deletions extension/data/modules/betterbuttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import * as TBApi from '../tbapi.ts';
import * as TBCore from '../tbcore.js';
import {Module} from '../tbmodule.jsx';
import {actionButton} from '../tbui.js';
import createLogger from '../util/logging.ts';
import {getSettingAsync} from '../util/settings.ts';

const self = new Module({
const log = createLogger('BButtons');

export default new Module({
name: 'Better Buttons',
id: 'BButtons',
enabledByDefault: true,
Expand Down Expand Up @@ -106,7 +109,6 @@ const self = new Module({
initCommentLock();
}
});
export default self;

// Bread and buttons
const $body = $('body');
Expand All @@ -116,7 +118,7 @@ function initModSave () {
if (TBCore.isModmail) {
return;
}
self.log('Adding mod save buttons');
log.debug('Adding mod save buttons');

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

// Add actions to the mod save buttons
$('body').on('click', 'button.save-mod', function () {
self.log('Mod save clicked!');
log.debug('Mod save clicked!');
commentObserver.observe(document.body, {
childList: true,
subtree: true,
Expand All @@ -173,7 +175,7 @@ function initModSave () {
$(this).closest('.usertext-buttons').find('button.save').click();
});
$('body').on('click', 'button.save-sticky', function () {
self.log('Mod save + sticky clicked!');
log.debug('Mod save + sticky clicked!');
commentObserver.observe(document.body, {
childList: true,
subtree: true,
Expand Down Expand Up @@ -231,7 +233,7 @@ function initDistinguishToggle () {
// User initiated click, this is the distinguish toggle on a top level comment

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

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

self.log('Adding distinguish toggle events');
log.debug('Adding distinguish toggle events');

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

function initRemoveConfirmation () {
self.log('Adding one-click remove events');
log.debug('Adding one-click remove events');

// Approve
$body.on('click', '.flat-list .approve-button .togglebutton', function () {
Expand Down Expand Up @@ -348,10 +350,10 @@ function initRemoveConfirmation () {
}

function initAutoApprove () {
self.log('Adding ignore reports toggle events');
log.debug('Adding ignore reports toggle events');

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

function initAutoIgnoreReports () {
self.log('Adding approve toggle events');
log.debug('Adding approve toggle events');

$body.on('click', '.big-mod-buttons > span > .pretty-button.positive', function () {
const $button = $(this).closest('.big-mod-buttons').find('> .neutral');
Expand Down Expand Up @@ -536,7 +538,7 @@ function initCommentLock () {
$lockButton.attr('tb-action', newAction);
$lockButton.text(newAction);
} catch (error) {
self.error('Error toggling lock on comment:\n', error);
log.error('Error toggling lock on comment:\n', error);
}
});

Expand Down
23 changes: 13 additions & 10 deletions extension/data/modules/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import TBListener from '../tblistener.js';
import {Module} from '../tbmodule.jsx';
import * as TBui from '../tbui.js';
import {icons} from '../util/icons.ts';
import createLogger from '../util/logging.ts';
import {purifyObject} from '../util/purify.js';

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

const log = createLogger('Comments');

const self = new Module({
name: 'Comments',
id: 'Comments',
Expand Down Expand Up @@ -96,7 +99,7 @@ self.initOldReddit = async function ({hideRemoved, approveComments, spamRemoved,
removedCounter += 1;
});

self.log(removedCounter);
log.debug(removedCounter);

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

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

$body.on('click', '.tb-hide-mod-comments', function () {
self.log('hiding mod actions');
log.debug('hiding mod actions');
hidden = true;
$modActions.closest('.thing').hide();
$(this).hide();
Expand All @@ -224,7 +227,7 @@ self.initOldReddit = async function ({hideRemoved, approveComments, spamRemoved,
window.addEventListener('TBNewThings', () => {
addHideModButton();
if (hidden) {
self.log('hiding mod actions');
log.debug('hiding mod actions');
$('.moderator, [data-subreddit="spam"]').closest('.thing').hide();
}
});
Expand All @@ -239,7 +242,7 @@ self.initOldReddit = async function ({hideRemoved, approveComments, spamRemoved,
);

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

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

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

self.log(FlatViewSearchName);
self.log(FlatViewSearchContent);
log.debug(FlatViewSearchName);
log.debug(FlatViewSearchContent);

$htmlCommentView.find('.tb-comment').each(function () {
const $this = $(this);
Expand Down Expand Up @@ -486,10 +489,10 @@ function init ({
});
}

self.log('openContextInPopup enabled.');
log.debug('openContextInPopup enabled.');

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

const $this = $(this);

Expand Down
Loading

0 comments on commit d054fc4

Please sign in to comment.