import logging.config
import datetime
import os
import sys
import socket

class PSLogger(logging.getLoggerClass()): 

   '''
   Sets up logging using provided values and this process hostname and PID if sending to file
   This will default to stout and no file output
   '''
   def setup(self, programName, basePath, skychunkName, host, pid, stdout=1, sendToFile=0):

       formatter = logging.Formatter('%(asctime)s | %(levelname)7s | %(message)s', '%Y-%m-%d %H:%M:%S')
       self.setLevel(logging.INFO)

       if sendToFile:

           # generate path
           PATH = basePath + "/log"
           if not os.path.exists(PATH): os.makedirs(PATH)
           FULLPATH = PATH + "/" + programName + "_" + skychunkName + "_" + host + "_" + str(pid) + ".log"
           # opens file to be appended
           hdlr = logging.FileHandler(FULLPATH, "a")
           hdlr.setFormatter(formatter)
           self.addHandler(hdlr) 

       if stdout:
           stdout = logging.StreamHandler(sys.__stdout__)
           stdout.setFormatter(formatter)
           self.addHandler(stdout)

       if sendToFile: print "Writing log to: " + FULLPATH

   def infoPair(self, first, second):
       self.info("%-40s%s" % (first, second))

   def errorPair(self, first, second):
       self.error("%-40s%s" % (first, second))

   def debugPair(self, first, second):
       self.debug("%-40s%s" % (first, second))

   def infoTitle(self, str):
       self.info("")
       self.info("%40s" % (str))
       self.info("")

   def infoBool(self, first, bool):
       if bool: str = "yes"
       else: str = "no"
       self.info("%-40s%s" % (first, str))

   def errorBool(self, first, bool):
       if bool: str = "yes"
       else: str = "no"
       self.error("%-40s%s" % (first, str))

   def infoSeparator(self):
       self.info("--------------------------------------------------------------------------")

