daklog.py 3.2 KB

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