-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.rb
executable file
·56 lines (48 loc) · 1.74 KB
/
log.rb
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
53
54
55
56
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative 'log_pb'
require 'google/protobuf/well_known_types' # Google::Protobuf::Timestamp
require 'optparse'
require 'json'
def get_serialized_log
log = Service::Logging::Log.new
log.context = Service::Logging::Log::Context.new(
timestamp: Google::Protobuf::Timestamp.new(seconds: Time.now.to_i, nanos: 0),
host_or_ip: '192.168.xxx.xxx',
service_name: 'test',
user: 'test'
)
log.level = Service::Logging::Log::Level::INFO
log.message = "This is a test log generated by [#{$PROGRAM_NAME}]."
Service::Logging::Log.encode(log)
end
def get_deserialized_log(serialized_log)
Service::Logging::Log.decode(serialized_log)
end
OptionParser.new do |parser|
parser.banner = "Usage: #{$PROGRAM_NAME} [option] FILENAME"
parser.on('-w', '--write FILENAME', 'write protobuf (binary) log') do |filename|
print "Writing protobuf sample log file [#{filename}]... "
File.open(filename, 'wb') do |file|
file.write(get_serialized_log)
end
puts '[DONE]'
end
parser.on('-r', '--read FILENAME', 'read protobuf (binary) log') do |filename|
puts "Dumping protobuf binary file [#{filename}]..."
File.open(filename, 'rb') do |file|
deserialized_log = get_deserialized_log(file.read)
puts deserialized_log.inspect
end
puts '[DONE]'
end
parser.on('-j', '--read-json FILENAME', 'read protobuf (binary) log as JSON') do |filename|
puts "Dumping protobuf binary file as JSON [#{filename}]..."
File.open(filename, 'rb') do |file|
deserialized_log = get_deserialized_log(file.read)
json_log = Service::Logging::Log.encode_json(deserialized_log)
puts JSON.pretty_generate(JSON[json_log])
end
puts '[DONE]'
end
end.parse!