From b8dc015b19ae6fbf6d0cf45b2853f4b509b5d41f Mon Sep 17 00:00:00 2001 From: Koichi Yokota Date: Sat, 13 Aug 2022 22:47:30 +0900 Subject: [PATCH] Use macro for debugging (#50) --- .github/workflows/ci.yml | 4 ++-- CHANGELOG.md | 6 +++++- Configurations/Common.xcconfig | 2 +- Configurations/Framework.xcconfig | 2 ++ Sources/Puppy/FileLogger.swift | 8 ++++---- Sources/Puppy/FileRotationLogger.swift | 10 +++++----- Sources/Puppy/Puppy.swift | 12 ++++-------- Tests/PuppyTests/FileLoggerTests.swift | 2 -- Tests/PuppyTests/FileManager+WindowsTests.swift | 2 -- Tests/PuppyTests/FileRotationLoggerTests.swift | 2 -- scripts/llvm-cov-script.sh | 2 +- 11 files changed, 24 insertions(+), 28 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 579253e..653b4b8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,7 +31,7 @@ jobs: - name: Build a Swift package run: swift build -v -c release - name: Test a Swift package - run: swift test -v + run: swift test -v -Xswiftc -DPUPPY_DEBUG swiftpm-backwards: name: SwiftPM - Swift ${{ matrix.swift-version }} on ${{ matrix.os }} @@ -53,7 +53,7 @@ jobs: - name: Build a Swift package run: swift build -v -c release - name: Test a Swift package - run: swift test -v + run: swift test -v -Xswiftc -DPUPPY_DEBUG cmake: name: CMake - Swift ${{ matrix.swift-version }} on ${{ matrix.os }} diff --git a/CHANGELOG.md b/CHANGELOG.md index a2be660..a53fc95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ -## [0.5.0](https://github.com/sushichop/Puppy/releases/tag/x.y.z) (2022-02-28) +## [x.y.z](https://github.com/sushichop/Puppy/releases/tag/x.y.z) (yyyy-mm-dd) + +- Use macro for debugging. [#50](https://github.com/sushichop/Puppy/pull/50) + +## [0.5.0](https://github.com/sushichop/Puppy/releases/tag/0.5.0) (2022-02-28) - Remove concurrency features. [#39](https://github.com/sushichop/Puppy/pull/39) - Fix the default rotation type to `numbering`. [#40](https://github.com/sushichop/Puppy/pull/40) diff --git a/Configurations/Common.xcconfig b/Configurations/Common.xcconfig index c8b4659..0eef02d 100644 --- a/Configurations/Common.xcconfig +++ b/Configurations/Common.xcconfig @@ -23,5 +23,5 @@ MARKETING_VERSION = 0.5.0 DYLIB_CURRENT_VERSION = 1 CURRENT_PROJECT_VERSION = $(DYLIB_CURRENT_VERSION) -SCP_COPYRIGHT = Copyright (c) 2020-2021 Koichi Yokota +SCP_COPYRIGHT = Copyright (c) 2020-2022 Koichi Yokota SCP_PRODUCT_BUNDLE_IDENTIFIER_PREFIX = net.sushichop diff --git a/Configurations/Framework.xcconfig b/Configurations/Framework.xcconfig index 9d90548..bbc8ee7 100644 --- a/Configurations/Framework.xcconfig +++ b/Configurations/Framework.xcconfig @@ -12,3 +12,5 @@ APPLICATION_EXTENSION_API_ONLY = YES ENABLE_BITCODE = YES BITCODE_GENERATION_MODE = bitcode OTHER_CFLAGS = -fembed-bitcode + +//SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) PUPPY_DEBUG diff --git a/Sources/Puppy/FileLogger.swift b/Sources/Puppy/FileLogger.swift index df48e7d..94b38a1 100644 --- a/Sources/Puppy/FileLogger.swift +++ b/Sources/Puppy/FileLogger.swift @@ -20,7 +20,7 @@ public class FileLogger: BaseLogger { self.fileURL = fileURL self.filePermission = filePermission self.flushMode = flushMode - debug("fileURL is \(fileURL).") + puppyDebug("initialized, fileURL: \(fileURL)") super.init(label) try validateFileURL(fileURL) try validateFilePermission(fileURL, filePermission: filePermission) @@ -85,7 +85,7 @@ public class FileLogger: BaseLogger { let directoryURL = fileURL.deletingLastPathComponent() do { try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil) - debug("created directoryURL, directoryURL: \(directoryURL)") + puppyDebug("created directoryURL, directoryURL: \(directoryURL)") } catch { throw FileError.creatingDirectoryFailed(at: directoryURL) } @@ -93,12 +93,12 @@ public class FileLogger: BaseLogger { if !FileManager.default.fileExists(atPath: fileURL.path) { let successful = FileManager.default.createFile(atPath: fileURL.path, contents: nil, attributes: [FileAttributeKey.posixPermissions: uintPermission]) if successful { - debug("succeeded in creating filePath") + puppyDebug("succeeded in creating filePath") } else { throw FileError.creatingFileFailed(at: fileURL) } } else { - debug("filePath exists, filePath: \(fileURL.path)") + puppyDebug("filePath exists, filePath: \(fileURL.path)") } if fileHandle == nil { diff --git a/Sources/Puppy/FileRotationLogger.swift b/Sources/Puppy/FileRotationLogger.swift index cefa2af..9ed1998 100644 --- a/Sources/Puppy/FileRotationLogger.swift +++ b/Sources/Puppy/FileRotationLogger.swift @@ -36,7 +36,7 @@ public class FileRotationLogger: FileLogger { for (index, oldArchivedFileURL) in oldArchivedFileURLs.enumerated() { let generationNumber = oldArchivedFileURLs.count + 1 - index let rotatedFileURL = oldArchivedFileURL.deletingPathExtension().appendingPathExtension("\(generationNumber)") - debug("generationNumber: \(generationNumber), rotatedFileURL: \(rotatedFileURL)") + puppyDebug("generationNumber: \(generationNumber), rotatedFileURL: \(rotatedFileURL)") if !FileManager.default.fileExists(atPath: rotatedFileURL.path) { try FileManager.default.moveItem(at: oldArchivedFileURL, to: rotatedFileURL) } @@ -68,7 +68,7 @@ public class FileRotationLogger: FileLogger { // Opens a new target file. do { - debug("will openFile in rotateFiles") + puppyDebug("will openFile in rotateFiles") try openFile() } catch { print("error in openFile while rotating, error: \(error.localizedDescription)") @@ -99,7 +99,7 @@ public class FileRotationLogger: FileLogger { } catch { print("error in ascArchivedFileURLs, error: \(error.localizedDescription)") } - debug("ascArchivedFileURLs: \(ascArchivedFileURLs)") + puppyDebug("ascArchivedFileURLs: \(ascArchivedFileURLs)") return ascArchivedFileURLs } @@ -108,9 +108,9 @@ public class FileRotationLogger: FileLogger { let archivedFileURLs = ascArchivedFileURLs(fileURL) if archivedFileURLs.count > maxArchivedFilesCount { for index in 0 ..< archivedFileURLs.count - Int(maxArchivedFilesCount) { - debug("\(archivedFileURLs[index]) will be removed...") + puppyDebug("\(archivedFileURLs[index]) will be removed...") try FileManager.default.removeItem(at: archivedFileURLs[index]) - debug("\(archivedFileURLs[index]) has been removed") + puppyDebug("\(archivedFileURLs[index]) has been removed") delegate?.fileRotationLogger(self, didRemoveArchivedFileURL: archivedFileURLs[index]) } } diff --git a/Sources/Puppy/Puppy.swift b/Sources/Puppy/Puppy.swift index 2f55e4d..028a140 100644 --- a/Sources/Puppy/Puppy.swift +++ b/Sources/Puppy/Puppy.swift @@ -11,9 +11,7 @@ typealias Log = Puppy public class Puppy { - public static var useDebug = false public static let `default` = Puppy() - public private(set) var loggers = Set() public init() {} @@ -108,10 +106,8 @@ public class Puppy { } @inlinable -func debug(_ items: Any) { - #if DEBUG - if Puppy.useDebug { - print("Puppy:", items) - } - #endif // DEBUG +func puppyDebug(_ items: Any) { + #if DEBUG && PUPPY_DEBUG + print("PUPPY_DEBUG:", items) + #endif // DEBUG && PUPPY_DEBUG } diff --git a/Tests/PuppyTests/FileLoggerTests.swift b/Tests/PuppyTests/FileLoggerTests.swift index d24acc5..4b51cce 100644 --- a/Tests/PuppyTests/FileLoggerTests.swift +++ b/Tests/PuppyTests/FileLoggerTests.swift @@ -5,11 +5,9 @@ final class FileLoggerTests: XCTestCase { override func setUpWithError() throws { try super.setUpWithError() - Puppy.useDebug = true } override func tearDownWithError() throws { - Puppy.useDebug = false try super.tearDownWithError() } diff --git a/Tests/PuppyTests/FileManager+WindowsTests.swift b/Tests/PuppyTests/FileManager+WindowsTests.swift index f83374f..07a5428 100644 --- a/Tests/PuppyTests/FileManager+WindowsTests.swift +++ b/Tests/PuppyTests/FileManager+WindowsTests.swift @@ -5,11 +5,9 @@ class FileManagerWindowsTests: XCTestCase { override func setUpWithError() throws { try super.setUpWithError() - Puppy.useDebug = true } override func tearDownWithError() throws { - Puppy.useDebug = false try super.tearDownWithError() } diff --git a/Tests/PuppyTests/FileRotationLoggerTests.swift b/Tests/PuppyTests/FileRotationLoggerTests.swift index 3dd5f59..f995555 100644 --- a/Tests/PuppyTests/FileRotationLoggerTests.swift +++ b/Tests/PuppyTests/FileRotationLoggerTests.swift @@ -5,11 +5,9 @@ final class FileRotationLoggerTests: XCTestCase { override func setUpWithError() throws { try super.setUpWithError() - Puppy.useDebug = true } override func tearDownWithError() throws { - Puppy.useDebug = false try super.tearDownWithError() } diff --git a/scripts/llvm-cov-script.sh b/scripts/llvm-cov-script.sh index a0ea00e..f89b40e 100755 --- a/scripts/llvm-cov-script.sh +++ b/scripts/llvm-cov-script.sh @@ -10,7 +10,7 @@ if [ "${SCRIPT_TYPE}" = "export" ]; then fi swift package clean -swift test --enable-code-coverage +swift test -v -Xswiftc -DPUPPY_DEBUG --enable-code-coverage BIN_PATH="$(swift build --show-bin-path)" XCTEST_PATH="$(find ${BIN_PATH} -name '*.xctest')"