-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
253 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { reactive } from 'vue' | ||
import type { LogMessage, LoggerInterface } from 'zeed' | ||
import { LogLevelAll, LoggerContext, LoggerMemoryHandler } from 'zeed' | ||
|
||
export type LogOui = LoggerInterface & { | ||
messages: LogMessage[] | ||
} | ||
|
||
export function useLog(name: string): LogOui { | ||
const messages: LogMessage[] = reactive([]) | ||
const logger = LoggerContext() | ||
logger.setHandlers([ | ||
LoggerMemoryHandler({ | ||
level: LogLevelAll, | ||
filter: '*', | ||
messages, | ||
}), | ||
]) | ||
const log = logger(name) as any | ||
log.messages = messages | ||
return log | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<script lang="ts" setup> | ||
import { useIntervalFn, useTimeoutFn } from '@vueuse/core' | ||
import { uuid } from 'zeed' | ||
import OuiText from './oui-text.vue' | ||
import { OuiLog, useLog } from '@/lib' | ||
const log = useLog('test') | ||
log('Hello World') | ||
log.info('Info') | ||
log.warn('Warning') | ||
log.error('Error') | ||
log('Some binary data', new Uint8Array([1, 2, 3, 99, 100, 101])) | ||
useIntervalFn(() => { | ||
log(`interval ${uuid()}`) | ||
}, 1000) | ||
</script> | ||
|
||
<template> | ||
<OuiText> | ||
<h2>Virtual List</h2> | ||
<div> | ||
<OuiLog :log="log" /> | ||
</div> | ||
</OuiText> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@require "../../stylus/index.styl"; | ||
|
||
.oui-log { | ||
border: 1px solid var(--s2-fg); | ||
border-radius: 4; | ||
font-family: "Jetbrains Mono", monospace; | ||
font-size: 12; | ||
line-height: 1.2; | ||
height: 200; | ||
|
||
._active { | ||
background: var(--p1-200) !important; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<script lang="ts" setup> | ||
import { ref, watch } from 'vue' | ||
import type { LogMessage } from 'zeed' | ||
import { Uint8ArrayToHexDump, browserSelectColorByName, formatMilliseconds, isArray, isString, logMessageFromCompact } from 'zeed' | ||
import type { OuiTableColumn } from './_types' | ||
import type { LogOui } from './log' | ||
import OuiTableview from './oui-tableview.vue' | ||
import './oui-log.styl' | ||
const props = defineProps<{ | ||
log: LogOui | ||
}>() | ||
const selected = ref<number>() | ||
interface RenderMessagesOptions { | ||
trace?: boolean // = true | ||
pretty?: boolean // = true | ||
} | ||
function formatMessages( | ||
messages: any[], | ||
opt: RenderMessagesOptions = {}, | ||
): any[] { | ||
const { trace = true, pretty = true } = opt | ||
return messages.map((obj) => { | ||
if (obj && typeof obj === 'object') { | ||
if (pretty && (obj instanceof Uint8Array || obj instanceof ArrayBuffer)) | ||
return `\n${Uint8ArrayToHexDump(obj)}\n` | ||
if (obj instanceof Error) { | ||
if (!trace) | ||
return `${obj.name || 'Error'}: ${obj.message}` | ||
return `${obj.name || 'Error'}: ${obj.message}\n${obj.stack}` | ||
} | ||
return obj | ||
} | ||
return String(obj) | ||
}) | ||
} | ||
function renderMessages( | ||
messages: any[], | ||
opt: RenderMessagesOptions = {}, | ||
): any[] { | ||
return formatMessages(messages, opt) | ||
} | ||
function handlerLogInfo(msg: LogMessage) { | ||
const name = msg.name || '' | ||
const color = browserSelectColorByName(name) | ||
const message = renderMessages(isArray(msg.messages) ? msg.messages : [msg.messages], { pretty: true }) | ||
// const message = msg.messages | ||
return { | ||
...msg, | ||
name, | ||
color, | ||
background: { | ||
1: '#d4f4ff', | ||
2: '#ffedbd', | ||
3: '#ffaab0', | ||
4: '#ffbbe9', | ||
}[msg.level] ?? 'transparent', | ||
message, | ||
} | ||
} | ||
const logs = ref<any>([]) | ||
function updateLogs() { | ||
const ll: LogMessage[] = [...props.log.messages] ?? [] | ||
const first = ll[0] | ||
const compact = isArray(first) | ||
let timestamp = (compact ? first[0] : first?.timestamp) ?? 0 | ||
// const date = new Date(timestamp).toLocaleString() | ||
logs.value = ll.map((_info: any) => { | ||
const info = compact ? logMessageFromCompact(_info) : _info | ||
const { name, message, color, background } = handlerLogInfo(info) | ||
const diff = Math.abs(timestamp - (info.timestamp ?? 0)) | ||
timestamp = (info.timestamp ?? 0) | ||
const timeDiffString = diff && `+${formatMilliseconds(diff)}` | ||
const datetime = new Date(info.timestamp).toLocaleString('de') | ||
return { | ||
...info, | ||
diff, | ||
datetime, | ||
timeDiffString, | ||
name, | ||
message, | ||
color, | ||
background, | ||
} | ||
}) | ||
} | ||
watch(() => props.log.messages.length, updateLogs) | ||
updateLogs() | ||
const columns: OuiTableColumn[] = [ | ||
{ title: 'Time', name: 'time', sortable: false, width: 96, align: 'right' }, | ||
// { title: 'Tag', name: 'tag', sortable: false }, | ||
{ title: 'Message', name: 'message', sortable: false }, | ||
] | ||
// const selectedItem = computed(() => list.value[selected.value ?? -1]) | ||
</script> | ||
|
||
<template> | ||
<OuiTableview | ||
v-model="selected" | ||
class="oui-log" | ||
name="log-table" | ||
selectable | ||
:data="logs" | ||
:columns="columns" | ||
:row-height="23" | ||
:row-attrs="(item: any) => ({ style: `background: ${item.background}` })" | ||
:header="false" | ||
:scroll-to-end="true" | ||
:resizeable="false" | ||
> | ||
<template #col-time="{ item }"> | ||
<span style="color: #888; white-space:pre"> | ||
{{ item.diff ? item.timeDiffString : '' }} | ||
</span> | ||
</template> | ||
<template #col-tag="{ item }"> | ||
<span :style="{ color: item.color, whiteSpace: 'pre', fontWeight: 600 }"> | ||
{{ item.name }} | ||
</span> | ||
</template> | ||
<template #col-message="{ item }"> | ||
<span> | ||
<template v-for="(o, i) in item.message" :key="o"> | ||
<template v-if="isString(o)"> | ||
<b v-if="i % 2 !== 0" style="font-weight:600;">{{ o.slice(0, 255) }}</b> | ||
<span v-else>{{ o.slice(0, 255) }}</span> | ||
</template> | ||
<template v-else> | ||
<!-- <b>{{ JSON.stringify(o) }}</b> --> | ||
<span class="code"><{{ o?.__class ?? typeof o }}></span> | ||
<!-- <OuiObject :value="o" /> --> | ||
</template> | ||
{{ ' ' }} | ||
</template> | ||
</span> | ||
</template> | ||
</OuiTableview> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters