diff options
-rw-r--r-- | repo_logging.py | 74 | ||||
-rw-r--r-- | tests/test_repo_logging.py | 115 |
2 files changed, 189 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)) | ||
diff --git a/tests/test_repo_logging.py b/tests/test_repo_logging.py new file mode 100644 index 00000000..ba8a9a9e --- /dev/null +++ b/tests/test_repo_logging.py | |||
@@ -0,0 +1,115 @@ | |||
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 | """Unit test for repo_logging module.""" | ||
16 | import unittest | ||
17 | from unittest.mock import MagicMock | ||
18 | |||
19 | from repo_logging import RepoLogger | ||
20 | |||
21 | |||
22 | class TestRepoLogger(unittest.TestCase): | ||
23 | def test_error_logs_error(self): | ||
24 | """Test if error fn outputs logs.""" | ||
25 | logger = RepoLogger(__name__) | ||
26 | RepoLogger.errors[:] = [] | ||
27 | result = None | ||
28 | |||
29 | def mock_handler(log): | ||
30 | nonlocal result | ||
31 | result = log.getMessage() | ||
32 | |||
33 | mock_out = MagicMock() | ||
34 | mock_out.level = 0 | ||
35 | mock_out.handle = mock_handler | ||
36 | logger.addHandler(mock_out) | ||
37 | |||
38 | logger.error("We're no strangers to love") | ||
39 | |||
40 | self.assertEqual(result, "We're no strangers to love") | ||
41 | |||
42 | def test_warning_logs_error(self): | ||
43 | """Test if warning fn outputs logs.""" | ||
44 | logger = RepoLogger(__name__) | ||
45 | RepoLogger.errors[:] = [] | ||
46 | result = None | ||
47 | |||
48 | def mock_handler(log): | ||
49 | nonlocal result | ||
50 | result = log.getMessage() | ||
51 | |||
52 | mock_out = MagicMock() | ||
53 | mock_out.level = 0 | ||
54 | mock_out.handle = mock_handler | ||
55 | logger.addHandler(mock_out) | ||
56 | |||
57 | logger.warning("You know the rules and so do I (do I)") | ||
58 | |||
59 | self.assertEqual(result, "You know the rules and so do I (do I)") | ||
60 | |||
61 | def test_error_aggregates_error_msg(self): | ||
62 | """Test if error fn aggregates error logs.""" | ||
63 | logger = RepoLogger(__name__) | ||
64 | RepoLogger.errors[:] = [] | ||
65 | |||
66 | logger.error("A full commitment's what I'm thinking of") | ||
67 | logger.error("You wouldn't get this from any other guy") | ||
68 | logger.error("I just wanna tell you how I'm feeling") | ||
69 | logger.error("Gotta make you understand") | ||
70 | |||
71 | self.assertEqual( | ||
72 | RepoLogger.errors[:], | ||
73 | [ | ||
74 | "A full commitment's what I'm thinking of", | ||
75 | "You wouldn't get this from any other guy", | ||
76 | "I just wanna tell you how I'm feeling", | ||
77 | "Gotta make you understand", | ||
78 | ], | ||
79 | ) | ||
80 | |||
81 | def test_log_aggregated_errors_logs_aggregated_errors(self): | ||
82 | """Test if log_aggregated_errors outputs aggregated errors.""" | ||
83 | logger = RepoLogger(__name__) | ||
84 | RepoLogger.errors[:] = [] | ||
85 | result = [] | ||
86 | |||
87 | def mock_handler(log): | ||
88 | nonlocal result | ||
89 | result.append(log.getMessage()) | ||
90 | |||
91 | mock_out = MagicMock() | ||
92 | mock_out.level = 0 | ||
93 | mock_out.handle = mock_handler | ||
94 | logger.addHandler(mock_out) | ||
95 | |||
96 | logger.error("Never gonna give you up") | ||
97 | logger.error("Never gonna let you down") | ||
98 | logger.error("Never gonna run around and desert you") | ||
99 | logger.log_aggregated_errors() | ||
100 | |||
101 | self.assertEqual( | ||
102 | result, | ||
103 | [ | ||
104 | "Never gonna give you up", | ||
105 | "Never gonna let you down", | ||
106 | "Never gonna run around and desert you", | ||
107 | "=" * 80, | ||
108 | "Repo command failed due to following errors:", | ||
109 | ( | ||
110 | "Never gonna give you up\n" | ||
111 | "Never gonna let you down\n" | ||
112 | "Never gonna run around and desert you" | ||
113 | ), | ||
114 | ], | ||
115 | ) | ||