-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixLogViewer.rb
More file actions
executable file
·187 lines (160 loc) · 5.14 KB
/
FixLogViewer.rb
File metadata and controls
executable file
·187 lines (160 loc) · 5.14 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#! /usr/bin/env ruby
# FixLogViewer.rb
# July 11, 2007
#
require 'rubygems'
require 'logger'
require 'optparse'
require 'pp'
require ENV["FLV_HOME"]+"/ReadFIXFieldSpec.rb"
begin
require 'term/ansicolor'
include Term::ANSIColor
rescue LoadError
require ENV["FLV_HOME"]+"/fakecolor.rb"
end
class FixLogViewer
def initialize(fixLogFileName, options)
@allFixFields = nil
@colorOutput = options[:color]
@evenOdd = "even"
if(options[:grep_to_apply] != nil)
@grep_to_apply = options[:grep_to_apply]
else
# TODO
end
@log = Logger.new(STDOUT)
if(options[:verbose] == true)
@log.level = Logger::DEBUG
else
@log.level = Logger::INFO
end
@log.debug("set colorOutput to " + @colorOutput.to_s)
if "-" == fixLogFileName
@log.info("will read from stdinput!")
ARGF.each{ |line| processLine(line)}
elsif fixLogFileName == nil || !File.exists?(fixLogFileName)
if(fixLogFileName == nil)
@log.error "No Fix Log file given! use the --help option to see how to use this script"
else
@log.error fixLogFileName + " does not exist!"
end
exit!
elsif File.exists?(fixLogFileName)
File.open(fixLogFileName, "r").each{ |line| processLine(line)}
end
end
private
def printInYellow(someText)
if(@colorOutput == true)
yellow(someText).reset
else
someText
end
end
def printInRed(someText)
if(@colorOutput == true)
red(someText).reset
else
someText
end
end
def printLine(lineToPrint)
if(@colorOutput == true)
if (@evenOdd == "even")
puts green(lineToPrint).reset
@evenOdd = "odd"
else
puts lineToPrint
@evenOdd = "even"
end
else
puts lineToPrint
end
end
def processLine(theLine)
# where the FIX message starts
start = theLine.index("8=FIX")
if(start == nil)
@log.warn("The line: '" + theLine.chomp + "' does not start with 8=FIX")
#exit 1
return
end
# where the FIX message ends
endIndex = theLine.rindex("\x01")
if(endIndex == nil)
@log.warn("The line: '" + theLine.chomp + "' does not end with an 0x01 (asci zero)")
#exit 1
return
end
theLine = theLine[start, endIndex]
allFieldInMessage = theLine.split("\x01")
if(@allFixFields == nil)
# need to know what version FIX we are so that
# we get the right field names for the field numbers
fixVersion = allFieldInMessage[0].split("=")[1];
@log.info("got fix version " + fixVersion)
@allFixFields = AllFixFields.new(fixVersion)
end
puts printInYellow(" =-=-=-=-=-=-==-=-=-=-=-=-==-=-=-=-=-=-= ")
@log.debug( "allFieldsInMessage.length: " + allFieldInMessage.length.to_s)
@log.debug( allFieldInMessage)
for index in (0..allFieldInMessage.length)
if(allFieldInMessage[index] != nil)
aFieldAndValue = allFieldInMessage[index].strip;
if(aFieldAndValue != nil && aFieldAndValue.length > 0)
@log.debug("processing index:" + index.to_s)
@log.debug(aFieldAndValue)
@log.debug(aFieldAndValue.length.to_s)
fieldNameAndValue = aFieldAndValue.split("=")
if(fieldNameAndValue.length > 1)
@log.debug("fieldNameAndValue.length is > 1")
@log.debug(fieldNameAndValue.length.to_s)
fieldName = @allFixFields.getFieldName(fieldNameAndValue[0])
@log.debug("field name: " + fieldName.to_s)
if(fieldName == nil) # looks like a custom field
fieldName = fieldNameAndValue[0]
end
lineToPrint = nil
padding = @allFixFields.getLongestFieldLength() - fieldName.length - 2
@log.debug("padding: " + padding.to_s)
if(fieldNameAndValue[0] == "35")
lineToPrint = fieldName +
" = ".rjust(padding) + printInRed(@allFixFields.getMessageType(fieldNameAndValue[1])) +
printInRed(" ".rjust(30 - @allFixFields.getMessageType(fieldNameAndValue[1]).length) + allFieldInMessage[index])
else
lineToPrint = fieldName + " = ".rjust(padding) + fieldNameAndValue[1] +
" ".rjust(30 - fieldNameAndValue[1].to_s.length) + allFieldInMessage[index]
end
printLine(lineToPrint)
end
end
end
end
end
end
options={}
#default options
options[:color]=false
#options[:grep_to_apply]=nil
options[:verbose]=false
optparse = OptionParser.new do |opts|
opts.banner = "Usage: FixLogViewer.rb [options] [fixlogfile]"
opts.on('--color', '-c', 'Generate color output') do
options[:color] = true
end
# TODO
#opts.on('--grep', '-g', 'Grep the fix logs for this (eg 35=8)') do |grep_to_apply|
# options[:grep_to_apply] = grep_to_apply
#end
opts.on('--verbose', '-v', 'Generate verbose debug output (useful for reporting/debugging errors)') do
options[:verbose]=true
end
opts.on('--help', '-h', 'Display help message') do
puts opts
puts "if fixlogfile is - then read fix logs from STDIN"
exit
end
end
optparse.parse!
FixLogViewer.new(ARGV[0], options)