From fc978eb9f20b61d10718d82e4690de26ef2f9303 Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 30 Mar 2024 20:22:53 +0100 Subject: [PATCH] mac/log: fallback to system logger if no mp_log is yet available since cocoa is initialising mpv and does several things before the mpv core does anything and the mpv_handle was passed to the App, this can be used to log such things before the mpv logging is available. helpful for debugging especially bundle related things. the logger mapping looks a bit unintuitive but error is basically yellow what our warning is and fatal is red what our error is. --- osdep/mac/app_hub.swift | 3 +++ osdep/mac/log_helper.swift | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/osdep/mac/app_hub.swift b/osdep/mac/app_hub.swift index 0172d3904f..cbb4e2b6a5 100644 --- a/osdep/mac/app_hub.swift +++ b/osdep/mac/app_hub.swift @@ -22,6 +22,7 @@ class AppHub: NSObject { var mpv: OpaquePointer? var input: InputHelper + var log: LogHelper var option: OptionHelper? var event: EventHelper? var menu: MenuBar? @@ -39,6 +40,7 @@ class AppHub: NSObject { private override init() { input = InputHelper() + log = LogHelper() super.init() if isApplication { menu = MenuBar(self) } #if HAVE_MACOS_MEDIA_PLAYER @@ -47,6 +49,7 @@ class AppHub: NSObject { } @objc func initMpv(_ mpv: OpaquePointer) { + log.log = mp_log_new(UnsafeMutablePointer(mpv), mp_client_get_log(mpv), "app") option = OptionHelper(UnsafeMutablePointer(mpv), mp_client_get_global(mpv)) input.option = option event = EventHelper(self, mpv) diff --git a/osdep/mac/log_helper.swift b/osdep/mac/log_helper.swift index 98bc19c221..0f65975da1 100644 --- a/osdep/mac/log_helper.swift +++ b/osdep/mac/log_helper.swift @@ -16,11 +16,20 @@ */ import Cocoa +import os class LogHelper { var log: OpaquePointer? + let logger = Logger(subsystem: "io.mpv", category: "mpv") - init(_ log: OpaquePointer?) { + let loggerMapping: [Int:OSLogType] = [ + MSGL_V: .debug, + MSGL_INFO: .info, + MSGL_WARN: .error, + MSGL_ERR: .fault, + ] + + init(_ log: OpaquePointer? = nil) { self.log = log } @@ -41,6 +50,11 @@ class LogHelper { } func send(message: String, type: Int) { + guard let log = log, UnsafeRawPointer(log).load(as: UInt8.self) != 0 else { + logger.log(level: loggerMapping[type] ?? .default, "\(message, privacy: .public)") + return + } + let args: [CVarArg] = [(message as NSString).utf8String ?? "NO MESSAGE"] mp_msg_va(log, Int32(type), "%s\n", getVaList(args)) }