Skip to content

Commit

Permalink
added --nobuf flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dev195 committed Feb 23, 2015
1 parent 46e691c commit cfbee9a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bin/decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ def main(*largs, **kwargs):
'\tpcap=PCAPFILE to write packets to a PCAP file\n' +
'\tsession=SESSION to write session text\n' +
'\tdirection=data direction to write (c,s,both,split)')
group.add_option('--nobuf', help='turn off output buffering', dest='nobuffer',
action='store_true', default=False)
group.add_option('-w', '--session', dest='session',
help='write session file, same as -o session=')
group.add_option('-W', '--pcap', dest='pcap', default=None,
Expand Down Expand Up @@ -566,6 +568,8 @@ def main(*largs, **kwargs):
# set output file (and other args if -o filename,key=val...)
if options.outfile:
outfile, outkw = util.strtok(options.outfile)
if options.nobuffer:
outkw.update(nobuffer=True)
# output extra?
if options.oextra:
outkw.update(extra=True)
Expand Down
8 changes: 8 additions & 0 deletions lib/output/colorout.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def _htmlwrite(self, text):
self.htmlbuffer += text
else:
self.fh.write(text)
if self.nobuffer:
self.fh.flush()

def htmldump(self):
'''
Expand Down Expand Up @@ -241,6 +243,8 @@ def _write_string(self, text, direction, timestamp, encoding=None):
elif self._COLORMODE == 'TTY' and self._timemode and timestamp != None:
self.fh.write('\x1b[36m%s UTC:\x1b[0m\n' %
datetime.datetime.utcfromtimestamp(timestamp))
if self.nobuffer:
self.fh.flush()

# Set Direction
if direction.lower() == 'cs':
Expand Down Expand Up @@ -275,6 +279,8 @@ def _write_string(self, text, direction, timestamp, encoding=None):
self._write_tty(text, colorTag)
else:
self.fh.write(text)
if self.nobuffer:
self.fh.flush()

# Plain Text
else:
Expand Down Expand Up @@ -308,6 +314,8 @@ def _write_tty(self, text, colorTag):
self.fh.write(colorTag + _line[0] + '\x1b[0m')
if len(_line) > 1:
self.fh.write('\n')
if self.nobuffer:
self.fh.flush()

def _HTMLHeader(self, title="Dshell"):

Expand Down
2 changes: 2 additions & 0 deletions lib/output/jsonout.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,7 @@ def alert(self, *args, **kw):
del kw[name]
self.fh.write(
json.dumps(kw, ensure_ascii=self.options['ensure_ascii']) + "\n")
if self.nobuffer:
self.fh.flush()

obj = JSONOutput
2 changes: 2 additions & 0 deletions lib/output/netflowout.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,7 @@ def __alert(self, *args, **kw):
kw['endtime'] - kw['starttime'])
)
)
if self.nobuffer:
self.fh.flush()

obj = NetflowOutput
7 changes: 7 additions & 0 deletions lib/output/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def __init__(self, *a, **kw):
self.timeformat = (kw.get('timeformat', self._DEFAULT_TIMEFORMAT))
self.delim = (kw.get('delim', self._DEFAULT_DELIM))

# Run flush() after every relevant write() if this is true
self.nobuffer = (kw.get('nobuffer', False))

if 'pcap' in kw:
self.pcapwriter = PCAPWriter(kw['pcap'])
else:
Expand Down Expand Up @@ -241,6 +244,8 @@ def write(self, obj, **kw):
self.sessionwriter.write(obj, **kw)
elif self.fh:
self.fh.write(str(obj))
if self.nobuffer:
self.fh.flush()

def close(self):
'''close output if not stdout'''
Expand Down Expand Up @@ -269,6 +274,8 @@ def alert(self, *args, **kw):
rec = self.parse(*args, **kw)
if rec:
self.fh.write(self.format % rec)
if self.nobuffer:
self.fh.flush()


class DBOutput(Output):
Expand Down

0 comments on commit cfbee9a

Please sign in to comment.