1

I'm using a C library in an iOS app. Sometimes the library calls a printf command and prints to the console in Xcode. Is there a way to access the printed text within swift?

I'd like to make some of the outputs visible within the app.

subjord
  • 109
  • 1
  • 10
  • 1
    see this once http://stackoverflow.com/questions/24003092/how-to-print-to-console-using-swift-playground – Anbu.Karthik Aug 05 '16 at 13:24
  • 1
    Please elaborate more. Add screenshots or example as to what exactly you want to do. – Meet Aug 05 '16 at 13:40
  • The C library function int printf(const char *format, ...) sends formatted output to stdout. You have to change the function in the lib to output where you need it – Peter Ahlberg Aug 05 '16 at 14:01
  • I use a library witch got an process output of an function, that is only printed to the command line. Now I somehow need to recover the printed text to use it within the app. – subjord Aug 09 '16 at 10:50

2 Answers2

1

If I understand what you're asking....

I'm not sure whether it's possible in pure Swift but you could add a .m file to your Swift project and intercept printf calls in there. When you receive one, you can decide what else you need to do with it.

In this example, I post a notification during printf that I'm listening for inside "AppDelegate.swift".

int printf(const char * __restrict format, ...)
{
    va_list args;
    va_start(args, format);
    NSString *f = [[NSString alloc] initWithUTF8String:format];
    NSString *string  = [[NSString alloc] initWithFormat:f arguments:args];
    puts([string UTF8String]);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"printfNotification"
                                                        object:string];
    va_end(args);
    return (int)[string length];
}
Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • So I just write this code in an printfinterception.m, which I place in my project folder? After this I create a func printfNotification() within AppDeligate.swift and it works? Did I forgot some steps? How do i tell xCode to use the new printf function, when its called in the library? – subjord Aug 09 '16 at 11:19
  • By default, libraries are used to resolve symbols that are not explicitly present in your compiled code. Unless you're doing something at link time to force symbols to load from a library, supplying your own `printf` should make the library lookup unnecessary. – Phillip Mills Aug 09 '16 at 12:39
  • (Missing step: you do have to tell your delegate to observe the notification.) – Phillip Mills Aug 09 '16 at 12:43
1
func dprint(_ items: Any...) {
    
    let string: String
    if items.count == 1, let s = items.first as? String {
        string = s
    } else if items.count > 1, let format = items.first as? String, let arguments = Array(items[1..<items.count]) as? [CVarArg] {
        string = String(format: format, arguments: arguments)
    } else {
        string = ""
    }
    print(string)
    
}
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179