iOS查看日志利器 —— socat

很多时候,自己做的tweak只是记录下一些信息然后NSLog出来,内容就在系统日志里。特别是自己要查看的app日志和系统日志或者其他app日志混合在一起的时候,一直盯着看,眼睛都花了吧?

除了tail -f /var/log/syslog之外,你还可以选择她 —— socat。今天就在搜索如何过滤日志,结果发现了个工具。
socat是一個netcat(nc)的替代產品,可以稱得上nc++。socat的特點就是在兩個流之間建立一個雙向的 通道。socat的地址類型很多,有ip、tcp、udp、ipv6、pipe、exec、system、open、proxy、openssl等等。用在查看和过滤日志上,有点大材小用。不过,能解决问题的工具就是好工具!!!

  1. 在iOS设备安装
    apt-get install socat

  2. 连接到系统日志的sock文件
    socat - UNIX-CONNECT:/var/run/lockdown/syslog.sock

  3. 进入到命令行交互界面,这时可以输入help查看帮助

McMillenteki-iPod:~ root# socat - UNIX-CONNECT:/var/run/lockdown/syslog.sock

========================
ASL is here to serve you
> help 
Commands
    quit                 exit session
    select [val]         get [set] current database
                         val must be "file", "mem", or "mini"
    file [on/off]        enable / disable file store
    memory [on/off]      enable / disable memory store
    mini [on/off]        enable / disable mini memory store
    stats                database statistics
    flush                flush database
    dbsize [val]         get [set] database size (# of records)
    watch                print new messages as they arrive
    stop                 stop watching for new messages
    raw                  use raw format for printing messages
    std                  use standard format for printing messages
    *                    show all log messages
    * key val            equality search for messages (single key/value pair)
    * op key val         search for matching messages (single key/value pair)
    * [op key val] ...   search for matching messages (multiple key/value pairs)
                         operators:  =  <  >  ! (not equal)  T (key exists)  R (regex)
                         modifiers (must follow operator):
                                 C=casefold  N=numeric  S=substring  A=prefix  Z=suffix
  1. 日志的查看
    输入watch查看,输入stop停止(不过停止貌似没用,也不知道为啥,知道的请告诉我)

  2. 日志过滤
    从help中可以看到, * key val命令就直接可以按照键值的相等匹配来过滤了,实战一下:比如我们要过滤PID为490的进程输入的日志,就输入:
    *** PID 490**这时候,可以看到输出的全部是pid=490的日志了,新的日志也会按照这个条件过滤。

  3. 更多的过滤条件
    你也许疑惑,我怎么知道* key val的key就可以输入PID呢?其实日志是有格式的,记得help里的raw和std吧,输入raw后,可以看到输出的日志的形式是:
    [ASLMessageID 6374] [Level 4] [Time 1397061155] [TimeNanoSec 597631000] [Sender \M-f\M^I\M^K\M-f\M^\M-:\M-i\M^S\M^C\M-e\M-#\M-0Free] [Facility com.apple.abcd] [PID 490] [Message -[NSURLConnection initWithRequest:[url=http://googleads.g.doubleclick.net:80/mads/gma?u_audio=1&request_id=69&ios_jb=]http://googleads.g.doubleclick.n … quest_id=69&ios_jb= delegate:<GADURLConnection: 0x20e320a0> startImmediately:0]] [ReadUID 501]
    中括号里的,全部是[key val]的形式,所以,key={
    ASLMessageID, //asl消息id
    Level, //级别
    Time, //时间
    TimeNanoSec, //时间ns
    Sender, //消息的发送者
    Facility, //按照bundle id来过滤日志可以用这个key
    PID, //进程id
    Message, //消息内容
    ReadUID //读取所使用的用户id
    }

    如果按照bundle id=com.apple.xxxxx来过滤日志消息,可以输入:
    *** Facility com.apple.xxxxx**

help中还可以设置更多的过滤条件,欢迎大家尝试和分享~

7 个赞

不错,多谢分享。