FlutterLoganPlugin.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2018-present, 美团点评
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #import <objc/runtime.h>
  23. #import <objc/message.h>
  24. #import "FlutterLoganPlugin.h"
  25. #import "Logan.h"
  26. @implementation FlutterLoganPlugin
  27. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  28. FlutterMethodChannel* channel = [FlutterMethodChannel
  29. methodChannelWithName:@"flutter_logan"
  30. binaryMessenger:[registrar messenger]];
  31. FlutterLoganPlugin* instance = [[FlutterLoganPlugin alloc] init];
  32. [registrar addMethodCallDelegate:instance channel:channel];
  33. }
  34. - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  35. NSLog(@"call method %@ args=%@",call.method,call.arguments);
  36. SEL sel = NSSelectorFromString([call.method stringByAppendingString:@":result:"]);
  37. if(sel && [self respondsToSelector:sel]){
  38. ((void(*)(id,SEL,...))objc_msgSend)(self,sel,call.arguments,result);
  39. }else{
  40. result(FlutterMethodNotImplemented);
  41. }
  42. }
  43. - (void)init:(NSDictionary *)args result:(FlutterResult)result{
  44. if(![args isKindOfClass:[NSDictionary class]]){
  45. result(@(NO));
  46. return;
  47. }
  48. NSString *key = args[@"aesKey"];
  49. NSString *iv = args[@"aesIv"];
  50. NSNumber *maxFileLen = args[@"maxFileLen"];
  51. if(key.length >0 && iv.length >0){
  52. loganInit([NSData dataWithBytes:key.UTF8String length:key.length],[NSData dataWithBytes:iv.UTF8String length:iv.length] , maxFileLen.integerValue);
  53. result(@(YES));
  54. }else{
  55. result(@(NO));
  56. }
  57. }
  58. - (void)log:(NSDictionary *)args result:(FlutterResult)result{
  59. if(![args isKindOfClass:[NSDictionary class]]){
  60. result(nil);
  61. return;
  62. }
  63. NSNumber *type = args[@"type"];
  64. NSString *log = args[@"log"];
  65. logan(type.integerValue, log);
  66. result(nil);
  67. }
  68. - (void)flush:(NSDictionary *)args result:(FlutterResult)result{
  69. loganFlush();
  70. result(nil);
  71. }
  72. - (void)getUploadPath:(NSDictionary *)args result:(FlutterResult)result{
  73. if(![args isKindOfClass:[NSDictionary class]]){
  74. result(@"");
  75. return;
  76. }
  77. NSString *date = args[@"date"];
  78. if(date.length >0){
  79. loganUploadFilePath(date, ^(NSString * _Nullable filePath) {
  80. result(filePath);
  81. });
  82. }else{
  83. result(@"");
  84. }
  85. }
  86. - (void)upload:(NSDictionary *)args result:(FlutterResult)result{
  87. if(![args isKindOfClass:[NSDictionary class]]){
  88. result(@NO);
  89. return;
  90. }
  91. NSString *urlStr = args[@"serverUrl"];
  92. NSString *date = args[@"date"];
  93. NSString *appid = args[@"appId"];
  94. NSString *unionId = args[@"unionId"];
  95. NSString *deviceId = args[@"deviceId"];
  96. loganUpload(urlStr,date,appid,unionId,deviceId,^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error){
  97. if(error){
  98. result(@NO);
  99. }else{
  100. result(@YES);
  101. }
  102. });
  103. }
  104. - (void)cleanAllLogs:(NSArray *)param result:(FlutterResult)result{
  105. loganClearAllLogs();
  106. result(nil);
  107. }
  108. @end