源于iOS App開發(fā)時,控制臺日志混亂,不便于分析的需要,產(chǎn)生了這篇文章。
基礎(chǔ)票
說說NSLog
- 調(diào)用NSLogv => Logs an error message to the Apple System Log facility.
- Output from NSLogv is serialized, in that only one thread in a process can be doing the writing/logging described above at a time.
- STDERR_FILENO
- ASL
iOS日志重定向到文件
{
//to log to document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *loggingPath = [documentsPath stringByAppendingPathComponent:@"/mylog.log"];
NSLog(@"%@", loggingPath);
//redirect NSLog
freopen([loggingPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
}
這樣我們就可以在電腦上面直接tail -f這個文件,配合grep等命令進(jìn)行日志分析了。
不進(jìn)行日志重定向
iOS模擬器的日志其實(shí)是輸出到系統(tǒng)中某個文件中了,好像是定時壓縮一個保存起來,我們只要找到那個文件就可以tailf了。
根據(jù)當(dāng)前模擬器的identifier(windows-devices菜單)按圖示找到當(dāng)前模擬器腺癌的system.log文件,右鍵finder顯示,然后直接tailf就可以啦。
如何更方便呢?
To give yourself faster access to logs via terminal, you can add an alias to your .bash_profile file by typing:
echo "alias simulatorlog='tail -f ~/Library/Logs/iOS\ Simulator/7.0/system.log'" >> ~/.bash_profile
After it you can simply type simulatorlog into terminal window to access the logs.
像我自己的電腦,是這樣配的(查看iPhone6的日志):
alias mylog6='tail -f /Users/test/Library/Logs/CoreSimulator/0A5E599A-D202-4502-8E3C-B6E97840E7ED/system.log'
查看日志時,直接mylog6 | grep something
Enjoy.
============================分割線
===============================
分享篇
下面是組內(nèi)分享時用的md文件,是一個大概的總結(jié)性的東西,雖然沒有詳細(xì)的展開(分享時口說唄,寫起來好累贅的樣子),但感覺對于理思路還是挺好的。
Basic Loggers Related(主要是Logger分類)
- Apple System Logger
- Standard output
- standard error
- File Logger
- HTTP Server Logger
- Telnet Server Logger
- Telnet協(xié)議是TCP/IP協(xié)議族中的一員,是Internet遠(yuǎn)程登陸服務(wù)的標(biāo)準(zhǔn)協(xié)議和主要方式。它為用戶提供了在本地計算機(jī)上完成遠(yuǎn)程主機(jī)工作的能力。在終端使用者的電腦上使用telnet程序,用它連接到服務(wù)器。終端使用者可以在telnet程序中輸入命令,這些命令會在服務(wù)器上運(yùn)行,就像直接在服務(wù)器的控制臺上輸入一樣。
- TCP Server Logger
- UDP Server Logger
- TCP Client Logger
- UDP Client Logger
- Onscreen Logging Overlay
- Custom Loggers
- Custom Logger fomatters
Basic Libraries Related(理清這幾個庫的關(guān)系)
- _
-
_
- XLFacility
-
GCDNetworking
- GCDNetworking is a networking framework based on GCD available under a friendly New BSD License.
-
GCDWebServer
- GCDWebServer is a modern and lightweight GCD based HTTP 1.1 server designed to be embedded in OS X & iOS apps.
-
GCDTelnetServer
- GCDTelnetServer is a drop-in embedded Telnet server for iOS and OS X apps.
-
_
-
CocoaAsyncSocket
- CocoaAsyncSocket provides easy-to-use and powerful asynchronous socket libraries for Mac and iOS. The classes are described below.
- GCDAsyncSocket is a TCP/IP socket networking library built atop Grand Central Dispatch.
- GCDAsyncUdpSocket is a UDP/IP socket networking library built atop Grand Central Dispatch.
- 日志實(shí)現(xiàn)用了CocoaLumberjack
-
CocoaHTTPServer(FMock工具用的這個)
- CocoaHTTPServer is a small, lightweight, embeddable HTTP server for Mac OS X or iOS applications.
- 底層實(shí)現(xiàn)用了CocoaAsyncSocket
-
RoutingHTTPServer
- A routing API for CocoaHTTPServer
- 底層實(shí)現(xiàn)用了CocoaHTTPServer
-
CocoaAsyncSocket
Guide(演示內(nèi)容)
- XLFacility --> HTTPServer
http://127.0.0.1:8080
- XLFacility --> TelnetServer
telnet localhost 2323
- XLFacility --> Onscreen Logging Overlay
- CocoaLumberjack --> UDPClient
start udp server
- test udp client logger
- 下面是一個簡單的udp server代碼:
#!/usr/bin/env python
# UDP Echo Server - udpserver.py
# code by www.cppblog.com/jerryma
import socket, traceback
host = '127.0.0.1'
port = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
while 1:
try:
message, address = s.recvfrom(8192)
print "Got data from", address, ": ", message
s.sendto(message, address)
except (KeyboardInterrupt, SystemExit):
raise
except:
traceback.print_exc()