Subversion Repositories SmartDukaan

Rev

Rev 418 | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 28-Jun-2010

@author: ashish
'''
import sys
import thrift

from shop2020.config.client.ConfigClient import ConfigClient
from shop2020.utils.Utils import log_entry, to_java_date
import shlex
import subprocess
import datetime
from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
from scribe import scribe

class DataLoggerImpl:
    def __init__(self):
        '''
        Start local scribe server here.
        Follwing are the properties
        datalogging_service_local_hostname,
        datalogging_service_local_port,
        datalogging_service_remote_hostname,
        datalogging_service_remote_port
        '''
        self.local_port = "9005"
        self.remote_host = "localhost"
        self.remote_port = "9006"
        self.category = "default"
        
        try:
            config_client = ConfigClient()
            self.local_port = config_client.get_property("datalogging_service_local_port")
            self.remote_host= config_client.get_property("datalogging_service_remote_hostname")
            self.remote_port = config_client.get_property("datalogging_service_remote_port")
        except:
            log_entry("DataLogger", "could not read config server. Using local values")
            
        args = "scribed -p %s -c %s" %(self.local_port, "/tmp/scribe_local.conf")
        
        args = shlex.split(args)
        
        log_entry("DataLogger", args)
        
        self.p = subprocess.Popen(args)
        
        self.socket = TSocket.TSocket(host='localhost', port=int(self.local_port))
        self.transport = TTransport.TFramedTransport(self.socket)
        self.protocol = TBinaryProtocol.TBinaryProtocol(trans=self.transport, strictRead=False, strictWrite=False)
        while True:
            try:
                self.client = scribe.Client(iprot=self.protocol, oprot=self.protocol)
                self.transport.open()                
            except:
                log_entry("DataLogger", "Connection not made. continuing")
                continue
            log_entry("DataLogger", "Connection successfull. breaking")
            break

        
    def log(self, messageType, message):
        if message:
            if messageType:
                message_to_log = "%d:%d:%s\n" % (messageType, to_java_date(datetime.datetime.now()), message)
                log_entry_unit = scribe.LogEntry(self.category, message_to_log)
                result = self.client.Log(messages=[log_entry_unit])