sdk添加日志保存功能
authorliangzhenying <867174124@qq.com>
Fri, 27 Dec 2024 03:38:49 +0000 (11:38 +0800)
committerliangzhenying <867174124@qq.com>
Fri, 27 Dec 2024 03:38:49 +0000 (11:38 +0800)
12 files changed:
MouseSdk/AiMouseSdk/ByteDance/ByteDanceSdk.m
MouseSdk/AiMouseSdk/Manager/AiDeviceAuthManager.m
MouseSdk/AiMouseSdk/Manager/BleManager.m
MouseSdk/AiMouseSdk/Manager/DeviceManger.h
MouseSdk/AiMouseSdk/Manager/DeviceManger.m
MouseSdk/AiMouseSdk/Manager/LogManager.h
MouseSdk/AiMouseSdk/Manager/LogManager.m
MouseSdk/AiMouseSdk/TlwMouseSdk/TlwTools.m
MouseSdk/MouseSdk.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/IDEFindNavigatorScopes.plist [new file with mode: 0644]
MouseSdk/MouseSdk.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate
MouseSdk/MouseSdk/ViewController.m
MouseSdk/Pods/Pods.xcodeproj/xcuserdata/yaozuopan.xcuserdatad/xcschemes/xcschememanagement.plist

index 1d436d9..0060307 100644 (file)
@@ -33,7 +33,7 @@
 }
 
 - (void)initSdk{
-    [[LogManager shared] addLog:@"开始初始化"];
+    [LogManager log:@"开始初始化"];
     stb_app_printf_str_register(cus_printf_str);
 //    NSString *appId = @"tis_test";
     NSString *appId = @"app_10dyx2ai21r2qkar";
@@ -113,7 +113,7 @@ void STB_STDCALL cus_printf_str(char* str)
 {
     dispatch_async(dispatch_get_main_queue(), ^{
         NSString *logStr = [NSString stringWithFormat:@"%s", str];
-        [[LogManager shared] addLog:logStr];
+        [LogManager log:logStr];
     });
     
 
@@ -122,23 +122,23 @@ static void cust_event_callback_func(uint32_t type,uint8_t *report, uint32_t dat
     dispatch_async(dispatch_get_main_queue(), ^{
         switch (type) {
             case STB_DYNREG_SUCCESS:
-                [[LogManager shared] addLog:@"STB_DYNREG_SUCCESS"];
+                [LogManager log:@"STB_DYNREG_SUCCESS"];
                 break;
             case STB_DYNREG_FAIL:
-                [[LogManager shared] addLog:@"STB_DYNREG_FAIL"];
+                [LogManager log:@"STB_DYNREG_FAIL"];
                 break;
             case STB_MQTT_CONNECT_SUCCESS:
-                [[LogManager shared] addLog:@"STB_MQTT_CONNECT_SUCCESS"];
+                [LogManager log:@"STB_MQTT_CONNECT_SUCCESS"];
                             stb_app_get_token_async();
                 break;
             case STB_MQTT_CONNECT_FAIL:
-                [[LogManager shared] addLog:@"STB_MQTT_CONNECT_FAIL"];
+                [LogManager log:@"STB_MQTT_CONNECT_FAIL"];
                 break;
             case STB_MQTT_CONNECT_OFFLINE:
-                [[LogManager shared] addLog:@"STB_MQTT_CONNECT_OFFLINE"];
+                [LogManager log:@"STB_MQTT_CONNECT_OFFLINE"];
                 break;
             case STB_MQTT_CONNECT_ONLINE:
-                [[LogManager shared] addLog:@"STB_MQTT_CONNECT_ONLINE"];
+                [LogManager log:@"STB_MQTT_CONNECT_ONLINE"];
                 
                 break;
             case STB_DM_DATA_ARRIVED_TOKEN:
@@ -146,7 +146,7 @@ static void cust_event_callback_func(uint32_t type,uint8_t *report, uint32_t dat
                 //token
                 NSData *data = [[NSData alloc] initWithBytes:report length:data_len];
                 [ByteDanceSdk shared].info.token = [AiMouseTools dataToUtf8String:data];
-                [[LogManager shared] addLog:[NSString stringWithFormat:@"STB_DM_DATA_ARRIVED_TOKEN = %@",[ByteDanceSdk shared].info.token]];
+                [LogManager log:[NSString stringWithFormat:@"STB_DM_DATA_ARRIVED_TOKEN = %@",[ByteDanceSdk shared].info.token]];
                 [[ByteDanceSdk shared] checkAuthSuccess];
                 break;
             }
@@ -155,7 +155,7 @@ static void cust_event_callback_func(uint32_t type,uint8_t *report, uint32_t dat
             {
                 NSData *data = [[NSData alloc] initWithBytes:report length:data_len];
                 [ByteDanceSdk shared].info.expireTime = [AiMouseTools dataToUtf8String:data];
-//                [[LogManager shared] addLog:[NSString stringWithFormat:@"STB_DM_DATA_ARRIVED_TOKEN_TIMEOUT = %@",[ByteDanceSdk shared].info.expireTime]];
+//                [LogManager log:[NSString stringWithFormat:@"STB_DM_DATA_ARRIVED_TOKEN_TIMEOUT = %@",[ByteDanceSdk shared].info.expireTime]];
                 [[ByteDanceSdk shared] checkAuthSuccess];
                 break;
             }
@@ -164,7 +164,7 @@ static void cust_event_callback_func(uint32_t type,uint8_t *report, uint32_t dat
                 //host
                 NSData *data = [[NSData alloc] initWithBytes:report length:data_len];
                 [ByteDanceSdk shared].info.host = [AiMouseTools dataToUtf8String:data];
-                [[LogManager shared] addLog:[NSString stringWithFormat:@"STB_DM_DATA_ARRIVED_HOST = %@",[ByteDanceSdk shared].info.host]];
+                [LogManager log:[NSString stringWithFormat:@"STB_DM_DATA_ARRIVED_HOST = %@",[ByteDanceSdk shared].info.host]];
                 [[ByteDanceSdk shared] checkAuthSuccess];
                 break;
             }
@@ -173,19 +173,19 @@ static void cust_event_callback_func(uint32_t type,uint8_t *report, uint32_t dat
                 //params
                 NSData *data = [[NSData alloc] initWithBytes:report length:data_len];
                 [ByteDanceSdk shared].info.urlParams = [AiMouseTools dataToUtf8String:data];
-                [[LogManager shared] addLog:[NSString stringWithFormat:@"STB_DM_DATA_ARRIVED_PARAMS_SIGN = %@",[ByteDanceSdk shared].info.urlParams]];
+                [LogManager log:[NSString stringWithFormat:@"STB_DM_DATA_ARRIVED_PARAMS_SIGN = %@",[ByteDanceSdk shared].info.urlParams]];
                 [[ByteDanceSdk shared] checkAuthSuccess];
                 break;
             }
                 
             default:
-                [[LogManager shared] addLog:@"UNKNOWN_MSG"];
+                [LogManager log:@"UNKNOWN_MSG"];
                 break;
         }
         
         NSData *data = [[NSData alloc] initWithBytes:report length:data_len];
-        [[LogManager shared] addLog:[NSString stringWithFormat:@"event_callback type = %u,data = %@",type,[AiMouseTools dataToHexString:data]]];
-        [[LogManager shared] addLog:[NSString stringWithFormat:@"str = %@",[AiMouseTools dataToUtf8String:data]]];
+        [LogManager log:[NSString stringWithFormat:@"event_callback type = %u,data = %@",type,[AiMouseTools dataToHexString:data]]];
+        [LogManager log:[NSString stringWithFormat:@"str = %@",[AiMouseTools dataToUtf8String:data]]];
     });
 }
 
@@ -197,7 +197,7 @@ int32_t STB_STDCALL cus_hal_send_data2device(uint8_t* data, uint32_t len)
 //        return 0;
 //    }
         NSData *cmdData = [[NSData alloc] initWithBytes:data length:len];
-        [[LogManager shared] addLog:[NSString stringWithFormat:@"send_data2device = %@",[AiMouseTools dataToHexString_placeholder:cmdData]]];
+        [LogManager log:[NSString stringWithFormat:@"send_data2device = %@",[AiMouseTools dataToHexString_placeholder:cmdData]]];
         NSMutableData *fullData = [[NSMutableData alloc] init];
         
         uint8_t head[] = { 0x2d, 0x24, 0x24};
index b6cacd6..5bfd7ca 100644 (file)
@@ -7,6 +7,7 @@
 
 #import "AiDeviceAuthManager.h"
 #import <Cocoa/Cocoa.h>
+#import "LogManager.h"
 
 @interface AiDeviceAuthManager()<NSAlertDelegate>
 
     //        [alert addButtonWithTitle:@"取消"];
     NSInteger result = [alert runModal];
     if (result == NSAlertFirstButtonReturn) {
-        NSLog(@"用户点击了确定");
+        [LogManager log:@"输入监控用户点击了确定"];
         [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_ListenEvent"]];
     } else if (result == NSAlertSecondButtonReturn) {
-        NSLog(@"用户点击了取消");
+        [LogManager log:@"输入监控用户点击了取消"];
     }
     [NSApp terminate:nil];
 }
     //        [alert addButtonWithTitle:@"取消"];
     NSInteger result = [alert runModal];
     if (result == NSAlertFirstButtonReturn) {
-        NSLog(@"用户点击了确定");
+        [LogManager log:@"麦克风用户点击了确定"];
         [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone"]];
     } else if (result == NSAlertSecondButtonReturn) {
-        NSLog(@"用户点击了取消");
+        [LogManager log:@"麦克风用户点击了取消"];
     }
     [NSApp terminate:nil];
 }
index 1b4f1dd..38e0978 100644 (file)
         [self.curentPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithResponse];
     }
     else{
-//        NSLog(@"Characteristic = %@",self.deviceInfoCha);
         [self.curentPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithoutResponse];
     }
 }
index aa2de3a..0f28cbe 100644 (file)
@@ -19,6 +19,8 @@ typedef NS_ENUM(NSInteger,AiMouseProductType) {
 NS_ASSUME_NONNULL_BEGIN
 
 @interface DeviceManger : NSObject
+@property (nonatomic,strong)NSString *version;//sdk版本
+@property (nonatomic, assign)int debugLevel;//调试模式等级,0什么日志都不会输出,1输出日志,2输出日志且会将日志保存到本地文件
 @property (nonatomic, readonly) int appType;//1威鼠不鉴权;2威鼠字节鉴权;3千寻不鉴权;4千寻字节鉴权
 @property (nonatomic, readonly)BOOL isConneted;//是否连接
 
index 515307e..ea437d5 100644 (file)
@@ -15,6 +15,7 @@
 #import "KeyboardHidManager.h"
 #import "KeyboardSdk.h"
 #import "ByteDanceSdk.h"
+#import "LogManager.h"
 
 @interface DeviceManger()<TlwMouseDelegate,AiMouseDelegate,NMouseSdkDelegate,KeyboardSdkDelegate>
 
 //        [self setupBle];
 //        [self setupHid];
 //        [self setupTimer];
+        self.version = @"MouseSdk_v2024122602";
     }
     return self;
 }
 - (void)initSdk:(int)appType{
     _appType = appType;
-    
-    NSString *version = @"MouseSdk_v2024122602";
-    NSLog(@"[AiMouseSdk] %@",version);
-    NSLog(@"[AiMouseSdk] %d",appType);
-    [self showLog:version];
     [self setupSDK];
     [self setupBle];
     [self setupHid];
     [self setupTimer];
 }
