daklog.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. import fcntl
  21. import os
  22. import pwd
  23. import time
  24. import sys
  25. import utils
  26. ################################################################################
  27. class Logger(object):
  28. "Logger object"
  29. __shared_state = {}
  30. def __init__(self, program='unknown', debug=False, print_starting=True, include_pid=False):
  31. self.__dict__ = self.__shared_state
  32. self.program = program
  33. self.debug = debug
  34. self.include_pid = include_pid
  35. if not getattr(self, 'logfile', None):
  36. self._open_log(debug)
  37. if print_starting:
  38. self.log(["program start"])
  39. def _open_log(self, debug):
  40. # Create the log directory if it doesn't exist
  41. from daklib.config import Config
  42. logdir = Config()["Dir::Log"]
  43. if not os.path.exists(logdir):
  44. umask = os.umask(00000)
  45. os.makedirs(logdir, 0o2775)
  46. os.umask(umask)
  47. # Open the logfile
  48. logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m"))
  49. logfile = None
  50. if debug:
  51. logfile = sys.stderr
  52. else:
  53. umask = os.umask(0o0002)
  54. logfile = utils.open_file(logfilename, 'a')
  55. os.umask(umask)
  56. self.logfile = logfile
  57. def log (self, details):
  58. "Log an event"
  59. # Prepend timestamp, program name, and user name
  60. details.insert(0, utils.getusername())
  61. details.insert(0, self.program)
  62. timestamp = time.strftime("%Y%m%d%H%M%S")
  63. details.insert(0, timestamp)
  64. # Force the contents of the list to be string.join-able
  65. details = [ str(i) for i in details ]
  66. fcntl.lockf(self.logfile, fcntl.LOCK_EX)
  67. # Write out the log in TSV
  68. self.logfile.write("|".join(details)+'\n')
  69. # Flush the output to enable tail-ing
  70. self.logfile.flush()
  71. fcntl.lockf(self.logfile, fcntl.LOCK_UN)
  72. def close (self):
  73. "Close a Logger object"
  74. self.log(["program end"])
  75. self.logfile.close()