diff -r cdb6403c2498 -r 8b50694f841e log_search.py --- a/log_search.py Mon Feb 09 11:05:53 2009 +0200 +++ b/log_search.py Mon Feb 09 11:46:17 2009 +0200 @@ -37,7 +37,7 @@ mode_to_flag = { 'r': hype.Database.DBREADER, 'w': hype.Database.DBREADER | hype.Database.DBWRITER | hype.Database.DBCREAT, - 'a': hype.Database.DBREADER | hype.Database.DBWRITER, + 'a': hype.Database.DBREADER | hype.Database.DBWRITER | hype.Database.DBCREAT, '*': hype.Database.DBREADER | hype.Database.DBWRITER | hype.Database.DBCREAT | hype.Database.DBTRUNC, } @@ -49,7 +49,7 @@ # open if not self.db.open(path, flags) : - raise Exception("Index open failed: %s" % (path, )) + raise Exception("Index open failed: %s, mode=%s, flags=%#06x: %s" % (path, mode, flags, self.db.err_msg(self.db.error()))) def insert (self, channel, lines) : """ @@ -120,7 +120,7 @@ type = int(doc.attr('@type')) timestamp = datetime.datetime.fromtimestamp(int(doc.attr('@timestamp')), pytz.utc) source = doc.attr('@source') - data = doc.cat_texts() + data = doc.cat_texts().decode('utf8') # build+yield to (channel_id, LogLine) tuple yield (channel_id, log_line.LogLine(None, type, timestamp, source, data)) @@ -151,76 +151,3 @@ yield line -def cmd_load (options, channel_name, date) : - """ - Loads the logs for a specific channel/date into the index - """ - - import channels - - # open the LogSearchIndex - index = LogSearchIndex(options.index_path, '*' if options.create_index else 'a') - - # open the channel - channel = channels.channel_list.lookup(channel_name) - - # parse date - date = datetime.datetime.strptime(date, '%Y-%m-%d').replace(tzinfo=channel.source.tz) - - # load lines for date - lines = channel.source.get_date(date) - - # insert - index.insert(channel, lines) - -def cmd_search (options, channel_name, query) : - """ - Search the index for events on a specific channel with the given query - """ - - import channels - - # open the LogSearchIndex - index = LogSearchIndex(options.index_path, '*' if options.create_index else 'a') - - # open the channel - channel = channels.channel_list.lookup(channel_name) - - # search - lines = index.search_simple(channel, query) - - # display as plaintext - for line in options.formatter.format_txt(lines) : - print line - -if __name__ == '__main__' : - from optparse import OptionParser - import log_formatter - - # define parser - parser = OptionParser( - usage = "%prog [options] [ ... ]", - add_help_option = True, - ) - - # define command-line arguments - parser.add_option("-I", "--index", dest="index_path", help="Index database path", metavar="PATH", default="logs/index") - parser.add_option("--create", dest="create_index", help="Create index database", default=False) - parser.add_option("-f", "--formatter", dest="formatter_name", help="LogFormatter to use", default="irssi") - parser.add_option("-z", "--timezone", dest="tz_name", help="Timezone for output", metavar="TZ", default="UTC") - - # parse - options, args = parser.parse_args() - - # postprocess stuff - options.tz = pytz.timezone(options.tz_name) - options.formatter = log_formatter.by_name(options.formatter_name)(options.tz) - - # pop command - command = args.pop(0) - - # inspect - func = globals()['cmd_%s' % command] - - # call - func(options, *args)