}
- (void)initSdk{
- [[LogManager shared] addLog:@"开始初始化"];
+ [LogManager log:@"开始初始化"];
stb_app_printf_str_register(cus_printf_str);
// NSString *appId = @"tis_test";
NSString *appId = @"app_10dyx2ai21r2qkar";
{
dispatch_async(dispatch_get_main_queue(), ^{
NSString *logStr = [NSString stringWithFormat:@"%s", str];
- [[LogManager shared] addLog:logStr];
+ [LogManager log:logStr];
});
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:
//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;
}
{
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;
}
//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;
}
//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]]];
});
}
// 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};
#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];
}
[self.curentPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithResponse];
}
else{
-// NSLog(@"Characteristic = %@",self.deviceInfoCha);
[self.curentPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithoutResponse];
}
}
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;//是否连接
#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);
}
#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
#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
+(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
--- /dev/null
+<?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>
- (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?@"鉴权状态:鉴权成功":@"鉴权状态:鉴权失败";
<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>