Skip to content

Commit

Permalink
Use log with CONFIG toggle for debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
caewok committed Jan 22, 2024
1 parent 33c3611 commit 4ea68f2
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
5 changes: 3 additions & 2 deletions scripts/Ruler.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import {
tokenIsSnapped,
iterateGridUnderLine,
squareGridShape,
hexGridShape } from "./util.js";
hexGridShape,
log } from "./util.js";

/**
* Modified Ruler
Expand Down Expand Up @@ -497,7 +498,7 @@ function _onMouseUp(wrapped, event) {
*/
function _getMovementToken(wrapped) {
if ( !this.waypoints.length ) {
// console.debug("Waypoints length 0");
log("Waypoints length 0");
return undefined;
}

Expand Down
5 changes: 5 additions & 0 deletions scripts/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ Hooks.once("init", function() {
// Cannot access localization until init.
PREFER_TOKEN_CONTROL.title = game.i18n.localize(PREFER_TOKEN_CONTROL.title);
registerGeometry();

// Configuration
CONFIG[MODULE_ID] = { debug: false };


game.modules.get(MODULE_ID).api = {
iterateGridUnderLine,
PATCHER,
Expand Down
3 changes: 0 additions & 3 deletions scripts/pathfinding/Wall.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ PATCHES.PATHFINDING = {};

// When canvas is ready, the existing walls are not created, so must re-do here.
Hooks.on("canvasReady", async function() {
// console.debug(`outerBounds: ${canvas.walls.outerBounds.length}`);
// console.debug(`innerBounds: ${canvas.walls.innerBounds.length}`);

const t0 = performance.now();
SCENE_GRAPH.clear();
const walls = [
Expand Down
10 changes: 5 additions & 5 deletions scripts/segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SPEED, MODULES_ACTIVE, MODULE_ID } from "./const.js";
import { Settings } from "./settings.js";
import { Ray3d } from "./geometry/3d/Ray3d.js";
import { Point3d } from "./geometry/3d/Point3d.js";
import { perpendicularPoints } from "./util.js";
import { perpendicularPoints, log } from "./util.js";
import { Pathfinder } from "./pathfinding/pathfinding.js";

/**
Expand Down Expand Up @@ -68,7 +68,7 @@ export function _getMeasurementSegments(wrapped) {
const t2 = performance.now();
const newSegments = constructPathfindingSegments(segments, segmentMap);
const t3 = performance.now();
//console.debug(`${newSegments.length} segments processed in ${t3-t2} ms.`);
log(`${newSegments.length} segments processed in ${t3-t2} ms.`);
return newSegments;
}

Expand All @@ -88,17 +88,17 @@ function calculatePathPointsForSegment(segment, token) {
const path = pf.runPath(A, B);
let pathPoints = Pathfinder.getPathPoints(path);
const t1 = performance.now();
//console.debug(`Found ${pathPoints.length} path points between ${A.x},${A.y} -> ${B.x},${B.y} in ${t1 - t0} ms.`);
log(`Found ${pathPoints.length} path points between ${A.x},${A.y} -> ${B.x},${B.y} in ${t1 - t0} ms.`);

// Clean the path
const t2 = performance.now();
pathPoints = Pathfinder.cleanPath(pathPoints);
const t3 = performance.now();
//console.debug(`Cleaned to ${pathPoints?.length} path points between ${A.x},${A.y} -> ${B.x},${B.y} in ${t3 - t2} ms.`);
log(`Cleaned to ${pathPoints?.length} path points between ${A.x},${A.y} -> ${B.x},${B.y} in ${t3 - t2} ms.`);

// If less than 3 points after cleaning, just use the original segment.
if ( pathPoints.length < 2 ) {
//console.debug(`Only ${pathPoints.length} path points found.`, [...pathPoints]);
log(`Only ${pathPoints.length} path points found.`, [...pathPoints]);
return [];
}

Expand Down
5 changes: 3 additions & 2 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ canvas

import { MODULE_ID, MODULES_ACTIVE, SPEED } from "./const.js";
import { ModuleSettingsAbstract } from "./ModuleSettingsAbstract.js";
import { log } from "./util.js";

const SETTINGS = {
CONTROLS: {
Expand Down Expand Up @@ -252,15 +253,15 @@ function toggleTokenRulerWaypoint(context, add = true) {
const position = canvas.mousePosition;
const ruler = canvas.controls.ruler;
if ( !canvas.tokens.active || !ruler || !ruler.active ) return;
// console.debug(`${add ? "add" : "remove"}TokenRulerWaypoint`);
log(`${add ? "add" : "remove"}TokenRulerWaypoint`);

// Keep track of when we last added/deleted a waypoint.
const now = Date.now();
const delta = now - MOVE_TIME;
if ( delta < 100 ) return true; // Throttle keyboard movement once per 100ms
MOVE_TIME = now;

// console.debug(`${add ? "adding" : "removing"}TokenRulerWaypoint`);
log(`${add ? "adding" : "removing"}TokenRulerWaypoint`);
if ( add ) ruler._addWaypoint(position);
else if ( ruler.waypoints.length > 1 ) ruler._removeWaypoint(position); // Removing the last waypoint throws errors.
}
Expand Down
4 changes: 1 addition & 3 deletions scripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import { MODULE_ID } from "./const.js";

export function log(...args) {
try {
const isDebugging = game.modules.get("_dev-mode")?.api?.getPackageDebugValue(MODULE_ID);
if (isDebugging) console.log(MODULE_ID, "|", ...args);

if ( CONFIG[MODULE_ID].debug ) console.debug(MODULE_ID, "|", ...args);
} catch(e) {
// Empty
}
Expand Down

0 comments on commit 4ea68f2

Please sign in to comment.