+-(void)setDebugLevel:(int)debugLevel{
+    [LogManager shared].debugLevel = debugLevel;
+    [self showLog:self.version];
+    [self showLog:[NSString stringWithFormat:@"app type %d",self.appType]];
+}
 #pragma mark ------ 设备初始化
 - (void)setupSDK{
     self.kbSdk = [[KeyboardSdk alloc] init];
     if (self.aiMouseAddLogCallBack) {
         self.aiMouseAddLogCallBack(log);
     }
+    [LogManager log:log];
 //    NSLog(@"[AiMouseSdk]%@",log);
 }
 
index afbdf05..440eeb3 100644 (file)
@@ -7,14 +7,22 @@
 
 #import <Foundation/Foundation.h>
 
-NS_ASSUME_NONNULL_BEGIN
-
 @interface LogManager : NSObject
-+(LogManager *)shared;
+@property (nonatomic, assign)int debugLevel;//调试模式等级,0什么日志都不会输出,1输出日志,2输出日志且会将日志保存到本地文件
 
-- (void)addLog:(NSString *)log type:(NSString *)type;
++ (instancetype)shared;
 
-- (void)addLog:(NSString *)log;
-@end
+- (NSString *)sdkFolder;
+
++ (void)logs:(NSArray *)logs;
+
++ (void)log:(NSString *)log;
+
++ (void)logs:(NSArray *)logs line:(NSInteger)line separator:(NSString *)separator terminator:(NSString *)terminator;
 
