[{"id":793,"web_url":"https://patchwork.libcamera.org/comment/793/","msgid":"<c42e14b9-f0a5-0e55-f6fe-b3b223999fce@ideasonboard.com>","date":"2019-02-13T08:17:32","subject":"Re: [libcamera-devel] [PATCH 2/2] utils: checkstyle: Catch LOG()\n\tusage without an explicit category","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Hi Laurent,\n\nOn 12/02/2019 22:25, Laurent Pinchart wrote:\n> Add support for checkers not related to code formatting to the\n> checkstyle.py script, and create a first checker that catches usage of\n> the LOG() macro without an explicit category.\n\nAh, now I see why you said it was easy to add extra checkers.\n\nIt's easy when you know the architecture of the script at least :)\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  utils/checkstyle.py | 55 ++++++++++++++++++++++++++++++++++++++-------\n>  1 file changed, 47 insertions(+), 8 deletions(-)\n> \n> diff --git a/utils/checkstyle.py b/utils/checkstyle.py\n> index f3005d1fbb52..9abd2687b1f9 100755\n> --- a/utils/checkstyle.py\n> +++ b/utils/checkstyle.py\n> @@ -146,7 +146,7 @@ class DiffHunk(object):\n>              s += Colours.reset()\n>              s += '\\n'\n>  \n> -        return s\n> +        return s[:-1]\n\nI think this chops off the last character in s. Why are you doing that ?\n(especially as without seeing the rest of the context, you've only\n'just' added a '\\n'\n\nOr is that it - to remove the last \\n ?\n\n\nAha ok it clearly is to remove the \\n.\n\n>      def append(self, line):\n>          if line[0] == ' ':\n> @@ -229,6 +229,29 @@ available_formatters = {\n>  # Style checking\n>  #\n>  \n> +class LogCategoryChecker(object):\n> +    log_regex = re.compile('\\\\bLOG\\((Debug|Info|Warning|Error|Fatal)\\)')\n> +\n> +    def __init__(self, content):\n> +        self.__content = content\n> +\n> +    def check(self, line_numbers):\n> +        issues = []\n> +        for line_number in line_numbers:\n> +            line = self.__content[line_number-1]\n> +            if not LogCategoryChecker.log_regex.search(line):\n> +                continue\n> +\n> +            issues.append([line_number, line, 'LOG() should use categories'])\n> +\n> +        return issues\n> +\n> +\n> +available_checkers = {\n> +    'log_category': LogCategoryChecker,\n> +}\n> +\n> +\n>  def check_file(top_level, commit, filename, formatters):\n>      # Extract the line numbers touched by the commit.\n>      diff = subprocess.run(['git', 'diff', '%s~..%s' % (commit, commit), '--',\n> @@ -260,20 +283,36 @@ def check_file(top_level, commit, filename, formatters):\n>      formatted = formatted.splitlines(True)\n>      diff = difflib.unified_diff(after, formatted)\n>  \n> -    # Split the diff in hunks, recording line number ranges for each hunk.\n> +    # Split the diff in hunks, recording line number ranges for each hunk, and\n> +    # filter out hunks that are not touched by the commit.\n>      formatted_diff = parse_diff(diff)\n> -\n> -    # Filter out hunks that are not touched by the commit.\n>      formatted_diff = [hunk for hunk in formatted_diff if hunk.intersects(lines)]\n> -    if len(formatted_diff) == 0:\n> +\n> +    # Check for code issues not related to formatting.\n> +    issues = []\n> +    for checker in available_checkers:\n> +        checker = available_checkers[checker](after)\n> +        for hunk in commit_diff:\n> +            issues += checker.check(hunk.side('to').touched)\n> +\n> +    # Print the detected issues.\n> +    if len(issues) == 0 and len(formatted_diff) == 0:\n>          return 0\n>  \n>      print('%s---' % Colours.fg(Colours.Red), filename)\n>      print('%s+++' % Colours.fg(Colours.Green), filename)\n\nInterestingly, these both print red and green respectively...\n\n> -    for hunk in formatted_diff:\n> -        print(hunk)\n>  \n> -    return len(formatted_diff)\n> +    if len(formatted_diff):\n> +        for hunk in formatted_diff:\n> +            print(hunk)\n> +\n> +    if len(issues):\n> +        issues.sort()\n> +        for issue in issues:\n> +            print('%s#%u: %s' % (Colours.fg(Colours.Yellow), issue[0], issue[2]))\n\nBut this looks distinctly brown or is it orange ...\nI wonder if that's due to my shell perhaps.\n\n> +            print('+%s%s' % (issue[1].rstrip(), Colours.reset()))\n> +\n> +    return len(formatted_diff) + len(issues)\n>  \n>  \n>  def check_style(top_level, commit, formatters):\n>","headers":{"Return-Path":"<kieran.bingham@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3859C610B1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 13 Feb 2019 09:17:36 +0100 (CET)","from [192.168.0.21]\n\t(cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 81D9685;\n\tWed, 13 Feb 2019 09:17:35 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1550045855;\n\tbh=vlVFLw5cS1ibBRbvM9rY0wQo9dtZ/lE2vmvBoyj+I0c=;\n\th=Reply-To:Subject:To:References:From:Date:In-Reply-To:From;\n\tb=Nbf2BMxnBHz8D99E25zYGfXG7cpuqblhLS1L5Zhk4Jzk3DgpzPcJIBOdFa8RI3USX\n\t65ofKvi94KYfijfC8gNG7LP7s/EVj3BMN4NbZeld1gTvdC2/rCILKhw4aONUAVOhlg\n\tb7bQ6SpFV+GVRoKmY8CqEhku36W2+1qsjTAJVIH0=","Reply-To":"kieran.bingham@ideasonboard.com","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","References":"<20190212222557.8898-1-laurent.pinchart@ideasonboard.com>\n\t<20190212222557.8898-2-laurent.pinchart@ideasonboard.com>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Openpgp":"preference=signencrypt","Autocrypt":"addr=kieran.bingham@ideasonboard.com; keydata=\n\tmQINBFYE/WYBEACs1PwjMD9rgCu1hlIiUA1AXR4rv2v+BCLUq//vrX5S5bjzxKAryRf0uHat\n\tV/zwz6hiDrZuHUACDB7X8OaQcwhLaVlq6byfoBr25+hbZG7G3+5EUl9cQ7dQEdvNj6V6y/SC\n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CIECWYem6pLwaw6IAL7oX+tEqIMPph/G/jwZcdS6Hkyt/esHPuHNwX4guqTbVEuRqbDzDI\n\t2DJO5FbxABEBAAGJAiUEGAEKAA8CGwwFAlnDlGsFCQeA/gIACgkQoR5GchCkYf1yYRAAq+Yo\n\tnbf9DGdK1kTAm2RTFg+w9oOp2Xjqfhds2PAhFFvrHQg1XfQR/UF/SjeUmaOmLSczM0s6XMeO\n\tVcE77UFtJ/+hLo4PRFKm5X1Pcar6g5m4xGqa+Xfzi9tRkwC29KMCoQOag1BhHChgqYaUH3yo\n\tUzaPwT/fY75iVI+yD0ih/e6j8qYvP8pvGwMQfrmN9YB0zB39YzCSdaUaNrWGD3iCBxg6lwSO\n\tLKeRhxxfiXCIYEf3vwOsP3YMx2JkD5doseXmWBGW1U0T/oJF+DVfKB6mv5UfsTzpVhJRgee7\n\t4jkjqFq4qsUGxcvF2xtRkfHFpZDbRgRlVmiWkqDkT4qMA+4q1y/dWwshSKi/uwVZNycuLsz+\n\t+OD8xPNCsMTqeUkAKfbD8xW4LCay3r/dD2ckoxRxtMD9eOAyu5wYzo/ydIPTh1QEj9SYyvp8\n\tO0g6CpxEwyHUQtF5oh15O018z3ZLztFJKR3RD42VKVsrnNDKnoY0f4U0z7eJv2NeF8xHMuiU\n\tRCIzqxX1GVYaNkKTnb/Qja8hnYnkUzY1Lc+OtwiGmXTwYsPZjjAaDX35J/RSKAoy5wGo/YFA\n\tJxB1gWThL4kOTbsqqXj9GLcyOImkW0lJGGR3o/fV91Zh63S5TKnf2YGGGzxki+ADdxVQAm+Q\n\tsbsRB8KNNvVXBOVNwko86rQqF9drZuw=","Organization":"Ideas on Board","Message-ID":"<c42e14b9-f0a5-0e55-f6fe-b3b223999fce@ideasonboard.com>","Date":"Wed, 13 Feb 2019 08:17:32 +0000","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101\n\tThunderbird/60.4.0","MIME-Version":"1.0","In-Reply-To":"<20190212222557.8898-2-laurent.pinchart@ideasonboard.com>","Content-Type":"text/plain; charset=utf-8","Content-Language":"en-US","Content-Transfer-Encoding":"8bit","Subject":"Re: [libcamera-devel] [PATCH 2/2] utils: checkstyle: Catch LOG()\n\tusage without an explicit category","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Wed, 13 Feb 2019 08:17:36 -0000"}},{"id":796,"web_url":"https://patchwork.libcamera.org/comment/796/","msgid":"<20190213102935.GL31044@bigcity.dyn.berto.se>","date":"2019-02-13T10:29:35","subject":"Re: [libcamera-devel] [PATCH 2/2] utils: checkstyle: Catch LOG()\n\tusage without an explicit category","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Laurent,\n\nThanks for your patch.\n\nOn 2019-02-13 00:25:57 +0200, Laurent Pinchart wrote:\n> Add support for checkers not related to code formatting to the\n> checkstyle.py script, and create a first checker that catches usage of\n> the LOG() macro without an explicit category.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nWith Kierans explanation on the change in DiffHunk\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\nI like how we can add checks like this, nice work.\n\n> ---\n>  utils/checkstyle.py | 55 ++++++++++++++++++++++++++++++++++++++-------\n>  1 file changed, 47 insertions(+), 8 deletions(-)\n> \n> diff --git a/utils/checkstyle.py b/utils/checkstyle.py\n> index f3005d1fbb52..9abd2687b1f9 100755\n> --- a/utils/checkstyle.py\n> +++ b/utils/checkstyle.py\n> @@ -146,7 +146,7 @@ class DiffHunk(object):\n>              s += Colours.reset()\n>              s += '\\n'\n>  \n> -        return s\n> +        return s[:-1]\n>  \n>      def append(self, line):\n>          if line[0] == ' ':\n> @@ -229,6 +229,29 @@ available_formatters = {\n>  # Style checking\n>  #\n>  \n> +class LogCategoryChecker(object):\n> +    log_regex = re.compile('\\\\bLOG\\((Debug|Info|Warning|Error|Fatal)\\)')\n> +\n> +    def __init__(self, content):\n> +        self.__content = content\n> +\n> +    def check(self, line_numbers):\n> +        issues = []\n> +        for line_number in line_numbers:\n> +            line = self.__content[line_number-1]\n> +            if not LogCategoryChecker.log_regex.search(line):\n> +                continue\n> +\n> +            issues.append([line_number, line, 'LOG() should use categories'])\n> +\n> +        return issues\n> +\n> +\n> +available_checkers = {\n> +    'log_category': LogCategoryChecker,\n> +}\n> +\n> +\n>  def check_file(top_level, commit, filename, formatters):\n>      # Extract the line numbers touched by the commit.\n>      diff = subprocess.run(['git', 'diff', '%s~..%s' % (commit, commit), '--',\n> @@ -260,20 +283,36 @@ def check_file(top_level, commit, filename, formatters):\n>      formatted = formatted.splitlines(True)\n>      diff = difflib.unified_diff(after, formatted)\n>  \n> -    # Split the diff in hunks, recording line number ranges for each hunk.\n> +    # Split the diff in hunks, recording line number ranges for each hunk, and\n> +    # filter out hunks that are not touched by the commit.\n>      formatted_diff = parse_diff(diff)\n> -\n> -    # Filter out hunks that are not touched by the commit.\n>      formatted_diff = [hunk for hunk in formatted_diff if hunk.intersects(lines)]\n> -    if len(formatted_diff) == 0:\n> +\n> +    # Check for code issues not related to formatting.\n> +    issues = []\n> +    for checker in available_checkers:\n> +        checker = available_checkers[checker](after)\n> +        for hunk in commit_diff:\n> +            issues += checker.check(hunk.side('to').touched)\n> +\n> +    # Print the detected issues.\n> +    if len(issues) == 0 and len(formatted_diff) == 0:\n>          return 0\n>  \n>      print('%s---' % Colours.fg(Colours.Red), filename)\n>      print('%s+++' % Colours.fg(Colours.Green), filename)\n> -    for hunk in formatted_diff:\n> -        print(hunk)\n>  \n> -    return len(formatted_diff)\n> +    if len(formatted_diff):\n> +        for hunk in formatted_diff:\n> +            print(hunk)\n> +\n> +    if len(issues):\n> +        issues.sort()\n> +        for issue in issues:\n> +            print('%s#%u: %s' % (Colours.fg(Colours.Yellow), issue[0], issue[2]))\n> +            print('+%s%s' % (issue[1].rstrip(), Colours.reset()))\n> +\n> +    return len(formatted_diff) + len(issues)\n>  \n>  \n>  def check_style(top_level, commit, formatters):\n> -- \n> Regards,\n> \n> Laurent Pinchart\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from mail-lj1-x241.google.com (mail-lj1-x241.google.com\n\t[IPv6:2a00:1450:4864:20::241])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B1B89610B1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 13 Feb 2019 11:29:37 +0100 (CET)","by mail-lj1-x241.google.com with SMTP id j19so739703ljg.5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 13 Feb 2019 02:29:37 -0800 (PST)","from localhost (89-233-230-99.cust.bredband2.com. [89.233.230.99])\n\tby smtp.gmail.com with ESMTPSA id\n\t67-v6sm3359500ljc.26.2019.02.13.02.29.36\n\t(version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256);\n\tWed, 13 Feb 2019 02:29:36 -0800 (PST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to\n\t:user-agent; bh=zdJ0UW2iNFDmyB13Y61lW/+HVv0qEONTy4CLr4C642k=;\n\tb=RreHxjfuBMSKU3XtwEeEMJFEnKhnizClGNHa7hSCrdRl/8ST+Ce2BwJdwUEI6+42fN\n\t7mwCtkU4fPl5W/Ja0RZtGtODnOQwojsEEO4/Fi5l8xSmY7pFVSCaFWz+A2hCgiYQVlLh\n\to09/J4HafmJbdE+q/1NWxoC20kqZybFfkMqtlMeo2jw40vGU5wQXp67MD7cdSdD3aHrU\n\tw/DO9bVZsOczlBdMYKBgNq/hlYfFLNgI27ZS7SmN9TqOEhExvfT3kBthB74lxXot2goe\n\t4reLjeSC5kP6E6rj2jhuPwJMpdgHCpKGdxj8sLJPXEORd76aoWC9JRMagCdzMhsdvWRz\n\tei9A==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to:user-agent;\n\tbh=zdJ0UW2iNFDmyB13Y61lW/+HVv0qEONTy4CLr4C642k=;\n\tb=S4TRKxORsQ7j5bqbrGZW80aDQmUqSitP/TpYUu93kES+demKRpXW1Fs7ACviQCt4JV\n\t4qmlGKAcEwYWoDJbWY1KDRfiqJ9wzPEC6DXr6vG4TX7MF27oJ6x0pOZhqJ5XMBsqCbhE\n\tOhknGyl+yWeKeUoaUF9i0YsiJNPDR+LmEp1Eiwnx4HKE4Bkag5Y28SeRxmdfbxIhQRos\n\tHKBDKPKQ9QofGn13WBGoTwPTZYzyWZFM7o09D/BLb8U2c8L0CZqy2MAQsuQa2X4x3Nl6\n\t35u+DsI3Mf1ycpDq+NJiXjDr4y9UmeKAlvroHUiITQrwpCGswO6co34CoTAr9CLMn2xy\n\tC9zw==","X-Gm-Message-State":"AHQUAuZg0dJZDeE1NT9fAc3hcSa0dZVausSCoE6RSjgY3N8WjSw4uXvs\n\teh2L+zCPAZrdinWUe+N13wKO4A==","X-Google-Smtp-Source":"AHgI3IY6W7IoeD1DF/nldu6E7HpEIRSSiQ9rXYR3bguy9trdW/LpkEnNUProZUUwG6OIIyQfaYyynQ==","X-Received":"by 2002:a2e:8105:: with SMTP id\n\td5-v6mr1306171ljg.76.1550053776782; \n\tWed, 13 Feb 2019 02:29:36 -0800 (PST)","Date":"Wed, 13 Feb 2019 11:29:35 +0100","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190213102935.GL31044@bigcity.dyn.berto.se>","References":"<20190212222557.8898-1-laurent.pinchart@ideasonboard.com>\n\t<20190212222557.8898-2-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=iso-8859-1","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20190212222557.8898-2-laurent.pinchart@ideasonboard.com>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH 2/2] utils: checkstyle: Catch LOG()\n\tusage without an explicit category","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Wed, 13 Feb 2019 10:29:38 -0000"}},{"id":805,"web_url":"https://patchwork.libcamera.org/comment/805/","msgid":"<20190213110844.GA5332@pendragon.ideasonboard.com>","date":"2019-02-13T11:08:44","subject":"Re: [libcamera-devel] [PATCH 2/2] utils: checkstyle: Catch LOG()\n\tusage without an explicit category","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Kieran,\n\nOn Wed, Feb 13, 2019 at 08:17:32AM +0000, Kieran Bingham wrote:\n> On 12/02/2019 22:25, Laurent Pinchart wrote:\n> > Add support for checkers not related to code formatting to the\n> > checkstyle.py script, and create a first checker that catches usage of\n> > the LOG() macro without an explicit category.\n> \n> Ah, now I see why you said it was easy to add extra checkers.\n> \n> It's easy when you know the architecture of the script at least :)\n\nFeel free to get yourself familiar with it too ;-)\n\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> > ---\n> >  utils/checkstyle.py | 55 ++++++++++++++++++++++++++++++++++++++-------\n> >  1 file changed, 47 insertions(+), 8 deletions(-)\n> > \n> > diff --git a/utils/checkstyle.py b/utils/checkstyle.py\n> > index f3005d1fbb52..9abd2687b1f9 100755\n> > --- a/utils/checkstyle.py\n> > +++ b/utils/checkstyle.py\n> > @@ -146,7 +146,7 @@ class DiffHunk(object):\n> >              s += Colours.reset()\n> >              s += '\\n'\n> >  \n> > -        return s\n> > +        return s[:-1]\n> \n> I think this chops off the last character in s. Why are you doing that ?\n> (especially as without seeing the rest of the context, you've only\n> 'just' added a '\\n'\n> \n> Or is that it - to remove the last \\n ?\n> \n> \n> Aha ok it clearly is to remove the \\n.\n\nYes, as the string is printed with the print() function, an extra \\n is\nadded.\n\n> >      def append(self, line):\n> >          if line[0] == ' ':\n> > @@ -229,6 +229,29 @@ available_formatters = {\n> >  # Style checking\n> >  #\n> >  \n> > +class LogCategoryChecker(object):\n> > +    log_regex = re.compile('\\\\bLOG\\((Debug|Info|Warning|Error|Fatal)\\)')\n> > +\n> > +    def __init__(self, content):\n> > +        self.__content = content\n> > +\n> > +    def check(self, line_numbers):\n> > +        issues = []\n> > +        for line_number in line_numbers:\n> > +            line = self.__content[line_number-1]\n> > +            if not LogCategoryChecker.log_regex.search(line):\n> > +                continue\n> > +\n> > +            issues.append([line_number, line, 'LOG() should use categories'])\n> > +\n> > +        return issues\n> > +\n> > +\n> > +available_checkers = {\n> > +    'log_category': LogCategoryChecker,\n> > +}\n> > +\n> > +\n> >  def check_file(top_level, commit, filename, formatters):\n> >      # Extract the line numbers touched by the commit.\n> >      diff = subprocess.run(['git', 'diff', '%s~..%s' % (commit, commit), '--',\n> > @@ -260,20 +283,36 @@ def check_file(top_level, commit, filename, formatters):\n> >      formatted = formatted.splitlines(True)\n> >      diff = difflib.unified_diff(after, formatted)\n> >  \n> > -    # Split the diff in hunks, recording line number ranges for each hunk.\n> > +    # Split the diff in hunks, recording line number ranges for each hunk, and\n> > +    # filter out hunks that are not touched by the commit.\n> >      formatted_diff = parse_diff(diff)\n> > -\n> > -    # Filter out hunks that are not touched by the commit.\n> >      formatted_diff = [hunk for hunk in formatted_diff if hunk.intersects(lines)]\n> > -    if len(formatted_diff) == 0:\n> > +\n> > +    # Check for code issues not related to formatting.\n> > +    issues = []\n> > +    for checker in available_checkers:\n> > +        checker = available_checkers[checker](after)\n> > +        for hunk in commit_diff:\n> > +            issues += checker.check(hunk.side('to').touched)\n> > +\n> > +    # Print the detected issues.\n> > +    if len(issues) == 0 and len(formatted_diff) == 0:\n> >          return 0\n> >  \n> >      print('%s---' % Colours.fg(Colours.Red), filename)\n> >      print('%s+++' % Colours.fg(Colours.Green), filename)\n> \n> Interestingly, these both print red and green respectively...\n> \n> > -    for hunk in formatted_diff:\n> > -        print(hunk)\n> >  \n> > -    return len(formatted_diff)\n> > +    if len(formatted_diff):\n> > +        for hunk in formatted_diff:\n> > +            print(hunk)\n> > +\n> > +    if len(issues):\n> > +        issues.sort()\n> > +        for issue in issues:\n> > +            print('%s#%u: %s' % (Colours.fg(Colours.Yellow), issue[0], issue[2]))\n> \n> But this looks distinctly brown or is it orange ...\n> I wonder if that's due to my shell perhaps.\n\nIt prints orange-brown here, probably the same colour as on your screen.\nThe colour being displayed depends on your terminal's theme.\n\n> > +            print('+%s%s' % (issue[1].rstrip(), Colours.reset()))\n> > +\n> > +    return len(formatted_diff) + len(issues)\n> >  \n> >  \n> >  def check_style(top_level, commit, formatters):","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0C0EF60DBB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 13 Feb 2019 12:08:48 +0100 (CET)","from pendragon.ideasonboard.com\n\t(dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi\n\t[IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7848285;\n\tWed, 13 Feb 2019 12:08:47 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1550056127;\n\tbh=sTQbDG/HnITsxU2IWbYMneZhqWCVZKYIX1NogaPSKxo=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=fcazE3kezJ1Ak9QRpqlt4e9wmDZs3PrKU32EUHJVo+b+HJ6ybGzJev1Ve6mBN+K6y\n\t4yTjRKlsF1vvtfm46xUaiIpdFwTwN4BKE1SCg8TyF6ZCxOKQD1L8eTYxU5zifDwnVW\n\ttuc3gspr8ZE14i6QNq4jzMItH+xwgXw6Jpr1Xni0=","Date":"Wed, 13 Feb 2019 13:08:44 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190213110844.GA5332@pendragon.ideasonboard.com>","References":"<20190212222557.8898-1-laurent.pinchart@ideasonboard.com>\n\t<20190212222557.8898-2-laurent.pinchart@ideasonboard.com>\n\t<c42e14b9-f0a5-0e55-f6fe-b3b223999fce@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<c42e14b9-f0a5-0e55-f6fe-b3b223999fce@ideasonboard.com>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH 2/2] utils: checkstyle: Catch LOG()\n\tusage without an explicit category","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Wed, 13 Feb 2019 11:08:48 -0000"}}]