Skip to content

Commit

Permalink
Refactored to use new logging and remove os.log. (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
mellowgeek authored Jun 8, 2020
1 parent 664e42d commit cc0278b
Show file tree
Hide file tree
Showing 32 changed files with 40 additions and 75 deletions.
13 changes: 5 additions & 8 deletions CoEpi/api/AlamofireLogger.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import Alamofire
import os.log
import Foundation

final class AlamofireLogger: EventMonitor {

func requestDidResume(_ request: Request) {
let body = request.request.flatMap { $0.httpBody.map { String(decoding: $0, as: UTF8.self) } } ?? "None"
os_log("Request Started: %{public}@, body: %{public}@", log: networkingLog, type: .debug, "\(request)",
"\(body)")

log.d("Request Started: \(request), body: \(body)")
}

func request<Value>(_ request: DataRequest, didParseResponse response: DataResponse<Value, Error>) {
os_log("Response Received: %{public}@", log: networkingLog, type: .debug, "\(request)")
log.d("Request Received: \(request)")
}

func request(_ request: DataRequest, didParseResponse response: DataResponse<Data?, AFError>) {
os_log("Response Received (unserialized): %{public}@", log: networkingLog, type: .debug,
response.debugDescription)
log.d("Request Received (unserialized): \(response.debugDescription)")
}

func requestDidFinish(_ request: Request) {
os_log("Request did finish: %{public}@", log: networkingLog, type: .debug,
request.response.debugDescription)
log.d("Request did finish: \(request.response.debugDescription)")
}
}
1 change: 0 additions & 1 deletion CoEpi/bg task/BackgroundTask.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation
import BackgroundTasks
import os.log
import RxSwift

/// Long running background task
Expand Down
9 changes: 4 additions & 5 deletions CoEpi/bg task/BackgroundTaskScheduler.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import UIKit
import BackgroundTasks
import os.log

/// Registers and schedules background processing tasks
class BackgroundTaskScheduler {
Expand All @@ -26,20 +25,20 @@ class BackgroundTaskScheduler {
}

private func handleTask(task: BGProcessingTask) {
os_log("handleTask: ", log: servicesLog, type: .debug, "\(task)")
log.d("handleTask: \(task)")

schedule(delay: self.task.scheduleInterval)

self.task.execute(task: task)

task.expirationHandler = {
task.setTaskCompleted(success: false)
os_log("Background task expired", log: servicesLog, type: .debug)
log.d("Background task expired")
}
}

private func schedule(delay: TimeInterval) {
os_log("Scheduling BG task. Delay: %{public}@", log: servicesLog, type: .debug, "\(delay)")
log.d("Scheduling BG task. Delay: \(delay)")

let request = BGProcessingTaskRequest(identifier: task.identifier)
request.earliestBeginDate = Date(timeIntervalSinceNow: delay)
Expand All @@ -49,7 +48,7 @@ class BackgroundTaskScheduler {
// TODO flag to use bg thread. Or maybe just use always thread.
try BGTaskScheduler.shared.submit(request)
} catch let e {
os_log("Unable to submit task: %{public}@, error: %{public}@", log: servicesLog, type: .debug, "\(request)", "\(e)")
log.e("Unable to submit task: \(request), error: \(e)")
}
}
}
3 changes: 1 addition & 2 deletions CoEpi/bg task/alerts/FetchAlertsBackgroundTask.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import RxSwift
import os.log
import BackgroundTasks
import Action

Expand All @@ -25,7 +24,7 @@ class FetchAlertsBackgroundTask: BackgroundTask {
}

func execute(task: BGProcessingTask) {
os_log("Starting fetch alerts bg task...", log: servicesLog, type: .debug)
log.d("Starting fetch alerts bg task...")

fetchAlertsAction.execute()
}
Expand Down
5 changes: 2 additions & 3 deletions CoEpi/ble/BleAdapter.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation
import TCNClient
import RxSwift
import os.log

class BleAdapter {
private let tcnService: TCNBluetoothService
Expand All @@ -12,7 +11,7 @@ class BleAdapter {
init(tcnGenerator: TcnGenerator) {
tcnService = TCNBluetoothService(tcnGenerator: { [myTcn] in
let tcnResult = tcnGenerator.generateTcn()
os_log("Generated TCN: %{public}@", type: .error, "\(tcnResult)")
log.d("Generated TCN: \(tcnResult)")

return {
switch tcnResult {
Expand All @@ -28,7 +27,7 @@ class BleAdapter {
discovered.onNext(data)
}) { error in
// TODO What kind of errors? Should we notify the user?
os_log("TCN service error: %{public}@", type: .error, "\(error)")
log.e("TCN service error: \(error)")
}

tcnService.start()
Expand Down
1 change: 0 additions & 1 deletion CoEpi/cross/PeriodicAlertsFetcher.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation
import RxSwift
import os.log

class PeriodicAlertsFetcher {
private let disposeBag = DisposeBag()
Expand Down
3 changes: 1 addition & 2 deletions CoEpi/cross/ReportsHandler.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import os.log

protocol MatchingReportsHandler {
func handleMatchingReports(reports: [ReceivedCenReport])
Expand All @@ -25,7 +24,7 @@ class MatchingReportsHandlerImpl: MatchingReportsHandler {
notifiyNewAlerts(count: insertedCount)
}

os_log("Alerts update task finished. Saved new reports: %{public}d", log: servicesLog, type: .debug, insertedCount)
log.d("Alerts update task finished. Saved new reports: \(insertedCount)")
}

private func notifiyNewAlerts(count: Int) {
Expand Down
7 changes: 3 additions & 4 deletions CoEpi/cross/ScannedCensHandler.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import os.log
import RxSwift

class ScannedCensHandler {
Expand All @@ -20,16 +19,16 @@ class ScannedCensHandler {
.distinctUntilChanged()

.subscribe(onNext: { [tcnsRecorder] data in
os_log("Observed CEN: %{public}@", log: bleLog, "\(data.toHex())")
log.d("Observed CEN: \(data.toHex())")

let res = tcnsRecorder.recordTcn(tcn: data)

if (!res.isSuccess()) {
os_log("Error recording TCN: %{public}@", log: bleLog, type: .debug, "\(res)")
log.e("Error recording TCN: \(res)")
}

}, onError: { error in
os_log("Error in central cen observer: %{public}@", log: bleLog, error.localizedDescription)
log.e("Error in central cen observer: \(error.localizedDescription)")
}).disposed(by: disposeBag)
}

Expand Down
5 changes: 2 additions & 3 deletions CoEpi/dao/AlertDao.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import os.log
import RealmSwift
import RxSwift

Expand Down Expand Up @@ -54,10 +53,10 @@ class RealmAlertDao: AlertDao, RealmDao {
}

func delete(alert: Alert) {
os_log("ACKing alert: %{public}@", type: .debug, "\(alert)")
log.d("ACKing alert: \(alert)")

guard let realmAlert = findAlertBy(id: alert.id) else {
os_log("Couldn't find alert to delete: %{public}@", type: .error, "\(alert)")
log.e("Couldn't find alert to delete: \(alert)")
return
}

Expand Down
9 changes: 4 additions & 5 deletions CoEpi/dao/RealmCENReportDao.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation
import RealmSwift
import RxSwift
import os.log

// TODO remove, move functionality to RawAlertDao
protocol CENReportDao {
Expand Down Expand Up @@ -38,24 +37,24 @@ class RealmCENReportDao: CENReportDao, RealmDao {
func insert(report: ReceivedCenReport) -> Bool {
let result = realm.objects(RealmCENReport.self).filter("id = %@", report.report.id)
if result.count == 0 {
os_log("Report didn't exist in db, inserting: %{public}@", type: .debug, report.description)
log.d("Report didn't exist in db, inserting: \(report.description)")
let newCENReport = RealmCENReport(report)
write {
realm.add(newCENReport)
}
return true
} else {
//duplicate entry: skipping
os_log("Report already in db, id = %{public}@", type: .debug, report.report.id)
log.d("Report already in db, id = \(report.report.id)")
return false
}
}

func delete(report: ReceivedCenReport) {
os_log("ACKing report: %{public}@", type: .debug, "\(report)")
log.d("ACKing report: \(report)")

guard let realmReport = findReportBy(id: report.report.id) else {
os_log("Couldn't find report to delete: %{public}@", type: .error, "\(report)")
log.e("Couldn't find report to delete: \(report)")
return
}

Expand Down
4 changes: 1 addition & 3 deletions CoEpi/native/LibResult.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import os.log

struct LibResult<T: Decodable>: Decodable {
let status: Int
Expand Down Expand Up @@ -43,8 +42,7 @@ extension Unmanaged where Instance == CFString {
let resultValue: CFString = takeRetainedValue()
let resultString = resultValue as String

os_log("Deserializing native core result: %{public}@", log: servicesLog,
type: .debug, "\(resultString)")
log.d("Deserializing native core result: \(resultString)")

// TODO review safety of utf-8 force unwrap
let data = resultString.data(using: .utf8)!
Expand Down
5 changes: 2 additions & 3 deletions CoEpi/repo/AlertRepo.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation
import RxCocoa
import RxSwift
import os.log

protocol AlertRepo {
var alerts: Observable<[Alert]> { get }
Expand Down Expand Up @@ -45,13 +44,13 @@ class AlertRepoImpl: AlertRepo {
// TODO so we have to update "last fetched time segment" only if alerts save was success
// TODO NOTE that storage will likely be moved to Rust. Let's wait until this is cleared.
if alertDao.insert(alert: alert) {
os_log("Inserted new alert: %{alert}@", log: servicesLog, type: .debug, "\(alert)")
log.d("Inserted new alert: \(alert)")
}
}
updateReportsStateSubject.accept(.success(data: ()))

case .failure(let error):
os_log("Error fetching alerts: %{public}@", log: servicesLog, type: .debug, "\(error)")
log.e("Error fetching alerts: \(error)")
updateReportsStateSubject.accept(OperationState.failure(error: error))
}

Expand Down
1 change: 0 additions & 1 deletion CoEpi/repo/SymptomRepo.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation
import RxSwift
import os.log

protocol SymptomRepo {
func symptoms() -> [Symptom]
Expand Down
3 changes: 1 addition & 2 deletions CoEpi/ui/alerts/AlertsViewModel.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Dip
import RxCocoa
import RxSwift
import os.log
import Foundation

class AlertsViewModel {
Expand All @@ -22,7 +21,7 @@ class AlertsViewModel {

updateStatusText = alertRepo.updateReportsState
.do(onNext: { result in
os_log("Got alerts result in view model: %{public}@", log: servicesLog, type: .debug, "\(result)")
log.d("Got alerts result in view model: \(result)")
})
.filter { $0.shouldShowText() }
.map { $0.asText() }
Expand Down
3 changes: 1 addition & 2 deletions CoEpi/ui/home/HomeViewModel.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import RxSwift
import os.log

class HomeViewModel {
let title = L10n.Ux.Home.title
Expand All @@ -12,7 +11,7 @@ class HomeViewModel {
self.rootNav = rootNav

startPermissions.granted.subscribe(onNext: { granted in
os_log("Start permissions granted: %{public}@", log: servicesLog, type: .debug, "\(granted)")
log.d("Start permissions granted: \(granted)")
}).disposed(by: disposeBag)

startPermissions.request()
Expand Down
3 changes: 1 addition & 2 deletions CoEpi/ui/notifications/AppBadgeUpdater.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation
import UserNotifications
import os.log
import UIKit

protocol AppBadgeUpdater {
Expand All @@ -22,7 +21,7 @@ class AppBadgeUpdaterImpl: AppBadgeUpdater {
else { return }

DispatchQueue.main.async {
os_log("Updating app badge: %{public}@", log: servicesLog, type: .debug, "\(number)")
log.d("Updating app badge: \(number)")
UIApplication.shared.applicationIconBadgeNumber = number
}
}
Expand Down
3 changes: 1 addition & 2 deletions CoEpi/ui/notifications/NotificationShower.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import UIKit
import os.log

protocol NotificationShower {
func showNotification(data: NotificationData)
Expand All @@ -25,7 +24,7 @@ class NotificationShowerImpl: NotificationShower {
}

private func showNotification(data: NotificationData, canPlaySound: Bool) {
os_log("Showing notification", log: servicesLog, type: .debug)
log.d("Showing notification")

let content = UNMutableNotificationContent()
content.title = data.title
Expand Down
3 changes: 1 addition & 2 deletions CoEpi/ui/notifications/NotificationsDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import UIKit
import UserNotifications
import os.log

class NotificationsDelegate: NSObject, UNUserNotificationCenterDelegate {

Expand All @@ -26,7 +25,7 @@ class NotificationsDelegate: NSObject, UNUserNotificationCenterDelegate {
let identifierStr = response.notification.request.identifier

guard let identifier = NotificationId(rawValue: identifierStr) else {
os_log("Selected notification with unknown id: %{public}@", log: servicesLog, type: .debug, identifierStr)
log.d("Selected notification with unknown id: \(identifierStr)")
return
}

Expand Down
3 changes: 1 addition & 2 deletions CoEpi/ui/permissions/StartPermissions.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import UIKit
import RxSwift
import RxCocoa
import os.log

protocol StartPermissions {
var granted: Observable<Bool> { get }
Expand All @@ -21,7 +20,7 @@ class StartPermissionsImpl: StartPermissions {
func request() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if let error = error {
os_log("Error requesting permission: %{public}@", log: servicesLog, type: .debug, "\(error)")
log.e("Error requesting permission: \(error)")
}
self.grantedSubject.accept(granted)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Dip
import RxCocoa
import RxSwift
import os.log

class CoughDaysViewModel {
private let symptomFlowManager: SymptomFlowManager
Expand Down Expand Up @@ -30,7 +29,7 @@ class CoughDaysViewModel {
symptomFlowManager.setCoughDays(.some(SymptomInputs.Days(value: days))).expect()
} else {
// TODO handle
os_log("Invalid input: %{public}@ TODO handle", log: servicesLog, type: .debug, "\(daysStr)")
log.d("Invalid input: \(daysStr) TODO handle")
}
}
daysIsEmpty.accept(daysStr.isEmpty)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Dip
import RxCocoa
import RxSwift
import os.log

class CoughHowViewModel {
private let symptomFlowManager: SymptomFlowManager
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Dip
import RxCocoa
import RxSwift
import os.log

class CoughTypeViewModel {
private let symptomFlowManager: SymptomFlowManager
Expand Down
Loading

0 comments on commit cc0278b

Please sign in to comment.