Skip to content

Commit

Permalink
[main] Use new tp-api log message redirection option to direct all me…
Browse files Browse the repository at this point in the history
…ssages to internal logger; Removes stdout redirection workaround.
  • Loading branch information
mpaperno committed Sep 11, 2023
1 parent 4b69a8d commit 0a65d4b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
3 changes: 2 additions & 1 deletion base/plugin-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"plugin" : "INFO",
"icon" : "INFO",
"imgcache" : "INFO",
"logging" : "WARNING"
"logging" : "WARNING",
"tpclient" : "ERROR"
},
"endpoints": {
"Console": { "minLevel": "NONE" },
Expand Down
33 changes: 21 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Point, PointType, Size } from './modules/geometry';
import DynamicIcon from "./modules/DynamicIcon";
import * as m_el from "./modules/elements";
import { default as g_globalImageCache, ImageCache } from './modules/ImageCache'
import { ConsoleEndpoint, Logger, logging /* , LogLevel */ } from './modules/logging';
import { ConsoleEndpoint, Logger, logging , LogLevel } from './modules/logging';
import { setTPClient, PluginSettings } from './common'
import { dirname as pdirname, resolve as presolve } from 'path';
const { version: pluginVersion } = require('../package.json'); // 'import' causes lint error in VSCode
Expand All @@ -27,6 +27,14 @@ const CONFIG_FILEPATH = presolve(EXEC_BASE_PATH, "plugin-config.json");
// If there's a better x-platform way to find TPs config path, then fixme.
const DEFAULT_IMAGE_FILE_BASE_PATH = presolve(EXEC_BASE_PATH, '..', '..');

// Translate TPClient log level strings to our LogLevel enum.
const TPClientLogLevel = {
"ERROR": LogLevel.ERROR,
"WARN" : LogLevel.WARNING,
"INFO" : LogLevel.INFO,
"DEBUG": LogLevel.DEBUG,
}


// -------------------------------
// Logging
Expand All @@ -43,16 +51,9 @@ if (!logging().haveEndpoints) {
if (!logging().configuration.endpoints?.Console)
logging().registerEndpoint(ConsoleEndpoint.instance())
}
else if (!logging().haveEndpointName('Console')) {
// This is a (hopefully temporary) hack to direct all TPClient's stdout logging to our own logger (presumably a file).
// We can only do this if we're not logging to the console/stdout ourselves, since that would cause a fun endless loop.
// The simplistic `writeTpLog()` assumes each chunk of data will be a full line of text, which basically works because Console doesn't flush the
// output until it sees a newline anyway (typically). A more proper way would be to buffer the data as it comes in and take out full line(s)
// once we have them, but that currently seems like overkill for the few simple messages TPClient logs.
// This also just treats all messages as errors vs. parsing out the actual level (client should only be logging errors anyway).
const writeTpLog = (chunk: any): boolean => { logger.error(chunk.trim()); return true; }
process.stdout.write = writeTpLog;
}

// Init a logger specifically for TPClient messages so they can be filtered by level.
const tpLogger: Logger = logging().getLogger('tpclient');


// -------------------------------
Expand All @@ -68,7 +69,9 @@ var g_quitting: boolean = false;
ImageCache.cacheOptions.baseImagePath = DEFAULT_IMAGE_FILE_BASE_PATH;

// Create Touch Portal API client
const TPClient = new TP.Client();
const TPClient = new TP.Client({
logCallback: tpClientLogCallback
});
// share the TP client with other modules
setTPClient(TPClient);

Expand All @@ -88,6 +91,12 @@ function quit(reason: string, exitCode: number = 0) {
setTimeout(() => { process.exit(exitCode); }, 50)
}

// Direct TPClient log messages to our own logger instance.
function tpClientLogCallback(level: string, message?: any, ...args: any[]) {
const lvl: LogLevel = TPClientLogLevel[level] || LogLevel.INFO;
tpLogger.log(lvl, message, ...args);
}

// This is used for actions which update existing layers in an icon.
// Checks if action data contains valid "_index" field and returns its value in 'index' if (0 < value < currentLen);
// Otherwise returns the currentLen input value. The 'dataValid' member indicates if the index field was present and valid.
Expand Down

0 comments on commit 0a65d4b

Please sign in to comment.