diff options
| author | Aravind Vasudevan <aravindvasudev@google.com> | 2023-07-26 19:16:59 +0000 |
|---|---|---|
| committer | LUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-07-31 16:49:57 +0000 |
| commit | 8c35d948cfa76ec685ad36fb1cb3a0fcc749f428 (patch) | |
| tree | 718ae5fd39f100897a90a42c263b4d282c20a00b /repo_logging.py | |
| parent | 1d2e99d0289a36e8c2a53ff3bf5690f0f780ba63 (diff) | |
| download | git-repo-8c35d948cfa76ec685ad36fb1cb3a0fcc749f428.tar.gz | |
[repo logging] Add logging module
Bug: b/292704435
Change-Id: I8834591f661c75449f8be5de1c61ecd43669026d
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/380714
Tested-by: Aravind Vasudevan <aravindvasudev@google.com>
Reviewed-by: Joanna Wang <jojwang@google.com>
Commit-Queue: Aravind Vasudevan <aravindvasudev@google.com>
Diffstat (limited to 'repo_logging.py')
| -rw-r--r-- | repo_logging.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/repo_logging.py b/repo_logging.py new file mode 100644 index 00000000..67db05fb --- /dev/null +++ b/repo_logging.py | |||
| @@ -0,0 +1,74 @@ | |||
| 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 | ||
| 18 | import multiprocessing | ||
| 19 | |||
| 20 | from color import Coloring | ||
| 21 | |||
| 22 | SEPARATOR = "=" * 80 | ||
| 23 | |||
| 24 | |||
| 25 | class LogColoring(Coloring): | ||
| 26 | """Coloring outstream for logging.""" | ||
| 27 | |||
| 28 | def __init__(self, config): | ||
| 29 | super().__init__(config, "logs") | ||
| 30 | self.error = self.colorer("error", fg="red") | ||
| 31 | self.warning = self.colorer("warn", fg="yellow") | ||
| 32 | |||
| 33 | |||
| 34 | class ConfigMock: | ||
| 35 | """Default coloring config to use when Logging.config is not set.""" | ||
| 36 | |||
| 37 | def __init__(self): | ||
| 38 | self.default_values = {"color.ui": "auto"} | ||
| 39 | |||
| 40 | def GetString(self, x): | ||
| 41 | return self.default_values.get(x, None) | ||
| 42 | |||
| 43 | |||
| 44 | class RepoLogger(logging.Logger): | ||
| 45 | """Repo Logging Module.""" | ||
| 46 | |||
| 47 | # Aggregates error-level logs. This is used to generate an error summary | ||
| 48 | # section at the end of a command execution. | ||
| 49 | errors = multiprocessing.Manager().list() | ||
| 50 | |||
| 51 | def __init__(self, name, config=None, **kwargs): | ||
| 52 | super().__init__(name, **kwargs) | ||
| 53 | self.config = config if config else ConfigMock() | ||
| 54 | self.colorer = LogColoring(self.config) | ||
| 55 | |||
| 56 | def error(self, msg, *args, **kwargs): | ||
| 57 | """Print and aggregate error-level logs.""" | ||
| 58 | colored_error = self.colorer.error(msg, *args) | ||
| 59 | RepoLogger.errors.append(colored_error) | ||
| 60 | |||
| 61 | super().error(colored_error, **kwargs) | ||
| 62 | |||
| 63 | def warning(self, msg, *args, **kwargs): | ||
| 64 | """Print warning-level logs with coloring.""" | ||
| 65 | colored_warning = self.colorer.warning(msg, *args) | ||
| 66 | super().warning(colored_warning, **kwargs) | ||
| 67 | |||
| 68 | def log_aggregated_errors(self): | ||
| 69 | """Print all aggregated logs.""" | ||
| 70 | super().error(self.colorer.error(SEPARATOR)) | ||
| 71 | super().error( | ||
| 72 | self.colorer.error("Repo command failed due to following errors:") | ||
| 73 | ) | ||
| 74 | super().error("\n".join(RepoLogger.errors)) | ||
