Skip to content

Commit

Permalink
Merge pull request #1 from SkylerGodfrey/Fix-Print
Browse files Browse the repository at this point in the history
Update to OSLog
  • Loading branch information
SkylerGodfrey authored Oct 21, 2024
2 parents 408f84f + 956627c commit 9c798a9
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 143 deletions.
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

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions Source/Extensions/LoggerExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// 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 Main = Logger(subsystem: subsystem, category: "Main")
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 @@ -35,6 +36,8 @@ import Cocoa
@objc dynamic static let shared = Model()
@objc dynamic var identities: [SigningIdentity] = []
@objc dynamic var selectedExecutables: [Executable] = []

Check failure on line 39 in Source/Model/Model.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
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,9 +26,13 @@
//

import Foundation
import OSLog

class PPPCServicesManager {

Check failure on line 32 in Source/Model/PPPCServicesManager.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
typealias MDMServiceKey = String

Check failure on line 34 in Source/Model/PPPCServicesManager.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
let logger = Logger.PPPCServicesManager

static let shared = PPPCServicesManager()

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,10 +7,12 @@
//

import Foundation
import os.log
import OSLog

struct UploadManager {
let serverURL: String

Check failure on line 14 in Source/Networking/UploadManager.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
let logger = Logger.UploadManager

struct VerificationInfo {
let mustSign: Bool
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)")

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 @@ -37,6 +37,8 @@ struct UploadInfoView: View {
@State private var useSite: Bool = false
@State private var siteId: Int = -1
@State private var siteName: String = ""

Check failure on line 40 in Source/SwiftUI/UploadInfoView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
let logger = Logger.UploadInfoView

/// The type of authentication the user wants to use.
///
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)")
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 @@ -57,6 +58,8 @@ class SaveViewController: NSViewController {
@IBOutlet weak var identitiesPopUp: NSPopUpButton!
@IBOutlet var identitiesPopUpAC: NSArrayController!
@IBOutlet weak var saveButton: NSButton!

Check failure on line 61 in Source/View Controllers/SaveViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
let logger = Logger.SaveViewController

var defaultsController = NSUserDefaultsController.shared

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)")
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 @@ -144,6 +144,8 @@ class TCCProfileViewController: NSViewController {
self.insertIntoAppleEvents($0)
}
}

Check failure on line 147 in Source/View Controllers/TCCProfileViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
let logger = Logger.TCCProfileViewController

private func toggleAuthorizationKey(theSwitch: NSSwitch, showAlert: Bool) {
switch theSwitch.state {
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

0 comments on commit 9c798a9

Please sign in to comment.