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.
This commit is contained in:
der richter 2024-03-30 20:22:53 +01:00
parent 7619cceb87
commit fc978eb9f2
2 changed files with 18 additions and 1 deletions

View File

@ -22,6 +22,7 @@ class AppHub: NSObject {
var mpv: OpaquePointer? var mpv: OpaquePointer?
var input: InputHelper var input: InputHelper
var log: LogHelper
var option: OptionHelper? var option: OptionHelper?
var event: EventHelper? var event: EventHelper?
var menu: MenuBar? var menu: MenuBar?
@ -39,6 +40,7 @@ class AppHub: NSObject {
private override init() { private override init() {
input = InputHelper() input = InputHelper()
log = LogHelper()
super.init() super.init()
if isApplication { menu = MenuBar(self) } if isApplication { menu = MenuBar(self) }
#if HAVE_MACOS_MEDIA_PLAYER #if HAVE_MACOS_MEDIA_PLAYER
@ -47,6 +49,7 @@ class AppHub: NSObject {
} }
@objc func initMpv(_ mpv: OpaquePointer) { @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)) option = OptionHelper(UnsafeMutablePointer(mpv), mp_client_get_global(mpv))
input.option = option input.option = option
event = EventHelper(self, mpv) event = EventHelper(self, mpv)

View File

@ -16,11 +16,20 @@
*/ */
import Cocoa import Cocoa
import os
class LogHelper { class LogHelper {
var log: OpaquePointer? 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 self.log = log
} }
@ -41,6 +50,11 @@ class LogHelper {
} }
func send(message: String, type: Int) { 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"] let args: [CVarArg] = [(message as NSString).utf8String ?? "NO MESSAGE"]
mp_msg_va(log, Int32(type), "%s\n", getVaList(args)) mp_msg_va(log, Int32(type), "%s\n", getVaList(args))
} }