Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/logging arguments parsing #337

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

castlele
Copy link
Contributor

Передача CVarArgs для StaticString

Теперь передача нескольких аргументов работает без багов:

logger.verbose("🎉 first: %@, second: %i, third: %f", "hello", 1, 1.234)
logger.error("🎉 first: %@, second: %i, third: %f", "hello", 1, 1.234)
logger.debug("🎉 first: %@, second: %i, third: %f", "hello", 1, 1.234)
logger.info("🎉 first: %@, second: %i, third: %f", "hello", 1, 1.234)
logger.fault("🎉 first: %@, second: %i, third: %f", "hello", 1, 1.234)

Вывод будет таким:

[PointsOfInterest] 🎉 first: hello, second: 1, third: 1.234000
[PointsOfInterest] 🎉 first: hello, second: 1, third: 1.234000
[PointsOfInterest] 🎉 first: hello, second: 1, third: 1.234000
[PointsOfInterest] 🎉 first: hello, second: 1, third: 1.234000
[PointsOfInterest] 🎉 first: hello, second: 1, third: 1.234000

Так же теперь работает string interpolation

logger.verbose("🐈 Hello \(type(of: self)), i'm machine number {\(123)}")
logger.error("🐈 Hello \(type(of: self)), i'm machine number {\(123)}")
logger.debug("🐈 Hello \(type(of: self)), i'm machine number {\(123)}")
logger.info("🐈 Hello \(type(of: self)), i'm machine number {\(123)}")
logger.fault("🐈 Hello \(type(of: self)), i'm machine number {\(123)}")

Comment on lines 30 to 32
func log(type: OSLogType, log: OSLog?, _ message: String) {
// empty implementation
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а зачем пустая имплементация?

var mutableMessage = message

mutableMessage.withUTF8 { (buf: UnsafeBufferPointer<UInt8>) in
buf.baseAddress!.withMemoryRebound(to: CChar.self, capacity: buf.count) { str in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

force unwrap плохо

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В стандартной реализации тоже force unwrap. Видимо в данном случае можно 🤷‍♂️

public struct DefaultLoggerHandler: LogHandler {

public var logInfo: OSLog
public var logger: LogOutputRepresentater?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

с неймингом LogOutputRepresentater явно что-то не то. хотя бы опечатка, но я бы ещё поменял на что-то более чётко отражающее суть

@available(iOS 14.0, *)
extension Logger: LogOutputRepresentater {
public func log(type: OSLogType, log: OSLog?, _ message: String) {
self.log(level: type, "\(message)")
Copy link
Contributor

@petropavel13 petropavel13 Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а нам вообще нужно \() использовать? не можем напрямую отправить строку?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не можем там в качестве аргумента OsLogMessage и он скастить не может

Comment on lines +51 to +53
public func log(_ message: StaticString, log: OSLog? = nil, type: OSLogType, _ arguments: CVarArg...) {
let stringMessage = String(format: "\(message)", arguments: arguments)
self.log(type: type, log: log, stringMessage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

правильно ли я понимаю, что в этом моменте мы всю секьюрность os_log теряем?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Как я понял, мы ничего не теряем, ведь конечная реализация логера - стандартная реализация os_log
https://github.com/apple/swift/blob/swift-5.2-branch/stdlib/public/Darwin/os/os_log.swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants