Commit 56d0b764 authored by Julien Muchembled's avatar Julien Muchembled

neolog: use argparse instead of optparse

parent 05e19861
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import bz2, gzip, errno, optparse, os, signal, sqlite3, sys, time import argparse, bz2, gzip, errno, os, signal, sqlite3, sys, time
from bisect import insort from bisect import insort
from logging import getLevelName from logging import getLevelName
from zlib import decompress from zlib import decompress
...@@ -225,38 +225,38 @@ def emit_many(log_list): ...@@ -225,38 +225,38 @@ def emit_many(log_list):
insort(event_list, (-event[0], next, emit, event)) insort(event_list, (-event[0], next, emit, event))
def main(): def main():
parser = optparse.OptionParser() parser = argparse.ArgumentParser(description='NEO Log Reader')
parser.add_option('-a', '--all', action="store_true", _ = parser.add_argument
_('-a', '--all', action="store_true",
help='decode body of packets') help='decode body of packets')
parser.add_option('-A', '--decompress', action="store_true", _('-A', '--decompress', action="store_true",
help='decompress data when decode body of packets (implies --all)') help='decompress data when decode body of packets (implies --all)')
parser.add_option('-d', '--date', metavar='FORMAT', _('-d', '--date', metavar='FORMAT',
help='custom date format, according to strftime(3)') help='custom date format, according to strftime(3)')
parser.add_option('-f', '--follow', action="store_true", _('-f', '--follow', action="store_true",
help='output appended data as the file grows') help='output appended data as the file grows')
parser.add_option('-F', '--flush', action="append", type="int", _('-F', '--flush', action="append", type=int, metavar='PID',
help='with -f, tell process PID to flush logs approximately N' help='with -f, tell process PID to flush logs approximately N'
' seconds (see -s)', metavar='PID') ' seconds (see -s)')
parser.add_option('-n', '--node', action="append", _('-n', '--node', action="append",
help='only show log entries from the given node' help='only show log entries from the given node'
' (only useful for logs produced by threaded tests),' ' (only useful for logs produced by threaded tests),'
" special value '-' hides the column") " special value '-' hides the column")
parser.add_option('-s', '--sleep-interval', type="float", default=1, _('-s', '--sleep-interval', type=float, default=1, metavar='N',
help='with -f, sleep for approximately N seconds (default 1.0)' help='with -f, sleep for approximately N seconds (default 1.0)'
' between iterations', metavar='N') ' between iterations')
parser.add_option('--from', dest='filter_from', _('--from', dest='filter_from', metavar='N',
help='show records more recent that timestamp N if N > 0,' help='show records more recent that timestamp N if N > 0, or now+N'
' or now+N if N < 0; N can also be a string that is' ' if N < 0; N can also be a string that is parseable by dateutil')
' parseable by dateutil ', metavar='N') _('file', nargs='+',
options, args = parser.parse_args() help='log file, compressed (gz, bz2 or xz) or not')
if options.sleep_interval <= 0: args = parser.parse_args()
if args.sleep_interval <= 0:
parser.error("sleep_interval must be positive") parser.error("sleep_interval must be positive")
if not args: filter_from = args.filter_from
parser.error("no log specified")
filter_from = options.filter_from
if filter_from: if filter_from:
try: try:
filter_from = float(options.filter_from) filter_from = float(args.filter_from)
except ValueError: except ValueError:
from dateutil.parser import parse from dateutil.parser import parse
x = parse(filter_from) x = parse(filter_from)
...@@ -267,24 +267,24 @@ def main(): ...@@ -267,24 +267,24 @@ def main():
else: else:
if filter_from < 0: if filter_from < 0:
filter_from += time.time() filter_from += time.time()
node_list = options.node or [] node_list = args.node or []
try: try:
node_list.remove('-') node_list.remove('-')
node_column = False node_column = False
except ValueError: except ValueError:
node_column = True node_column = True
log_list = [Log(db_path, log_list = [Log(db_path,
2 if options.decompress else 1 if options.all else 0, 2 if args.decompress else 1 if args.all else 0,
options.date, filter_from, node_column, node_list) args.date, filter_from, node_column, node_list)
for db_path in args] for db_path in args.file]
if options.follow: if args.follow:
try: try:
pid_list = options.flush or () pid_list = args.flush or ()
while True: while True:
emit_many(log_list) emit_many(log_list)
for pid in pid_list: for pid in pid_list:
os.kill(pid, signal.SIGRTMIN) os.kill(pid, signal.SIGRTMIN)
time.sleep(options.sleep_interval) time.sleep(args.sleep_interval)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
else: else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment