iOS Learning note 40-log redirection

Source: Internet
Author: User
<span id="Label3"></p>first, Log Redirection<p><p>In our iOS development process, we often use <code>NSLog</code> the log information printed to the console for code debugging, but the prerequisite for debugging is to connect Xcode.<br>If you are performing a real-machine debugging and cannot connect to Xcode at the same time, you can not directly view the output log in Xcode's console, but the program will still execute those logs, if you want to view the log, we need to save the output log information to a file, and then to VIEW.</p></p><p><p>Standard <code>NSLog</code> printing defaults to standard error ( <code>stderr</code> ), we only need to modify <code>NSLog</code> the print output path to achieve log redirection</p></p>The following is the log redirection code:<pre class="objc"><code class="hljs objectivec"><span class="hljs-meta">#pragma mark-user method to write output information of NSLog to a file<span class="hljs-comment">/* Save the NSLog print information to a file in the document directory */-(<span class="hljs-keyword">Void) redirectlogtodocumentfolder{<span class="hljs-comment">Get sandbox path<span class="hljs-built_in">Nsarray *paths =<span class="hljs-built_in">Nssearchpathfordirectoriesindomains (<span class="hljs-built_in">nsdocumentdirectory,<span class="hljs-built_in">nsuserdomainmask,<span class="hljs-literal">YES);<span class="hljs-built_in">NSString *documentdirectory = [paths objectatindex:<span class="hljs-number">0];<span class="hljs-comment"><span class="hljs-comment">//get Print output file path <span class="hljs-built_in">nsstring *filename = [ <span class="hljs-built_in">nsstring stringwithformat:<span class="hljs-string">@ "myData.log"]; <span class=" hljs-built_in ">nsstring *logfilepath = [documentdirectory stringbyappendingpathcomponent:filename]; <span class="hljs-comment">//first Delete the existing file <span class="hljs-built_in">nsfilemanager *defaultmanager = [<span class="]" hljs-built_in ">nsfilemanager defaultmanager]; [defaultmanager removeitematpath:logfilepath error:<span class="hljs-literal">nil]; <span class="hljs-comment">//redirects The output of NSLog to a file because printf printing in C is printed to stdout, which is also redirected to a file Freopen ([logfilepath Cstringusingencoding:<span class="hljs-built_in">nsasciistringencoding],<span class="hljs-string"> "a +", Stdout) ; Freopen ([logfilepath cstringusingencoding:<span class="hljs-built_in">nsasciistringencoding], <span class="hljs-string"> "a +", stderr);} </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre><p><p>In <code>AppDelegate</code> the <code>application:didFinishLaunchingWithOptions:</code> call:</p></p><pre class="objc"><pre class="objc"><code class="hljs objectivec">- (<span class="hljs-built_in">BOOL)application:(<span class="hljs-built_in">UIApplication *)application didFinishLaunchingWithOptions:(<span class="hljs-built_in">NSDictionary *)launchOptions{ <span class="hljs-comment">// 重定向Log日志信息到Document文件中 [<span class="hljs-keyword">self redirectLogToDocumentFolder]; <span class="hljs-keyword">return <span class="hljs-literal">YES;}</span></span></span></span></span></span></span></code></pre></pre><p><p>It's not over yet, because your log log has been printed to the Real-world sandbox file and you need to copy it to your Mac for viewing, and you'll need to set up a shared file configuration:</p></p> <blockquote> <blockquote> <p>Add a key to the Application's <code>Info.plist</code> file <code>UIFileSharingEnabled</code> and set the key value to <code>YES</code> .</p> </blockquote> </blockquote><p><p>Once the iOS device is plugged into the User's computer, itunes displays a file sharing area in the Apps tab of the selected Device. After you set up a file share, users can add files to the directory or move files to the desktop Computer. That is, once the file share is set up, once the device is connected to the computer, you can view the shared folder of the specified application via itune and copy the files to your computer.<br></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><br><br></p></p>Ii. file stream Knowledge Supplement 1. Open File<p><p><code>FILE *fp = fopen(文件路径,打开方式);</code></p></p>Open in the Way: <blockquote> <blockquote> <ul> <li><code>r</code>: read, If the file does not exist return null, the presence is opened</li> <li><code>w</code>: write, Overwrite if file exists, do not exist then create</li> <li><code>a</code>append, Append if file exists, do not exist then create</li> <li><code>r+</code>: On <code>r</code> the basis of the increase of the Write permission</li> <li><code>w+</code>: On <code>w</code> the basis of the more Read permission is added</li> <li><code>a+</code>: On <code>a</code> the basis of the more Read permission is added</li> <li><code>t</code>: Text file (default), combined with rt, wt, at, rt+</li> <li><code>b</code>: Binary files, combined with rb, wb, ab, rb+</li> </ul> </blockquote> </blockquote>2. File operation <ol> <li><li><code>char ch = fgetc(文件指针);</code><br>Function: reads a character from a file<br>"fgetc and getc no difference, same usage."</li></li> <li><li><code>fputc(字符,文件指针);</code><br>Function: writes a character to a file</li></li> <li><li><code>fgets(字符数组,长度n,文件指针);</code><br>Function: reads the n-1 character from the file pointer to the file designators to the character array, automatically after the addition, the character array is used to save the string you read out<br>"encountered" \ n "end Read now"</li></li> <li><li><code>fputs(字符串,文件指针);</code><br>Function: writes a string to the file pointed to by the document pointer</li></li> <li><li><code>fread(保存数据的首地址,每块的大小,多少块,文件指针);</code><br>Function: Block read (file pointer) from the file pointed to by the file pointer, tell it how many blocks (how many blocks) to read at a time, how large each block (the second parameter), and where to save (the first Parameter)<br>"it only cares about the specific number of bytes that are read, and doesn't care if There's no line break in them."</li></li> <li><li><code>fwrite(数据的首地址,每块的大小,多少块,文件指针);</code><br>Function: writes data chunking to the file pointed to by the file Pointer. The first is to tell the system, what data to write, the second is to tell the system how many bytes per block, The third is to tell the system how many blocks to write, the fourth is to write to which file</li></li> <li><li><code>fscanf(文件指针,格式化控制符,地址列表);</code><br>Function: format Read</li></li> <li><li><code>fprintf(文件指针,格式化控制符,参数列表);</code><br>Function: Format Write</li></li> <li><li><code>freopen(文件路径path, 打开方式mode, 文件指针fp);</code><br>Role: implements redirection to direct a predefined standard stream file to a file specified by path. The standard flow file is specifically referred to <code>stdin</code> , <code>stdout</code> and <code>stderr</code> . It <code>stdin</code> is the standard input stream, the default is the keyboard, <code>stdout</code> is the standard output stream, The default is the screen, <code>stderr</code> is the standard error stream, generally set the screen as the Default.</li></li> </ol>3. Close the file<p><p><code>fclose(文件指针);</code></p></p><p><p>iOS Learning note 40-log redirection</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.