Skip to content

Commit

Permalink
add log exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Jun 6, 2024
1 parent 84ee594 commit ed75bfe
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 101 deletions.
89 changes: 0 additions & 89 deletions dashboard/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
},
"dependencies": {
"@types/pako": "^2.0.3",
"@vueuse/core": "^10.9.0",
"axios": "^1.6.2",
"chart.js": "^4.4.1",
"js-sha256": "^0.11.0",
Expand Down
9 changes: 3 additions & 6 deletions dashboard/src/api/v0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ export async function getLogFiles(token: string): Promise<FileInfo[]> {
}

export async function getLogFile(token: string, name: string, noEncrypt?: boolean): Promise<ArrayBuffer> {
const LOGFILE_URL = `${window.location.origin}/api/v0/log_file`
const u = new URL(name, LOGFILE_URL)
const LOGFILE_URL = `${window.location.origin}/api/v0/log_file/`
const u = new URL(name.startsWith('/') ? name.substr(1) : name, LOGFILE_URL)
if (noEncrypt) {
u.searchParams.set('no_encrypt', '1')
}
Expand All @@ -429,10 +429,7 @@ export async function getLogFile(token: string, name: string, noEncrypt?: boolea

export async function getLogFileURL(token: string, name: string, noEncrypt?: boolean): Promise<string> {
const LOGFILE_URL = `${window.location.origin}/api/v0/log_file/`
if (name.startsWith('/')) {
name = name.substr(1)
}
const u = new URL(name, LOGFILE_URL)
const u = new URL(name.startsWith('/') ? name.substr(1) : name, LOGFILE_URL)
if (noEncrypt) {
u.searchParams.set('no_encrypt', '1')
}
Expand Down
3 changes: 3 additions & 0 deletions dashboard/src/assets/lang/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"i18n": "Internationalization",
"language": "Language",

"debugs_and_logs": "Debugs & Logs",
"logs": "Logs",

"notification": "Notification",
"notify": {
"when.disabled": "Notify when disabled",
Expand Down
3 changes: 3 additions & 0 deletions dashboard/src/assets/lang/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"i18n": "国际化",
"language": "语言",

"debugs_and_logs": "调试 & 日志",
"logs": "日志",

"notification": "推送",
"notify": {
"when.disabled": "下线时发送通知",
Expand Down
79 changes: 79 additions & 0 deletions dashboard/src/components/FileListCard.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<script setup lang="ts">
import Card from 'primevue/card'
import type { FileInfo } from '@/api/v0'
import { formatNumber, formatBytes } from '@/utils'
defineProps<{
name: string
files: FileInfo[] | null
}>()
defineEmits<{
(e: 'click', file: FileInfo, index: number): void,
}>()
</script>
<template>
<Card>
<template #title>
<div class="flex-row-center">
<lable>{{ name }}</lable>
</div>
</template>
<template #content>
<template v-if="!files">
<i>Loading...</i>
</template>
<template v-else>
<div class="file-list-box">
<div v-for="(file, i) in files" v-key="file.name" class="file-elem">
<div class="file-name" @click="$emit('click', file, i)">
{{file.name}}
</div>
<div class="flex-row-center">
<div class="file-size">{{formatBytes(file.size)}}</div>
<slot :index="i" :file="file" />
</div>
</div>
</div>
</template>
</template>
</Card>
</template>
<style scoped>
.file-list-box {
display: flex;
flex-direction: column;
width: 100%;
}
.file-elem {
display: flex;
flex-direction: row;
justify-content: space-between;
width: 100%;
padding: 0.5rem 0.2rem;
border-bottom: var(--surface-border) 1px solid;
}
.file-name {
width: 10rem;
padding-right: 0.3rem;
font-weight: bold;
cursor: pointer;
user-select: all;
}
.file-name:hover {
color: var(--surface-900);
text-decoration: underline;
}
.file-size {
width: 5rem;
margin-right: 0.3rem;
font-weight: 200;
user-select: none;
}
</style>
5 changes: 5 additions & 0 deletions dashboard/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const router = createRouter({
name: 'settings',
component: () => import('@/views/SettingsView.vue'),
},
{
path: '/loglist',
name: 'loglist',
component: () => import('@/views/LogListView.vue'),
},
{
path: '/settings/notifications',
name: 'settings/notifications',
Expand Down
1 change: 0 additions & 1 deletion dashboard/src/views/HomeView.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script setup lang="ts">
import { onMounted, ref, computed, watch, inject, type Ref } from 'vue'
import { computedAsync } from '@vueuse/core'
import { RouterLink } from 'vue-router'
import { useRequest } from 'vue-request'
import Button from 'primevue/button'
Expand Down
60 changes: 60 additions & 0 deletions dashboard/src/views/LogListView.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<script setup lang="ts">
import { ref, inject, onMounted, type Ref } from 'vue'
import Button from 'primevue/button'
import { useToast } from 'primevue/usetoast'
import FileListCard from '@/components/FileListCard.vue'
import {
getLogFiles,
getLogFile,
getLogFileURL,
type FileInfo,
} from '@/api/v0'
import { tr } from '@/lang'
const toast = useToast()
const token = inject('token') as Ref<string | null>
const logList = ref<FileInfo[] | null>(null)
async function refreshFileList(): Promise<void> {
if (!token.value) {
return
}
logList.value = await getLogFiles(token.value)
}
async function openFile(file: FileInfo): Promise<void> {
if (!token.value) {
return
}
const buf = await getLogFile(token.value, file.name, true)
console.log(buf)
alert('TODO: display log file content')
}
async function downloadLogFile(file: FileInfo): Promise<void> {
if (!token.value) {
return
}
const u = await getLogFileURL(token.value, file.name, false)
window.open(u)
}
onMounted(() => {
refreshFileList()
})
</script>
<template>
<div>
<h1>
{{ tr('title.logs') }}
</h1>
<FileListCard class="filelist-card" :name="'logs/'" :files="logList" v-slot="{ index, file }" @click="openFile">
<Button icon="pi pi-file-export" @click="downloadLogFile(file)"/>
</FileListCard>
</div>
</template>
<style scoped>
.filelist-card {
}
</style>
14 changes: 14 additions & 0 deletions dashboard/src/views/SettingsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ onMounted(() => {
</div>
</template>
</Card>
<Card class="settings-group">
<template #title>
<div class="flex-row-center settings-group-title">
<lable>{{ tr('title.debugs_and_logs') }}</lable>
</div>
</template>
<template #content>
<div class="flex-row-center settings-elem">
<RouterLink to="/loglist" style="color: inherit">
{{ tr('title.logs') }}
</RouterLink>
</div>
</template>
</Card>
<Card class="settings-group">
<template #title>
<div class="flex-row-center settings-group-title">
Expand Down
9 changes: 5 additions & 4 deletions scripts/decrypt-log.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ func main() {
func createNewFile(name string) (fd *os.File, err error) {
basename, _, _ := strings.Cut(name, ".")
fd, err = os.OpenFile(basename+".log", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
if !errors.Is(err, os.ErrExist) {
return
}
if err == nil {
return
}
if !errors.Is(err, os.ErrExist) {
return
}
for i := 2; i < 100; i++ {
fd, err = os.OpenFile(fmt.Sprintf("%s.%d.log", basename, i), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
Expand Down

0 comments on commit ed75bfe

Please sign in to comment.