idb/FBControlCore/Utility/FBControlCoreLogger.m

182 строки
5.3 KiB
Mathematica
Исходник Обычный вид История

2015-09-18 19:58:01 +03:00
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "FBControlCoreLogger.h"
2015-09-18 19:58:01 +03:00
#import <asl.h>
2016-02-03 18:00:24 +03:00
@interface FBASLClientWrapper : NSObject
@property (nonatomic, assign, readonly) asl_object_t client;
@end
@implementation FBASLClientWrapper
- (instancetype)initWithClient:(asl_object_t)client
{
self = [super init];
if (!self) {
return nil;
}
_client = client;
return self;
}
- (void)dealloc
{
asl_free(self.client);
}
@end
2016-02-03 12:30:24 +03:00
2015-12-29 12:22:31 +03:00
/**
2016-02-03 12:30:24 +03:00
Manages asl client handles.
2015-12-29 12:22:31 +03:00
*/
2016-02-03 12:30:24 +03:00
@interface FBASLClientManager : NSObject
2015-12-29 11:56:19 +03:00
2016-02-03 14:13:09 +03:00
@property (nonatomic, assign, readonly) int fileDescriptor;
2016-02-03 12:30:24 +03:00
@property (nonatomic, assign, readonly) BOOL debugLogging;
2016-02-03 18:00:24 +03:00
@property (nonatomic, strong, readonly) NSMapTable *queueTable;
2015-12-29 11:56:19 +03:00
@end
2016-02-03 12:30:24 +03:00
@implementation FBASLClientManager
2015-12-29 11:56:19 +03:00
2016-02-03 14:13:09 +03:00
- (instancetype)initWithWritingToFileDescriptor:(int)fileDescriptor debugLogging:(BOOL)debugLogging
2015-12-29 11:56:19 +03:00
{
self = [super init];
if (!self) {
return nil;
}
2016-02-03 14:13:09 +03:00
_fileDescriptor = fileDescriptor;
2016-02-03 12:30:24 +03:00
_debugLogging = debugLogging;
2016-02-03 18:00:24 +03:00
_queueTable = [NSMapTable mapTableWithKeyOptions:NSMapTableWeakMemory valueOptions:NSMapTableObjectPointerPersonality];
2016-02-03 12:30:24 +03:00
2015-12-29 11:56:19 +03:00
return self;
}
2016-02-03 12:30:24 +03:00
- (asl_object_t)clientHandleForQueue:(dispatch_queue_t)queue
2015-12-29 11:56:19 +03:00
{
2016-02-03 18:00:24 +03:00
@synchronized (self)
{
FBASLClientWrapper *clientWrapper = [self.queueTable objectForKey:queue];
if (clientWrapper.client) {
return clientWrapper.client;
}
asl_object_t client = asl_open("FBControlCore", "com.facebook.FBControlCore", 0);
2016-02-03 18:00:24 +03:00
int filterLimit = self.debugLogging ? ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG) : ASL_FILTER_MASK_UPTO(ASL_LEVEL_INFO);
if (self.fileDescriptor >= STDIN_FILENO) {
int result = asl_add_output_file(client, self.fileDescriptor, ASL_MSG_FMT_STD, ASL_TIME_FMT_LCL, filterLimit, ASL_ENCODE_SAFE);
if (result != 0) {
asl_log(client, NULL, ASL_LEVEL_ERR, "Failed to add File Descriptor %d to client with error %d", self.fileDescriptor, result);
}
}
clientWrapper = [[FBASLClientWrapper alloc] initWithClient:client];
[self.queueTable setObject:clientWrapper forKey:queue];
2016-02-03 12:30:24 +03:00
return client;
}
2015-12-29 11:56:19 +03:00
}
@end
@interface FBControlCoreLogger_ASL : NSObject <FBControlCoreLogger>
2015-12-29 11:56:19 +03:00
2016-02-03 12:30:24 +03:00
@property (nonatomic, strong, readonly) FBASLClientManager *clientManager;
@property (nonatomic, assign, readonly) asl_object_t client;
2015-12-29 11:56:19 +03:00
@property (nonatomic, assign, readonly) int currentLevel;
2016-02-03 14:23:14 +03:00
@property (nonatomic, copy, readonly) NSString *prefix;
2015-12-29 11:56:19 +03:00
@end
@implementation FBControlCoreLogger_ASL
2015-12-29 11:56:19 +03:00
2016-02-03 14:23:14 +03:00
- (instancetype)initWithClientManager:(FBASLClientManager *)clientManager client:(asl_object_t)client currentLevel:(int)currentLevel prefix:(NSString *)prefix
2015-12-29 11:56:19 +03:00
{
self = [super init];
if (!self) {
return nil;
}
2016-02-03 12:30:24 +03:00
_clientManager = clientManager;
_client = client;
2015-12-29 11:56:19 +03:00
_currentLevel = currentLevel;
2016-02-03 14:23:14 +03:00
_prefix = prefix;
2015-12-29 11:56:19 +03:00
return self;
}
- (id<FBControlCoreLogger>)log:(NSString *)string
2015-12-29 11:56:19 +03:00
{
2016-02-03 14:23:14 +03:00
string = self.prefix ? [self.prefix stringByAppendingFormat:@" %@", string] : string;
2016-02-03 13:01:37 +03:00
asl_log(self.client, NULL, self.currentLevel, string.UTF8String, NULL);
2015-12-29 11:56:19 +03:00
return self;
}
- (id<FBControlCoreLogger>)logFormat:(NSString *)format, ...
2015-12-29 11:56:19 +03:00
{
va_list args;
va_start(args, format);
NSString *string = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
return [self log:string];
}
- (id<FBControlCoreLogger>)info
2015-12-29 11:56:19 +03:00
{
return [[FBControlCoreLogger_ASL alloc] initWithClientManager:self.clientManager client:self.client currentLevel:ASL_LEVEL_INFO prefix:self.prefix];
2015-12-29 11:56:19 +03:00
}
- (id<FBControlCoreLogger>)debug
2015-12-29 11:56:19 +03:00
{
return [[FBControlCoreLogger_ASL alloc] initWithClientManager:self.clientManager client:self.client currentLevel:ASL_LEVEL_DEBUG prefix:self.prefix];
2015-12-29 11:56:19 +03:00
}
- (id<FBControlCoreLogger>)error
2015-12-29 11:56:19 +03:00
{
return [[FBControlCoreLogger_ASL alloc] initWithClientManager:self.clientManager client:self.client currentLevel:ASL_LEVEL_ERR prefix:self.prefix];
2015-12-29 11:56:19 +03:00
}
- (id<FBControlCoreLogger>)onQueue:(dispatch_queue_t)queue
2015-12-29 17:32:53 +03:00
{
2016-02-03 12:30:24 +03:00
asl_object_t client = [self.clientManager clientHandleForQueue:queue];
return [[FBControlCoreLogger_ASL alloc] initWithClientManager:self.clientManager client:client currentLevel:self.currentLevel prefix:self.prefix];
2016-02-03 14:23:14 +03:00
}
- (id<FBControlCoreLogger>)withPrefix:(NSString *)prefix
2016-02-03 14:23:14 +03:00
{
return [[FBControlCoreLogger_ASL alloc] initWithClientManager:self.clientManager client:self.client currentLevel:self.currentLevel prefix:prefix];
2015-12-29 17:32:53 +03:00
}
2015-12-29 11:56:19 +03:00
@end
@implementation FBControlCoreLogger
2015-09-18 19:58:01 +03:00
+ (id<FBControlCoreLogger>)aslLoggerWritingToStderrr:(BOOL)writeToStdErr withDebugLogging:(BOOL)debugLogging
2015-12-29 11:56:19 +03:00
{
2016-02-03 15:47:46 +03:00
int fileDescriptor = writeToStdErr ? STDERR_FILENO : 0;
return [self aslLoggerWritingToFileDescriptor:fileDescriptor withDebugLogging:debugLogging];
}
+ (id<FBControlCoreLogger>)aslLoggerWritingToFileDescriptor:(int)fileDescriptor withDebugLogging:(BOOL)debugLogging
2016-02-03 15:47:46 +03:00
{
FBASLClientManager *clientManager = [[FBASLClientManager alloc] initWithWritingToFileDescriptor:fileDescriptor debugLogging:debugLogging];
asl_object_t client = [clientManager clientHandleForQueue:dispatch_get_main_queue()];
FBControlCoreLogger_ASL *logger = [[FBControlCoreLogger_ASL alloc] initWithClientManager:clientManager client:client currentLevel:ASL_LEVEL_INFO prefix:nil];
2015-12-29 11:56:19 +03:00
return logger;
}
2015-09-18 19:58:01 +03:00
@end