Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
Better Swift interoperability
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardopereira committed Nov 25, 2020
1 parent 1ff03c0 commit ff8ef47
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 93 deletions.
4 changes: 2 additions & 2 deletions lelib.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */,
Expand Down
35 changes: 11 additions & 24 deletions lelib/LELog.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,29 @@

#import <Foundation/Foundation.h>

/* Obj-C API */

@protocol LELoggableObject <NSObject>

@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.
Expand All @@ -51,5 +39,4 @@

@end



NS_ASSUME_NONNULL_END
85 changes: 28 additions & 57 deletions lelib/LELog.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#import "LELog.h"
#import "LEBackgroundThread.h"
#import "LogFiles.h"
#import "lelib.h"
#import "lecore.h"

extern LEBackgroundThread* backgroundThread;
Expand All @@ -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];

Expand All @@ -34,100 +32,78 @@ - (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> leLoggableObject = (id<LELoggableObject>)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, ^{
sharedInstance = [LELog new];
});
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;
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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
16 changes: 6 additions & 10 deletions lelibTests/lelibTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ff8ef47

Please sign in to comment.