You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.2 KiB

  1. #-*- coding: utf-8 -*-
  2. # Copyright 2008-2010 Mir Calculate. http://www.calculate-linux.org
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import time
  17. class log():
  18. """Класс для записи в лог"""
  19. # Директория хранения логов
  20. logDir = "/var/log/calculate"
  21. def __init__(self,fileName, addDataTime=True):
  22. self.logFile = os.path.join(self.logDir,fileName)
  23. if not os.path.exists(self.logDir):
  24. os.makedirs(self.logDir)
  25. self.addDataTime = addDataTime
  26. def addLog(self, textLog):
  27. """Добавляет текст в лог файл"""
  28. if not os.path.exists(self.logFile):
  29. try:
  30. fd = os.open(self.logFile, os.O_CREAT,0600)
  31. os.close(fd)
  32. except:
  33. print "Error creating file %s"%self.logFile
  34. return False
  35. textWrite = textLog
  36. if not textLog[-1:] == "\n" :
  37. textWrite = "%s\n"%textLog
  38. if self.addDataTime:
  39. textWrite = "%s %s"%(time.strftime("%d/%m/%Y %H:%M:%S",\
  40. time.localtime()),textWrite)
  41. try:
  42. FD = open (self.logFile, "a")
  43. FD.write(textWrite)
  44. FD.close()
  45. except:
  46. print "Error writing to file %s"%self.logFile
  47. return False
  48. return True
  49. def writeError(self, textLog):
  50. """Добавляет сообщение об ошибке в log"""
  51. return self.addLog("ERROR: %s" %textLog)
  52. def writeSuccess(self, textLog):
  53. """Добавляет сообщение об успехе в log"""
  54. return self.addLog("SUCCESS: %s" %textLog)