daklog.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """
  2. Logging functions
  3. @contact: Debian FTP Master <ftpmaster@debian.org>
  4. @copyright: 2001, 2002, 2006 James Troup <james@nocrew.org>
  5. @license: GNU General Public License version 2 or later
  6. """
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  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. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. ################################################################################
  19. import fcntl
  20. import os
  21. import time
  22. import sys
  23. import traceback
  24. from . import utils
  25. ################################################################################
  26. class Logger:
  27. "Logger object"
  28. __shared_state = {}
  29. def __init__(self, program='unknown', debug=False, print_starting=True, include_pid=False):
  30. self.__dict__ = self.__shared_state
  31. self.program = program
  32. self.debug = debug
  33. self.include_pid = include_pid
  34. if not getattr(self, 'logfile', None):
  35. self._open_log(debug)
  36. if print_starting:
  37. self.log(["program start"])
  38. def _open_log(self, debug) -> None:
  39. # Create the log directory if it doesn't exist
  40. from daklib.config import Config
  41. logdir = Config()["Dir::Log"]
  42. if not os.path.exists(logdir):
  43. umask = os.umask(00000)
  44. os.makedirs(logdir, 0o2775)
  45. os.umask(umask)
  46. # Open the logfile
  47. logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m"))
  48. logfile = None
  49. if debug:
  50. logfile = sys.stderr
  51. else:
  52. umask = os.umask(0o0002)
  53. logfile = open(logfilename, 'a')
  54. os.umask(umask)
  55. self.logfile = logfile
  56. def log(self, details: list[str]) -> None:
  57. "Log an event"
  58. # Prepend timestamp, program name, and user name
  59. details.insert(0, utils.getusername())
  60. details.insert(0, self.program)
  61. timestamp = time.strftime("%Y%m%d%H%M%S")
  62. details.insert(0, timestamp)
  63. # Force the contents of the list to be string.join-able
  64. details = [str(i) for i in details]
  65. fcntl.lockf(self.logfile, fcntl.LOCK_EX)
  66. # Write out the log in TSV
  67. self.logfile.write("|".join(details) + '\n')
  68. # Flush the output to enable tail-ing
  69. self.logfile.flush()
  70. fcntl.lockf(self.logfile, fcntl.LOCK_UN)
  71. def log_traceback(self, info, ex) -> None:
  72. "Log an exception with a traceback"
  73. self.log([info, repr(ex)])
  74. for line in traceback.format_exc().split('\n')[:-1]:
  75. self.log(['traceback', line])
  76. def close(self) -> None:
  77. "Close a Logger object"
  78. self.log(["program end"])
  79. self.logfile.close()