예제 #1
0
    def ParseMsg(self, msg):
        """Converts msg to a console safe pair of plain and ANSI-annotated strings.

    Args:
      msg: str or text.TypedText, the message to parse into plain and
        ANSI-annotated strings.
    Returns:
      str, str: A plain text string and a string that may also contain ANSI
        constrol sequences. If ANSI is not supported or color is disabled,
        then the second string will be identical to the first.
    """
        plain_text, styled_text = msg, msg
        if isinstance(msg, text.TypedText):
            typed_text_parser = style_parser.GetTypedTextParser()
            plain_text = typed_text_parser.ParseTypedTextToString(
                msg, stylize=False)
            styled_text = typed_text_parser.ParseTypedTextToString(
                msg, stylize=self.isatty())
        plain_text = console_attr.SafeText(plain_text,
                                           encoding=LOG_FILE_ENCODING,
                                           escape=False)
        styled_text = console_attr.SafeText(styled_text,
                                            encoding=LOG_FILE_ENCODING,
                                            escape=False)
        return plain_text, styled_text
예제 #2
0
    def format(self, record):
        record = copy.copy(record)

        if isinstance(record.msg, text.TypedText):
            record.msg = style_parser.GetTypedTextParser(
            ).ParseTypedTextToString(record.msg, stylize=False)

        # There are some cases where record.args ends up being a dict.
        if isinstance(record.args, tuple):
            new_args = []
            for arg in record.args:
                if isinstance(arg, text.TypedText):
                    arg = style_parser.GetTypedTextParser(
                    ).ParseTypedTextToString(arg, stylize=False)
                new_args.append(arg)
            record.args = tuple(new_args)
        # The log file handler expects text strings always, and encodes them to
        # utf-8 before writing to the file.
        with _SafeDecodedLogRecord(record, LOG_FILE_ENCODING):
            msg = super(_LogFileFormatter, self).format(record)
        return msg
예제 #3
0
 def testGetParser(self, platform, interactive_ux, show_structured_logs,
                   disable_color, enabled, expected_enabled):
     self.StartObjectPatch(
         platforms.OperatingSystem,
         'Current').return_value = (platform
                                    or platforms.OperatingSystem.LINUX)
     properties.VALUES.core.interactive_ux_style.Set(interactive_ux
                                                     or 'NORMAL')
     properties.VALUES.core.show_structured_logs.Set(show_structured_logs
                                                     or 'never')
     properties.VALUES.core.disable_color.Set(disable_color)
     style_parser = parser.GetTypedTextParser(enabled=enabled)
     self.assertEqual(expected_enabled, style_parser.style_enabled)
예제 #4
0
    def format(self, record):
        """Formats the record using the proper formatter."""
        show_structured_output = self.ShowStructuredOutput()

        # The logged msg was a TypedText so convert msg to a normal str.
        stylize = self.terminal and not show_structured_output
        record = copy.copy(record)
        if isinstance(record.msg, text.TypedText):
            record.msg = style_parser.GetTypedTextParser(
            ).ParseTypedTextToString(record.msg, stylize=stylize)

        # There are some cases where record.args ends up being a dict.
        if isinstance(record.args, tuple):
            new_args = []
            for arg in record.args:
                if isinstance(arg, text.TypedText):
                    arg = style_parser.GetTypedTextParser(
                    ).ParseTypedTextToString(arg, stylize=stylize)
                new_args.append(arg)
            record.args = tuple(new_args)

        if show_structured_output:
            return self.structured_formatter.format(record)
        return self.default_formatter.format(record)
예제 #5
0
 def __init__(self, *args, **kwargs):
   self._parser = parser.GetTypedTextParser()
   super(_MultilineStagedProgressTracker, self).__init__(*args, **kwargs)