3

I would like to check if NSLog() for some string has actually printed out something.

Is there a way I can check the length of the output of an NSLog statement or create a string from it? It's for a proof of concept, not a practical solution, so best practice concerns can be set aside.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
John Lane
  • 1,112
  • 1
  • 14
  • 32
  • 2
    Are you trying to capture `NSLog` statements coming from code that you don't control, or is this something where you could replace the `NSLog` statement with something else? – gaige Jul 05 '12 at 11:36
  • I'm trying to capture statements. – John Lane Jul 05 '12 at 11:37
  • 1
    Well , I'm not sure how everything should be done , but I'm thinking a starting point could be checking the `stdout` file. I think NSLog is writing in it , just like printf does. What I don't know is if and when the `stdout` flushes itself. – George Jul 05 '12 at 11:49
  • `NSLog` actually writes to `stderr`. – Nikolai Ruhe Jul 08 '13 at 18:24

2 Answers2

0

Try checking the length:

NSLog(@"%i",[string length]);

int len = [myString length];
if(len == 0){
   NSLog(@"String is empty");
}
else{
  NSLog(@"String is : %@", myString);
}

If you want to check whether the string is null or not then try:

if ((NSNull *)string != [NSNull null]){

}
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Rinju Jain
  • 1,694
  • 1
  • 14
  • 23
  • This doesn't work for me. I know that I am passing data to an NSLog and I know it isn't null. I want to try and retrieve it back from the log as a proof of concept. – John Lane Jul 05 '12 at 12:16
0

You may want to redirect your NSLog() output to a file. Take a look at "Redirect NSLog to stdout?".

This will only affect NSLog() calls from your application.

int fd = creat ("/Users/parag/Desktop/my_log", S_IRUSR | S_IWUSR | S_IRGRP |
S_IROTH);
close (STDERR_FILENO);
dup (fd);
close (fd);
NSLog(@"this will be written to my_log"); 
// Now you can retrieve it back from the my_log as a proof of concept
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144