0

我正在研究一个将用于我自己的调试目的的类。我打算将它作为调试器控制台的设备版本。大多数情况下,我关心捕获 NSLog() 语句。无论我登录到控制台,我只能从fgetc(). 我期望从中看到的fgetc()是自上次调用以来发送到 stderr 的字符update。下面是 UITextView 的一个子类:

stdErrFP在标题中定义为:FILE* stdErrFP;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
       stdErrFP = fdopen (fileno (stderr) , "w");
       if (!stdErrFP)
           NSLog(@"errno - %s", strerror(errno));
  
       [self update];
       [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(makeSomeNoise) userInfo:nil repeats:YES];
       [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(update) userInfo:nil repeats:YES];
    }
    return self;
}

-(void) update{
 char errBuff[2] = {0x00, '\0'};
 do{
  errBuff[0] = fgetc(stdErrFP);
  self.text = [NSString stringWithFormat:@"%@%@", self.text,  [NSString stringWithCString:errBuff]];
 }while (errBuff[0] != EOF); 
}

-(void) makeSomeNoise{
 CFShow([NSString stringWithString:@"noisy CFFunction"]);
 NSLog(@"noisy NSLog");
 char message[] = "noisy fprintf";
 fprintf(stderr, message);
}

-(void)makeSomeNoise有这么多不同的日志类型,因为Eric Sadun 写道 NSLog 不记录到 stderr,所以我想排除这种可能性。我不确定这是否与我的问题有关,但我注意到fdopen()除非标志为“w”,否则总是失败,这是不对的。

更新:因为fdopen()我错了,不是只有“w”有效,而是只有“r”无效。(即“a”和“w”都有效)。当我使用 "r" 时的 errno 是:No such file or directory. 所以看起来我没有正确连接到stderr。StackOverflow 天才,请帮忙。

4

1 回答 1

0

尽我所能编码,您无法从标准错误中读取。stderr 缓冲区就像一根消防水带,试图抓取任何有意义的东西都是非常依赖时间的。

于 2010-07-16T02:52:23.773 回答