2019-09-29 16:35:12 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv 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.
|
|
|
|
*
|
|
|
|
* mpv 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 mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
2024-03-30 19:22:53 +00:00
|
|
|
import os
|
2019-09-29 16:35:12 +00:00
|
|
|
|
2024-03-26 20:50:35 +00:00
|
|
|
class LogHelper {
|
2019-09-29 16:35:12 +00:00
|
|
|
var log: OpaquePointer?
|
2024-03-30 19:22:53 +00:00
|
|
|
let logger = Logger(subsystem: "io.mpv", category: "mpv")
|
2019-09-29 16:35:12 +00:00
|
|
|
|
2024-04-27 20:11:33 +00:00
|
|
|
let loggerMapping: [Int: OSLogType] = [
|
2024-03-30 19:22:53 +00:00
|
|
|
MSGL_V: .debug,
|
|
|
|
MSGL_INFO: .info,
|
|
|
|
MSGL_WARN: .error,
|
2024-04-27 20:11:33 +00:00
|
|
|
MSGL_ERR: .fault
|
2024-03-30 19:22:53 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
init(_ log: OpaquePointer? = nil) {
|
2019-09-29 16:35:12 +00:00
|
|
|
self.log = log
|
|
|
|
}
|
|
|
|
|
2024-03-26 22:45:18 +00:00
|
|
|
func verbose(_ message: String) {
|
|
|
|
send(message: message, type: MSGL_V)
|
2019-09-29 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 22:45:18 +00:00
|
|
|
func info(_ message: String) {
|
|
|
|
send(message: message, type: MSGL_INFO)
|
2019-09-29 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 22:45:18 +00:00
|
|
|
func warning(_ message: String) {
|
|
|
|
send(message: message, type: MSGL_WARN)
|
2019-09-29 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 22:45:18 +00:00
|
|
|
func error(_ message: String) {
|
|
|
|
send(message: message, type: MSGL_ERR)
|
2019-09-29 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 22:45:18 +00:00
|
|
|
func send(message: String, type: Int) {
|
2024-04-10 17:41:38 +00:00
|
|
|
guard let log = log else {
|
2024-03-30 19:22:53 +00:00
|
|
|
logger.log(level: loggerMapping[type] ?? .default, "\(message, privacy: .public)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-26 22:45:18 +00:00
|
|
|
let args: [CVarArg] = [(message as NSString).utf8String ?? "NO MESSAGE"]
|
|
|
|
mp_msg_va(log, Int32(type), "%s\n", getVaList(args))
|
2019-09-29 16:35:12 +00:00
|
|
|
}
|
2024-04-10 17:41:38 +00:00
|
|
|
|
|
|
|
deinit {
|
|
|
|
// only a manual dereferencing will trigger this, cleanup properly in that case
|
|
|
|
ta_free(UnsafeMutablePointer(log))
|
|
|
|
log = nil
|
|
|
|
}
|
2019-09-29 16:35:12 +00:00
|
|
|
}
|