Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 1 | # Copyright (C) 2023 The Android Open Source Project |
| 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 | """Logic for printing user-friendly logs in repo.""" |
| 16 | |
| 17 | import logging |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame^] | 18 | from typing import Any, List |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 19 | |
| 20 | from color import Coloring |
| 21 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 22 | |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 23 | SEPARATOR = "=" * 80 |
| 24 | |
| 25 | |
| 26 | class LogColoring(Coloring): |
| 27 | """Coloring outstream for logging.""" |
| 28 | |
| 29 | def __init__(self, config): |
| 30 | super().__init__(config, "logs") |
| 31 | self.error = self.colorer("error", fg="red") |
| 32 | self.warning = self.colorer("warn", fg="yellow") |
| 33 | |
| 34 | |
| 35 | class ConfigMock: |
| 36 | """Default coloring config to use when Logging.config is not set.""" |
| 37 | |
| 38 | def __init__(self): |
| 39 | self.default_values = {"color.ui": "auto"} |
| 40 | |
| 41 | def GetString(self, x): |
| 42 | return self.default_values.get(x, None) |
| 43 | |
| 44 | |
| 45 | class RepoLogger(logging.Logger): |
| 46 | """Repo Logging Module.""" |
| 47 | |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame^] | 48 | def __init__(self, name: str, config=None, **kwargs): |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 49 | super().__init__(name, **kwargs) |
| 50 | self.config = config if config else ConfigMock() |
| 51 | self.colorer = LogColoring(self.config) |
| 52 | |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame^] | 53 | def error(self, msg: Any, *args, **kwargs): |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 54 | """Print and aggregate error-level logs.""" |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame^] | 55 | colored_error = self.colorer.error(str(msg), *args) |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 56 | super().error(colored_error, **kwargs) |
| 57 | |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame^] | 58 | def warning(self, msg: Any, *args, **kwargs): |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 59 | """Print warning-level logs with coloring.""" |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame^] | 60 | colored_warning = self.colorer.warning(str(msg), *args) |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 61 | super().warning(colored_warning, **kwargs) |
| 62 | |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame^] | 63 | def log_aggregated_errors(self, errors: List[Exception]): |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 64 | """Print all aggregated logs.""" |
| 65 | super().error(self.colorer.error(SEPARATOR)) |
| 66 | super().error( |
| 67 | self.colorer.error("Repo command failed due to following errors:") |
| 68 | ) |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame^] | 69 | super().error("\n".join(map(str, errors))) |