|
5 | 5 | import os |
6 | 6 | import re |
7 | 7 | import sys |
| 8 | +import time |
8 | 9 | import weakref |
9 | 10 | import logging |
10 | 11 | import ctypes.util |
|
18 | 19 | logger.setLevel(logging.WARNING) |
19 | 20 |
|
20 | 21 |
|
21 | | -err_hashes = {} |
| 22 | +err_hashes = {} # hash -> [short-message, count, next-time] |
22 | 23 |
|
23 | 24 | _re_wgpu_ob = re.compile(r"`<[a-z|A-Z]+-\([0-9]+, [0-9]+, [a-z|A-Z]+\)>`") |
24 | 25 |
|
@@ -47,16 +48,31 @@ def log_exception(kind): |
47 | 48 | msg = str(err) |
48 | 49 | msgh = error_message_hash(msg) |
49 | 50 | if msgh not in err_hashes: |
| 51 | + # Prepare a short variant of the message for later use |
| 52 | + short_msg = kind + ": " + msg.split("\n")[0].strip() |
| 53 | + short_msg = short_msg if len(short_msg) <= 70 else short_msg[:69] + "…" |
| 54 | + err_hashes[msgh] = [short_msg, 1, 0] |
50 | 55 | # Provide the exception, so the default logger prints a stacktrace. |
51 | 56 | # IDE's can get the exception from the root logger for PM debugging. |
52 | | - err_hashes[msgh] = 1 |
53 | 57 | logger.error(kind, exc_info=err) |
54 | 58 | else: |
55 | 59 | # We've seen this message before, return a one-liner instead. |
56 | | - err_hashes[msgh] = count = err_hashes[msgh] + 1 |
57 | | - msg = kind + ": " + msg.split("\n")[0].strip() |
58 | | - msg = msg if len(msg) <= 70 else msg[:69] + "…" |
59 | | - logger.error(msg + f" ({count})") |
| 60 | + short_count_tm = err_hashes[msgh] |
| 61 | + short, count, tm = short_count_tm |
| 62 | + short_count_tm[1] = count = count + 1 |
| 63 | + # Show the message now? |
| 64 | + show_message = False |
| 65 | + cur_time = time.perf_counter() |
| 66 | + if count <= 5: |
| 67 | + show_message = True |
| 68 | + else: |
| 69 | + if cur_time > tm: |
| 70 | + show_message = True |
| 71 | + # Log the messages and schedule when to show it next. |
| 72 | + # Next message is after 1-3 seconds (3 when count reaches 300). |
| 73 | + if show_message: |
| 74 | + short_count_tm[2] = cur_time + min(max(count / 100, 1), 3) |
| 75 | + logger.error(f"{short} ({count})") |
60 | 76 |
|
61 | 77 |
|
62 | 78 | # %% Weak bindings |
|
0 commit comments