Echo
Echo provides screen output functionality — printing formatted text to HTML elements or custom output handlers. It maintains an internal stream that captures all printed content, useful for debug pages and test output.
Package: io-oidis-commons
import { Echo } from "@io-oidis-commons/.../Utils/Echo.js";
Initialization
Echo.Init($targetIdOrHandler? : string | IOHandler, $forceInit? : boolean) : void
Initialize Echo with an output target. If called without arguments, it defaults to an HTML element with id="Content":
// Default — targets element with id="Content"
Echo.Init();
// Target a specific HTML element by ID
Echo.Init("outputPanel");
// Use a custom IOHandler
Echo.Init(myCustomHandler);
// Force re-initialization (overwrite existing handler)
Echo.Init("newTarget", true);
In most Oidis test pages and debug views, an element with id="Content" exists by convention, so calling Echo.Init() without arguments works out of the box.
Printing Methods
Echo.Print($message : string, $clearBeforePrint? : boolean) : void
Echo.Println($message : string) : void
Echo.Printf($messageOrObject : string | any, ...$parameters : any[]) : void
Echo.PrintCode($message : string) : void
// Print raw text
Echo.Print("Hello");
// Print and clear previous content first
Echo.Print("Fresh start", true);
// Print with newline
Echo.Println("Line 1");
Echo.Println("Line 2");
// Printf — formatted output with positional placeholders
Echo.Printf("User {0} has {1} items", userName, count);
// Printf — object inspection (auto-converts non-string to readable output)
Echo.Printf(complexObject);
// Print code block (HTML-escaped, in <pre> tags)
Echo.PrintCode("<div>This shows as escaped HTML</div>");
Printf Formatting
Printf is the most versatile print method. It accepts either:
- A string with
{0},{1}, ... placeholders (usesStringUtils.Formatinternally) - Any object — automatically converted to a readable string representation
// String formatting
Echo.Printf("Status: {0}, Items: {1}, Total: {2}", status, items.length, total);
// Object inspection (useful for debugging)
Echo.Printf(response.data);
Echo.Printf({ action: "save", records: modified.length });
Screen Management
Echo.Clear() : void // Clear screen content (keep stream)
Echo.ClearAll() : void // Clear screen content AND stream
Echo.Println("Step 1 complete");
Echo.Println("Step 2 complete");
Echo.Clear(); // Screen is empty, but stream still has both lines
Echo.ClearAll(); // Screen is empty AND stream is reset
Stream Capture
Echo.getStream() : string
Returns the complete content stream since the last ClearAll(). Useful for capturing test output or building reports:
Echo.ClearAll();
Echo.Println("Test 1: PASS");
Echo.Println("Test 2: PASS");
Echo.Println("Test 3: FAIL");
const results = Echo.getStream();
// → "Test 1: PASS\nTest 2: PASS\nTest 3: FAIL\n"
Event Callback
Echo.setOnPrint($handler : ($message : string) => void) : void
Set a callback that fires every time a message is printed. Useful for forwarding output to additional targets:
Echo.setOnPrint(($message) => {
LogIt.Info("Echo output:", $message);
// Or forward to a WebSocket, file, etc.
});
Handler Type
Echo.getHandlerType() : IOHandlerType
Returns the type of the currently active output handler (HTML element, custom handler, etc.).
When to Use Echo
| Scenario | Use Echo | Use LogIt |
|---|---|---|
| Debug/test page output | Yes | No — LogIt goes to console |
| Visible on-screen results | Yes | No |
| Development debugging | No | Yes |
| Production error logging | No | Yes |
| Capturing output as string | Yes (getStream) | No |
Echo is for visible screen output — debug pages, test result displays, and interactive demos. For development debugging that goes to the console/terminal, use LogIt. For persistent audit trails, use SystemLog.
Complete Method Reference
| Method | Description |
|---|---|
Init($target?, $force?) | Initialize with element ID or IOHandler |
getHandlerType() | Current output handler type |
Print($message, $clear?) | Print raw text |
Println($message) | Print with newline |
Printf($msgOrObj, ...$params) | Formatted print or object inspection |
PrintCode($message) | Print in <pre> with escaped HTML |
Clear() | Clear screen (keep stream) |
ClearAll() | Clear screen and stream |
getStream() | Get captured content stream |
setOnPrint($handler) | Set print event callback |
See also: Utilities Overview | Logging | StringUtils | Convert