print-ts.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. # Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. #
  4. # This file is part of GDB.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. # This is a simple program that can be used to print timestamps on
  19. # standard output. The inspiration for it was ts(1)
  20. # (<https://joeyh.name/code/moreutils/>). We have our own version
  21. # because unfortunately ts(1) is or may not be available on all
  22. # systems that GDB supports.
  23. #
  24. # The usage is simple:
  25. #
  26. # #> some_command | print-ts.py [FORMAT]
  27. #
  28. # FORMAT must be a string compatible with "strftime". If nothing is
  29. # provided, we choose a reasonable format.
  30. import fileinput
  31. import datetime
  32. import sys
  33. import os
  34. if len(sys.argv) > 1:
  35. fmt = sys.argv[1]
  36. else:
  37. fmt = "[%b %d %H:%M:%S]"
  38. mypid = os.getpid()
  39. for line in fileinput.input("-"):
  40. sys.stdout.write(
  41. "{} [{}] {}".format(datetime.datetime.now().strftime(fmt), mypid, line)
  42. )
  43. sys.stdout.flush()