buddhism/utils/debug.js

79 lines
1.5 KiB
JavaScript
Raw Normal View History

2025-07-30 10:45:26 +08:00
/**
* 调试工具函数
* 提供安全的日志输出方法避免真机调试时的文件系统问题
*/
// 是否启用调试模式
2025-08-14 11:22:53 +08:00
const DEBUG_MODE = false
2025-07-30 10:45:26 +08:00
/**
* 安全的日志输出
* @param {string} message - 日志消息
* @param {string} type - 日志类型 (log, warn, error)
*/
export function safeLog(message, type = 'log') {
2025-08-14 11:22:53 +08:00
if (!DEBUG_MODE) return
2025-07-30 10:45:26 +08:00
try {
switch (type) {
case 'warn':
2025-08-14 11:22:53 +08:00
console.warn(message)
break
2025-07-30 10:45:26 +08:00
case 'error':
2025-08-14 11:22:53 +08:00
console.error(message)
break
2025-07-30 10:45:26 +08:00
default:
2025-08-14 11:22:53 +08:00
console.log(message)
2025-07-30 10:45:26 +08:00
}
} catch (e) {
// 如果console输出失败使用uni.showToast
uni.showToast({
title: typeof message === 'string' ? message : '调试信息',
icon: 'none',
2025-08-14 11:22:53 +08:00
duration: 2000,
})
2025-07-30 10:45:26 +08:00
}
}
/**
* 安全的错误处理
* @param {Error} error - 错误对象
* @param {string} context - 错误上下文
*/
export function safeError(error, context = '') {
2025-08-14 11:22:53 +08:00
if (!DEBUG_MODE) return
2025-07-30 10:45:26 +08:00
try {
2025-08-14 11:22:53 +08:00
console.error(`[${context}]`, error)
2025-07-30 10:45:26 +08:00
} catch (e) {
uni.showToast({
title: `错误: ${context}`,
2025-08-14 11:22:53 +08:00
icon: 'none',
})
2025-07-30 10:45:26 +08:00
}
}
/**
* 调试信息输出
* @param {string} message - 调试信息
*/
export function debug(message) {
2025-08-14 11:22:53 +08:00
safeLog(message, 'log')
2025-07-30 10:45:26 +08:00
}
/**
* 警告信息输出
* @param {string} message - 警告信息
*/
export function warn(message) {
2025-08-14 11:22:53 +08:00
safeLog(message, 'warn')
2025-07-30 10:45:26 +08:00
}
/**
* 错误信息输出
* @param {string} message - 错误信息
*/
export function error(message) {
2025-08-14 11:22:53 +08:00
safeLog(message, 'error')
}