# Path: pUtil.py
# def tolog(msg, tofile=True, label='INFO', essential=False):
#     """ Write date+msg to pilot log and to stdout """
# 
#     try:
#         import inspect
# 
#         MAXLENGTH = 12
#         # getting the name of the module that is invoking tolog() and adjust the length
#         try:
#             module_name = os.path.basename(inspect.stack()[1][1])
#         except Exception, e:
#             module_name = "unknown"
#             #print "Exception caught by tolog(): ", e,
#         module_name_cut = module_name[0:MAXLENGTH].ljust(MAXLENGTH)
#         msg = "%i|%s| %s" % (os.getpid(),module_name_cut, msg)
# 
#         t = timeStampUTC(format='%Y-%m-%d %H:%M:%S')
#         if tofile:
#             appendToLog("%s|%s\n" % (t, msg))
# 
#         # remove backquotes from the msg since they cause problems with batch submission of pilot
#         # (might be present in error messages from the OS)
#         msg = msg.replace("`","'")
#         msg = msg.replace('"','\\"')
#         print "%s| %s" % (t, msg)
# 
#         # write any FAILED messages to stderr
#         if "!!FAILED!!" in msg:
#             try:
#                 print >> sys.stderr, "%s| %s" % (t, msg)
#             except:
#                 print "Failed to print to sys.stderr: %s" % (t, msg)
#     except:
#         print "!!WARNING!!4000!! %s" % traceback.format_exc()


import sys,os,ssl
import time
    import pika
from pUtil import tolog

    def close(self):
        self.channel.close()
        self.connection.close()
        #self.channel = None
        #self.connection = None
    
    def send_msg(self,
                 message_body,
                 routing_key,
                 exchange_name = None,
                 message_headers = {},
                 priority = 0, # make message persistent
                 delivery_mode = 2, # default
                ):
        
        if exchange_name is None:
            exchange_name = self.exchange_name
        
        timestamp = time.time()
    
        # create the message properties
        properties = pika.BasicProperties(
                                          delivery_mode = delivery_mode,
                                          priority      = priority,
                                          timestamp     = timestamp,
                                          headers       = message_headers,
                                         )
    
        tolog("MQ ARGO: sending message body:\n" +  str(message_body))
        tolog('MQ ARGO: sending message to exchange: ' + self.exchange_name)

# Path: pUtil.py
# def tolog(msg, tofile=True, label='INFO', essential=False):
#     """ Write date+msg to pilot log and to stdout """
# 
#     try:
#         import inspect
# 
#         MAXLENGTH = 12
#         # getting the name of the module that is invoking tolog() and adjust the length
#         try:
#             module_name = os.path.basename(inspect.stack()[1][1])
#         except Exception, e:
#             module_name = "unknown"
#             #print "Exception caught by tolog(): ", e,
#         module_name_cut = module_name[0:MAXLENGTH].ljust(MAXLENGTH)
#         msg = "%i|%s| %s" % (os.getpid(),module_name_cut, msg)
# 
#         t = timeStampUTC(format='%Y-%m-%d %H:%M:%S')
#         if tofile:
#             appendToLog("%s|%s\n" % (t, msg))
# 
#         # remove backquotes from the msg since they cause problems with batch submission of pilot
#         # (might be present in error messages from the OS)
#         msg = msg.replace("`","'")
#         msg = msg.replace('"','\\"')
#         print "%s| %s" % (t, msg)
# 
#         # write any FAILED messages to stderr
#         if "!!FAILED!!" in msg:
#             try:
#                 print >> sys.stderr, "%s| %s" % (t, msg)
#             except:
#                 print "Failed to print to sys.stderr: %s" % (t, msg)
#     except:
#         print "!!WARNING!!4000!! %s" % traceback.format_exc()


# Path: MessageInterface.py
import sys,os,ssl
import time
    import pika
from pUtil import tolog

    def close(self):
        self.channel.close()
        self.connection.close()
        #self.channel = None
        #self.connection = None
    
    def send_msg(self,
                 message_body,
                 routing_key,
                 exchange_name = None,
                 message_headers = {},
                 priority = 0, # make message persistent
                 delivery_mode = 2, # default
                ):
        
        if exchange_name is None:
            exchange_name = self.exchange_name
        
        timestamp = time.time()
    
        # create the message properties
        properties = pika.BasicProperties(
                                          delivery_mode = delivery_mode,
                                          priority      = priority,
                                          timestamp     = timestamp,
                                          headers       = message_headers,
                                         )
    
        tolog("MQ ARGO: sending message body:\n" +  str(message_body))
        tolog('MQ ARGO: sending message to exchange: ' + self.exchange_name)

        tolog('MQ ARGO: sending message with routing key: ' + routing_key)
