Scott Baker | 8b725f5 | 2019-06-11 16:14:39 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from xosconfig import Config |
| 16 | from multistructlog import create_logger |
| 17 | |
| 18 | log = create_logger(Config().get('logging')) |
| 19 | |
| 20 | squelched_messages = {} |
| 21 | |
| 22 | |
| 23 | def debug_once(msg, *args, **kwargs): |
| 24 | """ Output a given debug message only once. If we see the same messag |
| 25 | again, then ignore it. |
| 26 | """ |
| 27 | count = squelched_messages.get(msg, 0) |
| 28 | |
| 29 | if count == 0: |
| 30 | # This is the first time we've seen this message. |
| 31 | log.debug(msg, *args, **kwargs) |
| 32 | elif count == 1: |
| 33 | # We've seen a duplicate. Let the user know we're supressing. |
| 34 | log.debug("[Further messages suppressed] " + msg, *args, **kwargs) |
| 35 | |
| 36 | squelched_messages[msg] = count + 1 |