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
17 changes: 16 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ if(MLX_BUILD_CUDA)
CUDNN::cudnn_all # Promoted from PRIVATE, required by mlx
)
endif()

# cLogSupport package small C shim that exposes os_log_type_enabled() to Swift
# (see #491)
set(cLogSupport_include ${CMAKE_CURRENT_LIST_DIR}/Source/cLogSupport/include)
set(cLogSupport_module_dir ${CMAKE_CURRENT_BINARY_DIR}/cLogSupport-module)
file(
WRITE ${cLogSupport_module_dir}/module.modulemap
"module cLogSupport {\n header \"${cLogSupport_include}/cLogSupport.h\"\n export *\n}\n"
)

add_library(cLogSupport STATIC
${CMAKE_CURRENT_LIST_DIR}/Source/cLogSupport/cLogSupport.c)
target_include_directories(cLogSupport PUBLIC ${cLogSupport_include}
${cLogSupport_module_dir})

file(GLOB MLX-src ${CMAKE_CURRENT_LIST_DIR}/Source/MLX/*.swift)

if(NOT MLX_BUILD_METAL)
Expand All @@ -114,7 +129,7 @@ add_library(MLX STATIC ${MLX-src})
target_include_directories(MLX
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/Source/Cmlx/include)
# Note: mlxc is target name exported by mlx-c
target_link_libraries(MLX PUBLIC mlxc Numerics)
target_link_libraries(MLX PUBLIC mlxc Numerics cLogSupport)
target_compile_options(MLX PRIVATE -package-name MLX)

# MLXRandom package
Expand Down
7 changes: 7 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,17 @@ let package = Package(
dependencies: ["Cmlx"]
),

.target(
name: "cLogSupport",
path: "Source/cLogSupport",
publicHeadersPath: "include"
),

.target(
name: "MLX",
dependencies: [
"Cmlx",
"cLogSupport",
.product(name: "Numerics", package: "swift-numerics"),
],
exclude: mlxSwiftExcludes,
Expand Down
7 changes: 6 additions & 1 deletion Source/MLX/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,23 @@ extension MLXLogger {

#if canImport(OSLog)
import OSLog
import cLogSupport

/// On systems that support it, a log handler that is backed by OSLog.
public struct OSLogHandler: MLXLogHandler {

private let logger: os.Logger

// see #491
private let osLog: OSLog

public static func install() {
MLXLogger.factory = { OSLogHandler(label: $0) }
}

public init(label: String) {
self.logger = Logger(subsystem: "mlx-swift", category: label)
self.osLog = OSLog(subsystem: "mlx-swift", category: label)
}

public func log(
Expand All @@ -250,7 +255,7 @@ extension MLXLogger {
case .warning: .error
case .error: .error
}
if logger.isEnabled(type: level) {
if is_os_log_enabled(osLog, level) {
let message = message()
logger.log(level: level, "\(message, privacy: .public)")
}
Expand Down
1 change: 1 addition & 0 deletions Source/cLogSupport/cLogSupport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void cLogSupport(void) { }
9 changes: 9 additions & 0 deletions Source/cLogSupport/include/cLogSupport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#if defined(__APPLE__)
#import <os/log.h>
#import <stdbool.h>

static inline bool is_os_log_enabled(os_log_t log, os_log_type_t type) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OSLog.Logger.isEnabled(type:) was added in 26.4 but availability claims macOS 11. At the same time os_log_type_enabled was made unavailable from swift. We have to workaround this in C to make something callable.

return os_log_type_enabled(log, type);
}
#endif

2 changes: 1 addition & 1 deletion tools/create-xcframework.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Frameworks to package
FRAMEWORKS=("MLX" "Cmlx" "MLXNN" "MLXOptimizers")
FRAMEWORKS=("MLX" "Cmlx" "cLogSupport" "MLXNN" "MLXOptimizers")

# Destinations to build for (ordered array)
PLATFORM_NAMES=("iOS" "macOS" "Mac Catalyst" "tvOS" "visionOS")
Expand Down
Loading
Loading