printf

  • Is there a function in one of the frameworks that's like printf, but
    allows the output conversion '%@' in its template? Eg

      likePrintf("%@ is allowable", @"this string");
    or better:
      likePrintf(@"%@ is allowable", @"this string");

    I KNOW about NSLog. The problem is all that stuff that it prints first:

    NSLog(@"log just this");
    2007-12-13 07:35:53.125 helloWorld[8101:10b] log just this
  • Check out NSStrings +[stringWithFormat:] method. You can then take the
    resulting string and give it to whatever print function you like,
    converting it with -[UTF8String] if necessary.

    Hank

    On Dec 13, 2007, at 9:40 AM, Roland Silver wrote:

    > Is there a function in one of the frameworks that's like printf, but
    > allows the output conversion '%@' in its template? Eg
    >
    > likePrintf("%@ is allowable", @"this string");
    > or better:
    > likePrintf(@"%@ is allowable", @"this string");
    >
    > I KNOW about NSLog. The problem is all that stuff that it prints
    > first:
    >
    > NSLog(@"log just this");
    > 2007-12-13 07:35:53.125 helloWorld[8101:10b] log just this
    >

    Hank Heijink
    <hank.list...>
  • On Dec 13, 2007 6:40 AM, Roland Silver <rollos...> wrote:
    > Is there a function in one of the frameworks that's like printf, but
    > allows the output conversion '%@' in its template? Eg
    >
    > likePrintf("%@ is allowable", @"this string");
    > or better:
    > likePrintf(@"%@ is allowable", @"this string");

    No, but it's relatively easy to write your own:

    int likePrintf(NSString *format, ...) {
        va_list args;
        va_start(args, format);

        NSString    *output = [[NSString alloc] initWithFormat: format
    arguments: args];
        va_end(args);

        int result  = printf("%s", [output UTF8String]);
        [output release];

        return result;
    }

    --
    Clark S. Cox III
    <clarkcox3...>
  • One additional tip:

    If you're writing log-file-like output and want it as readable
    ASCII text, consider using -[NSString
    cStringUsingEncoding:NSNonLossyASCIIStringEncoding] instead of
    -[NSString UTF8String].

    While UTF8 is great for machine-to-machine transport,
    NSNonLossyASCIIStringEncoding encodes non-ASCII characters using
    a simple ASCII escape sequence (\0134) that makes reading or
    parsing the file using plain ol' text editors/viewers easy. Even
    better, NSNonLossyASCIIStringEncoding is bidirectional like
    UTF8, so if you ever have to read the text back in as data use
    the same encoding; NSNonLossyASCIIStringEncoding will parse the
    escape sequences and convert them back to their original Unicode values.

    Clark Cox <mailto:<clarkcox3...> wrote (Thursday, December
    13, 2007 8:03 AM -0800):

    > On Dec 13, 2007 6:40 AM, Roland Silver <rollos...>  wrote:
    >> Is there a function in one of the frameworks that's like printf, but
    >> allows the output conversion '%@' in its template? Eg
    >>
    >> likePrintf("%@ is allowable", @"this string");
    >> or better:
    >> likePrintf(@"%@ is allowable", @"this string");
    >
    > No, but it's relatively easy to write your own:
    >
    > int likePrintf(NSString *format, ...) {
    > va_list args;
    > va_start(args, format);
    >
    > NSString    *output = [[NSString alloc] initWithFormat: format
    > arguments: args];
    > va_end(args);
    >
    > int result  = printf("%s", [output UTF8String]);
    > [output release];
    >
    > return result;
    > }
    >
    --
    James Bucanek
  • On Dec 13, 2007 7:51 AM, James Bucanek <subscriber...> wrote:
    > One additional tip:
    >
    > If you're writing log-file-like output and want it as readable
    > ASCII text, consider using -[NSString
    > cStringUsingEncoding:NSNonLossyASCIIStringEncoding] instead of
    > -[NSString UTF8String].
    >
    > While UTF8 is great for machine-to-machine transport,
    > NSNonLossyASCIIStringEncoding encodes non-ASCII characters using
    > a simple ASCII escape sequence (\0134) that makes reading or
    > parsing the file using plain ol' text editors/viewers easy.

    However, every popular text editor/viewer on MacOSX that I can think
    of using to view logs is capable of displaying UTF-8 text. By using
    NSNonLossyASCIIStringEncoding you're just obfuscating the text.

    > Even
    > better, NSNonLossyASCIIStringEncoding is bidirectional like
    > UTF8, so if you ever have to read the text back in as data use
    > the same encoding; NSNonLossyASCIIStringEncoding will parse the
    > escape sequences and convert them back to their original Unicode values.
    >
    > Clark Cox <mailto:<clarkcox3...> wrote (Thursday, December
    > 13, 2007 8:03 AM -0800):
    >
    >
    >> On Dec 13, 2007 6:40 AM, Roland Silver <rollos...> wrote:
    >>> Is there a function in one of the frameworks that's like printf, but
    >>> allows the output conversion '%@' in its template? Eg
    >>>
    >>> likePrintf("%@ is allowable", @"this string");
    >>> or better:
    >>> likePrintf(@"%@ is allowable", @"this string");
    >>
    >> No, but it's relatively easy to write your own:
    >>
    >> int likePrintf(NSString *format, ...) {
    >> va_list args;
    >> va_start(args, format);
    >>
    >> NSString    *output = [[NSString alloc] initWithFormat: format
    >> arguments: args];
    >> va_end(args);
    >>
    >> int result  = printf("%s", [output UTF8String]);
    >> [output release];
    >>
    >> return result;
    >> }
    >>
    > --
    > James Bucanek
    >
    >

    --
    Clark S. Cox III
    <clarkcox3...>
  • What about this stated functionality of NSLogv from Apple's
    documentation.
    "Output from NSLogv is serialized, in that only one thread in a
    process can be doing the writing/logging described above at a time.
    All attempts at writing/logging a message complete before the next
    thread can begin its attempts."

    "The effects of NSLogv are not serialized with subsystems other than
    those discussed above (such as the standard I/O package) and do not
    produce side effects on those subsystems (such as causing buffered
    output to be flushed, which may be undesirable)."

    Does a printf provide this or is this something to worry about? I just
    finished writing my own simple replacement for NSLog several days ago
    using printf and was wondering if I was going to run into problems
    down the road.

    Richard

    On Dec 13, 2007, at 8:03 AM, Clark Cox wrote:

    > No, but it's relatively easy to write your own:
  • In my experience, if two threads are calling printf at once, their
    output can be arbitrarily interleaved. It's not unstable, but it can be
    a little goofy.

    Richard Somers wrote:
    > What about this stated functionality of NSLogv from Apple's
    > documentation.
    > "Output from NSLogv is serialized, in that only one thread in a
    > process can be doing the writing/logging described above at a time.
    > All attempts at writing/logging a message complete before the next
    > thread can begin its attempts."
    >
    > "The effects of NSLogv are not serialized with subsystems other than
    > those discussed above (such as the standard I/O package) and do not
    > produce side effects on those subsystems (such as causing buffered
    > output to be flushed, which may be undesirable)."
    >
    > Does a printf provide this or is this something to worry about? I just
    > finished writing my own simple replacement for NSLog several days ago
    > using printf and was wondering if I was going to run into problems
    > down the road.
    >
    > Richard
    >
    > On Dec 13, 2007, at 8:03 AM, Clark Cox wrote:
    >
    >> No, but it's relatively easy to write your own:

  • On Dec 14, 2007 8:51 AM, Richard Somers <rsomers.lists...> wrote:
    > What about this stated functionality of NSLogv from Apple's
    > documentation.
    > "Output from NSLogv is serialized, in that only one thread in a
    > process can be doing the writing/logging described above at a time.
    > All attempts at writing/logging a message complete before the next
    > thread can begin its attempts."
    >
    > "The effects of NSLogv are not serialized with subsystems other than
    > those discussed above (such as the standard I/O package) and do not
    > produce side effects on those subsystems (such as causing buffered
    > output to be flushed, which may be undesirable)."
    >
    > Does a printf provide this or is this something to worry about?

    printf is part of "the standard I/O package", so no, there's nothing
    to worry about.

    > I just
    > finished writing my own simple replacement for NSLog several days ago
    > using printf and was wondering if I was going to run into problems
    > down the road.

    No

    --
    Clark S. Cox III
    <clarkcox3...>
  • On Dec 14, 2007, at 8:51 AM, Richard Somers wrote:

    > I just finished writing my own simple replacement for NSLog several
    > days ago using printf
    >
    Any particular reason why?  If you want something more sophisticated,
    Timothy Reaves reminded us last night about Log4Cocoa (<>)... see also Chris' comments at <<A href="http://chanson.livejournal.com/tag/logging">http://chanson.livejournal.com/tag/logging>.

    mmalc
  • Log4Cocoa is to complex for my needs right now. I use logging for
    debugging, a lot. So much so that I decided that NSLog and printf just
    did not seem to fit my needs very well. I have a single debug
    replacement for NSLog for Objective-C and another one for printf for C
    or C++. The output is the same for both and simply "Debug (file:line)
    log message". Debugging control is not by level but set globally or
    for each file. With the clever use of the C preprocessor, macros, and
    a header file I can very easily and quickly turn debug logging off or
    on, globally for all files, or on a file by file basis. I also have
    indented function or method tracing which can be turned off or on the
    same way. Also all debug logging or debug function tracing is complied
    out in the release configuration.

    Richard

    On Dec 14, 2007, at 11:11 AM, mmalc crawford wrote:

    > On Dec 14, 2007, at 8:51 AM, Richard Somers wrote:
    >
    >> I just finished writing my own simple replacement for NSLog several
    >> days ago using printf
    >>
    > Any particular reason why?  If you want something more
    > sophisticated, Timothy Reaves reminded us last night about Log4Cocoa
    > (<http://sourceforge.net/projects/log4cocoa/>)... see also Chris'
    > comments at <http://chanson.livejournal.com/tag/logging>.
    >
    > mmalc
  • I just started using the debugger in Xcode Leopard. Wow! I may start
    using the debugger more and logging less. Thanks Apple.

    Richard

    On Dec 14, 2007, at 11:11 AM, mmalc crawford wrote:

    >
    > On Dec 14, 2007, at 8:51 AM, Richard Somers wrote:
    >
    >> I just finished writing my own simple replacement for NSLog several
    >> days ago using printf
    >>
    > Any particular reason why?  If you want something more
    > sophisticated, Timothy Reaves reminded us last night about Log4Cocoa
    > (<http://sourceforge.net/projects/log4cocoa/>)... see also Chris'
    > comments at <http://chanson.livejournal.com/tag/logging>.
    >
    > mmalc
    >
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today