投稿リクエストをオンラインでリッスンする簡単なWebサービスがあります。本体のパラメータとして送信された文字列をローカルファイルに書き込みます。
今、私はiosアプリを持っており、アプリのstderrの各メッセージをWebサービスに送信して、オンラインファイルにエラーを記録し、アプリのエラーをリアルタイムでリモートで確認できるようにしたいと考えています。
stderrメッセージを次のようなファイル(デバイスに対してローカル)にリダイレクトできることを知っています
+ (void) redirectNSLogToDocuments {
NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [allPaths objectAtIndex:0];
NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:@"errLog.txt"];
freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}
私はこのようなPOSTリクエストを送信できることを知っているので、文字列 'errorString'をリモートログに書き込みます
NSString *errorString=@"errorIOS";
NSString *errorData =[NSString stringWithFormat:@"errorString=%@",errorString];
NSData *postData = [errorData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://mywebserviceurl"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
しかし、エラーのメッセージが発生するたびにstderrメッセージをリダイレクトして、WebサービスへのPOSTリクエストをトリガーし、デバイスのローカルファイルに書き込む代わりにリモートログにメッセージを書き込むようにするにはどうすればよいですか?
このコードでパイプを取り付けてstderrを捕まえることができます
NSPipe* pipe = [NSPipe pipe];
NSFileHandle* pipeReadHandle = [pipe fileHandleForReading];
dup2([[pipe fileHandleForWriting] fileDescriptor], fileno(stderr));
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, [pipeReadHandle fileDescriptor], 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
dispatch_source_set_event_handler(source, ^{
void* data = malloc(4096);
ssize_t readResult = 0;
do
{
errno = 0;
readResult = read([pipeReadHandle fileDescriptor], data, 4096);
} while (readResult == -1 && errno == EINTR);
if (readResult > 0)
{
//AppKit UI should only be updated from the main thread
dispatch_async(dispatch_get_main_queue(),^{
NSString* stdOutString = [[NSString alloc] initWithBytesNoCopy:data length:readResult encoding:NSUTF8StringEncoding freeWhenDone:YES];
NSAttributedString* stdOutAttributedString = [[NSAttributedString alloc] initWithString:stdOutString];
[appFunctions postDataToRemoteLogWithMessage:stdOutAttributedString];
});
}
else{free(data);}
});
dispatch_resume(source);
したがって、関数を追加すると、結果を得ることができます
+ (NSString *) postDataToRemoteLogWithMessage:(NSString *)errorString{
// Send post request to webservice
NSString *errorData =[NSString stringWithFormat:@"errorString=%@",errorString];
NSData *postData = [errorData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://mywebserviceurl"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn) {
//NSLog(@"Connection Successful");
} else {
//NSLog(@"Connection could not be made");
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加