#define STDIN_FILENO 0 /* standard input file descriptor */ #define STDOUT_FILENO 1 /* standard output file descriptor */ #define STDERR_FILENO 2 /* standard error file descriptor */
NSLog(@"iOS NSLog"); fprintf (stderr, "%s\n", "fprintf log");
#import <asl.h>
// 从日志的对象aslmsg中获取我们需要的数据
+(instancetype)logMessageFromASLMessage:(aslmsg)aslMessage{
SystemLogMessage *logMessage = [[SystemLogMessage alloc] init];
const char *timestamp = asl_get(aslMessage, ASL_KEY_TIME);
if (timestamp) {
NSTimeInterval timeInterval = [@(timestamp) integerValue];
const char *nanoseconds = asl_get(aslMessage, ASL_KEY_TIME_NSEC);
if (nanoseconds) {
timeInterval += [@(nanoseconds) doubleValue] / NSEC_PER_SEC;
}
logMessage.timeInterval = timeInterval;
logMessage.date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
}
const char *sender = asl_get(aslMessage, ASL_KEY_SENDER);
if (sender) {
logMessage.sender = @(sender);
}
const char *messageText = asl_get(aslMessage, ASL_KEY_MSG);
if (messageText) {
logMessage.messageText = @(messageText);//NSLog写入的文本内容
}
const char *messageID = asl_get(aslMessage, ASL_KEY_MSG_ID);
if (messageID) {
logMessage.messageID = [@(messageID) longLongValue];
}
return logMessage;
}
+ (NSMutableArray<SystemLogMessage *> *)allLogMessagesForCurrentProcess{
asl_object_t query = asl_new(ASL_TYPE_QUERY);
// Filter for messages from the current process. Note that this appears to happen by default on device, but is required in the simulator.
NSString *pidString = [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]];
asl_set_query(query, ASL_KEY_PID, [pidString UTF8String], ASL_QUERY_OP_EQUAL);
aslresponse response = asl_search(NULL, query);
aslmsg aslMessage = NULL;
NSMutableArray *logMessages = [NSMutableArray array];
while ((aslMessage = asl_next(response))) {
[logMessages addObject:[SystemLogMessage logMessageFromASLMessage:aslMessage]];
}
asl_release(response);
return logMessages;
}
- (void)redirectStandardOutput{
//记录标准输出及错误流原始文件描述符
self.outFd = dup(STDOUT_FILENO);
self.errFd = dup(STDERR_FILENO);
#if BETA_BUILD
stdout->_flags = 10;
NSPipe *outPipe = [NSPipe pipe];
NSFileHandle *pipeOutHandle = [outPipe fileHandleForReading];
dup2([[outPipe fileHandleForWriting] fileDescriptor], STDOUT_FILENO);
[pipeOutHandle readInBackgroundAndNotify];
stderr->_flags = 10;
NSPipe *errPipe = [NSPipe pipe];
NSFileHandle *pipeErrHandle = [errPipe fileHandleForReading];
dup2([[errPipe fileHandleForWriting] fileDescriptor], STDERR_FILENO);
[pipeErrHandle readInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(redirectOutNotificationHandle:) name:NSFileHandleReadCompletionNotification object:pipeOutHandle];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(redirectErrNotificationHandle:) name:NSFileHandleReadCompletionNotification object:pipeErrHandle];
#endif
}
-(void)recoverStandardOutput{
#if BETA_BUILD
dup2(self.outFd, STDOUT_FILENO);
dup2(self.errFd, STDERR_FILENO);
[[NSNotificationCenter defaultCenter] removeObserver:self];
#endif
}
// 重定向之后的NSLog输出
- (void)redirectOutNotificationHandle:(NSNotification *)nf{
#if BETA_BUILD
NSData *data = [[nf userInfo] objectForKey:NSFileHandleNotificationDataItem];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// YOUR CODE HERE... 保存日志并上传或展示
#endif
[[nf object] readInBackgroundAndNotify];
}
// 重定向之后的错误输出
- (void)redirectErrNotificationHandle:(NSNotification *)nf{
#if BETA_BUILD
NSData *data = [[nf userInfo] objectForKey:NSFileHandleNotificationDataItem];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// YOUR CODE HERE... 保存日志并上传或展示
#endif
[[nf object] readInBackgroundAndNotify];
}
FILE * freopen ( const char * filename, const char * mode, FILE * stream );
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; NSString *loggingPath = [documentsPath stringByAppendingPathComponent:@"/xxx.log"]; //redirect NSLog freopen([loggingPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
// 重定向 int origin1 = dup(STDERR_FILENO); FILE * myFile = freopen([loggingPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr); // 恢复重定向 dup2(origin1, STDERR_FILENO);
- (dispatch_source_t)_startCapturingWritingToFD:(int)fd {
int fildes[2];
pipe(fildes); // [0] is read end of pipe while [1] is write end
dup2(fildes[1], fd); // Duplicate write end of pipe "onto" fd (this closes fd)
close(fildes[1]); // Close original write end of pipe
fd = fildes[0]; // We can now monitor the read end of the pipe
char* buffer = malloc(1024);
NSMutableData* data = [[NSMutableData alloc] init];
fcntl(fd, F_SETFL, O_NONBLOCK);
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, fd, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
dispatch_source_set_cancel_handler(source, ^{
free(buffer);
});
dispatch_source_set_event_handler(source, ^{
@autoreleasepool {
while (1) {
ssize_t size = read(fd, buffer, 1024);
if (size <= 0) {
break;
}
[data appendBytes:buffer length:size];
if (size < 1024) {
break;
}
}
NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//printf("aString = %s",[aString UTF8String]);
//NSLog(@"aString = %@",aString);
// Do something
}
});
dispatch_resume(source);
return source;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有