-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.py
executable file
·52 lines (46 loc) · 1.83 KB
/
log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
import log_pb2
from google.protobuf import timestamp_pb2, json_format
import time
import sys
from argparse import ArgumentParser
def get_serialized_log():
log = log_pb2.Log()
log.context.timestamp.GetCurrentTime()
log.context.host_or_ip = "192.168.xxx.xxx"
log.context.service_name = "test"
log.context.user = "test"
log.level = log_pb2.Log.Level.INFO
log.message = "This is a test log generated by [{0}].".format( sys.argv[0] )
return log.SerializeToString()
def get_deserialized_log( serialized_log ):
log = log_pb2.Log()
log.ParseFromString( serialized_log )
return log
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument( "-w", "--write", help="write protobuf (binary) log" )
parser.add_argument( "-r", "--read", help="read protobuf (binary) log" )
parser.add_argument( "-j", "--read-json", help="read protobuf (binary) log as JSON" )
args = parser.parse_args()
if args.write:
filename = args.write
print "Writing protobuf sample log file [{0}]...".format(filename),
serailized_log = get_serialized_log()
open( filename, "wb" ).write( serailized_log )
print "[DONE]"
elif args.read:
filename = args.read
print "Dumping protobuf binary file [{0}]...".format(filename)
binary_log = open( filename, "rb" ).read()
deserailized_log = get_deserialized_log( binary_log )
print deserailized_log
print "[DONE]"
elif args.read_json:
filename = args.read_json
print "Dumping protobuf binary file as JSON [{0}]...".format(filename)
binary_log = open( filename, "rb" ).read()
deserailized_log = get_deserialized_log( binary_log )
json_log = json_format.MessageToJson( deserailized_log )
print json_log
print "[DONE]"