mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 05:56:46 +08:00

主要更改: - 重构Log.go和Output.go为模块化架构 - 创建独立的logging和output模块 - 新增LevelBaseInfoSuccess默认日志级别(显示BASE、INFO、SUCCESS) - 添加运行时间显示到每条日志前面 - 保持完全向后兼容的API接口 - 支持多种输出格式(TXT、JSON、CSV) - 优化日志格式化和颜色显示 技术改进: - 模块化设计便于扩展和维护 - 智能时间格式化(毫秒→秒→分钟→小时) - 支持缓冲和批量输出 - 线程安全的并发处理
183 lines
4.9 KiB
Go
183 lines
4.9 KiB
Go
package Common
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/fatih/color"
|
|
"github.com/shadow1ng/fscan/Common/logging"
|
|
)
|
|
|
|
// 全局变量定义(保持向后兼容)
|
|
var (
|
|
// 扫描状态管理器,记录最近一次成功和错误的时间
|
|
status = logging.NewScanStatus()
|
|
|
|
// Num 表示待处理的总任务数量
|
|
Num int64
|
|
// End 表示已经完成的任务数量
|
|
End int64
|
|
|
|
// StartTime 开始时间(保持原有行为)
|
|
StartTime = time.Now()
|
|
)
|
|
|
|
// LogEntry 定义单条日志的结构(向后兼容)
|
|
type LogEntry = logging.LogEntry
|
|
|
|
// ScanStatus 用于记录和管理扫描状态的结构体(向后兼容)
|
|
type ScanStatus = logging.ScanStatus
|
|
|
|
// 定义系统支持的日志级别常量(向后兼容)
|
|
const (
|
|
LogLevelAll = string(logging.LevelAll)
|
|
LogLevelError = string(logging.LevelError)
|
|
LogLevelBase = string(logging.LevelBase)
|
|
LogLevelInfo = string(logging.LevelInfo)
|
|
LogLevelSuccess = string(logging.LevelSuccess)
|
|
LogLevelDebug = string(logging.LevelDebug)
|
|
LogLevelInfoSuccess = string(logging.LevelInfoSuccess)
|
|
LogLevelBaseInfoSuccess = string(logging.LevelBaseInfoSuccess)
|
|
)
|
|
|
|
// 全局日志管理器
|
|
var (
|
|
globalLogger *logging.Logger
|
|
loggerOnce sync.Once
|
|
)
|
|
|
|
// getGlobalLogger 获取全局日志管理器
|
|
func getGlobalLogger() *logging.Logger {
|
|
loggerOnce.Do(func() {
|
|
config := &logging.LoggerConfig{
|
|
Level: logging.LevelBaseInfoSuccess,
|
|
EnableColor: !NoColor,
|
|
SlowOutput: SlowLogOutput,
|
|
ShowProgress: true,
|
|
StartTime: StartTime,
|
|
LevelColors: map[logging.LogLevel]color.Attribute{
|
|
logging.LevelError: color.FgBlue, // 错误日志显示蓝色
|
|
logging.LevelBase: color.FgYellow, // 基础日志显示黄色
|
|
logging.LevelInfo: color.FgGreen, // 信息日志显示绿色
|
|
logging.LevelSuccess: color.FgRed, // 成功日志显示红色
|
|
logging.LevelDebug: color.FgWhite, // 调试日志显示白色
|
|
},
|
|
}
|
|
|
|
globalLogger = logging.NewLogger(config)
|
|
|
|
// 设置进度条(如果存在)
|
|
if ProgressBar != nil {
|
|
globalLogger.SetProgressBar(ProgressBar)
|
|
}
|
|
|
|
// 设置输出互斥锁
|
|
globalLogger.SetOutputMutex(&OutputMutex)
|
|
|
|
// 使用全局扫描状态
|
|
status = globalLogger.GetScanStatus()
|
|
})
|
|
return globalLogger
|
|
}
|
|
|
|
// InitLogger 初始化日志系统(保持原接口)
|
|
func InitLogger() {
|
|
// 禁用标准日志输出
|
|
log.SetOutput(io.Discard)
|
|
|
|
// 初始化全局日志管理器
|
|
getGlobalLogger().Initialize()
|
|
}
|
|
|
|
// SetLoggerConfig 设置日志配置
|
|
func SetLoggerConfig(enableColor, slowOutput bool, progressBar ProgressDisplay) {
|
|
config := &logging.LoggerConfig{
|
|
Level: logging.LevelBaseInfoSuccess,
|
|
EnableColor: enableColor,
|
|
SlowOutput: slowOutput,
|
|
ShowProgress: true,
|
|
StartTime: StartTime,
|
|
LevelColors: map[logging.LogLevel]color.Attribute{
|
|
logging.LevelError: color.FgBlue,
|
|
logging.LevelBase: color.FgYellow,
|
|
logging.LevelInfo: color.FgGreen,
|
|
logging.LevelSuccess: color.FgRed,
|
|
logging.LevelDebug: color.FgWhite,
|
|
},
|
|
}
|
|
|
|
newLogger := logging.NewLogger(config)
|
|
if progressBar != nil {
|
|
newLogger.SetProgressBar(progressBar)
|
|
}
|
|
newLogger.SetOutputMutex(&OutputMutex)
|
|
|
|
// 更新全局日志管理器
|
|
globalLogger = newLogger
|
|
status = newLogger.GetScanStatus()
|
|
}
|
|
|
|
// ProgressDisplay 进度条显示接口(向后兼容)
|
|
type ProgressDisplay = logging.ProgressDisplay
|
|
|
|
// LogDebug 记录调试日志(保持原接口)
|
|
func LogDebug(msg string) {
|
|
getGlobalLogger().Debug(msg)
|
|
}
|
|
|
|
// LogBase 记录进度信息(保持原接口)
|
|
func LogBase(msg string) {
|
|
getGlobalLogger().Base(msg)
|
|
}
|
|
|
|
// LogInfo 记录信息日志(保持原接口)
|
|
func LogInfo(msg string) {
|
|
getGlobalLogger().Info(msg)
|
|
}
|
|
|
|
// LogSuccess 记录成功日志(保持原接口)
|
|
func LogSuccess(result string) {
|
|
getGlobalLogger().Success(result)
|
|
}
|
|
|
|
// LogError 记录错误日志(保持原接口)
|
|
func LogError(errMsg string) {
|
|
getGlobalLogger().Error(errMsg)
|
|
}
|
|
|
|
// CheckErrs 检查是否为需要重试的错误(保持原接口)
|
|
func CheckErrs(err error) error {
|
|
return logging.CheckErrs(err)
|
|
}
|
|
|
|
// GetScanStatus 获取扫描状态(新增接口)
|
|
func GetScanStatus() *logging.ScanStatus {
|
|
return status
|
|
}
|
|
|
|
// UpdateScanProgress 更新扫描进度(新增接口)
|
|
func UpdateScanProgress(completed, total int64) {
|
|
status.SetCompleted(completed)
|
|
status.SetTotal(total)
|
|
|
|
// 更新全局变量(保持向后兼容)
|
|
End = completed
|
|
Num = total
|
|
}
|
|
|
|
// SetProgressBar 设置进度条(新增接口)
|
|
func SetProgressBar(progressBar ProgressDisplay) {
|
|
if globalLogger != nil {
|
|
globalLogger.SetProgressBar(progressBar)
|
|
}
|
|
}
|
|
|
|
// 兼容性别名,保持原有的使用方式
|
|
var (
|
|
// formatLogMessage 保持向后兼容(但不对外暴露实现)
|
|
// printLog 保持向后兼容(但不对外暴露实现)
|
|
// handleLog 保持向后兼容(但不对外暴露实现)
|
|
// clearAndWaitProgress 保持向后兼容(但不对外暴露实现)
|
|
) |