/ scripts / decode_dmesg
decode_dmesg
 1  #!/usr/bin/env python
 2  """\
 3  This script decodes hex floats (e.g., in dmesg) into real floats.  In the
 4  future maybe it will do other emc2-specific dmesg decoding.
 5  
 6  Usage:
 7  	dmesg | decode_dmesg
 8      cat /var/log/kern.log | decode_dmesg > output.txt
 9  """
10  
11  import re
12  import sys
13  
14  def repl(m):
15      s, w, f, e = m.group("s", "w", "f", "e")
16      f = f or "0"
17      v = long(w, 16) + long(f, 16) * (16 ** -len(f))
18      try:
19          v *= 2.0**long(e)
20      except OverflowError:
21          v = float("inf")
22      if s: v = -v
23      return "%g" % v
24  
25  pat = re.compile('(?i)(?P<s>-?)0[xX]'
26          '(?P<w>[0-9A-Fa-f])'
27          '(\\.(?P<f>[0-9A-Fa-f]*))?[Pp](?P<e>[+-]?[0-9]+)')
28  
29  for line in sys.stdin:
30      line = pat.sub(repl, line)
31      sys.stdout.write(line)
32  
33  #    Copyright 2010 Jeff Epler <jepler@unpythonic.net>
34  #
35  #    This program is free software; you can redistribute it and/or modify
36  #    it under the terms of the GNU General Public License as published by
37  #    the Free Software Foundation; either version 2 of the License, or
38  #    (at your option) any later version.
39  #
40  #    This program is distributed in the hope that it will be useful,
41  #    but WITHOUT ANY WARRANTY; without even the implied warranty of
42  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43  #    GNU General Public License for more details.
44  #
45  #    You should have received a copy of the GNU General Public License
46  #    along with this program; if not, write to the Free Software
47  #    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.