-NS_ASSUME_NONNULL_END
+- (void)saveLog:(NSString *)log;
+
+- (NSString *)getLogs;
+
+
+@end
index dab9a3a..652fdfe 100644 (file)
 
 #import "LogManager.h"
 
+@interface LogManager()
+@property (nonatomic, strong) NSFileHandle *fileHandle;
+@property (nonatomic, strong) NSMutableArray *logList;
+@end
+
+
 @implementation LogManager
 
-+(LogManager *)shared{
-    static LogManager *manager = nil;
-    static dispatch_once_t oncePredicate;
-    dispatch_once(&oncePredicate, ^{
-        manager = [[LogManager alloc]init];
-//        [manager initSdk];
++ (instancetype)shared {
+    static LogManager *shared = nil;
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        shared = [[LogManager alloc] init];
     });
-    return manager;
+    return shared;
+}
+
+- (instancetype)init {
+    if (self = [super init]) {
+        self.logList = [[NSMutableArray alloc] init];
+
+    }
+    return self;
+}
+
+-(void)setDebugLevel:(int)debugLevel{
+    _debugLevel = debugLevel;
+    if (debugLevel == 2) {
+        NSString *path = [self getPath];
+        self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
+    }
+}
+
+- (NSString *)dateString {
+    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss:SSS"];
+    return [formatter stringFromDate:[NSDate date]];
+}
+
+- (NSString *)sdkFolder{
+    NSString *foldPath = [NSString stringWithFormat:@"%@/Documents/AiMouseSdk", NSHomeDirectory()];
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    if (![fileManager fileExistsAtPath:foldPath]) {
+        NSError *error;
+        [fileManager createDirectoryAtPath:foldPath withIntermediateDirectories:YES attributes:nil error:&error];
+        if (error) {
+            return @"";
+        }
+    }
+    return foldPath;
 }
 
-- (void)addLog:(NSString *)log{
-    NSString *str = [NSString stringWithFormat:@"%@",log];
-    NSLog(str);
+- (NSString *)getPath {
+    NSString *foldPath = [self sdkFolder];
+    NSString *path = [foldPath stringByAppendingString:[NSString stringWithFormat:@"/%@.log",[self dateString]]];
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    if (![fileManager fileExistsAtPath:path]) {
+        [fileManager createFileAtPath:path contents:nil attributes:nil];
+        if (true) {
+            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss:SSS"];            
+            NSString *text1 = [formatter stringFromDate:[NSDate date]];
+            NSString *text2 = path;
+            NSString *logString = [NSString stringWithFormat:@"%@ ~ [LogManager] 日志路径 %@",text1,text2];
+            NSLog(@"%@",logString);
+        }
+    }
+    return path;
+}
+
++ (void)logs:(NSArray *)logs  {
+    @synchronized([LogManager shared]) {
+        if ([LogManager shared].debugLevel == 0) {
+            return;
+        }
+        
+        NSString *text1 = [[LogManager shared] dateString];
+        NSString *text2 = [logs componentsJoinedByString:@" "];
+        NSString *logString = [NSString stringWithFormat:@"%@ ~ [LogManager]%@",text1,text2];
+        NSLog(@"%@",logString);
+        
+        if ([LogManager shared].debugLevel == 2) {
+            logString = [logString stringByAppendingString:@"\n"];
+            [[LogManager shared] saveLog:logString];
+        }
+    }
+}
+
++ (void)log:(NSString *)log  {
+    [self logs:@[log]];
+}
+
++ (void)logs:(NSArray *)logs line:(NSInteger)line separator:(NSString *)separator terminator:(NSString *)terminator {
+    @synchronized([LogManager shared]) {
+        if ([LogManager shared].debugLevel == 0) {
+            return;
+        }
+        NSLog(@"%@ [AiMouseSdk-%03ld]: ", [[LogManager shared] dateString], (long)line);
+        for (id obj in logs) {
+            NSLog(@"%@", obj);
+        }
+        NSLog(@"%@", terminator);
+        
+        if ([LogManager shared].debugLevel == 2) {
+            NSString *logString = [[NSString stringWithFormat:@"%@ [StarburstSdk-%03ld]: ", [[LogManager shared] dateString], (long)line] stringByAppendingString:[logs componentsJoinedByString:separator]];
+            logString = [logString stringByAppendingString:terminator];
+            [[LogManager shared] saveLog:logString];
+        }
+
+    }
+}
+- (void)saveLog:(NSString *)log {
+    [self.fileHandle writeData:[log dataUsingEncoding:NSUTF8StringEncoding]];
+    [self.logList addObject:log];
+}
+
+
+- (NSString *)getLogs{
+    //    NSData *data = [[NSData alloc] initWithContentsOfFile:[self getPath]];
+    //    NSString *log = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
+    //    return  log;
+    NSString *log = [self.logList componentsJoinedByString:@""];
+    return  log;
 }
 @end
index be8f60a..d1635b1 100644 (file)
 +(NSString *)getCurrentTime{
 //    @"2024-08-05T10:07:39Z"
     NSDate *date = [NSDate date];
-//    NSLog(@"当前零时区时间 %@", date);
-    
     NSDateFormatter *dateFormaterA = [[NSDateFormatter alloc]init];
     [dateFormaterA setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; // 设置时区为格林尼治标准时区
     [dateFormaterA setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
     NSString *dateStrA = [dateFormaterA stringFromDate:date];
     NSString *text1 = [[dateStrA componentsSeparatedByString:@" "] componentsJoinedByString:@"T"];
     NSString *text2 = [text1 stringByAppendingString:@"Z"];
-//    NSLog(@"%@",text2);
-    
-    
-    // 如果你需要将这个UTC时间显示为格林尼治时间,‌你可以这样做:‌
-//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
-//    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; // 设置时区为格林尼治标准时区
-//    NSString *gmTimeString = [dateFormatter stringFromDate:date]; // 将UTC日期转换为字符串表示形式,‌时区设置为GMT
-//    NSLog(@"%@ == ",gmTimeString);
-    
     return  text2;
 }
 @end
diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/IDEFindNavigatorScopes.plist b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/IDEFindNavigatorScopes.plist
new file mode 100644 (file)
index 0000000..5dd5da8
--- /dev/null
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<array/>
+</plist>
index 56de63c..a2715d4 100644 (file)
Binary files a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate and b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/yaozuopan.xcuserdatad/UserInterfaceState.xcuserstate differ
index 2ea1600..784a3f5 100644 (file)
 - (void)setupDeviceManger{
 //    [AiHidManager shared];
     DeviceManger *manager = [DeviceManger shared];
-    [manager initSdk:2];
+    [manager initSdk:1];
+    manager.debugLevel = 1;
+    
     __weak typeof(self) ws = self;
-    manager.aiMouseAddLogCallBack = ^(NSString * _Nonnull log) {
-        NSLog(@"%@",log);
-    };
+//    manager.aiMouseAddLogCallBack = ^(NSString * _Nonnull log) {
+//        NSLog(@"%@",log);
+//    };
     manager.authCallBack = ^(BOOL isSuccess) {
         dispatch_async(dispatch_get_main_queue(), ^{
             ws.authLabel.stringValue = isSuccess?@"鉴权状态:鉴权成功":@"鉴权状态:鉴权失败";
index c230d1a..a1cc8f6 100644 (file)
@@ -7,12 +7,12 @@
                <key>Masonry.xcscheme_^#shared#^_</key>
                <dict>
                        <key>orderHint</key>
-                       <integer>2</integer>
+                       <integer>3</integer>
                </dict>
                <key>Pods-MouseSdk.xcscheme_^#shared#^_</key>
                <dict>
                        <key>orderHint</key>
-                       <integer>3</integer>
+                       <integer>2</integer>
                </dict>
        </dict>
 </dict>