前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Basic debugging using logging for iOS apps

Basic debugging using logging for iOS apps

作者头像
公众号iOS逆向
发布2021-01-18 16:03:05
4980
发布2021-01-18 16:03:05
举报

文章目录

  • 前言
  • I 、 DEBUG 宏的应用
    • 1.1 发布模式关闭NSLog
  • II、 NSLog调试技巧
    • 2.1 Where to find NSLog's output
    • 2.2 Improved logging in Objective-C
    • 2.1 Objective-C's boxing capability (`装箱`快速构造数字对象)
    • 2.2 知识补充:装箱和拆箱
  • see also
    • Log facility 和level的关系
    • 常见的日志文件
    • 配置【生成/存储日志】策略: `vim /etc/rsyslog.conf`
    • 配置IP

前言

今天周末时间关系,此文只完成了部分内容,后续完整内容请移步到博客(kunnan.blog.csdn.net)

  • 本文demo(请到博客看链接:kunnan.blog.csdn.net)

I 、 DEBUG 宏的应用

  • 应用场景:区分调试模式和发布模式进行特殊处理
  • 自定义preprocessor macro:

The DEBUG preprocessor macro setting in an Xcode project

1.1 发布模式关闭NSLog

//调试模式
#ifdef DEBUG
#define NSLog(...)     NSLog(__VA_ARGS__)
#define KisDebug 1

//#define NSLog(fmt, ...) NSLog((@"[文件名:%s]\n" "[函数名:%s]\n" "[行号:%d] \n" fmt), __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);

#else//发布模式
#define NSLog(...)
#define KisDebug 0
#endif

II、 NSLog调试技巧

2.1 Where to find NSLog’s output

NSLog outputs messages to the Apple System Log facility or to the Console app

2.2 Improved logging in Objective-C

  • Improved logging in Objective-C

格式说明符

描述

func

%s

当前函数签名

LINE

%d

在源代码文件的当前行号

FILE

%s

源代码文件的完整路径

PRETTY_FUNCTION

%s

print the name of the function being called

表达

格式说明符

描述

NSStringFromSelector(_cmd)

%@

Name of the current selector

NSStringFromClass([self class])

%@

Name of the current object’s class

[[NSString stringWithUTF8String:FILE] lastPathComponent]

%@

Name of the source code file(源代码文件的名称)

[NSThread callStackSymbols]

%@

NSArray of the current stack trace as programmer

  • Format specifiers for data types

Type

Format specifier

Considerations

NSInteger

%ld or %lx

Cast the value to long.

NSUInteger

%lu or %lx

Cast the value to unsigned long.

CGFloat

%f or %g

%f works for floats and doubles when formatting; but note the technique described below for scanning.

CFIndex

%ld or %lx

The same as NSInteger.

pointer

%p or %zx

%p adds 0x to the beginning of the output. If you don’t want that, use %zx and no typecast.

2.1 Objective-C’s boxing capability (装箱快速构造数字对象)

The printf function NSLog offers a number of substitution tokens for printing numbers (%d, %ld, %f, %d, for example). As a convenience, you can use Objective-C’s boxing capability to save time and avoid compiler warnings.

  • For example

平常也可以使用@ 来快速包装数字类型以对象的形式进行存储和传参

[discountArray addObject:[[ self class] mj_objectWithKeyValues:@{@"placeholder":QCTLocal(@"please_input_card_num"),@"btnContent":QCTLocal(@"member_see"),@"EnterModelType":@2,@"isEnabled":@1,@"isLast":@1,@"block":block}]];

2.2 知识补充:装箱和拆箱

  • Java包装类(装箱和拆箱)

全部被final修饰,顺便提一下,java.lang.Math,System,String也被final修饰 包装类是使用面向对象的思想把简单的数据类型封装成类。 1.特点 包装类把简单的数据类型包装成类。 注:简单数据类型不是类,使用简单数据类型主要为了提高代码的运行效率 2.装箱和拆箱 把简单数据类型包装成对应的包装类称为boxing(示例:Integer i=1;将1包装成Integer再使用Object引用Integer对象) 把包装类型转换成简单数据类型称为unboxing(示例:Integer i=1,int p=i;//将包装类Integer转化成简单数据类型int) 注:Integer的拆箱方法为 int intValue(),其他的包装类以此类推 1)包装类都重写了toString方法,equals方法,hashCode方法 2)Integer的API: String toBinaryString(int i); 将int类型的数据以二进制字符串形式返回 int parseInt(String) 将字符串转化成int类型 int parseInt(Stringstr,int i ) 将字符串转化成对应的进制 类型,str为被转换的字符串,i为进制类型(10,8,16) Integer valueof(String str) 将字符串转换成Integer类型。

see also

  • String Format Specifiers
全部被final修饰,顺便提一下,java.lang.Math,System,String也被final修饰
包装类是使用面向对象的思想把简单的数据类型封装成类。
1.特点
包装类把简单的数据类型包装成类。
注:简单数据类型不是类,使用简单数据类型主要为了提高代码的运行效率
2.装箱和拆箱
把简单数据类型包装成对应的包装类称为boxing(示例:Integer i=1;将1包装成Integer再使用Object引用Integer对象)
把包装类型转换成简单数据类型称为unboxing(示例:Integer i=1,int p=i;//将包装类Integer转化成简单数据类型int)
注:Integer的拆箱方法为 int intValue(),其他的包装类以此类推
1)包装类都重写了toString方法,equals方法,hashCode方法
2)Integer的API:
String toBinaryString(int i); 将int类型的数据以二进制字符串形式返回
int parseInt(String) 将字符串转化成int类型
int parseInt(Stringstr,int i ) 将字符串转化成对应的进制 类型,str为被转换的字符串,i为进制类型(10,8,16)
Integer valueof(String str) 将字符串转换成Integer类型。
  • Format specifiers for data types

