如何在目标c中通过POST请求捕获stderr输出并将消息发送到远程日志文件?

萨沙·格里弗斯

我有一个简单的网络服务在线监听发布请求。它写入本地文件字符串作为正文中的参数发送给它。

现在,我有一个 ios 应用程序,我想将应用程序的 stderr 的每条消息发送到网络服务,以记录在线文件上的错误,并能够远程实时查看应用程序错误。

我知道我可以将 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 请求并将消息写入远程日志而不是写入设备本地文件?

萨沙·格里弗斯

可以使用此代码连接管道捕获标准错误

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将pprint模块的输出发送到日志文件

通过连接池将JMS消息发送到JBoss AS中的远程队列

如何通过Java(Android App)中的POST请求将音频文件发送到服务器?

如何通过GELF将Spring Boot访问日志发送到远程服务器?

如何将输出发送到stderr?

如何将cURL详细输出通过管道发送到日志记录模块?

在C#中,如何使用POST方法将数据从SQL Server发送到HTTP请求?

如何在python中定义参数并将其发送到CPLEX mod文件?

如何在Flutter中通过HTTP Post请求将AuthPayload发送到Firebase Realtime-Database

如何查看对从目标C可可发送到PHP的GET请求的响应

如何捕获kivy-client崩溃日志并将其发送到我的服务器?

如何通过php选择文件夹并将脚本发送到路径

如何在Drupal中创建表单并将其发送到模板文件

如何在C#中创建JSONArray并将数据发送到Android应用

如何在php中读取文本文件并将结果发送到其他html文件

如何通过SSH连接从Macbook Pro上的远程Linux计算机复制文件并将文件发送到远程Linux计算机?

如何使用“ scp”通过网关将我的文件直接发送到目标位置?

如何通过APN将推送通知消息发送到特定目标?

如何停止Loggly从打印到stdout(或stderr)并将日志发送到云?

如何从全局参数读取输入文件,进行修改并将其发送到输出?[XSLT]

如何将输出发送到日志文件和控制台?

Logstash-将输出从日志文件发送到麋鹿

如何获取SSH日志并发送到macOS中的远程syslog服务器?

如何防止日志异常捕获并将日志发送到数据库 c#?

是否可以运行 C++ 代码并将输出发送到 bash 文件中的 python 代码?

Apache NiFi 可以配置 logback.xml 文件以通过 UDP 将日志发送到远程服务器吗?

将 POST 请求从 js 文件发送到 php 文件

过滤日志并将输出实时发送到新文件

如何通过管道将命令发送到 grep 并获取退出状态,同时仍将命令输出发送到 stdout 并将错误发送到 stderr?