1#!/usr/bin/env python 2# 3# Copyright 2021 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the', help='License'); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an', help='AS IS' BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16import datetime 17import logging 18import time 19 20 21class TimeFormatter(logging.Formatter): 22 """A formatter used by the build system that: 23 24 - Strips whitespace. 25 - Formats time since start 26 """ 27 28 def __init__(self, fmt=None): 29 self.start_time = time.time() 30 super(TimeFormatter, self).__init__(fmt) 31 32 def formatTime(self, record, datefmt=None): 33 fmt = datefmt or "%H:%M:%S" 34 ct = self.converter(record.created) 35 dt = datetime.timedelta(seconds=record.created - self.start_time) 36 mm, ss = divmod(dt.total_seconds(), 60) 37 _, mm = divmod(mm, 60) 38 # 2 digit precision is sufficient. 39 dt_fmt = "%02d:%02d.%-2d" % (mm, ss, dt.microseconds % 100) 40 return "{}({})".format(time.strftime(fmt, ct), dt_fmt) 41 42 def format(self, record): 43 record.msg = str(record.msg).strip() 44 return super(TimeFormatter, self).format(record) 45