日志记录#
Arcade有几个选项可以记录有关时间安排和内部工作方式的其他信息。要做到这一点,有两种主要方法,一种是打开日志,另一种是查询OpenGL上下文。
启用日志记录#
打开日志记录的最快方法是将以下代码添加到主程序文件的开头:
arcade.configure_logging()
这将导致Arcade库输出一些基本调试信息:
2409.0003967285156 arcade.sprite_list DEBUG - [386411600] Creating SpriteList use_spatial_hash=True is_static=False
2413.9978885650635 arcade.gl.context INFO - Arcade version : 2.4a5
2413.9978885650635 arcade.gl.context INFO - OpenGL version : 3.3
2413.9978885650635 arcade.gl.context INFO - Vendor : NVIDIA Corporation
2413.9978885650635 arcade.gl.context INFO - Renderer : GeForce GTX 980 Ti/PCIe/SSE2
2413.9978885650635 arcade.gl.context INFO - Python : 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)]
2413.9978885650635 arcade.gl.context INFO - Platform : win32
3193.9964294433594 arcade.sprite_list DEBUG - [386411600] _calculate_sprite_buffer: 0.013532099999999936 sec
自定义日志配置#
如果您想要添加自己的日志记录,或者更改日志中打印的信息,只需多编写一点代码即可。
首先,在您的程序中导入 logging library :
import logging
打开日志记录的代码如下所示:
logging.basicConfig(level=logging.DEBUG)
通过使用格式化程序将时间、文件名甚至行号信息添加到输出中,您可以获得更多信息:
format = '%(asctime)s,%(msecs)03d %(levelname)-8s [%(filename)s:%(lineno)d %(funcName)s()] %(message)s'
logging.basicConfig(format=format,
datefmt='%H:%M:%S',
level=logging.DEBUG)
...这会将输出更改为如下所示:
13:40:50,226 DEBUG [sprite_list.py:720 _calculate_sprite_buffer()] [365177904] _calculate_sprite_buffer: 0.00849660000000041 sec
13:40:50,398 DEBUG [ui_element.py:58 on_mouse_over()] UIElement mouse over
通过将以下行之一放在程序的顶部,可以将日志记录添加到您自己的程序中:
# Get your own logger
LOG = logging.getLogger(__name__)
# or get Arcade's logger
LOG = logging.getLogger('arcade')
然后,任何时候您想要打印,只需使用:
LOG.debug("This is my debug statement.")
使用查询对象获取OpenGL统计信息#
如果您想了解绘制所需时间的更多信息,可以查询OpenGL上下文 arcade.Window.ctx
如本例所示:
def on_draw(self):
""" Render the screen. """
self.clear()
query = self.ctx.query()
with query:
# Put the drawing commands you want to get info on here:
self.my_sprite_list.draw()
print()
print(f"Time elapsed : {query.time_elapsed:,} ns")
print(f"Samples passed : {query.samples_passed:,}")
print(f"Primitives created : {query.primitives_generated:,}")
其输出如下所示:
Time elapsed : 7,136 ns
Samples passed : 390,142
Primitives created : 232