]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/coding_tools/make_umid
setup: organize misc/ scripts and tools and remove obsolete ones
[tahoe-lafs/tahoe-lafs.git] / misc / coding_tools / make_umid
1 #!/usr/bin/env python
2
3 """Create a short probably-unique string for use as a umid= argument in a
4 Foolscap log() call, to make it easier to locate the source code that
5 generated the message. The main text of the log message is frequently
6 unhelpful for this, and python doesn't make it cheap to compile in the
7 filename and line number of logging calls.
8
9 Given a message-unique-ID like 'aXoWcA', make your logging call look like:
10
11  log.msg('OMG badness', level=log.WEIRD, umid='aXoWcA')
12
13 Then later, if this message actually occurs, you can grep your source tree
14 for aXoWcA to locate the code that caused it.
15
16 Just stick to the convention that 'umid=' is reserved for this job. It is a
17 good idea to make all the logging statements that could provoke an Incident
18 (i.e. those at level=log.WEIRD or higher) have umid= arguments, to make it
19 easier to write classifier functions for the incident-gatherer.
20
21 """
22
23 '''
24 The following elisp code may be useful:
25
26  (defun insert-umid ()
27    (interactive)
28    (insert ", umid=\"")
29    (call-process "make_umid" nil t)
30    (delete-char -1)
31    (insert "\"")
32  )
33  (global-set-key (kbd "C-\`") 'insert-umid)
34 '''
35
36 # '   # emacs gets confused by the odd number of single-quotes there
37
38 import os, base64, sys
39
40 def make_id():
41     while True:
42         m = os.urandom(4) # this gives 6-character message ids
43         m = base64.b64encode(m)
44         if "/" in m or "+" in m:
45             continue
46         m = m.replace("=", "")
47         break
48     return m
49
50 count = 1
51 if len(sys.argv) > 1:
52     count = int(sys.argv[1])
53 for i in range(count):
54     print make_id()
55