Support iOS crash reporting

This commit is contained in:
2026-06-12 16:42:45 +08:00
parent e0bf4de792
commit 65b9f47d91
10 changed files with 378 additions and 54 deletions

View File

@@ -0,0 +1,190 @@
#import <Foundation/Foundation.h>
#if __has_include(<Bugly/Bugly.h>)
#import <Bugly/Bugly.h>
#define FOLDCC_BUGLY_IOS_AVAILABLE 1
#else
#define FOLDCC_BUGLY_IOS_AVAILABLE 0
#endif
static NSString *FoldCCBuglyString(const char *value)
{
if (value == NULL)
{
return @"";
}
NSString *result = [NSString stringWithUTF8String:value];
return result == nil ? @"" : result;
}
static NSArray *FoldCCBuglyStackFrames(NSString *stackTrace)
{
if (stackTrace.length == 0)
{
return @[];
}
NSArray *lines = [stackTrace componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSMutableArray *frames = [NSMutableArray arrayWithCapacity:lines.count];
for (NSString *line in lines)
{
NSString *trimmed = [line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (trimmed.length > 0)
{
[frames addObject:trimmed];
}
}
return frames;
}
static NSDictionary *FoldCCBuglyExtraInfo(NSString *extras)
{
if (extras.length == 0)
{
return @{};
}
NSData *data = [extras dataUsingEncoding:NSUTF8StringEncoding];
id parsed = data == nil ? nil : [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if ([parsed isKindOfClass:[NSDictionary class]])
{
return parsed;
}
return @{ @"UnityExtraInfo" : extras };
}
#if FOLDCC_BUGLY_IOS_AVAILABLE
static NSString *s_channel = nil;
static NSString *s_version = nil;
static NSString *s_user = nil;
static NSString *s_deviceId = nil;
static BOOL s_debugMode = NO;
static BOOL s_initialized = NO;
static BuglyLogLevel FoldCCBuglyLogLevel(int level)
{
switch (level)
{
case 1:
return BuglyLogLevelError;
case 2:
return BuglyLogLevelWarn;
case 3:
return BuglyLogLevelInfo;
case 4:
return BuglyLogLevelDebug;
default:
return BuglyLogLevelSilent;
}
}
#endif
extern "C"
{
void _BuglyInit(const char *appId, bool debug, int level)
{
#if FOLDCC_BUGLY_IOS_AVAILABLE
if (s_initialized)
{
return;
}
BuglyConfig *config = [[BuglyConfig alloc] init];
config.debugMode = debug;
config.reportLogLevel = FoldCCBuglyLogLevel(level);
config.channel = s_channel;
config.version = s_version;
config.deviceIdentifier = s_deviceId;
config.consolelogEnable = debug;
[Bugly startWithAppId:FoldCCBuglyString(appId) config:config];
if (s_user.length > 0)
{
[Bugly setUserIdentifier:s_user];
}
s_debugMode = debug;
s_initialized = YES;
#endif
}
void _BuglySetUserId(const char *userId)
{
#if FOLDCC_BUGLY_IOS_AVAILABLE
NSString *value = FoldCCBuglyString(userId);
s_user = [value copy];
if (s_initialized && value.length > 0)
{
[Bugly setUserIdentifier:value];
}
#endif
}
void _BuglySetTag(int tag)
{
#if FOLDCC_BUGLY_IOS_AVAILABLE
if (s_initialized)
{
[Bugly setTag:(NSUInteger)tag];
}
#endif
}
void _BuglySetKeyValue(const char *key, const char *value)
{
#if FOLDCC_BUGLY_IOS_AVAILABLE
NSString *keyString = FoldCCBuglyString(key);
if (s_initialized && keyString.length > 0)
{
[Bugly setUserValue:FoldCCBuglyString(value) forKey:keyString];
}
#endif
}
void _BuglyReportException(int type, const char *name, const char *reason, const char *stackTrace, const char *extras, bool quit)
{
#if FOLDCC_BUGLY_IOS_AVAILABLE
if (s_initialized)
{
[Bugly reportExceptionWithCategory:(NSUInteger)type
name:FoldCCBuglyString(name)
reason:FoldCCBuglyString(reason)
callStack:FoldCCBuglyStackFrames(FoldCCBuglyString(stackTrace))
extraInfo:FoldCCBuglyExtraInfo(FoldCCBuglyString(extras))
terminateApp:quit];
}
#endif
}
void _BuglyDefaultConfig(const char *channel, const char *version, const char *user, const char *deviceId)
{
#if FOLDCC_BUGLY_IOS_AVAILABLE
s_channel = [FoldCCBuglyString(channel) copy];
s_version = [FoldCCBuglyString(version) copy];
s_user = [FoldCCBuglyString(user) copy];
s_deviceId = [FoldCCBuglyString(deviceId) copy];
#endif
}
void _BuglyLogMessage(int level, const char *tag, const char *log)
{
#if FOLDCC_BUGLY_IOS_AVAILABLE
if (s_initialized)
{
[BuglyLog level:FoldCCBuglyLogLevel(level) tag:FoldCCBuglyString(tag) log:@"%@", FoldCCBuglyString(log)];
}
#endif
}
void _BuglyConfigCrashReporterType(int type)
{
}
void _BuglySetExtraConfig(const char *key, const char *value)
{
_BuglySetKeyValue(key, value);
}
}