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

Updated print and os_log calls to modern OSLog class. #129

Merged
merged 14 commits into from
Oct 30, 2024
Merged
8 changes: 6 additions & 2 deletions PPPC Utility.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
6EC40A16214ECF1E00BE4F17 /* SaveViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EC40A15214ECF1E00BE4F17 /* SaveViewController.swift */; };
6EC40A1C214EF87800BE4F17 /* SigningIdentity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EC40A1B214EF87800BE4F17 /* SigningIdentity.swift */; };
71061E54246106C800822D35 /* LoadExecutableError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71061E53246106C800822D35 /* LoadExecutableError.swift */; };
A149C78D2CC68FBF00E0789E /* LoggerExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A149C78C2CC68FA100E0789E /* LoggerExtensions.swift */; };
B5E09548250BCCFC00A40409 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E09547250BCCFC00A40409 /* Alert.swift */; };
C01BEDBA28636F57001B0B3B /* SemanticVersionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C01BEDB928636F57001B0B3B /* SemanticVersionTests.swift */; };
C03270BA28636330008B38E0 /* SemanticVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = C03270B928636330008B38E0 /* SemanticVersion.swift */; };
Expand Down Expand Up @@ -110,6 +111,7 @@
6EC40A1B214EF87800BE4F17 /* SigningIdentity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SigningIdentity.swift; sourceTree = "<group>"; };
71061E53246106C800822D35 /* LoadExecutableError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadExecutableError.swift; sourceTree = "<group>"; };
97227C6726248CD7000F26C1 /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
A149C78C2CC68FA100E0789E /* LoggerExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggerExtensions.swift; sourceTree = "<group>"; };
B5E09547250BCCFC00A40409 /* Alert.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Alert.swift; sourceTree = "<group>"; };
C01BEDB928636F57001B0B3B /* SemanticVersionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SemanticVersionTests.swift; sourceTree = "<group>"; };
C03270B928636330008B38E0 /* SemanticVersion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SemanticVersion.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -163,6 +165,7 @@
5F90EBDB2319931100738D09 /* Extensions */ = {
isa = PBXGroup;
children = (
A149C78C2CC68FA100E0789E /* LoggerExtensions.swift */,
5F90EBDC2319934F00738D09 /* ArrayExtensions.swift */,
345B01D523FDBF55008838B6 /* TCCProfileExtensions.swift */,
);
Expand Down Expand Up @@ -509,6 +512,7 @@
buildActionMask = 2147483647;
files = (
34DED4D623FDDB2B00C53FB9 /* TCCProfileConfigurationPanel.swift in Sources */,
A149C78D2CC68FBF00E0789E /* LoggerExtensions.swift in Sources */,
5F90EBE12319970500738D09 /* TCCProfileImporter.swift in Sources */,
5F95AE1623158F03002E0A22 /* SwiftyCMSDecoder.swift in Sources */,
C03270C028636397008B38E0 /* Networking.swift in Sources */,
Expand Down Expand Up @@ -743,7 +747,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = "1.4.0";
MARKETING_VERSION = 1.4.0;
PRODUCT_BUNDLE_IDENTIFIER = com.jamf.opensource.pppcutility;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -766,7 +770,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = "1.4.0";
MARKETING_VERSION = 1.4.0;
PRODUCT_BUNDLE_IDENTIFIER = com.jamf.opensource.pppcutility;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
228 changes: 114 additions & 114 deletions Resources/Base.lproj/Main.storyboard
macblazer marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions Source/Extensions/LoggerExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// LoggerExtensions.swift
// PPPC Utility
//
// Created by Skyler Godfrey on 10/21/24.
// Copyright © 2024 Jamf. All rights reserved.
//

// This extension simplifies the logger instance creation by calling the bundle Id
// and pre-declaring categories. Currently the predefined categories match the
// class name.

import OSLog

extension Logger {
static let subsystem = Bundle.main.bundleIdentifier!
static let TCCProfileViewController = Logger(subsystem: subsystem, category: "TCCProfileViewController")
static let PPPCServicesManager = Logger(subsystem: subsystem, category: "PPPCServicesManager")
static let Model = Logger(subsystem: subsystem, category: "Model")
static let SaveViewController = Logger(subsystem: subsystem, category: "SaveViewController")
static let UploadInfoView = Logger(subsystem: subsystem, category: "UploadInfoView")
static let UploadManager = Logger(subsystem: subsystem, category: "UploadManager")
}
11 changes: 7 additions & 4 deletions Source/Model/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
//

import Cocoa
import OSLog

@objc class Model: NSObject {

Expand All @@ -36,6 +37,8 @@ import Cocoa
@objc dynamic var identities: [SigningIdentity] = []
@objc dynamic var selectedExecutables: [Executable] = []

let logger = Logger.Model

func getAppleEventChoices(executable: Executable) -> [Executable] {
var executables: [Executable] = []

Expand All @@ -44,7 +47,7 @@ import Cocoa
case .success(let executable):
executables.append(executable)
case .failure(let error):
print(error)
self.logger.error("\(error)")
}
}

Expand All @@ -53,7 +56,7 @@ import Cocoa
case .success(let executable):
executables.append(executable)
case .failure(let error):
print(error)
self.logger.error("\(error)")
}
}

