Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added log_level as kwarg to PyBoy class #323

Merged
merged 1 commit into from
May 19, 2024
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
32 changes: 19 additions & 13 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h2 class="section-title" id="header-classes">Classes</h2>
<dl>
<dt id="pyboy.PyBoy"><code class="flex name class">
<span>class <span class="ident">PyBoy</span></span>
<span>(</span><span>gamerom, *, window='SDL2', scale=3, symbols=None, bootrom=None, sound=False, sound_emulated=False, cgb=None, **kwargs)</span>
<span>(</span><span>gamerom, *, window='SDL2', scale=3, symbols=None, bootrom=None, sound=False, sound_emulated=False, cgb=None, log_level='ERROR', **kwargs)</span>
</code></dt>
<dd>
<section class="desc"><p>PyBoy is loadable as an object in Python. This means, it can be initialized from another script, and be
Expand Down Expand Up @@ -100,6 +100,7 @@ <h2 id="kwargs">Kwargs</h2>
<li>sound (bool): Enable sound emulation and output.</li>
<li>sound_emulated (bool): Enable sound emulation without any output. Used for compatibility.</li>
<li>cgb (bool): Forcing Game Boy Color mode.</li>
<li>log_level (str): "CRITICAL", "ERROR", "WARNING", "INFO" or "DEBUG"</li>
<li>color_palette (tuple): Specify the color palette to use for rendering.</li>
<li>cgb_color_palette (list of tuple): Specify the color palette to use for rendering in CGB-mode for non-color games.</li>
</ul>
Expand All @@ -120,6 +121,7 @@ <h2 id="kwargs">Kwargs</h2>
sound=False,
sound_emulated=False,
cgb=None,
log_level=defaults[&#34;log_level&#34;],
**kwargs
):
&#34;&#34;&#34;
Expand Down Expand Up @@ -154,6 +156,7 @@ <h2 id="kwargs">Kwargs</h2>
* sound (bool): Enable sound emulation and output.
* sound_emulated (bool): Enable sound emulation without any output. Used for compatibility.
* cgb (bool): Forcing Game Boy Color mode.
* log_level (str): &#34;CRITICAL&#34;, &#34;ERROR&#34;, &#34;WARNING&#34;, &#34;INFO&#34; or &#34;DEBUG&#34;
* color_palette (tuple): Specify the color palette to use for rendering.
* cgb_color_palette (list of tuple): Specify the color palette to use for rendering in CGB-mode for non-color games.

Expand Down Expand Up @@ -185,7 +188,7 @@ <h2 id="kwargs">Kwargs</h2>
if k not in kwargs:
kwargs[k] = kwargs.get(k, defaults[k])

