2011/8/27 chris clark <clac...@gmail.com>:
> I know pyusb (in svn) is currently not really supported/ready but I have a
> usability question/observation.
>
Although not ready, many people are using it without problems. And I
am pleasure to support any question in the mailing list.

> If I have a (dumb) example script that uses the "basic" logging settings and
> pyusb fails to load a backend the OpenUSB load attempt ends up being logged
> when it too fails, a traceback is also dumped (the script keeps on going).
>
> Example script and output
>
> ## -------------------- cut here --
> """List usb devices using basic logging.
> This shows conflicts with pyusb logging implementation.
>
> """
>
>
> import logging
>
> do_logging = True  # And demo problem
> #do_logging = False  # Pick one :)
>
> log = logging
>
> if do_logging:
>     log.info('hello from test app, no more logging wil be issued form this
> script')
>
> import usb.core
> import usb.util
>
> import usb.backend.libusb10
>
>
> # Monkey patch libusb10 away, or simply run on a machine without libusb10
> def fake_get():
>     return None
>
> usb.backend.libusb10.get_backend = fake_get
>
> for dev in usb.core.find(find_all=True):
>     print dev
> ## -------------------- cut here --
>
> ## -------------------- cut here --
> ERROR:usb.backend.openusb:Error loading OpenUSB backend
> Traceback (most recent call last):
>   File "/home/clach04/dev/python/usb/pyusb/trunk/usb/backend/openusb.py",
> line 598, in get_backend
>     _lib = _load_library()
>   File "/home/clach04/dev/python/usb/pyusb/trunk/usb/backend/openusb.py",
> line 148, in _load_library
>     raise OSError('USB library could not be found')
> OSError: USB library could not be found
> <usb.core.Device object at 0x9d4f84c>
> <usb.core.Device object at 0x9d4f8cc>
> <usb.core.Device object at 0x9d4f92c>
> <usb.core.Device object at 0x9d4f96c>
> <usb.core.Device object at 0x9d4f9ac>
> <usb.core.Device object at 0x9d4f9ec>
> <usb.core.Device object at 0x9d4fa2c>
> <usb.core.Device object at 0x9d4fa6c>
> ## -------------------- cut here --
>
> is there a way to disable this behavior? I'm guessing this is some debug
> code that is causing this. There are 2 issues:
>
> 1) the back trace that gets dumped out
> 2) the error gets logged (without logging for the usb.backend.openusb logger
> being (explictly) turned on by the caller.
>
>
> #2 is probably fixable by explicitly setting a log level (of off) in the
> pyusb code. I could look into doing that if it would be helpful.
>
> The workarounds are:
>
> 1) Don't use the basic logger in a user script, e.g. add something like; log
> = logging.getLogger('mylogger') to the user script
> 2) use libusb10
>
> It isn't a huge issue but a little odd.
>

The problem is that the default behavior on logging module it to
dispatch the log handling to the parent when level is set to NOTSET. A
smarter way to do handling in app is to create your own logger:

log = logging.getLogger('foo')

Whatever, I created a patch to this problem, please test it and report
the status, so I can commit it to the source tree. Thanks for the bug
report.


-- 
Best Regards,
Wander Lairson Costa
Index: usb/__init__.py
===================================================================
--- usb/__init__.py	(revisão 90)
+++ usb/__init__.py	(cópia de trabalho)
@@ -59,7 +59,7 @@
                   'error': logging.ERROR,
                   'critical': logging.CRITICAL}
 
-        level = LEVELS.get(debug_level, logging.CRITICAL + 10)
+        level = LEVELS.get(debug_level, logging.CRITICAL)
         logger.setLevel(level = level)
 
         try:
@@ -75,6 +75,11 @@
             def emit(self, record):
                 pass
 
+        # We set the log level to avoid delegation to the
+        # parent log handler (if there is one).
+        # Thanks to Chris Clark to pointing this out.
+        logger.setLevel(logging.CRITICAL)
+
         logger.addHandler(NullHandler())
 
 
------------------------------------------------------------------------------
EMC VNX: the world's simplest storage, starting under $10K
The only unified storage solution that offers unified management 
Up to 160% more powerful than alternatives and 25% more efficient. 
Guaranteed. http://p.sf.net/sfu/emc-vnx-dev2dev
_______________________________________________
pyusb-users mailing list
pyusb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyusb-users

Reply via email to