Expand All @@ -62,7 +65,7 @@ import Cocoa
case .success(let executable):
executables.append(executable)
case .failure(let error):
print(error)
self.logger.error("\(error)")
}
}

Expand Down Expand Up @@ -290,7 +293,7 @@ extension Model {
case .success(let goodExecutable):
executable = goodExecutable
case .failure(let error):
print(error)
self.logger.error("\(error)")
}
}

Expand Down
6 changes: 5 additions & 1 deletion Source/Model/PPPCServicesManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
//

import Foundation
import OSLog

class PPPCServicesManager {

typealias MDMServiceKey = String

let logger = Logger.PPPCServicesManager

static let shared = PPPCServicesManager()

let allServices: [MDMServiceKey: PPPCServiceInfo]
Expand All @@ -50,7 +54,7 @@ class PPPCServicesManager {
hashed[service.mdmKey] = service
}
} catch {
print("Error loading PPPCServices.json: \(error)")
logger.error("Error loading PPPCServices.json: \(error)")
}

allServices = hashed
Expand Down
18 changes: 10 additions & 8 deletions Source/Networking/UploadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
//

import Foundation
import os.log
import OSLog

struct UploadManager {
let serverURL: String

let logger = Logger.UploadManager

struct VerificationInfo {
let mustSign: Bool
let organization: String
Expand All @@ -22,7 +24,7 @@ struct UploadManager {
}

func verifyConnection(authManager: NetworkAuthManager, completionHandler: @escaping (Result<VerificationInfo, VerificationError>) -> Void) {
os_log("Checking connection to Jamf Pro server", type: .default)
logger.info("Checking connection to Jamf Pro server")

Task {
let networking = JamfProAPIClient(serverUrlString: serverURL, tokenManager: authManager)
Expand All @@ -38,10 +40,10 @@ struct UploadManager {

result = .success(VerificationInfo(mustSign: mustSign, organization: orgName))
} catch is AuthError {
os_log("Invalid credentials.", type: .default)
logger.error("Invalid credentials.")
result = .failure(VerificationError.anyError("Invalid credentials."))
} catch {
os_log("Jamf Pro server is unavailable.", type: .default)
logger.error("Jamf Pro server is unavailable.")
result = .failure(VerificationError.anyError("Jamf Pro server is unavailable."))
}

Expand All @@ -50,14 +52,14 @@ struct UploadManager {
}

func upload(profile: TCCProfile, authMgr: NetworkAuthManager, siteInfo: (String, String)?, signingIdentity: SigningIdentity?, completionHandler: @escaping (Error?) -> Void) {
os_log("Uploading profile: %{public}s", profile.displayName)
logger.info("Uploading profile: \(profile.displayName, privacy: .public)")

let networking = JamfProAPIClient(serverUrlString: serverURL, tokenManager: authMgr)
Task {
let success: Error?
var identity: SecIdentity?
if let signingIdentity = signingIdentity {
os_log("Signing profile with \(signingIdentity.displayName)")
logger.info("Signing profile with \(signingIdentity.displayName)")
identity = signingIdentity.reference
}

Expand All @@ -67,9 +69,9 @@ struct UploadManager {
_ = try await networking.upload(computerConfigProfile: profileData)

success = nil
os_log("Uploaded successfully")
logger.info("Uploaded successfully")
} catch {
os_log("Error creating or uploading profile: %s", type: .error, error.localizedDescription)
logger.error("Error creating or uploading profile: \(error.localizedDescription)")
success = error
}

Expand Down
9 changes: 5 additions & 4 deletions Source/SwiftUI/UploadInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2023 Jamf Software

import os.log
import OSLog
import SwiftUI

struct UploadInfoView: View {
Expand Down Expand Up @@ -38,6 +38,8 @@ struct UploadInfoView: View {
@State private var siteId: Int = -1
@State private var siteName: String = ""

let logger = Logger.UploadInfoView

/// The type of authentication the user wants to use.
///
/// `String` type so it can be saved with `@AppStorage` above
Expand Down Expand Up @@ -171,7 +173,7 @@ struct UploadInfoView: View {

private func warning(_ info: StaticString, shouldDisplay: Bool) {
if shouldDisplay {
os_log(info, type: .default)
logger.info("\(info)")
macblazer marked this conversation as resolved.
Show resolved Hide resolved
warningInfo = "\(info)"
}
}
Expand Down Expand Up @@ -288,8 +290,7 @@ struct UploadInfoView: View {
password: password,
server: serverURL)
} catch {
os_log("Failed to save credentials with error: %s",
type: .error, error.localizedDescription)
logger.error("Failed to save credentials with error: \(error.localizedDescription)")
}
}
// Future on macOS 12+: focus on Payload Name field
Expand Down
13 changes: 8 additions & 5 deletions Source/View Controllers/SaveViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
//

import Cocoa
import OSLog

class SaveViewController: NSViewController {

Expand Down Expand Up @@ -58,6 +59,8 @@ class SaveViewController: NSViewController {
@IBOutlet var identitiesPopUpAC: NSArrayController!
@IBOutlet weak var saveButton: NSButton!

let logger = Logger.SaveViewController

var defaultsController = NSUserDefaultsController.shared

func updateIsReadyToSave() {
Expand Down Expand Up @@ -96,7 +99,7 @@ class SaveViewController: NSViewController {
identities.insert(SigningIdentity(name: "Not signed", reference: nil), at: 0)
identitiesPopUpAC.add(contentsOf: identities)
} catch {
print("Error loading identities: \(error)")
logger.error("Error loading identities: \(error)")
}

loadImportedTCCProfileInfo()
Expand Down Expand Up @@ -125,7 +128,7 @@ class SaveViewController: NSViewController {
}

func saveTo(url: URL) {
print("Saving to \(url)")
logger.info("Saving to \(url, privacy: .public)")
let model = Model.shared
let profile = model.exportProfile(organization: organizationLabel.stringValue,
identifier: payloadIdentifier,
Expand All @@ -134,13 +137,13 @@ class SaveViewController: NSViewController {
do {
var outputData = try profile.xmlData()
if let identity = identitiesPopUpAC.selectedObjects.first as? SigningIdentity, let ref = identity.reference {
print("Signing profile with \(identity.displayName)")
logger.info("Signing profile with \(identity.displayName)")
outputData = try SecurityWrapper.sign(data: outputData, using: ref)
}
try outputData.write(to: url)
print("Saved successfully")
logger.info("Saved successfully")
} catch {
print("Error: \(error)")
logger.error("Error: \(error)")
}
self.dismiss(nil)
}
Expand Down
12 changes: 7 additions & 5 deletions Source/View Controllers/TCCProfileViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//

import Cocoa
import os.log
import OSLog
import SwiftUI

enum TCCProfileDisplayValue: String {
Expand Down Expand Up @@ -145,6 +145,8 @@ class TCCProfileViewController: NSViewController {
}
}

let logger = Logger.TCCProfileViewController

private func toggleAuthorizationKey(theSwitch: NSSwitch, showAlert: Bool) {
switch theSwitch.state {
case .on:
Expand Down Expand Up @@ -182,7 +184,7 @@ class TCCProfileViewController: NSViewController {
identities = try SecurityWrapper.loadSigningIdentities()
} catch {
identities = []
os_log("Error loading identities: %s", type: .error, error.localizedDescription)
logger.error("Error loading identities: \(error.localizedDescription)")
}

let uploadView = UploadInfoView(signingIdentities: identities) {
Expand Down Expand Up @@ -254,7 +256,7 @@ class TCCProfileViewController: NSViewController {
block(executable)
case .failure(let error):
self?.showAlert(error, for: window)
print(error)
self?.logger.error("\(error)")
}
}
}
Expand Down Expand Up @@ -416,7 +418,7 @@ class TCCProfileViewController: NSViewController {
self?.insertIntoAppleEvents(executable)
case .failure(let error):
self?.showAlert(error, for: window)
print(error)
self?.logger.error("\(error)")
}
}
}
Expand Down Expand Up @@ -479,7 +481,7 @@ extension TCCProfileViewController: NSTableViewDataSource {
}
case .failure(let error):
self?.showAlert(error, for: window)
print(error)
self?.logger.error("\(error)")
}
}
}
Expand Down
Loading