From 89f73ff62aa3b3d73aa614064e6556e9789e65e9 Mon Sep 17 00:00:00 2001 From: lawrencelomax Date: Wed, 3 Feb 2016 11:23:14 +0000 Subject: [PATCH] Add Prefixing --- .../Events/FBSimulatorLoggingEventSink.m | 30 +++++++++---------- .../Framebuffer/FBSimulatorFramebuffer.m | 4 +-- .../Utility/FBSimulatorLogger.h | 8 +++++ .../Utility/FBSimulatorLogger.m | 20 +++++++++---- fbsimctl/fbsimctl/JSONLogger.m | 6 ++++ 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/FBSimulatorControl/Events/FBSimulatorLoggingEventSink.m b/FBSimulatorControl/Events/FBSimulatorLoggingEventSink.m index dc5d730b..10565672 100644 --- a/FBSimulatorControl/Events/FBSimulatorLoggingEventSink.m +++ b/FBSimulatorControl/Events/FBSimulatorLoggingEventSink.m @@ -18,7 +18,6 @@ @interface FBSimulatorLoggingEventSink () @property (nonatomic, strong, readonly) id logger; -@property (nonatomic, copy, readonly) NSString *prefix; @end @@ -28,17 +27,16 @@ + (instancetype)withSimulator:(FBSimulator *)simulator logger:(id)logger { - return [[self alloc] initWithPrefix:[NSString stringWithFormat:@"%@: ", simulator.udid] logger:logger.info]; + return [[self alloc] initWithLogger:[logger withPrefix:[NSString stringWithFormat:@"%@:", simulator.udid]]]; } -- (instancetype)initWithPrefix:(NSString *)prefix logger:(id)logger +- (instancetype)initWithLogger:(id)logger { self = [super init]; if (!self) { return nil; } - _prefix = prefix; _logger = logger; return self; @@ -48,62 +46,62 @@ - (void)containerApplicationDidLaunch:(FBProcessInfo *)applicationProcess { - [self.logger logFormat:@"%@Container Application Did Launch => %@", self.prefix, applicationProcess.shortDescription]; + [self.logger logFormat:@"Container Application Did Launch => %@", applicationProcess.shortDescription]; } - (void)containerApplicationDidTerminate:(FBProcessInfo *)applicationProcess expected:(BOOL)expected { - [self.logger logFormat:@"%@Container Application Did Terminate => %@ Expected %d", self.prefix, applicationProcess.shortDescription, expected]; + [self.logger logFormat:@"Container Application Did Terminate => %@ Expected %d", applicationProcess.shortDescription, expected]; } - (void)framebufferDidStart:(FBSimulatorFramebuffer *)framebuffer { - [self.logger logFormat:@"%@Framebuffer Did Start => %@", self.prefix, framebuffer]; + [self.logger logFormat:@"Framebuffer Did Start => %@", framebuffer]; } - (void)framebufferDidTerminate:(FBSimulatorFramebuffer *)framebuffer expected:(BOOL)expected { - [self.logger logFormat:@"%@Framebuffer Did Terminate => %@ Expected %d", self.prefix, framebuffer, expected]; + [self.logger logFormat:@"Framebuffer Did Terminate => %@ Expected %d", framebuffer, expected]; } - (void)simulatorDidLaunch:(FBProcessInfo *)launchdSimProcess { - [self.logger logFormat:@"%@Simulator Did launch => %@", self.prefix, launchdSimProcess.shortDescription]; + [self.logger logFormat:@"Simulator Did launch => %@", launchdSimProcess.shortDescription]; } - (void)simulatorDidTerminate:(FBProcessInfo *)launchdSimProcess expected:(BOOL)expected { - [self.logger logFormat:@"%@Simulator Did Terminate => %@ Expected %d", self.prefix, launchdSimProcess.shortDescription, expected]; + [self.logger logFormat:@"Simulator Did Terminate => %@ Expected %d", launchdSimProcess.shortDescription, expected]; } - (void)agentDidLaunch:(FBAgentLaunchConfiguration *)launchConfig didStart:(FBProcessInfo *)agentProcess stdOut:(NSFileHandle *)stdOut stdErr:(NSFileHandle *)stdErr { - [self.logger logFormat:@"%@Agent Did Launch => %@", self.prefix, agentProcess.shortDescription]; + [self.logger logFormat:@"Agent Did Launch => %@", agentProcess.shortDescription]; } - (void)agentDidTerminate:(FBProcessInfo *)agentProcess expected:(BOOL)expected { - [self.logger logFormat:@"%@Agent Did Terminate => Expected %d %@ ", self.prefix, expected, agentProcess.shortDescription]; + [self.logger logFormat:@"Agent Did Terminate => Expected %d %@", expected, agentProcess.shortDescription]; } - (void)applicationDidLaunch:(FBApplicationLaunchConfiguration *)launchConfig didStart:(FBProcessInfo *)applicationProcess stdOut:(NSFileHandle *)stdOut stdErr:(NSFileHandle *)stdErr { - [self.logger logFormat:@"%@Application Did Launch => %@", self.prefix, applicationProcess.shortDescription]; + [self.logger logFormat:@"Application Did Launch => %@", applicationProcess.shortDescription]; } - (void)applicationDidTerminate:(FBProcessInfo *)applicationProcess expected:(BOOL)expected { - [self.logger logFormat:@"%@Application Did Terminate => Expected %d %@", self.prefix, expected, applicationProcess.shortDescription]; + [self.logger logFormat:@"Application Did Terminate => Expected %d %@", expected, applicationProcess.shortDescription]; } - (void)diagnosticAvailable:(FBDiagnostic *)diagnostic { - [self.logger logFormat:@"%@Log Available => %@", self.prefix, diagnostic.shortDescription]; + [self.logger logFormat:@"Log Available => %@", diagnostic.shortDescription]; } - (void)didChangeState:(FBSimulatorState)state { - [self.logger logFormat:@"%@Did Change State => %@", self.prefix, [FBSimulator stateStringFromSimulatorState:state]]; + [self.logger logFormat:@"Did Change State => %@", [FBSimulator stateStringFromSimulatorState:state]]; } - (void)terminationHandleAvailable:(id)terminationHandle diff --git a/FBSimulatorControl/Framebuffer/FBSimulatorFramebuffer.m b/FBSimulatorControl/Framebuffer/FBSimulatorFramebuffer.m index b223c4a7..818e2578 100644 --- a/FBSimulatorControl/Framebuffer/FBSimulatorFramebuffer.m +++ b/FBSimulatorControl/Framebuffer/FBSimulatorFramebuffer.m @@ -61,6 +61,7 @@ static const NSInteger FBFramebufferLogFrameFrequency = 100; #pragma mark Initializers + (instancetype)withFramebufferService:(SimDeviceFramebufferService *)framebufferService hidPort:(mach_port_t)hidPort configuration:(FBSimulatorLaunchConfiguration *)launchConfiguration simulator:(FBSimulator *)simulator { + id logger = [simulator.logger withPrefix:[NSString stringWithFormat:@"%@:", simulator.udid]]; NSMutableArray *sinks = [NSMutableArray array]; BOOL useWindow = (launchConfiguration.options & FBSimulatorLaunchOptionsShowDebugWindow) == FBSimulatorLaunchOptionsShowDebugWindow; if (useWindow) { @@ -77,9 +78,8 @@ static const NSInteger FBFramebufferLogFrameFrequency = 100; id delegate = [FBFramebufferCompositeDelegate withDelegates:[sinks copy]]; dispatch_queue_t queue = dispatch_queue_create("com.facebook.FBSimulatorControl.simulatorframebuffer", DISPATCH_QUEUE_SERIAL); - id logger = [simulator.logger onQueue:queue]; - return [[self alloc] initWithFramebufferService:framebufferService onQueue:queue hidPort:hidPort eventSink:simulator.eventSink logger:logger delegate:delegate]; + return [[self alloc] initWithFramebufferService:framebufferService onQueue:queue hidPort:hidPort eventSink:simulator.eventSink logger:[logger onQueue:queue] delegate:delegate]; } - (instancetype)initWithFramebufferService:(SimDeviceFramebufferService *)framebufferService onQueue:(dispatch_queue_t)queue hidPort:(mach_port_t)hidPort eventSink:(id)eventSink logger:(id)logger delegate:(id)delegate diff --git a/FBSimulatorControl/Utility/FBSimulatorLogger.h b/FBSimulatorControl/Utility/FBSimulatorLogger.h index 5a179849..fb52f9f0 100644 --- a/FBSimulatorControl/Utility/FBSimulatorLogger.h +++ b/FBSimulatorControl/Utility/FBSimulatorLogger.h @@ -53,6 +53,14 @@ */ - (id)onQueue:(dispatch_queue_t)queue; +/** + Returns a Logger that will prefix all messages with the given string + + @param prefix the prefix to prepend to all messages. + @return a new Logger that will allows logging of messages on the provided queue. + */ +- (id)withPrefix:(NSString *)prefix; + @end @interface FBSimulatorLogger : NSObject diff --git a/FBSimulatorControl/Utility/FBSimulatorLogger.m b/FBSimulatorControl/Utility/FBSimulatorLogger.m index 6ab9c778..e8be8823 100644 --- a/FBSimulatorControl/Utility/FBSimulatorLogger.m +++ b/FBSimulatorControl/Utility/FBSimulatorLogger.m @@ -65,12 +65,13 @@ static const char *FBASLClientDispatchLocal = "fbsimulatorcontrol_asl_client"; @property (nonatomic, strong, readonly) FBASLClientManager *clientManager; @property (nonatomic, assign, readonly) asl_object_t client; @property (nonatomic, assign, readonly) int currentLevel; +@property (nonatomic, copy, readonly) NSString *prefix; @end @implementation FBSimulatorLogger_ASL -- (instancetype)initWithClientManager:(FBASLClientManager *)clientManager client:(asl_object_t)client currentLevel:(int)currentLevel +- (instancetype)initWithClientManager:(FBASLClientManager *)clientManager client:(asl_object_t)client currentLevel:(int)currentLevel prefix:(NSString *)prefix { self = [super init]; if (!self) { @@ -80,12 +81,14 @@ static const char *FBASLClientDispatchLocal = "fbsimulatorcontrol_asl_client"; _clientManager = clientManager; _client = client; _currentLevel = currentLevel; + _prefix = prefix; return self; } - (id)log:(NSString *)string { + string = self.prefix ? [self.prefix stringByAppendingFormat:@" %@", string] : string; asl_log(self.client, NULL, self.currentLevel, string.UTF8String, NULL); return self; } @@ -102,23 +105,28 @@ static const char *FBASLClientDispatchLocal = "fbsimulatorcontrol_asl_client"; - (id)info { - return [[FBSimulatorLogger_ASL alloc] initWithClientManager:self.clientManager client:self.client currentLevel:ASL_LEVEL_INFO]; + return [[FBSimulatorLogger_ASL alloc] initWithClientManager:self.clientManager client:self.client currentLevel:ASL_LEVEL_INFO prefix:self.prefix]; } - (id)debug { - return [[FBSimulatorLogger_ASL alloc] initWithClientManager:self.clientManager client:self.client currentLevel:ASL_LEVEL_DEBUG]; + return [[FBSimulatorLogger_ASL alloc] initWithClientManager:self.clientManager client:self.client currentLevel:ASL_LEVEL_DEBUG prefix:self.prefix]; } - (id)error { - return [[FBSimulatorLogger_ASL alloc] initWithClientManager:self.clientManager client:self.client currentLevel:ASL_LEVEL_ERR]; + return [[FBSimulatorLogger_ASL alloc] initWithClientManager:self.clientManager client:self.client currentLevel:ASL_LEVEL_ERR prefix:self.prefix]; } - (id)onQueue:(dispatch_queue_t)queue { asl_object_t client = [self.clientManager clientHandleForQueue:queue]; - return [[FBSimulatorLogger_ASL alloc] initWithClientManager:self.clientManager client:client currentLevel:self.currentLevel]; + return [[FBSimulatorLogger_ASL alloc] initWithClientManager:self.clientManager client:client currentLevel:self.currentLevel prefix:self.prefix]; +} + +- (id)withPrefix:(NSString *)prefix +{ + return [[FBSimulatorLogger_ASL alloc] initWithClientManager:self.clientManager client:self.client currentLevel:self.currentLevel prefix:prefix]; } @end @@ -133,7 +141,7 @@ static const char *FBASLClientDispatchLocal = "fbsimulatorcontrol_asl_client"; int fileDescriptor = writeToStdErr ? STDERR_FILENO : 0; FBASLClientManager *clientManager = [[FBASLClientManager alloc] initWithWritingToFileDescriptor:fileDescriptor debugLogging:debugLogging]; asl_object_t client = [clientManager clientHandleForQueue:dispatch_get_main_queue()]; - logger = [[FBSimulatorLogger_ASL alloc] initWithClientManager:clientManager client:client currentLevel:ASL_LEVEL_INFO]; + logger = [[FBSimulatorLogger_ASL alloc] initWithClientManager:clientManager client:client currentLevel:ASL_LEVEL_INFO prefix:nil]; }); return logger; } diff --git a/fbsimctl/fbsimctl/JSONLogger.m b/fbsimctl/fbsimctl/JSONLogger.m index 88909b69..fabae8e0 100644 --- a/fbsimctl/fbsimctl/JSONLogger.m +++ b/fbsimctl/fbsimctl/JSONLogger.m @@ -101,4 +101,10 @@ return [[JSONLogger alloc] initWithEventReporter:self.reporter currentLevel:ASL_LEVEL_ERR maxLevel:self.maxLevel dispatchToMain:dispatchToMain]; } +- (id)withPrefix:(NSString *)prefix +{ + // Ignore prefixing as 'subject' will be included instead. + return self; +} + @end