| 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)
|
|
|
52 |
self.client = scribe.Client(iprot=self.protocol, oprot=self.protocol)
|
|
|
53 |
self.transport.open()
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
def log(self, messageType, message):
|
|
|
57 |
if message:
|
|
|
58 |
if messageType:
|
|
|
59 |
message_to_log = "%d:%d:%s\n" % (messageType, to_java_date(datetime.datetime.now()), message)
|
|
|
60 |
log_entry_unit = scribe.LogEntry(self.category, message_to_log)
|
|
|
61 |
result = self.client.Log(messages=[log_entry_unit])
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
|