-
Notifications
You must be signed in to change notification settings - Fork 6
Logging Using Application Debug
Sciumo edited this page Oct 26, 2022
·
2 revisions
For use with a virtual list or equivalent.
TODO - log type support, files, etc.
import { Application, LogCallback, LogType } from "UnityEngine";
import { h} from "preact"
import { signal } from "preact/signals";
import { Style } from "preact/jsx";
export interface LogEntry {
ts: Date
message: string
stack?:string
type: LogType
}
export const LOG = signal<LogEntry[]>([]);
export var LOGCB: LogCallback = (logString:string,stackTrace:string,type:LogType) => {
LOG.value.unshift({ts:new Date(), message:logString,stack:stackTrace, type:type });
}
export function EnableLog() {
Application.add_logMessageReceived(LOGCB);
}
export function DisableLog() {
Application.remove_logMessageReceived(LOGCB);
}
export interface LogRowProps {
index: number
row: LogEntry
}
export function zeroPad2(n:number) {
if( n < 10 ) {
return "0" + n;
}
return "" + n;
}
export function formatTime( ts:Date ) {
return "[" + ts.getHours() + ":" + zeroPad2(ts.getMinutes()) + ":" + zeroPad2(ts.getSeconds()) + "]";
}
export var LOGEVENSTYLE:Style[] = [
{backgroundColor:"white"},
{backgroundColor:"#ccffcc"} ]
export var FORMATTIME = formatTime;
export var LogRow = (props:LogRowProps) => {
var style = LOGEVENSTYLE[props.index%2];
var log = props.row;
return <div style={style}>
{FORMATTIME(log.ts) + " " + log.message }
</div>
}