1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""A simple script to print the log in a readable format.
6
7Usage:   python tools/print_log.py <log_dir>
8Example: python tools/print_log.py tests/log/lumpy
9"""
10
11
12import glob
13import pickle
14import os
15import sys
16
17# Need to have this line because pickle needs firmware_log module to load logs.
18sys.path.append(os.getcwd())
19
20
21def _print_log(log_dir):
22    ext = '.log'
23    filenames = glob.glob(os.path.join(log_dir, '*.log'))
24    for filename in filenames:
25        print 'Printing %s ...' % filename
26        fw, date, glogs = pickle.load(open(filename))
27        prefix_spaces = ' ' * 2
28        print prefix_spaces + 'fw:   ', fw
29        print prefix_spaces + 'date: ', date
30        print prefix_spaces + 'glogs: '
31        for glog in glogs:
32            vlogs = glog.vlogs
33            if not vlogs:
34                continue
35
36            print prefix_spaces * 2 + '(%s %s)' % (glog.name, glog.variation)
37            for vlog in vlogs:
38                print prefix_spaces * 4 + '%s: ' % vlog.name
39                print prefix_spaces * 5 + 'score: %s' % str(vlog.score)
40                for metric in vlog.metrics:
41                    print (prefix_spaces * 5 + 'metric %s: %s' %
42                           (metric.name, metric.value))
43        print
44
45
46if __name__ == '__main__':
47    if len(sys.argv) != 2 or not os.path.exists(sys.argv[1]):
48        print 'Usage: python tools/%s <log_dir>' % sys.argv[0]
49        exit(1)
50    log_dir = sys.argv[1]
51    _print_log(log_dir)
52