source: trunk/misc/coding_tools/make_umid

Last change on this file was b856238, checked in by Alexandre Detiste <alexandre.detiste@…>, at 2024-02-15T15:53:34Z

remove old Python2 future statements

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