1. 程式人生 > >iOS 程式在UITextView中顯示NSLog日誌的方法,

iOS 程式在UITextView中顯示NSLog日誌的方法,

 最近開發程式,需要做個給測試人員的demo,客戶端可以實時 顯示程式的列印日誌的功能,

查找了很多的資料找到個方法,利用NSPipe即可以實現,

蘋果官方解釋:

      objects provide an object-oriented interface for accessing pipes. An NSPipe object represents both ends of a pipe and enables communication through the pipe. A pipe is a one-way communications channel between related processes; one process writes data, while the other process reads that data. The data that passes through the pipe is buffered; the size of the buffer is determined by the underlying operating system.NSPipe

 is an abstract class, the public interface of a class cluster.

   NSPipe是一個抽象類,一類叢集的公共介面。通過NSPipe 我們可以方便的讀取和寫入程序資料,

如果想實現將 程式的實時日誌列印,可以在控制器中新增下邊方法

- (void)redirectNotificationHandle:(NSNotification *)nf{ // 通知方法
    NSData *data = [[nf userInfo] objectForKey:NSFileHandleNotificationDataItem];
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    self.logTextView.text = [NSString stringWithFormat:@"%@\n\n%@",self.logTextView.text, str];// logTextView 就是要將日誌輸出的檢視(UITextView)
    NSRange range;
    range.location = [self.logTextView.text length] - 1;
    range.length = 0;
    [self.logTextView scrollRangeToVisible:range];
    [[nf object] readInBackgroundAndNotify];
}

- (void)redirectSTD:(int )fd{ 
    NSPipe * pipe = [NSPipe pipe] ;// 初始化一個NSPipe 物件
    NSFileHandle *pipeReadHandle = [pipe fileHandleForReading] ;
    dup2([[pipe fileHandleForWriting] fileDescriptor], fd) ;
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(redirectNotificationHandle:)
                                                 name:NSFileHandleReadCompletionNotification
                                               object:pipeReadHandle]; // 註冊通知
    [pipeReadHandle readInBackgroundAndNotify];
}

執行

 [self redirectSTD:STDOUT_FILENO];

    [self redirectSTD:STDERR_FILENO];

就可以實現輸出列印,

相關推薦

iOS 程式UITextView顯示NSLog日誌方法

 最近開發程式,需要做個給測試人員的demo,客戶端可以實時 顯示程式的列印日誌的功能, 查找了很多的資料找到個方法,利用NSPipe即可以實現, 蘋果官方解釋:       objects provide an object-oriented interface for a

網頁顯示pdf的方法

代碼 ner .cn logs 解決方法 ews param 復制 ng- 非常好的在網頁中顯示pdf的方法 今天有一需求,要在網頁中顯示pdf,於是立馬開始搜索解決方案,無意中發現一個非常好的解決方法,詳見http://blogs.adobe.com/pdfdev

Flask框架(SQLAlchemy(python3版本)查詢資料的方法以及定義顯示資訊 )

(SQLAlchemy中新增資料 )https://blog.csdn.net/wei18791957243/article/details/85256848      新增資料,新增好的表       SQlal

ios的webview引入h5的方法

1、 拖入webview控制元件,實現標頭檔案的控制元件的宣告 2、 在實現檔案內進行內容的載入 1)、載入網路html檔案 在viewDidLoad()這個初始化檔案上寫入 NSString *url=[NSStringstringWithFormat:@”http://

ios在真機中將NSLog日誌存入檔案並儲存到document目錄

下面的方法都是在Appdelegate.m中 - (void)redirectNSLogToDocumentFolder{         NSString *fileName =[NSStringstringWithFormat:@"%@.log",[NSDat

[NSLog日誌]ios在真機中將NSLog日誌存入檔案並儲存到document目錄

儲存Log日誌- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //當真機連線Mac

iOS SVProgressHUD延遲提示顯示時間的方法

SVProgressHUD對iOS開發者來說是一個很好用的第三方開發類庫,但是在用的時候,提示的顯示時間有的時候非常的短,在上查找了很多的資料,發現並沒有相關的資料.自己嘗試了一下通過這種方法能夠實現控制提醒的顯示時間,方法如下 然後在myMethod方法

定義一個介面再定義類實現該介面編寫應用程式呼叫介面的 3 個方法並將呼叫方法所得的結果輸出。

/**  * 定義一個介面,介面中有 3 個抽象方法如下。(1)“long fact(int m);”方法的功能為求引數的階乘。(2)“long intPower(int m,int n);”方法的功能為求引數 m 的 n 次方。(3)“boolean findFactor

在廣播接收器顯示對話方塊失敗並退出程式

在廣播中啟動Dialog時出現如下錯誤資訊: Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an applicat

非常好的在網頁顯示pdf的方法

今天有一需求,要在網頁中顯示pdf,於是立馬開始搜尋解決方案,無意中發現一個非常好的解決方法,詳見http://blogs.adobe.com/pdfdevjunkie/web_designers_guide。 記錄一下主要程式碼: <script type="

數據庫在顯示1或2在程序代表不同狀態

select case then case when end 不同 when ase nbsp select case when H=1 then ‘在售‘when H=2 then ‘售完‘ endas H from HHselect * from HH--case wh

ajax傳送csrf的方法(django環境)

獲取csrf隨機字串的方法: 通過其name=“csrfmiddlewaretoken”,來獲取如:1 document.getElementsByName("csrfmiddlewaretoken")[0]  2 $('input[name="csrfmiddlewaretoke

ThinkPHP3.2.5的Model基類有些文件沒提到的方法可以經常看看。

// +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +----------------------

java無法找到主方法無法找到類web應用第一次報500第二次報404

在重灌機器後,原來的許多java工程在重新被新安裝的Eclipse讀取過程中出現了java.lang.NoNoClassDefFoundError的問題,檢視工程目錄中bin路徑下沒有生成對應的.class文件網上查閱了很多資料,大部分都指示classpath設定不對。但是Eclipse本身並不需要配置cla

Python建立程序的方法以及併發並行殭屍程序孤兒程序的相關概念

一、併發與並行 並行:多個計算機核心在同時處理多個任務,這時多個任務間是並行關係 併發:同時處理多個任務,但是核心在多個任務間不斷地切換,達到好像都在處理執行的效果,但實際一個時間點核心只能處理其中一個任務。 二、程序 程序與執行緒是實現多工程式設計的實施方案 程序與執

C++int 轉LPCWSTR方法配合MessageBox使用

1.MultiByteToWideChar函式方式 int nctimes; string str; str = std::to_string(nctimes); size_t size = str.length(); wchar_t *buffer

區別JS類的靜態方法靜態變數例項方法例項變數

1.類的靜態方法     先來段程式碼之後分析 // JS類靜態函式 function BaseClass() { } // 類新增add函式 BaseClass.add = function()

關於 java 的 set,get方法而為什麼不推薦直接使用public

我不知道有沒有人遇到過,有一段時間,我都覺得那些 set,get的用處何在,我直接寫一個public直接拿不就行了,多爽,但是隨著使用的頻繁,越來越想去搜索一下這個問題,而不是按照官方的推薦,前輩們的使用都是建議set,get. 首先,對於網上那些說到的效率問題,真的感覺很喪,一種是通過一個方法

ASP.NET Core MVC的IActionFilter.OnActionExecuting方法可以獲取Controller的Action方法引數值

用過ASP.NET Core MVC中IActionFilter攔截器的開發人員,都知道這是一個非常強大的MVC攔截器。最近才發現IActionFilter的OnActionExecuting方法,甚至可以獲取Controller的Action方法引數值。   假如我們在ASP.NET Core