erge IOS1442
authorXavier Algarra <xavier@pep-project.org>
Mon, 31 Dec 2018 12:58:13 +0100
branchIOS1442
changeset 7418b3cc5ee317c0
parent 7417 f4214bc2ddbc
parent 7415 d476e81be337
child 7419 c6eeaa1765e7
erge
     1.1 --- a/pEpForiOS.xcodeproj/project.pbxproj	Mon Dec 31 12:57:42 2018 +0100
     1.2 +++ b/pEpForiOS.xcodeproj/project.pbxproj	Mon Dec 31 12:58:13 2018 +0100
     1.3 @@ -459,6 +459,8 @@
     1.4  		43BB86941DB4E76D00980267 /* CdAccount+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43BB86931DB4E76D00980267 /* CdAccount+Extension.swift */; };
     1.5  		43BBB5E71E267A3800104070 /* EncryptAndSendOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43BBB5E61E267A3800104070 /* EncryptAndSendOperation.swift */; };
     1.6  		43C046BF1CF48EA500BD0D9D /* AddressBook.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43C046BE1CF48EA500BD0D9D /* AddressBook.framework */; };
     1.7 +		43C273DB21C8F96D002EB4C8 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43C273DA21C8F96D002EB4C8 /* Logger.swift */; };
     1.8 +		43C273DD21C9024A002EB4C8 /* LoggerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43C273DC21C9024A002EB4C8 /* LoggerTest.swift */; };
     1.9  		43C322051EA89EED005073FB /* HandshakePartnerTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43C322041EA89EED005073FB /* HandshakePartnerTableViewCell.swift */; };
    1.10  		43C322071EA8E7AD005073FB /* UIButton+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43C322061EA8E7AD005073FB /* UIButton+Extension.swift */; };
    1.11  		43C322091EA90192005073FB /* PEP+UI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43C322081EA90192005073FB /* PEP+UI.swift */; };
    1.12 @@ -1108,6 +1110,8 @@
    1.13  		43BB86931DB4E76D00980267 /* CdAccount+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CdAccount+Extension.swift"; sourceTree = "<group>"; };
    1.14  		43BBB5E61E267A3800104070 /* EncryptAndSendOperation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EncryptAndSendOperation.swift; sourceTree = "<group>"; };
    1.15  		43C046BE1CF48EA500BD0D9D /* AddressBook.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AddressBook.framework; path = System/Library/Frameworks/AddressBook.framework; sourceTree = SDKROOT; };
    1.16 +		43C273DA21C8F96D002EB4C8 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
    1.17 +		43C273DC21C9024A002EB4C8 /* LoggerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggerTest.swift; sourceTree = "<group>"; };
    1.18  		43C322041EA89EED005073FB /* HandshakePartnerTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HandshakePartnerTableViewCell.swift; sourceTree = "<group>"; };
    1.19  		43C322061EA8E7AD005073FB /* UIButton+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIButton+Extension.swift"; sourceTree = "<group>"; };
    1.20  		43C322081EA90192005073FB /* PEP+UI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "PEP+UI.swift"; sourceTree = "<group>"; };
    1.21 @@ -2320,6 +2324,7 @@
    1.22  				434DDC2A20D10F9A00755F44 /* EncryptionTests.swift */,
    1.23  				438BA0F4214F89CC001A4A82 /* MailParsingTests.swift */,
    1.24  				4393673821C26F3D00FF8BB4 /* LogTest.swift */,
    1.25 +				43C273DC21C9024A002EB4C8 /* LoggerTest.swift */,
    1.26  			);
    1.27  			path = pEpForiOSTests;
    1.28  			sourceTree = "<group>";
    1.29 @@ -2362,6 +2367,7 @@
    1.30  				A1B50A811CD26FF100B1A997 /* Constants.swift */,
    1.31  				439D40791CC79423009A4E7C /* GCD.swift */,
    1.32  				43980EDF1CBD24B000A7FC3C /* Log.swift */,
    1.33 +				43C273DA21C8F96D002EB4C8 /* Logger.swift */,
    1.34  				435B42401D211E5900119048 /* MiscUtil.swift */,
    1.35  				5D039A9B1D0EE7F900AD59EC /* PEPUtil.swift */,
    1.36  				431798761CF87FE6007DD655 /* ReferenceCounter.swift */,
    1.37 @@ -3597,6 +3603,7 @@
    1.38  				49228A5520D4035100A51E9D /* DetailCellSegue.swift in Sources */,
    1.39  				43497B38211C62DE00AC42B0 /* MessageIdentitfying.swift in Sources */,
    1.40  				4307C4701ED81F3100A276A4 /* DefaultImapSyncDelegate.swift in Sources */,
    1.41 +				43C273DB21C8F96D002EB4C8 /* Logger.swift in Sources */,
    1.42  				00FD0CE82102014C00BA0C56 /* PrimarySplitViewcontroller+ScreenComposerProtocol.swift in Sources */,
    1.43  				434E5A1B20DB9C4600D7F88A /* EmailListViewModel+MessageFolderDelegate.swift in Sources */,
    1.44  				431E58F61ED57F6500EFA77F /* AccountVerificationServiceProtocol.swift in Sources */,
    1.45 @@ -3751,6 +3758,7 @@
    1.46  				150707DC21006CD000AA213F /* ComposeUtilTest.swift in Sources */,
    1.47  				15D439A5216F7E0E00EB3933 /* AccountPickerViewModelTest.swift in Sources */,
    1.48  				1574D07D2114696B00FEDC93 /* URL+MailToTest.swift in Sources */,
    1.49 +				43C273DD21C9024A002EB4C8 /* LoggerTest.swift in Sources */,
    1.50  				4335002E212D9A6A00BD5F2A /* EmailListViewModeTests+Threading_12_Messages.swift in Sources */,
    1.51  				4356FFEC21356CB600804089 /* ReplyAllPossibleCheckerTest.swift in Sources */,
    1.52  				430C80E01D0EADC200CD4582 /* PepAdapterTests.swift in Sources */,
     2.1 --- a/pEpForiOS/Util/Log.swift	Mon Dec 31 12:57:42 2018 +0100
     2.2 +++ b/pEpForiOS/Util/Log.swift	Mon Dec 31 12:58:13 2018 +0100
     2.3 @@ -17,18 +17,13 @@
     2.4      case error
     2.5  }
     2.6  
     2.7 -public func zlog(message: String,
     2.8 +public func zlog(_ message: StaticString,
     2.9                   function: String = #function,
    2.10                   filePath: String = #file,
    2.11 -                 fileLine: Int = #line) {
    2.12 -    if #available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *) {
    2.13 -        os_log("%@:%d %@ %{public}@", filePath, fileLine, function, message)
    2.14 -    }
    2.15 -}
    2.16 -
    2.17 -public func zlog(_ message: StaticString,
    2.18 +                 fileLine: Int = #line,
    2.19                   _ args: CVarArg...) {
    2.20      if #available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *) {
    2.21 +        os_log("%@:%d %@:", filePath, fileLine, function)
    2.22          switch args.count {
    2.23          case 1:
    2.24              os_log(message, args[0])
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/pEpForiOS/Util/Logger.swift	Mon Dec 31 12:58:13 2018 +0100
     3.3 @@ -0,0 +1,222 @@
     3.4 +//
     3.5 +//  Logger.swift
     3.6 +//  pEp
     3.7 +//
     3.8 +//  Created by Dirk Zimmermann on 18.12.18.
     3.9 +//  Copyright © 2018 p≡p Security S.A. All rights reserved.
    3.10 +//
    3.11 +
    3.12 +import Foundation
    3.13 +import os.log
    3.14 +
    3.15 +public class Logger {
    3.16 +    public enum Severity {
    3.17 +        /**
    3.18 +         OSLog.default
    3.19 +         */
    3.20 +        case `default`
    3.21 +        case info
    3.22 +        case debug
    3.23 +        case error
    3.24 +        case fault
    3.25 +
    3.26 +        @available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *)
    3.27 +        public func osLogType() -> OSLogType {
    3.28 +            switch self {
    3.29 +            case .default:
    3.30 +                return .default
    3.31 +            case .info:
    3.32 +                return .info
    3.33 +            case .debug:
    3.34 +                return .debug
    3.35 +            case .error:
    3.36 +                return .error
    3.37 +            case .fault:
    3.38 +                return .fault
    3.39 +            }
    3.40 +        }
    3.41 +    }
    3.42 +
    3.43 +    public init(subsystem: String, category: String) {
    3.44 +        if #available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *) {
    3.45 +            self.subsystem = nil
    3.46 +            self.category = nil
    3.47 +            osLogger = OSLog(subsystem: subsystem, category: category)
    3.48 +        } else {
    3.49 +            self.subsystem = subsystem
    3.50 +            self.category = category
    3.51 +            osLogger = nil
    3.52 +        }
    3.53 +    }
    3.54 +
    3.55 +    /**
    3.56 +     Logs to default.
    3.57 +     */
    3.58 +    public func log(function: String = #function,
    3.59 +                    filePath: String = #file,
    3.60 +                    fileLine: Int = #line,
    3.61 +                    _ message: StaticString,
    3.62 +                    _ args: CVarArg...) {
    3.63 +        saveLog(message: message,
    3.64 +                severity: .default,
    3.65 +                function: function,
    3.66 +                filePath: filePath,
    3.67 +                fileLine: fileLine,
    3.68 +                args: args)
    3.69 +    }
    3.70 +
    3.71 +    /**
    3.72 +     Logs to info.
    3.73 +     */
    3.74 +    public func info(function: String = #function,
    3.75 +                     filePath: String = #file,
    3.76 +                     fileLine: Int = #line,
    3.77 +                     _ message: StaticString,
    3.78 +                     _ args: CVarArg...) {
    3.79 +        saveLog(message: message,
    3.80 +                severity: .info,
    3.81 +                function: function,
    3.82 +                filePath: filePath,
    3.83 +                fileLine: fileLine,
    3.84 +                args: args)
    3.85 +    }
    3.86 +
    3.87 +    /**
    3.88 +     Logs to debug.
    3.89 +     */
    3.90 +    public func debug(function: String = #function,
    3.91 +                      filePath: String = #file,
    3.92 +                      fileLine: Int = #line,
    3.93 +                      _ message: StaticString,
    3.94 +                      _ args: CVarArg...) {
    3.95 +        saveLog(message: message,
    3.96 +                severity: .debug,
    3.97 +                function: function,
    3.98 +                filePath: filePath,
    3.99 +                fileLine: fileLine,
   3.100 +                args: args)
   3.101 +    }
   3.102 +
   3.103 +    /**
   3.104 +     Logs to error.
   3.105 +     */
   3.106 +    public func error(function: String = #function,
   3.107 +                      filePath: String = #file,
   3.108 +                      fileLine: Int = #line,
   3.109 +                      _ message: StaticString,
   3.110 +                      _ args: CVarArg...) {
   3.111 +        saveLog(message: message,
   3.112 +                severity: .error,
   3.113 +                function: function,
   3.114 +                filePath: filePath,
   3.115 +                fileLine: fileLine,
   3.116 +                args: args)
   3.117 +    }
   3.118 +
   3.119 +    /**
   3.120 +     Logs to fault.
   3.121 +     */
   3.122 +    public func fault(function: String = #function,
   3.123 +                      filePath: String = #file,
   3.124 +                      fileLine: Int = #line,
   3.125 +                      _ message: StaticString,
   3.126 +                      _ args: CVarArg...) {
   3.127 +        saveLog(message: message,
   3.128 +                severity: .fault,
   3.129 +                function: function,
   3.130 +                filePath: filePath,
   3.131 +                fileLine: fileLine,
   3.132 +                args: args)
   3.133 +    }
   3.134 +
   3.135 +    private let subsystem: String?
   3.136 +    private let category: String?
   3.137 +
   3.138 +    private let osLogger: Any?
   3.139 +
   3.140 +    private func saveLog(message: StaticString,
   3.141 +                         severity: Severity,
   3.142 +                         function: String = #function,
   3.143 +                         filePath: String = #file,
   3.144 +                         fileLine: Int = #line,
   3.145 +                         args: [CVarArg]) {
   3.146 +        if #available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *) {
   3.147 +            let theLog = osLogger as! OSLog
   3.148 +            let theType = severity.osLogType()
   3.149 +            os_log("%@:%d %@:",
   3.150 +                   log: theLog,
   3.151 +                   type: theType,
   3.152 +                   filePath,
   3.153 +                   fileLine,
   3.154 +                   function)
   3.155 +            switch args.count {
   3.156 +            case 0:
   3.157 +                os_log(message,
   3.158 +                       log: theLog,
   3.159 +                       type: theType)
   3.160 +            case 1:
   3.161 +                os_log(message,
   3.162 +                       log: theLog,
   3.163 +                       type: theType,
   3.164 +                       args[0])
   3.165 +            case 2:
   3.166 +                os_log(message,
   3.167 +                       log: theLog,
   3.168 +                       type: theType,
   3.169 +                       args[0], args[1])
   3.170 +            case 3:
   3.171 +                os_log(message,
   3.172 +                       log: theLog,
   3.173 +                       type: theType,
   3.174 +                       args[0], args[1], args[2])
   3.175 +            case 4:
   3.176 +                os_log(message,
   3.177 +                       log: theLog,
   3.178 +                       type: theType,
   3.179 +                       args[0], args[1], args[2], args[3])
   3.180 +            case 5:
   3.181 +                os_log(message,
   3.182 +                       log: theLog,
   3.183 +                       type: theType,
   3.184 +                       args[0], args[1], args[2], args[3], args[4])
   3.185 +            case 6:
   3.186 +                os_log(message,
   3.187 +                       log: theLog,
   3.188 +                       type: theType,
   3.189 +                       args[0], args[1], args[2], args[3], args[4], args[5])
   3.190 +            case 7:
   3.191 +                os_log(message,
   3.192 +                       log: theLog,
   3.193 +                       type: theType,
   3.194 +                       args[0], args[1], args[2], args[3], args[4], args[5], args[6])
   3.195 +            case 8:
   3.196 +                os_log(message,
   3.197 +                       log: theLog,
   3.198 +                       type: theType,
   3.199 +                       args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7])
   3.200 +            case 9:
   3.201 +                os_log(message,
   3.202 +                       log: theLog,
   3.203 +                       type: theType,
   3.204 +                       args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7],
   3.205 +                       args[8])
   3.206 +            case 10:
   3.207 +                os_log(message,
   3.208 +                       log: theLog,
   3.209 +                       type: theType,
   3.210 +                       args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7],
   3.211 +                       args[8], args[9])
   3.212 +            default:
   3.213 +                os_log("Using more than 10 parameters",
   3.214 +                       log: theLog,
   3.215 +                       type: theType)
   3.216 +                os_log(message,
   3.217 +                       log: theLog,
   3.218 +                       type: theType,
   3.219 +                       args)
   3.220 +            }
   3.221 +        } else {
   3.222 +            // TODO: use as_logging
   3.223 +        }
   3.224 +    }
   3.225 +}
     4.1 --- a/pEpForiOSTests/LogTest.swift	Mon Dec 31 12:57:42 2018 +0100
     4.2 +++ b/pEpForiOSTests/LogTest.swift	Mon Dec 31 12:58:13 2018 +0100
     4.3 @@ -14,8 +14,8 @@
     4.4  
     4.5  class LogTest: XCTestCase {
     4.6      func testSimple() {
     4.7 +        zlog("zlog simple message")
     4.8          zlog("%d %@", 1, "zlog blah")
     4.9 -        zlog(message: "zlog message")
    4.10          if #available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *) {
    4.11              os_log("%d %@", 1, "direct blah")
    4.12          }
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/pEpForiOSTests/LoggerTest.swift	Mon Dec 31 12:58:13 2018 +0100
     5.3 @@ -0,0 +1,21 @@
     5.4 +//
     5.5 +//  LoggerTest.swift
     5.6 +//  pEpForiOSTests
     5.7 +//
     5.8 +//  Created by Dirk Zimmermann on 18.12.18.
     5.9 +//  Copyright © 2018 p≡p Security S.A. All rights reserved.
    5.10 +//
    5.11 +
    5.12 +import XCTest
    5.13 +
    5.14 +@testable import pEpForiOS
    5.15 +@testable import MessageModel
    5.16 +
    5.17 +class LoggerTest: XCTestCase {
    5.18 +    func testSimple() {
    5.19 +        let log1 = Logger(subsystem: "sys1", category: "cat1")
    5.20 +        log1.log("hi (standalone)")
    5.21 +        log1.log("hi (one number): %d", 2)
    5.22 +        log1.log("hi (one number plus string): %d %@", 2, "parameters")
    5.23 +    }
    5.24 +}