Files
featherVoice/QXLive/Manager/QXLogger.h
2025-12-22 10:16:05 +08:00

115 lines
3.8 KiB
Objective-C

//
// QXLogger.h
// QXLive
//
// Created by 启星 on 2025/12/18.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSUInteger, QXLogLevel) {
QXLogLevelVerbose = 0,
QXLogLevelDebug,
QXLogLevelInfo,
QXLogLevelWarning,
QXLogLevelError,
QXLogLevelFatal,
};
typedef NS_OPTIONS(NSUInteger, QXLogTarget) {
QXLogTargetConsole = 1 << 0,
QXLogTargetFile = 1 << 1,
QXLogTargetNetwork = 1 << 2,
QXLogTargetAll = 0xFF
};
/**
日志配置
*/
@interface QXLogConfig : NSObject
+ (instancetype)sharedConfig;
@property (nonatomic, assign) QXLogLevel minLogLevel;
@property (nonatomic, assign) QXLogTarget logTarget;
@property (nonatomic, assign) NSUInteger maxLogFileSize;
@property (nonatomic, assign) NSUInteger maxLogFileCount;
@property (nonatomic, assign) NSUInteger logSaveDays;
@property (nonatomic, assign) BOOL enableNetworkUpload;
@property (nonatomic, copy, nullable) NSString *uploadURL;
@property (nonatomic, assign) BOOL enableConsoleColor;
@property (nonatomic, assign) BOOL addDateToFile;
@property (nonatomic, copy, nullable) NSString *logPrefix;
@property (nonatomic, assign) BOOL enableAsync;
@property (nonatomic, assign) BOOL encryptLogFile;
@end
/**
日志工具类
*/
@interface QXLogger : NSObject
+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
#pragma mark - 配置
+ (QXLogConfig *)config;
+ (void)updateConfig:(void (^)(QXLogConfig *config))block;
#pragma mark - 基础日志方法
+ (void)verbose:(NSString *)format, ... NS_FORMAT_FUNCTION(1, 2);
+ (void)verboseWithTag:(NSString *)tag format:(NSString *)format, ... NS_FORMAT_FUNCTION(2, 3);
+ (void)debug:(NSString *)format, ... NS_FORMAT_FUNCTION(1, 2);
+ (void)debugWithTag:(NSString *)tag format:(NSString *)format, ... NS_FORMAT_FUNCTION(2, 3);
+ (void)info:(NSString *)format, ... NS_FORMAT_FUNCTION(1, 2);
+ (void)infoWithTag:(NSString *)tag format:(NSString *)format, ... NS_FORMAT_FUNCTION(2, 3);
+ (void)warning:(NSString *)format, ... NS_FORMAT_FUNCTION(1, 2);
+ (void)warningWithTag:(NSString *)tag format:(NSString *)format, ... NS_FORMAT_FUNCTION(2, 3);
+ (void)error:(NSString *)format, ... NS_FORMAT_FUNCTION(1, 2);
+ (void)errorWithTag:(NSString *)tag format:(NSString *)format, ... NS_FORMAT_FUNCTION(2, 3);
+ (void)fatal:(NSString *)format, ... NS_FORMAT_FUNCTION(1, 2);
+ (void)fatalWithTag:(NSString *)tag format:(NSString *)format, ... NS_FORMAT_FUNCTION(2, 3);
#pragma mark - 特殊日志方法
+ (void)network:(NSString *)method url:(NSString *)url params:(nullable id)params response:(nullable id)response error:(nullable NSError *)error;
+ (void)performance:(NSString *)tag startTime:(CFAbsoluteTime)startTime;
+ (void)userAction:(NSString *)action params:(nullable NSDictionary *)params;
+ (void)crash:(NSException *)exception;
+ (void)memoryWarning;
#pragma mark - 文件操作
+ (NSArray<NSString *> *)getAllLogFilePaths;
+ (nullable NSString *)getLatestLogFilePath;
+ (nullable NSString *)getLogContentFromFile:(NSString *)filePath;
+ (void)cleanExpiredLogs;
+ (void)cleanAllLogFiles;
#pragma mark - 文件大小管理
+ (unsigned long long)getCurrentLogFileSize;
+ (CGFloat)getCurrentLogFileSizeMB;
+ (CGFloat)getTotalLogSizeMB;
+ (NSDictionary *)getLogFileInfo:(NSString *)filePath;
+ (NSArray<NSDictionary *> *)getAllLogFileInfos;
+ (void)checkFileSizeManually;
#pragma mark - 网络上报
+ (void)uploadLogsWithCompletion:(void (^)(BOOL success, NSError * _Nullable error))completion;
+ (void)setUploadHandler:(void (^)(NSArray<NSString *> *logFilePaths, void (^complete)(BOOL success)))handler;
#pragma mark - 调试工具
+ (void)enable:(BOOL)enabled;
+ (BOOL)isEnabled;
+ (void)addCustomLogHandler:(void (^)(QXLogLevel level, NSString *tag, NSString *message))handler;
#pragma mark - 初始化
+ (void)setupLogger;
@end
NS_ASSUME_NONNULL_END