1# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Common utilites used by cygprofile scripts.
6"""
7
8import logging
9
10
11class WarningCollector(object):
12  """Collects warnings, but limits the number printed to a set value."""
13  def __init__(self, max_warnings, level=logging.WARNING):
14    self._warnings = 0
15    self._max_warnings = max_warnings
16    self._level = level
17
18  def Write(self, message):
19    """Prints a warning if fewer than max_warnings have already been printed."""
20    if self._warnings < self._max_warnings:
21      logging.log(self._level, message)
22    self._warnings += 1
23
24  def WriteEnd(self, message):
25    """Once all warnings have been printed, use this to print the number of
26    elided warnings."""
27    if self._warnings > self._max_warnings:
28      logging.log(self._level, '%d more warnings for: %s' % (
29          self._warnings - self._max_warnings, message))
30