40 import diagnostic_msgs.msg
42 import operator, tempfile, subprocess
66 file = self.
_stats[name][
'file_name']
74 if not self.
_stats.has_key(name):
76 return self.
_stats[name][
'file_name']
81 for (topic, msg, t)
in bag.read_messages():
86 if (
not (topic ==
'/diagnostics')):
87 print "Discarding message on topic: %s" % topic
90 t = time.localtime(float(str(msg.header.stamp)) / 1000000000.0)
92 for status
in msg.status:
95 if (
not self.
_stats.has_key(name)):
99 for index, s
in enumerate(status.values):
100 fields[s.key] = index
102 self.
_stats[name][
'fields'] = fields
104 self.
_stats[name][
'level'] = status.level
105 self.
_stats[name][
'message'] = status.message
106 self.
_stats[name][
'hardware_id'] = status.hardware_id
109 datafile, tmp_name = tempfile.mkstemp()
110 self.
_stats[name][
'data_file'] = os.fdopen(datafile,
'w')
111 self.
_stats[name][
'data_name'] = tmp_name
115 if (
not [s.key
for s
in status.values] == self.
_stats[name][
'fields'].keys()):
116 for s
in status.values:
117 if not self.
_stats[name][
'fields'].has_key(s.key):
118 self.
_stats[name][
'fields'][s.key] = len(self.
_stats[name][
'fields'])
124 for key, val
in self.
_stats[name][
'fields'].iteritems():
126 for s
in status.values:
127 vals[self.
_stats[name][
'fields'][s.key]] = s.value.replace(
'\n',
' ').replace(
',',
' ')
129 msg = status.message.replace(
',',
' ').strip()
130 hw_id = status.hardware_id.replace(
',',
' ')
132 self.
_stats[name][
'data_file'].write(
','.join([time.strftime(
"%Y/%m/%d %H:%M:%S", t)] +
133 [str(status.level), msg, hw_id] + vals) +
'\n')
139 field_dict = sorted(self.
_stats[name][
'fields'].iteritems(), key=operator.itemgetter(1))
140 fields = map(operator.itemgetter(0), field_dict)
142 header_line =
','.join([
'Timestamp'] + [
'Level',
'Message',
'Hardware ID'] +
143 [f.replace(
',',
'').replace(
'\n',
' ')
for f
in fields]) +
'\n' 145 file_name = os.path.join(self.
output_dir, name.replace(
' ',
'_').replace(
'(',
'').replace(
')',
'').replace(
'/',
'__').replace(
'.',
'').replace(
'#',
'') +
'.csv')
147 output_file = file(file_name,
'w')
148 output_file.write(header_line)
152 self.
_stats[name][
'data_file'].close()
155 subprocess.call(
"cat %s >> %s" % (self.
_stats[name][
'data_name'], file_name), shell=
True)
157 os.remove(self.
_stats[name][
'data_name'])
159 self.
_stats[name][
'file_name'] = file_name
def __init__(self, output_dir, logfile)
def remove_files(self)
Removes all output files.
def process_log(self)
Use rosrecord to play back bagfile.
Converts and processes diagnostics logs to CSV format.
def _update(self, topic, msg)
Creates and updates data files with new messages.
def finish_logfile(self)
Close logfile, append data to header.
def get_filename(self, name)
Return filename of output.