-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eca84fd
commit a7e048a
Showing
3 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
import OSLog | ||
|
||
protocol FirebaseInternalLog { | ||
func notice(_ message: String) | ||
|
||
func info(_ message: String) | ||
|
||
func debug(_ message: String) | ||
|
||
func warning(_ message: String) | ||
|
||
func error(_ message: String) | ||
|
||
func fault(_ message: String) | ||
} | ||
|
||
extension OSLog: FirebaseInternalLog { | ||
func notice(_ message: String) { | ||
os_log("%s", log: self, type: .default, message) | ||
} | ||
|
||
func info(_ message: String) { | ||
os_log("%s", log: self, type: .info, message) | ||
} | ||
|
||
func debug(_ message: String) { | ||
os_log("%s", log: self, type: .debug, message) | ||
} | ||
|
||
func warning(_ message: String) { | ||
os_log("%s", log: self, type: .default, message) | ||
} | ||
|
||
func error(_ message: String) { | ||
os_log("%s", log: self, type: .error, message) | ||
} | ||
|
||
func fault(_ message: String) { | ||
os_log("%s", log: self, type: .fault, message) | ||
} | ||
} | ||
|
||
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) | ||
class FirebaseInternalLogger: FirebaseInternalLog { | ||
private let logger: Logger | ||
|
||
required init(subsystem: String, category: String) { | ||
logger = Logger(subsystem: subsystem, category: category) | ||
} | ||
|
||
func notice(_ message: String) { | ||
logger.notice("\(message)") | ||
} | ||
|
||
func info(_ message: String) { | ||
logger.info("\(message)") | ||
} | ||
|
||
func debug(_ message: String) { | ||
logger.debug("\(message)") | ||
} | ||
|
||
func warning(_ message: String) { | ||
logger.warning("\(message)") | ||
} | ||
|
||
func error(_ message: String) { | ||
logger.error("\(message)") | ||
} | ||
|
||
func fault(_ message: String) { | ||
logger.fault("\(message)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
import OSLog | ||
|
||
public struct FirebaseProduct { | ||
var name: String | ||
|
||
public init(name: String) { | ||
self.name = name | ||
} | ||
} | ||
|
||
public class FirebaseLogger { | ||
let subsystem: String = "com.google.firebase" | ||
|
||
let firebaseProduct: FirebaseProduct | ||
|
||
private lazy var logger: FirebaseInternalLog = { | ||
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) { | ||
return FirebaseInternalLogger(subsystem: subsystem, category: firebaseProduct.name) | ||
} else { | ||
return OSLog(subsystem: subsystem, category: firebaseProduct.name) | ||
} | ||
}() | ||
|
||
public init(firebaseProduct: FirebaseProduct) { | ||
self.firebaseProduct = firebaseProduct | ||
} | ||
|
||
public func notice(_ message: String) { | ||
logger.notice(message) | ||
} | ||
|
||
public func info(_ message: String) { | ||
logger.info(message) | ||
} | ||
|
||
public func debug(_ message: String) { | ||
logger.debug(message) | ||
} | ||
|
||
public func warning(_ message: String) { | ||
logger.warning(message) | ||
} | ||
|
||
public func error(_ message: String) { | ||
logger.error(message) | ||
} | ||
|
||
public func fault(_ message: String) { | ||
logger.fault(message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import FirebaseSharedSwift | ||
import Foundation | ||
|
||
// MARK: - Test Suite | ||
|
||
import XCTest | ||
|
||
extension FirebaseProduct { | ||
static let testLogger = FirebaseProduct(name: "firebase_logger_test") | ||
} | ||
|
||
class TestFirebaseLogger: XCTestCase { | ||
func testLoggerUsage() { | ||
let testLogger = FirebaseLogger(firebaseProduct: FirebaseProduct.testLogger) | ||
testLogger.debug("Set up successfully!") | ||
} | ||
} |