Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
304 ashish 1
'''
2
Created on 28-Jun-2010
3
 
4
@author: ashish
5
'''
6
import sys
7
import thrift
8
 
9
from shop2020.config.client.ConfigClient import ConfigClient
10
from shop2020.utils.Utils import log_entry, to_java_date
11
import shlex
12
import subprocess
13
import datetime
14
from thrift.transport import TSocket, TTransport
15
from thrift.protocol import TBinaryProtocol
16
from scribe import scribe
17
 
18
class DataLoggerImpl:
19
    def __init__(self):
20
        '''
21
        Start local scribe server here.
22
        Follwing are the properties
23
        datalogging_service_local_hostname,
24
        datalogging_service_local_port,
25
        datalogging_service_remote_hostname,
26
        datalogging_service_remote_port
27
        '''
28
        self.local_port = "9005"
29
        self.remote_host = "localhost"
30
        self.remote_port = "9006"
31
        self.category = "default"
32
 
33
        try:
34
            config_client = ConfigClient()
35
            self.local_port = config_client.get_property("datalogging_service_local_port")
36
            self.remote_host= config_client.get_property("datalogging_service_remote_hostname")
37
            self.remote_port = config_client.get_property("datalogging_service_remote_port")
38
        except:
39
            log_entry("DataLogger", "could not read config server. Using local values")
40
 
41
        args = "scribed -p %s -c %s" %(self.local_port, "/tmp/scribe_local.conf")
42
 
43
        args = shlex.split(args)
44
 
45
        log_entry("DataLogger", args)
46
 
47
        self.p = subprocess.Popen(args)
48
 
49
        self.socket = TSocket.TSocket(host='localhost', port=int(self.local_port))
50
        self.transport = TTransport.TFramedTransport(self.socket)
51
        self.protocol = TBinaryProtocol.TBinaryProtocol(trans=self.transport, strictRead=False, strictWrite=False)
418 rajveer 52
        while True:
53
            try:
54
                self.client = scribe.Client(iprot=self.protocol, oprot=self.protocol)
55
                self.transport.open()                
56
            except:
57
                log_entry("DataLogger", "Connection not made. continuing")
58
                continue
59
            log_entry("DataLogger", "Connection successfull. breaking")
60
            break
304 ashish 61
 
62
 
63
    def log(self, messageType, message):
64
        if message:
65
            if messageType:
66
                message_to_log = "%d:%d:%s\n" % (messageType, to_java_date(datetime.datetime.now()), message)
67
                log_entry_unit = scribe.LogEntry(self.category, message_to_log)
68
                result = self.client.Log(messages=[log_entry_unit])
483 rajveer 69
 
304 ashish 70
 
71