Newer
Older

Ramin Yaghoubzadeh Torky
committed
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of IPAACA, the
# "Incremental Processing Architecture
# for Artificial Conversational Agents".
#
# Copyright (c) 2009-2015 Social Cognitive Systems Group
# CITEC, Bielefeld University
#
# http://opensource.cit-ec.de/projects/ipaaca/
# http://purl.org/net/ipaaca
#
# This file may be licensed under the terms of of the
# GNU Lesser General Public License Version 3 (the ``LGPL''),
# or (at your option) any later version.
#
# Software distributed under the License is distributed
# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
# express or implied. See the LGPL for the specific language
# governing rights and limitations.
#
# You should have received a copy of the LGPL along with this
# program. If not, go to http://www.gnu.org/licenses/lgpl.html
# or write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# The development of this software was supported by the
# Excellence Cluster EXC 277 Cognitive Interaction Technology.
# The Excellence Cluster EXC 277 is a grant of the Deutsche
# Forschungsgemeinschaft (DFG) in the context of the German
# Excellence Initiative.
from __future__ import division, print_function
import sys
import ipaaca

Ramin Yaghoubzadeh Torky
committed
import io

Ramin Yaghoubzadeh Torky
committed

Ramin Yaghoubzadeh Torky
committed
def pretty_printed_time(t):
if t<0:
t = -t
sign = '-'
else:
sign = ' '
s = float(t)
h = int(s/3600)
m = int(s/60) - h*60
s -= h*3600 + m*60
ms = int((s-int(s))*1000)
return sign+'%01d:%02d:%02d.%03d'%(h, m, int(s), ms)

Ramin Yaghoubzadeh Torky
committed
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
REPLACEMENT_COLORS={
'30': 'black',
'31': 'red',
'32': 'green',
'33': 'cyan',
'34': 'blue',
'35': 'brown',
'36': 'magenta',
'37': 'grey',
'248': 'lightgray',
# should add more colors
}
def replace_ansi_html(s):
r = u''
in_esc = False
last_color = u''
#skip_one_double_wide_bar = False
for c in s:
if in_esc:
if c=='m':
in_esc = False
itms = last_color.replace('[','').replace(']','').split(';')
col = None
bold = False
if itms[0]=='':
r += '</code></font><code>'
else:
for i in itms:
if i=='1':
bold = True
elif i in REPLACEMENT_COLORS:
col = REPLACEMENT_COLORS[i]
if col is None:
if bold:
col = 'grey'
else:
col = 'black'
print('Warning: unknown colors '+str(itms))
r += '</code><font color="'+col+'"><code>'
else:
last_color += c
else:
#if c in u'▁▂▃▄▅▆▇█':
# if skip_one_double_wide_bar:
# skip_one_double_wide_bar = False
# else:
# r += c
# skip_one_double_wide_bar = True
#el
if c=='':
in_esc = True
last_color = ''
else:
r += c
return r
def print_header(formt, fields):
s = u''
if formt=='html':
s += '<html><head><meta charset="UTF-8"><title>flexdiam log view</title></head>\n<body>\n<table width="100%" border="1" bordercolor="lightgray" style="white-space:pre">\n<tr>'
for f in fields:
s+='<th>'+f+'</th>'
s += '</tr>'
return s
def print_footer(formt):
s = u''
if formt=='html':
s += '</tr>\n</body>\n</html>'
return s
def print_record(data, delimiter, formt):
if formt=='html':
s = u'<tr>'
for d in data:
d2 = d.replace('<', '<').replace('>', '>').replace('\n', '<br/>').replace('"', '"')
d3 = replace_ansi_html(d2)
#s += u'<td><code>' + d2.replace('<', '<').replace('>', '>').replace('\n', '<br/>') + u'</code></td>'
s += u'<td><code>' + d3 + u'</code></td>'
s += u'</tr>'
return s
else:
return delimiter.join(data)

Ramin Yaghoubzadeh Torky
committed
def modify(key, value, strip=False, pretty_printed_times=False, time_offset=0.0):
if key=='time':
f = float(value.strip()) - time_offset
return pretty_printed_time(f) if pretty_printed_times else str(f)
else:
return value.strip() if strip else value

Ramin Yaghoubzadeh Torky
committed
if __name__=='__main__':
try:
iap = ipaaca.IpaacaArgumentParser('ipaaca-logcat')
iap.add_argument(
'-s', '--strip-fields',
dest='strip', action='store_true',
default=False,

Ramin Yaghoubzadeh Torky
committed
help='Strip leading/trailing whitespace from all fields')
iap.add_argument(
'-p', '--pretty-printed-times',
dest='pretty_printed_times', action='store_true',
default=False,
help='Print human-readable times (hh:mm:ss.ms) instead of float seconds')

Ramin Yaghoubzadeh Torky
committed
iap.add_argument(
'--format',
dest='format', nargs=1,
default=['plain'],
help='output format (plain, html) (default plain)')
iap.add_argument(
'-o', '--output-file',
dest='output_file', nargs=1,
default=['-'],
help='output file name (default \'-\' -> standard terminal output)')
iap.add_argument(
'-d', '--delimiter',
dest='delimiter', nargs=1,
default=['\t'],
help='field delimiter, interpreted as python unicode string (default \'\\t\')')

Ramin Yaghoubzadeh Torky
committed
iap.add_argument(
'-t', '--align-times',
dest='align_times', nargs=2,
default=['0', '0'],
help='Calculate relative output timestamps (default: 0 0 => keep)\nFirst argument is a reference event timestamp from the log file\nSecond argument is the new output time for that same event')
iap.add_argument(
'-f', '--fields',
dest='fields', default=['time', 'text'], nargs='+',
help='fields to print (defaults: \'time\' \'text\')')
arguments = iap.parse_args()
delimiter = eval("u'"+arguments.delimiter[0]+"'", {"__builtins__":None}, {} )

Ramin Yaghoubzadeh Torky
committed
ref_t, out_t = float(arguments.align_times[0]), float(arguments.align_times[1])
time_offset = ref_t - out_t
#print(arguments); sys.exit(1)

Ramin Yaghoubzadeh Torky
committed
#modify = (lambda s: s.strip()) if arguments.strip else (lambda s: s)
#modify = lambda s: type(s).__name__

Ramin Yaghoubzadeh Torky
committed
fil = sys.stdout
if arguments.output_file[0] != '-':
fil = io.open(arguments.output_file[0], 'w', encoding='utf8')
fil.write(print_header(arguments.format[0], arguments.fields)+'\n')
for line in sys.stdin:
record = eval(line.strip(), {"__builtins__":None}, {} )

Ramin Yaghoubzadeh Torky
committed
data = [modify(f, unicode(record[f]), arguments.strip, arguments.pretty_printed_times, time_offset) for f in arguments.fields]
u = print_record(data, delimiter, arguments.format[0])
res = u'{}'.format(u) #.decode('utf-8')
fil.write(u''+res+'\n' )
fil.write(print_footer(arguments.format[0])+'\n')
if fil != sys.stdout: fil.close()
except (KeyboardInterrupt, SystemExit):
pass # ret below
except Exception, e:
print(u'Exception: '+unicode(traceback.format_exc()))
ipaaca.exit(1)
ipaaca.exit(0)