首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Objective-C中挂接Apple的`os_log_with_type`方法

如何在Objective-C中挂接Apple的`os_log_with_type`方法
EN

Stack Overflow用户
提问于 2020-11-21 22:25:01
回答 1查看 94关注 0票数 0

我想挂接"React Native“RCTLog日志方法_RCTLogJavaScriptInternal,源代码是:

代码语言:javascript
复制
void _RCTLogJavaScriptInternal(RCTLogLevel level, NSString *message)
{
  RCTLogFunction logFunction = RCTGetLocalLogFunction();
  BOOL log = RCT_DEBUG || (logFunction != nil);
  if (log && level >= RCTGetLogThreshold()) {
    if (logFunction) {
      logFunction(level, RCTLogSourceJavaScript, nil, nil, message);
    }
  }
}

RCTLogFunction RCTDefaultLogFunction =
    ^(RCTLogLevel level,
      RCTLogSource source,
      __unused NSString *fileName,
      __unused NSNumber *lineNumber,
      NSString *message) {
      os_log_with_type(RCTLogForLogSource(source), RCTLogTypeForLogLevel(level), "%{public}s", message.UTF8String);
    };

因此,如果我只连接苹果的os_log_with_type,我将获得RCTLog日志。这是我的代码,但不起作用。请帮帮我。谢谢!

代码语言:javascript
复制
#import <os/log.h>
#import "fishhook.h"

static void (*original_oslog)((os_log_t log, os_log_type_t type, const char *format, ...));

void hook_oslog(os_log_t log, os_log_type_t type, const char *format, ...) {
    NSLog(@"hook success!");
}

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        struct rebinding oslog_rebinding = { "os_log_with_type", hook_oslog, (void *)&original_oslog };
        rebind_symbols((struct rebinding[1]){oslog_rebinding}, 1);
    });
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-22 23:17:40

最后,我找到了解决方案。不需要钩子,React Native已经提供了相关接口。

代码语言:javascript
复制
RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
  NSLog(@"%@", message);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64944123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档