Robot Framework
stdoutlogsplitter.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import re
17 
18 from robot.utils import format_time
19 
20 from .loggerhelper import Message
21 
22 
23 
25 
26 
29  _split_from_levels = re.compile(r'^(?:\*'
30  r'(TRACE|DEBUG|INFO|HTML|WARN|ERROR)'
31  r'(:\d+(?:\.\d+)?)?' # Optional timestamp
32  r'\*)', re.MULTILINE)
33 
34  def __init__(self, output):
35  self._messages_messages = list(self._get_messages_get_messages(output.strip()))
36 
37  def _get_messages(self, output):
38  for level, timestamp, msg in self._split_output_split_output(output):
39  if timestamp:
40  timestamp = self._format_timestamp_format_timestamp(timestamp[1:])
41  yield Message(msg.strip(), level, timestamp=timestamp)
42 
43  def _split_output(self, output):
44  tokens = self._split_from_levels_split_from_levels.split(output)
45  tokens = self._add_initial_level_and_time_if_needed_add_initial_level_and_time_if_needed(tokens)
46  for i in range(0, len(tokens), 3):
47  yield tokens[i:i+3]
48 
50  if self._output_started_with_level_output_started_with_level(tokens):
51  return tokens[1:]
52  return ['INFO', None] + tokens
53 
54  def _output_started_with_level(self, tokens):
55  return tokens[0] == ''
56 
57  def _format_timestamp(self, millis):
58  return format_time(float(millis)/1000, millissep='.')
59 
60  def __iter__(self):
61  return iter(self._messages_messages)
A message created during the test execution.
Definition: message.py:27
Splits messages logged through stdout (or stderr) into Message objects.
def format_time(timetuple_or_epochsecs, daysep='', daytimesep=' ', timesep=':', millissep=None)
Returns a timestamp formatted from given time using separators.
Definition: robottime.py:208