Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6528985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:35:11+00:00 2026-05-25T09:35:11+00:00

Is it possible to write every NSLog not only into console, but into a

  • 0

Is it possible to write every NSLog not only into console, but into a file too? I want to prepare this without replacing NSLog into someExternalFunctionForLogging.

It will be real problem to replace all NSLog. Maybe there is possibility for parsing data from console or catching messages?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:35:12+00:00Added an answer on May 25, 2026 at 9:35 am

    Option 1: Use ASL

    NSLog outputs log to ASL (Apple’s version of syslog) and console, meaning it is already writing to a file in your Mac when you use the iPhone simulator. If you want to read it open the application Console.app, and type the name of your application in the filter field. To do the same in your iPhone device, you would need to use the ASL API and do some coding.

    Option 2: write to a file

    Let’s say you are running on the simulator and you don’t want to use the Console.app. You can redirect the error stream to a file of your liking using freopen:
    freopen([path cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
    See this explanation and sample project for details.

    Or you can override NSLog with a custom function using a macro. Example, add this class to your project:

    // file Log.h
    #define NSLog(args...) _Log(@"DEBUG ", __FILE__,__LINE__,__PRETTY_FUNCTION__,args);
    @interface Log : NSObject
    void _Log(NSString *prefix, const char *file, int lineNumber, const char *funcName, NSString *format,...);
    @end
    
    // file Log.m
    #import "Log.h"
    @implementation Log
    void _Log(NSString *prefix, const char *file, int lineNumber, const char *funcName, NSString *format,...) {
        va_list ap;
        va_start (ap, format);
        format = [format stringByAppendingString:@"\n"];
        NSString *msg = [[NSString alloc] initWithFormat:[NSString stringWithFormat:@"%@",format] arguments:ap];   
        va_end (ap);
        fprintf(stderr,"%s%50s:%3d - %s",[prefix UTF8String], funcName, lineNumber, [msg UTF8String]);
        [msg release];
    }
    @end
    

    And import it project wide adding the following to your <application>-Prefix.pch:

    #import "Log.h"
    

    Now every call to NSLog will be replaced with your custom function without the need to touch your existing code. However, the function above is only printing to console. To add file output, add this function above _Log:

    void append(NSString *msg){
        // get path to Documents/somefile.txt
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"logfile.txt"];
        // create if needed
        if (![[NSFileManager defaultManager] fileExistsAtPath:path]){
            fprintf(stderr,"Creating file at %s",[path UTF8String]);
            [[NSData data] writeToFile:path atomically:YES];
        } 
        // append
        NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path];
        [handle truncateFileAtOffset:[handle seekToEndOfFile]];
        [handle writeData:[msg dataUsingEncoding:NSUTF8StringEncoding]];
        [handle closeFile];
    }
    

    and add this line below fprintf in the _Log function:

    append(msg);
    

    File writing also works in your iPhone device, but the file will be created in a directory inside it, and you won’t be able to access unless you add code to send it back to your mac, or show it on a view inside your app, or use iTunes to add the documents directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to write code in a Flex application that will only be
I wonder is it possible to write a console application that will surf to
I'm attempting to write a program that will generate a text file with every
Is it possible to write a script that will execute every day in certain
Is it possible to write cron expression for trigger that must fire every day
Write a program to find the largest possible rectangle of letters such that every
Clearly I must not be doing something right but this happens on random occasions.
I was wondering, is it possible to read/write a file from/to a certain user
I want to write to a text file using javascript. I know that it
Possible Duplicate: Why does C# require you to write a null check every time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.