Debug.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2011 Nick Mathewson, Michael Stone
  4. # Copyright 2013 The Tor Project
  5. #
  6. # You may do anything with this work that copyright law would normally
  7. # restrict, so long as you retain the above notice(s) and this license
  8. # in all redistributed copies and derived works. There is no warranty.
  9. from __future__ import print_function
  10. import cgitb
  11. import os
  12. import sys
  13. # Get verbose tracebacks, so we can diagnose better.
  14. cgitb.enable(format="plain")
  15. # Set debug_flag=True in order to debug this program or to get hints
  16. # about what's going wrong in your system.
  17. debug_flag = os.environ.get("CHUTNEY_DEBUG", "") != ""
  18. def debug(s):
  19. "Print a debug message on stdout if debug_flag is True."
  20. if debug_flag:
  21. print("DEBUG: %s" % s)
  22. def main():
  23. global debug_flag
  24. debug("This message should appear if $CHUTNEY_DEBUG is true.")
  25. debug_flag = True
  26. debug("This message should always appear.")
  27. debug_flag = False
  28. debug("This message should never appear.")
  29. # We don't test tracebacks, because it's hard to know what to expect
  30. # (and they make python exit with a non-zero exit status)
  31. return 0
  32. if __name__ == '__main__':
  33. sys.exit(main())