From ff8ef47e8a6e6924a72f621d0e8e374abdb14930 Mon Sep 17 00:00:00 2001 From: Ricardo Pereira Date: Wed, 25 Nov 2020 21:27:11 +0000 Subject: [PATCH] Better Swift interoperability --- lelib.xcodeproj/project.pbxproj | 4 +- lelib/LELog.h | 35 +++++--------- lelib/LELog.m | 85 +++++++++++---------------------- lelibTests/lelibTests.m | 16 +++---- 4 files changed, 47 insertions(+), 93 deletions(-) diff --git a/lelib.xcodeproj/project.pbxproj b/lelib.xcodeproj/project.pbxproj index aab529d..fb3b734 100644 --- a/lelib.xcodeproj/project.pbxproj +++ b/lelib.xcodeproj/project.pbxproj @@ -283,12 +283,12 @@ isa = PBXGroup; children = ( 4BB806A7181D3DF1003066E5 /* lelib.h */, + 4B08F54818436082003D813B /* LELog.h */, + 4B08F54918436082003D813B /* LELog.m */, 4BE1B5C61897BF57008DD96E /* LeNetworkStatus.h */, 4BE1B5C71897BF5C008DD96E /* LeNetworkStatus.m */, 4BBB74FB184AC88600B393BC /* LEBackgroundThread.h */, 4BBB74F9184AC86F00B393BC /* LEBackgroundThread.m */, - 4B08F54818436082003D813B /* LELog.h */, - 4B08F54918436082003D813B /* LELog.m */, 4BBA0161184BDEE000AB5C9A /* LogFile.h */, 4BBA0162184BDEE000AB5C9A /* LogFile.m */, 4BBA0165184BDEFC00AB5C9A /* LogFiles.h */, diff --git a/lelib/LELog.h b/lelib/LELog.h index b90f758..a8ddfe3 100644 --- a/lelib/LELog.h +++ b/lelib/LELog.h @@ -8,41 +8,29 @@ #import -/* Obj-C API */ - -@protocol LELoggableObject - -@optional - -- (NSString*)leDescription; - -@end +NS_ASSUME_NONNULL_BEGIN @interface LELog : NSObject -+ (LELog*)sharedInstance; ++ (LELog *)sharedInstance; ++ (LELog *)sessionWithToken:(NSString *)token; -+ (LELog*)sessionWithToken:(NSString*)token; -/* - Display all messages on TTY for debug purposes - */ +/// Display all messages on TTY for debug purposes. @property (nonatomic) BOOL debugLogs; -/* - Appends space separated token to each log message. - */ -@property (atomic, copy) NSString* token; +/// Appends space separated token to each log message. +@property (nullable, atomic, copy) NSString *token; -/* +/** When object implements LELoggableObject interface, it logs return value of leDescription method. Otherwise, tries to log return value of standard description method. */ -- (void)log:(NSObject*)object; +- (void)log:(NSString *)object; -+ (void)log:(NSObject*)object; ++ (void)log:(NSString *)object; -/* +/** Log UIApplicationDidFinishLaunchingNotification, UIApplicationDidBecomeActiveNotification, UIApplicationWillEnterForegroundNotification, UIApplicationWillResignActiveNotification, UIApplicationWillTerminateNotification. @@ -51,5 +39,4 @@ @end - - +NS_ASSUME_NONNULL_END diff --git a/lelib/LELog.m b/lelib/LELog.m index 925ca8c..6919c90 100644 --- a/lelib/LELog.m +++ b/lelib/LELog.m @@ -11,7 +11,6 @@ #import "LELog.h" #import "LEBackgroundThread.h" #import "LogFiles.h" -#import "lelib.h" #import "lecore.h" extern LEBackgroundThread* backgroundThread; @@ -21,11 +20,10 @@ @implementation LELog -- (id)init -{ +- (id)init { self = [super init]; if (le_init()) return nil; - + NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationWillEnterForegroundNotification object:nil]; @@ -34,39 +32,23 @@ - (id)init return self; } -- (void)dealloc -{ +- (void)dealloc { NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; [center removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; } -- (void)log:(NSObject*)object -{ - NSString* text = nil; - - if ([object respondsToSelector:@selector(leDescription)]) { - id leLoggableObject = (id)object; - text = [leLoggableObject leDescription]; - } else if ([object isKindOfClass:[NSString class]]) { - text = (NSString*)object; - } else { - text = [object description]; - } - +- (void)log:(NSString *)text { text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@"\u2028"]; - LE_DEBUG(@"%@", text); - le_write_string(text); le_poke(); } -+ (void)log:(NSObject *)object{ - - [[self sharedInstance] log:object]; ++ (void)log:(NSString *)text { + [[self sharedInstance] log:text]; } -+ (LELog*)sharedInstance -{ + ++ (LELog *)sharedInstance { static dispatch_once_t once; static LELog* sharedInstance; dispatch_once(&once, ^{ @@ -74,60 +56,54 @@ + (LELog*)sharedInstance }); return sharedInstance; } -+(LELog*)sessionWithToken:(NSString*)token{ - + ++ (LELog *)sessionWithToken:(NSString*)token { LELog * leLog = [self sharedInstance]; [leLog setToken:token]; return leLog; } -- (void)setToken:(NSString *)token -{ + +- (void)setToken:(NSString *)token { le_set_token([token cStringUsingEncoding:NSUTF8StringEncoding]); } -- (void)setDebugLogs:(BOOL)debugLogs -{ +- (void)setDebugLogs:(BOOL)debugLogs { le_set_debug_logs(debugLogs); } -- (NSString*)token -{ +- (NSString *)token { __block NSString* r = nil; dispatch_sync(le_write_queue, ^{ - if (!le_token || le_token[0]) { r = nil; } else { r = [NSString stringWithUTF8String:le_token]; } }); - + return r; } -- (void)notificationReceived:(NSNotification*)notification -{ +- (void)notificationReceived:(NSNotification*)notification { if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) { - if (self.logApplicationLifecycleNotifications) { [self log:notification.name]; } - + le_poke(); - return; } - + if ([notification.name isEqualToString:UIApplicationDidBecomeActiveNotification]) { [self log:notification.name]; return; } - + if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) { [self log:notification.name]; return; } - + if ([notification.name isEqualToString:UIApplicationDidFinishLaunchingNotification]) { [self log:notification.name]; return; @@ -142,17 +118,15 @@ - (void)notificationReceived:(NSNotification*)notification [self log:notification.name]; return; } - + if ([notification.name isEqualToString:UIApplicationDidReceiveMemoryWarningNotification]) { [self log:notification.name]; return; } } -- (void)registerForNotifications -{ +- (void)registerForNotifications { NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; - [center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationDidBecomeActiveNotification object:nil]; [center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationWillResignActiveNotification object:nil]; [center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationDidFinishLaunchingNotification object:nil]; @@ -161,8 +135,7 @@ - (void)registerForNotifications [center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; } -- (void)unregisterFromNotifications -{ +- (void)unregisterFromNotifications { NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; [center removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; [center removeObserver:self name:UIApplicationWillResignActiveNotification object:nil]; @@ -172,21 +145,19 @@ - (void)unregisterFromNotifications [center removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; } -- (void)setLogApplicationLifecycleNotifications:(BOOL)logApplicationLifecycleNotifications -{ +- (void)setLogApplicationLifecycleNotifications:(BOOL)logApplicationLifecycleNotifications { @synchronized(self) { - if (logApplicationLifecycleNotifications == _logApplicationLifecycleNotifications) return; - + _logApplicationLifecycleNotifications = logApplicationLifecycleNotifications; - + if (logApplicationLifecycleNotifications) { [self registerForNotifications]; - } else { + } + else { [self unregisterFromNotifications]; } } } - @end diff --git a/lelibTests/lelibTests.m b/lelibTests/lelibTests.m index 4bc88ac..f2cd1ad 100644 --- a/lelibTests/lelibTests.m +++ b/lelibTests/lelibTests.m @@ -16,35 +16,31 @@ @interface lelibTests : XCTestCase @implementation lelibTests -- (void)setUp -{ +- (void)setUp { [super setUp]; // Put setup code here. This method is called before the invocation of each test method in the class. } -- (void)tearDown -{ +- (void)tearDown { // Put teardown code here. This method is called after the invocation of each test method in the class. [super tearDown]; } -- (void)testLog -{ +- (void)testLog { LELog* log = [LELog sharedInstance]; log.token = @"f66815d1-702c-414b-8dcc-bb73de372584"; [log log:@"ahoj"]; - + NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; while ([runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]); - + XCTFail(@"Test exited runloop"); } + - (void)testNilToken{ - LELog* log = [LELog sharedInstance]; log.token = nil; [log log:@"try to log without token"]; - } @end