Skip to content

Commit

Permalink
feat: add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
chenfan0 committed Sep 26, 2024
1 parent ee191e5 commit 5003752
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/main/crawler/capture-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ const log = debug('fideo-crawler-capture-error')
*/
export function captureError(fn) {
return async function (...args) {
const writeLog = args[args.length - 1]
const realArgs = args.slice(0, args.length - 1)
try {
return await fn.apply(this, args)
const res = await fn.apply(this, realArgs)
writeLog(`Fetch Live Res: ${JSON.stringify(res, null, 2)}`)
return res
} catch (e) {
const message = e.message

writeLog(`Fetch Live Error: ${message}`)

log('error:', message)

// if (message.includes('timeout')) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/crawler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const supportPlatform = [
* @param {{ url: string, proxy?: string, cookie?: string }} info
* @returns {Promise<{code: number, liveUrls?: string[]}>}
*/
export async function getLiveUrls(info) {
export async function getLiveUrls(info, writeLog) {
const { roomUrl, proxy, cookie } = info
let host
try {
Expand All @@ -112,7 +112,7 @@ export async function getLiveUrls(info) {
}
}

const res = await getLiveUrlsFn(roomUrl, { proxy, cookie })
const res = await getLiveUrlsFn(roomUrl, { proxy, cookie }, writeLog)
log('res:', res)
return res
}
32 changes: 25 additions & 7 deletions src/main/ffmpeg/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const checkFileExist = async (filepath: string) => {
})
}

async function convertFlvToMp4(sourcePath: string) {
async function convertFlvToMp4(sourcePath: string, writeLog: (content: string) => void) {
if (!(await checkFileExist(sourcePath))) return
const process = ffmpeg()
const output = sourcePath.replace(FLV_FLAG, '.mp4')
Expand All @@ -96,19 +96,28 @@ async function convertFlvToMp4(sourcePath: string) {
.audioCodec('copy')
.inputFormat('flv')
.outputFormat('mp4')
.on('start', () => {
writeLog(`Convert Flv To Mp4 Start: ${sourcePath}`)
})
.on('end', () => {
fs.unlinkSync(sourcePath)
writeLog(`Convert Flv To Mp4 Success: ${sourcePath}`)
_resolve()
})
.on('error', () => {
writeLog(`Convert Flv To Mp4 Error: ${sourcePath}`)
_resolve()
})
.save(output)

return p
}

async function convert(sourcePath: string, convertToMP4 = true) {
async function convert(
sourcePath: string,
writeLog: (content: string) => void,
convertToMP4 = true
) {
if (!convertToMP4) return
if (!(await checkFileExist(sourcePath))) return
const stats = fs.statSync(sourcePath)
Expand All @@ -122,10 +131,10 @@ async function convert(sourcePath: string, convertToMP4 = true) {
.map((file) => path.join(sourcePath, file))

for (const flvFile of flvFiles) {
await convertFlvToMp4(flvFile)
await convertFlvToMp4(flvFile, writeLog)
}
} else {
await convertFlvToMp4(sourcePath)
await convertFlvToMp4(sourcePath, writeLog)
}
}

Expand Down Expand Up @@ -195,6 +204,7 @@ async function detectStreamResolution(streamConfig: IStreamConfig) {

export async function recordStream(
streamConfig: IStreamConfig,
writeLog: (title: string, content: string) => void,
cb?: (code: number, errMsg?: string) => void
) {
log('start record stream')
Expand Down Expand Up @@ -224,6 +234,8 @@ export async function recordStream(
detectResolution
} = streamConfig

writeLog(title, `RecordStream Config: ${JSON.stringify(streamConfig, null, 2)}`)

detectResolution && detectStreamResolution(streamConfig)

const secondSegmentTime = Number(segmentTime) * 60
Expand All @@ -239,6 +251,7 @@ export async function recordStream(
}

if (recordStreamFfmpegProcessMap[title] !== RECORD_DUMMY_PROCESS) {
writeLog(title, 'Record Stream is Killed')
_resolve({
code: FFMPEG_ERROR_CODE.USER_KILL_PROCESS
})
Expand Down Expand Up @@ -278,6 +291,9 @@ export async function recordStream(
.audioCodec('copy')
.on('start', (...args) => {
log('record live start', args.join(' '))

writeLog(title, `Record Live Start: ${args.join(' ')}`)

_resolve({
code: SUCCESS_CODE
})
Expand All @@ -290,17 +306,19 @@ export async function recordStream(
const msg = args.join(' ')

log('record live end: ', msg)
writeLog(title, `Record Live End: ${msg}`)

killRecordStreamFfmpegProcess(title)

cb?.(SUCCESS_CODE)
await convert(convertSource, convertToMP4)
await convert(convertSource, writeLog.bind(null, title), convertToMP4)
cb?.(SUCCESS_CODE)
})
.on('error', async (error) => {
const errMsg = error.message
log('record live error: ', errMsg)

log('record live error: ', errMsg)
writeLog(title, `Record Live Error: ${errMsg}`)
const isResolutionChange = resolutionChangeSet.has(title)
// 清空数据
killRecordStreamFfmpegProcess(title)
Expand All @@ -319,7 +337,7 @@ export async function recordStream(
}

cb?.(errCode, errMsg)
await convert(convertSource, convertToMP4)
await convert(convertSource, writeLog.bind(null, title), convertToMP4)
cb?.(errCode, errMsg)
})
.save(output + FLV_FLAG)
Expand Down
12 changes: 10 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ import {
downloadReq
} from './ffmpeg'

import { writeLogWrapper } from './log/index'

export const writeLog = writeLogWrapper(app.getPath('userData'))

async function checkUpdate() {
try {
const json = await fetch(
Expand Down Expand Up @@ -152,7 +156,6 @@ function showNotification(title: string, body: string) {

async function handleMakeSureDependenciesExist() {
const userDataPath = app.getPath('userData')
console.log('userDataPath:', userDataPath)
const [isFFmpegExist, isFfprobeExist] = await Promise.all([
checkFfmpegExist(userDataPath),
checkFfprobeExist(userDataPath)
Expand Down Expand Up @@ -216,7 +219,11 @@ app.whenReady().then(async () => {
*/
setRecordStreamFfmpegProcessMap(title, RECORD_DUMMY_PROCESS)

const { code: liveUrlsCode, liveUrls } = await getLiveUrls({ roomUrl, proxy, cookie })
const { code: liveUrlsCode, liveUrls } = await getLiveUrls(
{ roomUrl, proxy, cookie },
writeLog.bind(null, title)
)

if (liveUrlsCode !== SUCCESS_CODE) {
return {
code: liveUrlsCode
Expand All @@ -226,6 +233,7 @@ app.whenReady().then(async () => {

const { code: recordStreamCode } = await recordStream(
streamConfig,
writeLog,
(code: number, errMsg?: string) => {
win?.webContents.send(STREAM_RECORD_END, title, code, errMsg)
clearTimerWhenAllFfmpegProcessEnd()
Expand Down
29 changes: 29 additions & 0 deletions src/main/log/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fsp from 'node:fs/promises'
import path from 'node:path'
import dayjs from 'dayjs'

export function writeLogWrapper(baseDir: string) {
const logPrefixPath = path.resolve(baseDir, 'logs')
console.log('logPrefixPath:', logPrefixPath)
return async (title: string, content: string) => {
const logPath = path.resolve(logPrefixPath, title)
return fsp
.mkdir(logPath, { recursive: true })
.catch((e) => {
console.log(e)
})
.then(() => {
const logFilePath = path.resolve(logPath, `${dayjs().format('YYYY-MM-DD')}.txt`)
content = `[${dayjs().format('YYYY-MM-DD HH:mm:ss')}] ${content}`
fsp
.readFile(logFilePath, 'utf8')
.then((existingContent) => {
const prefix = existingContent ? '\n' : 'Fideo LOG: \n'
return fsp.appendFile(logFilePath, `${prefix}${content}`)
})
.catch(() => {
return fsp.writeFile(logFilePath, `Fideo LOG: \n${content}`)
})
})
}
}

0 comments on commit 5003752

Please sign in to comment.