UNPKG

microvium

Version:

A compact, embeddable scripting engine for microcontrollers for executing small scripts written in a subset of JavaScript.

122 lines (121 loc) 4.91 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TraceFile = void 0; /* A TraceFile is used for debug output. Writes to the file are batched for efficiency, but if the global `TraceFile.flushAll` _property_ is accessed, the getter performs a flush synchronously. To use this module as intended, add `TraceFile.flushAll` as a watch in the debugger. Then any time the debugger steps or hits a breakpoint, the contents of all the TraceFiles will be flushed to disk (e.g. to be visible in the IDE if they're open). */ const fs = __importStar(require("fs")); const path = __importStar(require("path")); class TraceFile { constructor(filename, automaticFlushDelayMs = 1000) { this.automaticFlushDelayMs = automaticFlushDelayMs; this.buffer = new Array(); // Keeping track of the global constructor because if this module is // included multiple times then we need to make sure all instances share the // same TraceFile "all" set, and that we remove ourselves (in dispose) from // the correct instance. this.globalConstructor = globalThis.TraceFile; this.globalConstructor.all.add(this); this.filename = path.resolve(filename); // Wipe file fs.writeFileSync(this.filename, ''); this.nextFlushThreshold = Date.now() + this.automaticFlushDelayMs; } // Hint: add `TraceFile.flushAll` to the debug watch list to automatically flush on breakpoints static get flushAll() { for (const file of TraceFile.all) { file.flush(); } return new Date().toString(); } dispose() { this.flush(); this.globalConstructor.all.delete(this); } // Hint: add `TraceFile.flushAll` to the debug watch list to automatically flush on breakpoints flush() { if (!this.dumpContent && this.buffer.length === 0) { return; } let toFlush = this.buffer; this.buffer = []; if (this.dumpContent) { toFlush.unshift(this.dumpContent()); this.dumpContent = undefined; fs.writeFileSync(this.filename, toFlush.join('')); } else { fs.appendFileSync(this.filename, toFlush.join('')); } this.nextFlushThreshold = Date.now() + 1000; } checkFlush() { if (Date.now() >= this.nextFlushThreshold) { this.flush(); } } // Hint: add `TraceFile.flushAll` to the debug watch list to automatically flush on breakpoints // (Accessible as an instance method because the class name may be inaccessible to the watch list in some contexts) get flushAll() { return TraceFile.flushAll; } // Append content to the file write(content) { this.buffer.push(content); this.checkFlush(); } // Append a line of content to the file writeLine(line) { this.write(line + '\n'); } // Wipes the file and replaces its content with `content`. // Note: the callback will only be invoked at the time that it flushes. This // is to improve efficiency when you need to logically dump at high frequency // but only need it to show up when you hit a breakpoint (or occasionally). dump(content, ...state) { if (typeof content === 'string') { const str = content; content = () => str; } if (state.length > 0) { const inner = content; content = () => inner(...state); } this.buffer = []; this.dumpContent = content; this.checkFlush(); } } exports.TraceFile = TraceFile; TraceFile.all = new Set(); globalThis.TraceFile = TraceFile; //# sourceMappingURL=trace-file.js.map