美文网首页
Swift5-swift 自定义print

Swift5-swift 自定义print

作者: Jesscia_Liu | 来源:发表于2020-04-24 17:57 被阅读0次

DEBUG调试模式打印,Release发布模式不打印

1.release/debug模式修改

  • 菜单:Product-Scheme-Edit Scheme
  • 快捷键:cmd+shift+<

2.实现

  • 使用时放到自定义LDefines.swift 类外
class LDefines: NSObject {
    
}

/**
  使用:printLog("hello world")   debug模式打印,release模式不打印
  使用:printLog("hello world", logError: true) release模式也可打印
*/
func printLog(_ items: Any...,
              logError: Bool = false,
              file: String = #file,
              method: String = #function,
              line: Int = #line)
{
    if logError {
        print(message: items, fileName: file,methodName: method,lineNumber: line)
    } else {
        #if DEBUG
            print(message: items, fileName: file,methodName: method,lineNumber: line)
        #endif
    }
}

private func print<T>(message: T, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) {
    //获取当前时间
    let now = Date()
    // 创建一个日期格式器
    let dformatter = DateFormatter()
    dformatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
    // 要把路径最后的字符串截取出来
    let lastName = ((fileName as NSString).pathComponents.last!)
    print("\(dformatter.string(from: now)) [\(lastName)][第\(lineNumber)行] \n\t\t \(message)")
}

相关文章

  • Swift5-swift 自定义print

    DEBUG调试模式打印,Release发布模式不打印 1.release/debug模式修改 菜单:Product...

  • Function 函数

    自定义函数 def say_hello():print("Hi, there!")print("Welcome t...

  • iOS - Description

    自定义 po 打印,print & NSLog 类的打印输出 description - print 日志输出 d...

  • 6.Dart-函数作用域

    /** * 内置方法/ 函数: * print() * * 自定义方法: * 自定义方法的基本格式 * 返回...

  • Swift5 Print相关

    1、Tips 自定义 print:CustomStringConvertible 协议;debugPrint:Cu...

  • 自定义Log

    实现的功能: 在需要print的时候,利用自定义Log输出当前print语句所在文件及代码所在行数. #if DE...

  • Swift 自定义 Print()

    在项目中添加全局方法(可以直接创建一个swift文件,把方法拷贝就好了) 其中 ** DEBUGLOG** 是一个...

  • 【转】自定义print

    转自:https://blog.csdn.net/bobbob32/article/details/1040112...

  • Dart中的函数-----上

    一、内置方法/函数 print(); 二、自定义方法 三、传参 方法的作用域

  • 学习到了几个比较舒服的代码风格

    看了别人的写的代码, 学习到了几个比较舒服的代码风格 自定义Log 替换print使用自定义log() 写入到本地...

网友评论

      本文标题:Swift5-swift 自定义print

      本文链接:https://www.haomeiwen.com/subject/uzfqwhtx.html