fmod/core/
debug.rs

1// Copyright (c) 2024 Lily Lyons
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use fmod_sys::*;
8use lanyard::Utf8CString;
9
10use std::ffi::{c_char, c_int};
11
12#[derive(PartialEq, Eq, Debug)]
13pub enum DebugMode {
14    TTY,
15    File(Utf8CString),
16    Callback(
17        unsafe extern "C" fn(
18            FMOD_DEBUG_FLAGS,
19            *const c_char,
20            c_int,
21            *const c_char,
22            *const c_char,
23        ) -> FMOD_RESULT,
24    ),
25}
26
27bitflags::bitflags! {
28    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
29    pub struct DebugFlags: FMOD_DEBUG_FLAGS {
30        const NONE = FMOD_DEBUG_LEVEL_NONE;
31        const ERROR = FMOD_DEBUG_LEVEL_ERROR;
32        const WARNING = FMOD_DEBUG_LEVEL_WARNING;
33        const LOG = FMOD_DEBUG_LEVEL_LOG;
34        const MEMORY = FMOD_DEBUG_TYPE_MEMORY;
35        const FILE = FMOD_DEBUG_TYPE_FILE;
36        const CODEC = FMOD_DEBUG_TYPE_CODEC;
37        const TRACE = FMOD_DEBUG_TYPE_TRACE;
38        const DISPLAY_TIMESTAMPS = FMOD_DEBUG_DISPLAY_TIMESTAMPS;
39        const DISPLAY_LINENUMBERS = FMOD_DEBUG_DISPLAY_LINENUMBERS;
40        const DISPLAY_THREAD = FMOD_DEBUG_DISPLAY_THREAD;
41    }
42}
43
44impl From<FMOD_DEBUG_FLAGS> for DebugFlags {
45    fn from(value: FMOD_DEBUG_FLAGS) -> Self {
46        DebugFlags::from_bits_truncate(value)
47    }
48}
49
50impl From<DebugFlags> for FMOD_DEBUG_FLAGS {
51    fn from(value: DebugFlags) -> Self {
52        value.bits()
53    }
54}
55
56/// Specify the level and delivery method of log messages when using the logging version of FMOD.
57///
58/// This function will return [`FMOD_RESULT::FMOD_ERR_UNSUPPORTED`] when using the non-logging (release) versions of FMOD.
59/// The logging version of FMOD can be recognized by the 'L' suffix in the library name, fmodL.dll or libfmodL.so for instance.
60///
61/// By default this crate links against non-logging versions of FMOD in release builds.
62/// This behaviour can be changed with the "fmod-sys/force-debug" feature.
63///
64/// Note that:
65///     [`DebugFlags::LOG`] produces informational, warning and error messages.
66///     [`DebugFlags::WARNING`] produces warnings and error messages.
67///     [`DebugFlags::ERROR`] produces error messages only.
68pub fn initialize(flags: DebugFlags, mode: DebugMode) -> Result<()> {
69    match mode {
70        DebugMode::TTY => unsafe {
71            FMOD_Debug_Initialize(flags.into(), FMOD_DEBUG_MODE_TTY, None, std::ptr::null())
72                .to_result()
73        },
74        DebugMode::File(file) => unsafe {
75            FMOD_Debug_Initialize(flags.into(), FMOD_DEBUG_MODE_FILE, None, file.as_ptr())
76                .to_result()
77        },
78        DebugMode::Callback(c) => unsafe {
79            FMOD_Debug_Initialize(
80                flags.into(),
81                FMOD_DEBUG_MODE_CALLBACK,
82                Some(c),
83                std::ptr::null(),
84            )
85            .to_result()
86        },
87    }
88}