blob: 7f0a231cdc99c3a6861468afeb92c4c2dc08e228 [file] [log] [blame]
Scott Baker8b725f52019-06-11 16:14:39 -07001# 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
15from xosconfig import Config
16from multistructlog import create_logger
17
18log = create_logger(Config().get('logging'))
19
20squelched_messages = {}
21
22
23def 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