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

Quiet pybullet import msgs #821

Merged
merged 4 commits into from
May 6, 2021
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ process
- Fixed traffic generation offset bug for shifted maps. See issue #790.
- Fixed bugs in traffic history and changed interface to it. See issue #732.
- Update `ego_open_agent` to use the package instead of the zoo directory version.
- Quieted error logs generated by failed Envision connections. See issue #819.
- Quieted error logs generated by failed Envision connections as well as noisy pybullet log messages. See issue #819.

## [0.4.15] - 2021-03-18
### Added
Expand Down
4 changes: 2 additions & 2 deletions smarts/core/sumo_traffic_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from smarts.core.coordinates import Heading, Pose
from smarts.core.provider import Provider, ProviderState
from smarts.core.utils import networking
from smarts.core.utils.logging import suppress_stdout
from smarts.core.utils.logging import suppress_output
from smarts.core.utils.sumo import SUMO_PATH, traci
from smarts.core.vehicle import VEHICLE_CONFIGS, VehicleState

Expand Down Expand Up @@ -163,7 +163,7 @@ def _initialize_traci_conn(self, num_retries=5):
)
time.sleep(0.05) # give SUMO time to start
try:
with suppress_stdout():
with suppress_output(stdout=False):
self._traci_conn = traci.connect(
sumo_port,
numRetries=100,
Expand Down
54 changes: 34 additions & 20 deletions smarts/core/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ def isnotebook():
libc = ctypes.CDLL(None)
try:
c_stderr = ctypes.c_void_p.in_dll(libc, "stderr")
c_stdout = ctypes.c_void_p.in_dll(libc, "stdout")
except:
# macOS
c_stderr = ctypes.c_void_p.in_dll(libc, "__stderrp")
c_stdout = ctypes.c_void_p.in_dll(libc, "__stdoutp")


def try_fsync(fd):
Expand All @@ -63,39 +65,51 @@ def try_fsync(fd):


@contextmanager
def suppress_stdout():
original_stderr = sys.stderr
def suppress_output(stderr=True, stdout=True):
cleanup_stderr = None
cleanup_stdout = None
try:
original_stderr_fno = sys.stderr.fileno()
if stderr:
cleanup_stderr = _suppress_fileout("stderr")
if stdout:
cleanup_stdout = _suppress_fileout("stdout")
yield
finally:
if stderr and cleanup_stderr:
cleanup_stderr(c_stderr)
if stdout and cleanup_stdout:
cleanup_stdout(c_stdout)


def _suppress_fileout(stdname):
original = getattr(sys, stdname)
try:
original_std_fno = original.fileno()
except UnsupportedOperation as e:
if not isnotebook():
raise e
## This case is notebook which does not have issues with the c_printf
try:
yield
finally:
return
dup_stderr_fno = os.dup(original_stderr_fno)
return None

dup_std_fno = os.dup(original_std_fno)
devnull_fno = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull_fno, original_stderr_fno)
sys.stderr = os.fdopen(devnull_fno, "w")
os.dup2(devnull_fno, original_std_fno)
setattr(sys, stdname, os.fdopen(devnull_fno, "w"))

try:
yield
finally:
sys.stderr.flush()
libc.fflush(c_stderr)
def cleanup(c_stdobj):
getattr(sys, stdname).flush()
libc.fflush(c_stdobj)
try_fsync(devnull_fno)
os.close(devnull_fno)

os.dup2(dup_stderr_fno, original_stderr_fno)
os.close(dup_stderr_fno)
os.dup2(dup_std_fno, original_std_fno)
os.close(dup_std_fno)
try:
sys.stderr.close()
getattr(sys, stdname).close()
except OSError as e:
# This happens in some environments and is fine so we should ignore just it
if e.errno != 9: # [Errno 9] Bad file descriptor
raise e
finally:
sys.stderr = original_stderr
setattr(sys, stdname, original)

return cleanup
4 changes: 2 additions & 2 deletions smarts/core/utils/pybullet.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from smarts.core.utils.logging import suppress_stdout
from smarts.core.utils.logging import suppress_output

# XXX: Importing pybullet logs an annoying build version tag. There's no "friendly"
# way to fix this since they simply use print(...). Disabling logging at the
# time of import is our hack.
with suppress_stdout():
with suppress_output():
from pybullet import *
from pybullet_utils import bullet_client