Skip to content

Commit

Permalink
Make the logLevel property changeable (#5)
Browse files Browse the repository at this point in the history
* Make the `logLevel` property changable

* Update `CHANGELOG.md`
  • Loading branch information
ns-vasilev authored Jan 19, 2024
1 parent 83d5737 commit e649dda
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
1 change: 0 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ opt_in_rules: # some rules are only opt-in
- attributes
- closure_body_length
- closure_end_indentation
- closure_spacing
- collection_alignment
- contains_over_filter_count
- contains_over_filter_is_empty
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.

## [Unreleased]
#### Added
- Make the `logLevel` property changeable
- Added in Pull Request [#5](https://github.com/space-code/log/pull/5).
- Add files to comply with community standards
- Added in Pull Request [#4](https://github.com/space-code/log/pull/4).
- Update GitHub Actions workflow
Expand Down
15 changes: 12 additions & 3 deletions Sources/Log/Classes/Core/Logger/Logger.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
//
// log
// Copyright © 2023 Space Code. All rights reserved.
// Copyright © 2024 Space Code. All rights reserved.
//

import Foundation

// MARK: - Logger

/// A class responsible for logging functionality.
open class Logger {
// MARK: Properties

/// The current log level for this logger.
let logLevel: LogLevel
private var _logLevel: Atomic<LogLevel>

/// The current log level for this logger.
public var logLevel: LogLevel {
get { _logLevel.value }
set { _logLevel.value = newValue }
}

/// An array of printer strategies to handle the log output.
let printers: [IPrinterStrategy]

Expand All @@ -26,7 +35,7 @@ open class Logger {
logLevel: LogLevel
) {
self.printers = printers
self.logLevel = logLevel
_logLevel = Atomic(value: logLevel)
}

// MARK: Private
Expand Down
43 changes: 43 additions & 0 deletions Sources/Log/Classes/Helpers/Atomic/Atomic.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// log
// Copyright © 2024 Space Code. All rights reserved.
//

import Foundation

// MARK: - Atomic

/// The Atomic class is designed to provide thread-safe access to a mutable value.
final class Atomic<Value> {
// MARK: Properties

/// NSLock instance for synchronization.
private let lock = NSLock()
/// The actual mutable value.
private var _value: Value

/// Computed property to get and set the value atomically
var value: Value {
get { lock.synchronized { _value } }
set { lock.synchronized { _value = newValue }}
}

// MARK: Initialization

/// Initializes the Atomic instance with an initial value.
init(value: Value) {
_value = value
}
}

// MARK: - Extensions

private extension NSLock {
/// Synchronizes a code block using the NSLock instance.
/// This helps ensure that only one thread can access the critical section at a time.
func synchronized<T>(block: () throws -> T) rethrows -> T {
lock()
defer { unlock() }
return try block()
}
}
17 changes: 16 additions & 1 deletion Tests/LogTests/UnitTests/LogTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// log
// Copyright © 2023 Space Code. All rights reserved.
// Copyright © 2024 Space Code. All rights reserved.
//

import Log
Expand Down Expand Up @@ -119,6 +119,21 @@ final class LogTests: XCTestCase {
XCTAssertNil(printerMock.invokedLogParameters?.message)
}

func test_thatLoggerDoesNotPrintAnything_whenLogLevelValueDidChange() {
// given
let sut = prepareSut()

// when
sut.logLevel = .info
sut.debug(message: .message)
sut.info(message: .message)

// then
XCTAssertEqual(sut.logLevel, .info)
XCTAssertEqual(printerMock.invokedLogCount, 1)
XCTAssertEqual(printerMock.invokedLogParameters?.message, .message)
}

// MARK: Private

private func prepareSut(logLevel: LogLevel = .all) -> Logger {
Expand Down

0 comments on commit e649dda

Please sign in to comment.