Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
*
*/

import CommonsLib
import UIKit
import SwiftUI
import OSLog
import UniformTypeIdentifiers
import FactoryKit

class ShareViewController: UIViewController, Sendable {

private static let logger = Logger(subsystem: "ee.ria.digidoc.RIADigiDoc", category: "ShareViewController")

class ShareViewController: UIViewController, Sendable, Loggable {
let viewModel = Container.shared.shareViewModel()

override func viewDidLoad() {
Expand Down Expand Up @@ -127,7 +124,7 @@ class ShareViewController: UIViewController, Sendable {
))
}
} catch let error {
ShareViewController.logger.error("Unable to load item: \(error.localizedDescription)")
ShareViewController.logger().error("Unable to load item: \(error.localizedDescription)")
}
}
}
Expand Down
24 changes: 9 additions & 15 deletions Extensions/FileImportShareExtension/ViewModel/ShareViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

import Foundation
import OSLog
import UniformTypeIdentifiers
import FactoryKit
import CommonsLib
Expand All @@ -27,12 +26,7 @@ import Alamofire

@Observable
@MainActor
class ShareViewModel: ShareViewModelProtocol {
private static let logger = Logger(
subsystem: "ee.ria.digidoc.FileImportShareExtension",
category: "ShareViewController"
)

class ShareViewModel: ShareViewModelProtocol, Loggable {
private let fileManager: FileManagerProtocol
private let resourceChecker: URLResourceCheckerProtocol

Expand All @@ -48,7 +42,7 @@ class ShareViewModel: ShareViewModelProtocol {

@discardableResult
func importFiles(_ items: [ImportedFileItem]) async -> Bool {
ShareViewModel.logger.debug("Importing files...")
ShareViewModel.logger().debug("Importing files...")
guard !items.isEmpty else {
await MainActor.run { [weak self] in
self?.status = .failed
Expand All @@ -63,9 +57,9 @@ class ShareViewModel: ShareViewModelProtocol {
)

if isImported {
ShareViewModel.logger.debug("Files imported successfully")
ShareViewModel.logger().debug("Files imported successfully")
} else {
ShareViewModel.logger.error("Could not import files")
ShareViewModel.logger().error("Could not import files")
}

await MainActor.run { [weak self] in
Expand All @@ -74,7 +68,7 @@ class ShareViewModel: ShareViewModelProtocol {

return isImported
} catch {
ShareViewModel.logger.error("Unable to import files: \(error.localizedDescription)")
ShareViewModel.logger().error("Unable to import files: \(error.localizedDescription)")
await MainActor.run { [weak self] in
self?.status = .failed
}
Expand Down Expand Up @@ -174,7 +168,7 @@ class ShareViewModel: ShareViewModelProtocol {

return true
} catch {
ShareViewModel.logger.error("Unable to cache file: \(error.localizedDescription)")
ShareViewModel.logger().error("Unable to cache file: \(error.localizedDescription)")
}
} else if itemUrl.isValidURL() {
return await downloadFileFromUrl(itemUrl)
Expand All @@ -184,7 +178,7 @@ class ShareViewModel: ShareViewModelProtocol {
}

func downloadFileFromUrl(_ itemUrl: URL) async -> Bool {
ShareViewModel.logger.debug("Downloading file from \(itemUrl.absoluteString)")
ShareViewModel.logger().debug("Downloading file from \(itemUrl.absoluteString)")

do {
let destinationURL = try Directories.getTempDirectory(
Expand All @@ -204,7 +198,7 @@ class ShareViewModel: ShareViewModelProtocol {
for await progress in request.downloadProgress() {
let fileName = itemUrl.lastPathComponent
let downloadProgress = progress.fractionCompleted * 100
ShareViewModel.logger.debug(
ShareViewModel.logger().debug(
"\(String(format: "Download progress for file '%@': %.2f%%", fileName, downloadProgress))"
)
}
Expand All @@ -216,7 +210,7 @@ class ShareViewModel: ShareViewModelProtocol {
return await cacheFileOnUrl(fileURL)
} catch let error {
let errorDescription = error.localizedDescription
ShareViewModel.logger.error(
ShareViewModel.logger().error(
"\(String(format: "Unable to download file %@: %@", itemUrl.absoluteString, errorDescription))"
)
return false
Expand Down
18 changes: 4 additions & 14 deletions Modules/CommonsLib/Sources/CommonsLib/Bundle/BundleUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,18 @@
*/

import Foundation
import OSLog

public struct BundleUtil {

private static let logger = Logger(subsystem: "ee.ria.digidoc.CommonsLib", category: "BundleUtil")

public struct BundleUtil: Loggable {
public static func getBundleIdentifier() -> String {
let bundleIdentifier = Bundle.main.bundleIdentifier ?? "ee.ria.digidoc"
logger.debug("Using bundle identifier: \(bundleIdentifier)")
return bundleIdentifier
return Bundle.main.bundleIdentifier ?? "ee.ria.digidoc"
}

public static func getBundleShortVersionString(bundle: Bundle = Bundle.main) -> String {
let versionString = bundle.infoDictionary?["CFBundleShortVersionString"] as? String ?? "3.0.0"
logger.debug("Bundle short version string from info.plist: \(versionString)")
return versionString
return bundle.infoDictionary?["CFBundleShortVersionString"] as? String ?? "3.0.0"
}

public static func getBundleVersion(bundle: Bundle = Bundle.main) -> String {
let appVersion = bundle.infoDictionary?["CFBundleVersion"] as? String ?? "1"
logger.debug("Bundle version from info.plist: \(appVersion)")
return appVersion
return bundle.infoDictionary?["CFBundleVersion"] as? String ?? "1"
}

public static func getAppVersion(bundle: Bundle = Bundle.main) -> String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ public extension Container {
var urlResourceChecker: Factory<URLResourceCheckerProtocol> {
self { URLResourceChecker() }
}

var isLoggingEnabled: Factory<Bool> {
// Default value, will be overridden on app initialization
self { false }
.singleton
}
}
46 changes: 46 additions & 0 deletions Modules/CommonsLib/Sources/CommonsLib/Logging/Loggable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2017 - 2025 Riigi Infosüsteemi Amet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

import Foundation
import OSLog

public protocol Loggable: Sendable {}

public extension Loggable {
static func logger(file: String = #fileID) -> Logger {
guard LoggingSystem.configuration.isLoggingEnabled else {
return Logger(.disabled)
}

let filePath = String(file)
let moduleName = URL(fileURLWithPath: filePath)
.lastPathComponent
.split(separator: ".")
.first
.map(String.init) ?? "UnknownModule"

let subsystem = "\(BundleUtil.getBundleIdentifier()).\(moduleName)"

return Logger(subsystem: subsystem, category: category)
}

static var category: String {
String(describing: Self.self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017 - 2025 Riigi Infosüsteemi Amet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

import Foundation

public struct LoggingConfiguration: Sendable {
public let isLoggingEnabled: Bool

public init(isLoggingEnabled: Bool) {
self.isLoggingEnabled = isLoggingEnabled
}
}
26 changes: 26 additions & 0 deletions Modules/CommonsLib/Sources/CommonsLib/Logging/LoggingSystem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2017 - 2025 Riigi Infosüsteemi Amet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

import FactoryKit

public struct LoggingSystem: Sendable {
public static let configuration: LoggingConfiguration = LoggingConfiguration(
isLoggingEnabled: Container.shared.isLoggingEnabled()
)
}
10 changes: 3 additions & 7 deletions Modules/CommonsLib/Sources/CommonsLib/System/SystemUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
*/

import Foundation
import OSLog

public struct SystemUtil {

private static let logger = Logger(subsystem: "ee.ria.digidoc.CommonsLib", category: "SystemUtil")

public struct SystemUtil: Loggable {
public static var isSimulator: Bool {
#if targetEnvironment(simulator)
logger.debug("App is running on a simulator")
logger().debug("App is running on a simulator")
return true
#else
return false
Expand All @@ -36,7 +32,7 @@ public struct SystemUtil {
public static func getOSVersion() -> String {
let osVersion = ProcessInfo.processInfo.operatingSystemVersion
let versionString = "\(osVersion.majorVersion).\(osVersion.minorVersion).\(osVersion.patchVersion)"
logger.debug("Operating system version: \(versionString)")
logger().debug("Operating system version: \(versionString)")
return versionString
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
*/

import Foundation
import OSLog
import FactoryKit
import CommonsLib
import UtilsLib

actor ConfigurationCache: ConfigurationCacheProtocol {

private static let logger = Logger(subsystem: "ee.ria.digidoc.ConfigLib", category: "ConfigurationCache")
actor ConfigurationCache: ConfigurationCacheProtocol, Loggable {

private let fileManager: FileManagerProtocol

Expand Down Expand Up @@ -100,7 +97,7 @@ actor ConfigurationCache: ConfigurationCacheProtocol {

try data.write(to: configFile)
} catch {
ConfigurationCache.logger.error("\(error.localizedDescription)")
ConfigurationCache.logger().error("\(error.localizedDescription)")
throw ConfigurationCacheError.unableToCacheFile(fileName)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
*/

import Foundation
import OSLog
import UtilsLib
import CommonsLib

@MainActor
class ConfigurationViewModel {
private static let logger = Logger(subsystem: "ee.ria.digidoc.RIADigiDoc", category: "ConfigurationViewModel")
class ConfigurationViewModel: Loggable {

private(set) var configuration: ConfigurationProvider?

Expand All @@ -45,7 +43,7 @@ class ConfigurationViewModel {
cacheDir: Directories.getConfigDirectory(fileManager: fileManager),
proxyInfo: proxyInfo
) else {
ConfigurationViewModel.logger.error("No configuration updates available.")
ConfigurationViewModel.logger().error("No configuration updates available.")
return
}

Expand All @@ -58,14 +56,14 @@ class ConfigurationViewModel {
}
}
} catch {
ConfigurationViewModel.logger.error("Unable to fetch configuration: \(error.localizedDescription)")
ConfigurationViewModel.logger().error("Unable to fetch configuration: \(error.localizedDescription)")
}
}

func getConfiguration() async -> ConfigurationProvider? {
do {
guard let updates = await repository.getConfigurationUpdates() else {
ConfigurationViewModel.logger.error("Configuration updates provider is nil")
ConfigurationViewModel.logger().error("Configuration updates provider is nil")
return nil
}

Expand All @@ -75,7 +73,7 @@ class ConfigurationViewModel {
}
}
} catch {
ConfigurationViewModel.logger.error("Unable to get configuration: \(error.localizedDescription)")
ConfigurationViewModel.logger().error("Unable to get configuration: \(error.localizedDescription)")
return nil
}
return nil
Expand Down
Loading