Logger.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Logger.py
  5. #
  6. # Copyright 2022 Stephen Stengel <stephen.stengel@cwu.edu>
  7. import time
  8. def main(args):
  9. outFileName = "time-to-death.txt"
  10. timeToSleep = 1 # in seconds
  11. print("Hi!")
  12. print("This program will run until canceled with ctrl-c or the" \
  13. + " battery dies.")
  14. print("The total runtime of the battery will be recorded in a " \
  15. + "file named: \n" + outFileName)
  16. runLogger(outFileName, timeToSleep)
  17. return 0
  18. def runLogger(outFileName, timeToSleep):
  19. startingSeconds = time.time()
  20. while(True):
  21. with open(outFileName, "w") as timeLog:
  22. outputLine = "Last clock time: "
  23. outputLine += str(time.asctime(time.localtime())) + "\n"
  24. outputLine += "Elapsed time in seconds: "
  25. outputLine += str(int(time.time() - startingSeconds)) + "\n"
  26. timeLog.write(outputLine)
  27. timeLog.flush() ## This is needed to write to the file before closing the file.
  28. time.sleep(timeToSleep)
  29. if __name__ == '__main__':
  30. import sys
  31. sys.exit(main(sys.argv))