log_level(kwargs.pop(&#34;log_level&#34;))
_log_level(log_level)

if not os.path.isfile(gamerom):
raise FileNotFoundError(f&#34;ROM file {gamerom} was not found!&#34;)
Expand Down Expand Up @@ -419,9 +422,10 @@ <h2 id="kwargs">Kwargs</h2>
self.mb.breakpoint_reinject()

# NOTE: PC has not been incremented when hitting breakpoint!
breakpoint_index = self.mb.breakpoint_reached()
if breakpoint_index != -1:
self.mb.breakpoint_remove(breakpoint_index)
breakpoint_meta = self.mb.breakpoint_reached()
if breakpoint_meta != (-1, -1, -1):
bank, addr, _ = breakpoint_meta
self.mb.breakpoint_remove(bank, addr)
self.mb.breakpoint_singlestep_latch = 0

if not self._handle_hooks():
Expand Down Expand Up @@ -1164,6 +1168,7 @@ <h2 id="kwargs">Kwargs</h2>
raise ValueError(&#34;Hook already registered for this bank and address.&#34;)
self.mb.breakpoint_add(bank, addr)
bank_addr_opcode = (bank &amp; 0xFF) &lt;&lt; 24 | (addr &amp; 0xFFFF) &lt;&lt; 8 | (opcode &amp; 0xFF)
logger.debug(&#34;Adding hook for opcode %08x&#34;, bank_addr_opcode)
self._hooks[bank_addr_opcode] = (callback, context)

def hook_deregister(self, bank, addr):
Expand Down Expand Up @@ -1196,12 +1201,12 @@ <h2 id="kwargs">Kwargs</h2>
if bank is None and isinstance(addr, str):
bank, addr = self._lookup_symbol(addr)

index = self.mb.breakpoint_find(bank, addr)
if index == -1:
breakpoint_meta = self.mb.breakpoint_find(bank, addr)
if not breakpoint_meta:
raise ValueError(&#34;Breakpoint not found for bank and addr&#34;)
_, _, opcode = breakpoint_meta

_, _, opcode = self.mb.breakpoints_list[index]
self.mb.breakpoint_remove(index)
self.mb.breakpoint_remove(bank, addr)
bank_addr_opcode = (bank &amp; 0xFF) &lt;&lt; 24 | (addr &amp; 0xFFFF) &lt;&lt; 8 | (opcode &amp; 0xFF)
self._hooks.pop(bank_addr_opcode)

Expand Down Expand Up @@ -2466,6 +2471,7 @@ <h2 id="args">Args</h2>
raise ValueError(&#34;Hook already registered for this bank and address.&#34;)
self.mb.breakpoint_add(bank, addr)
bank_addr_opcode = (bank &amp; 0xFF) &lt;&lt; 24 | (addr &amp; 0xFFFF) &lt;&lt; 8 | (opcode &amp; 0xFF)
logger.debug(&#34;Adding hook for opcode %08x&#34;, bank_addr_opcode)
self._hooks[bank_addr_opcode] = (callback, context)</code></pre>
</details>
</dd>
Expand Down Expand Up @@ -2529,12 +2535,12 @@ <h2 id="args">Args</h2>
if bank is None and isinstance(addr, str):
bank, addr = self._lookup_symbol(addr)

index = self.mb.breakpoint_find(bank, addr)
if index == -1:
breakpoint_meta = self.mb.breakpoint_find(bank, addr)
if not breakpoint_meta:
raise ValueError(&#34;Breakpoint not found for bank and addr&#34;)
_, _, opcode = breakpoint_meta

_, _, opcode = self.mb.breakpoints_list[index]
self.mb.breakpoint_remove(index)
self.mb.breakpoint_remove(bank, addr)
bank_addr_opcode = (bank &amp; 0xFF) &lt;&lt; 24 | (addr &amp; 0xFFFF) &lt;&lt; 8 | (opcode &amp; 0xFF)
self._hooks.pop(bank_addr_opcode)</code></pre>
</details>
Expand Down
2 changes: 1 addition & 1 deletion pyboy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def valid_file_path(path):
"--log-level",
default=defaults["log_level"],
type=str,
choices=["ERROR", "WARNING", "INFO", "DEBUG", "DISABLE"],
choices=["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"],
help="Set logging level"
)
parser.add_argument(
Expand Down
2 changes: 2 additions & 0 deletions pyboy/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def log_level(level):
level = ERROR
elif level == "CRITICAL":
level = CRITICAL
elif level == "DISABLE":
level = CRITICAL
elif isinstance(level, int):
pass
else:
Expand Down
7 changes: 5 additions & 2 deletions pyboy/pyboy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from pyboy.api.memory_scanner import MemoryScanner
from pyboy.api.screen import Screen
from pyboy.api.tilemap import TileMap
from pyboy.logging import get_logger, log_level
from pyboy.logging import get_logger
from pyboy.logging import log_level as _log_level
from pyboy.plugins.manager import PluginManager, parser_arguments
from pyboy.utils import IntIOWrapper, WindowEvent

Expand Down Expand Up @@ -49,6 +50,7 @@ def __init__(
sound=False,
sound_emulated=False,
cgb=None,
log_level=defaults["log_level"],
**kwargs
):
"""
Expand Down Expand Up @@ -83,6 +85,7 @@ def __init__(
* sound (bool): Enable sound emulation and output.
* sound_emulated (bool): Enable sound emulation without any output. Used for compatibility.
* cgb (bool): Forcing Game Boy Color mode.
* log_level (str): "CRITICAL", "ERROR", "WARNING", "INFO" or "DEBUG"
* color_palette (tuple): Specify the color palette to use for rendering.
* cgb_color_palette (list of tuple): Specify the color palette to use for rendering in CGB-mode for non-color games.

Expand Down Expand Up @@ -114,7 +117,7 @@ def __init__(
if k not in kwargs:
kwargs[k] = kwargs.get(k, defaults[k])

log_level(kwargs.pop("log_level"))
_log_level(log_level)

if not os.path.isfile(gamerom):
raise FileNotFoundError(f"ROM file {gamerom} was not found!")
Expand Down
Loading