Type

Format specifier

Considerations

NSInteger

%ld or %lx

Cast the value to long.

NSUInteger

%lu or %lx

Cast the value to unsigned long.

CGFloat

%f or %g

%f works for floats and doubles when formatting; but note the technique described below for scanning.

CFIndex

%ld or %lx

The same as NSInteger.

pointer

%p or %zx

%p adds 0x to the beginning of the output. If you don’t want that, use %zx and no typecast.

  • Basic debugging

Log facility 和level的关系

NSLog outputs messages to the Apple System Log facility or to the Console app

  • facility:是系统对某种类型事件的定义。

LOG_AUTH LOG_AUTHPRIV 安全认证 LOG_CRON clock daemon (cron and at) 计划任务事件 LOG_DAEMON 后台进程 LOG_FTP ftp daemon LOG_KERN kernel messages LOG_LOCAL0 through LOG_LOCAL7 用户自定义设备 LOG_LPR printer subsystem LOG_MAIL 邮件系统mail subsystem LOG_NEWS news subsystem LOG_SYSLOG syslogd自身产生的日志 LOG_USER (default)

  • level :指遇到何种情况才记录日志

从下到上,级别从低到高,记录的信息越来越少

LOG_EMERG 紧急,致命,服务无法继续运行,如配置文件丢失 LOG_ALERT 报警,需要立即处理,如磁盘空使用95% LOG_CRIT 致命行为 LOG_ERR 错误行为 LOG_WARNING 警告信息 LOG_NOTICE 普通,重要的标准信息 LOG_INFO 标准信息 LOG_DEBUG 调试信息,排错所需,一般不建议使用

常见的日志文件

tail /var/log/messages //系统主日志文件 tail -20 /var/log/messages tail -f /var/log/messages //动态查看日志文件的尾部 tailf /var/log/secure //认证、安全 tail /var/log/maillog //跟邮件postfix相关 tail /var/log/cron //crond、at进程产生的日志 tail /var/log/dmesg //和系统启动相关 tail /var/log/audit/audit.log //系统审计日志 tail /var/log/yum.log //yum tail /var/log/mysqld.log //MySQL tail /var/log/xferlog //和访问FTP服务器相关 w //当前登录的用户 /var/log/wtmp last //最近登录的用户 /var/log/btmp lastlog //所有用户的登录情况 /var/log/lastlog

配置【生成/存储日志】策略: vim /etc/rsyslog.conf

由设备+级别+存放位置组成

FACILITY+LEVEL+FILE组成: authpriv.* /var/log/secure(SSH信息) mail.* -/var/log/maillog(发邮件) cron.* /var/log/cron(创建任务) authpriv.* * <代表所有终端> authpriv. @192.168.10.230 authpriv.* @@192.168.10.230

配置IP

  • /etc/sysconfig/network-scripts/ifcfg-enp2s0中各个参数的意义

vim /etc/sysconfig/network-scripts/ifcfg-enp2s0 NAME=“enp2s0” DEVICE=“enp2s0” ONBOOT=yes //是否启用该设备 BOOTPROTO=none //手动(none/static)还是自动(dhcp) IPADDR=172.16.120.246 //地址 PREFIX=24 //掩码 IPADDR1=192.168.200.246 //额外地址 PREFIX1=24 //额外掩码,注意编号 GATEWAY=172.16.120.254 //网关 DNS1=8.8.8.8 //DNS DNS2=114.114.114.114 //DNS2

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-11-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 iOS逆向 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 前言
  • I 、 DEBUG 宏的应用
    • 1.1 发布模式关闭NSLog
    • II、 NSLog调试技巧
      • 2.1 Where to find NSLog’s output
        • 2.2 Improved logging in Objective-C
          • 2.1 Objective-C’s boxing capability (装箱快速构造数字对象)
            • 2.2 知识补充:装箱和拆箱
            • see also
              • Log facility 和level的关系
                • 常见的日志文件
                  • 配置【生成/存储日志】策略: vim /etc/rsyslog.conf
                    • 配置IP
                    相关产品与服务
                    日志服务
                    日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档