Robot Framework
debugfile.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 from robot.errors import DataError
17 from robot.utils import get_timestamp, file_writer, seq2str2
18 
19 from .logger import LOGGER
20 from .loggerhelper import IsLogged
21 
22 
23 def DebugFile(path):
24  if not path:
25  LOGGER.info('No debug file')
26  return None
27  try:
28  outfile = file_writer(path, usage='debug')
29  except DataError as err:
30  LOGGER.error(err.message)
31  return None
32  else:
33  LOGGER.info('Debug file: %s' % path)
34  return _DebugFileWriter(outfile)
35 
36 
38 
41  _separators = {'SUITE': '=', 'TEST': '-', 'KEYWORD': '~'}
42 
43  def __init__(self, outfile):
44  self._indent_indent = 0
45  self._kw_level_kw_level = 0
46  self._separator_written_last_separator_written_last = False
47  self._outfile_outfile = outfile
48  self._is_logged_is_logged = IsLogged('DEBUG')
49 
50  def start_suite(self, suite):
51  self._separator_separator('SUITE')
52  self._start_start('SUITE', suite.longname)
53  self._separator_separator('SUITE')
54 
55  def end_suite(self, suite):
56  self._separator_separator('SUITE')
57  self._end_end('SUITE', suite.longname, suite.elapsedtime)
58  self._separator_separator('SUITE')
59  if self._indent_indent == 0:
60  LOGGER.output_file('Debug', self._outfile_outfile.name)
61  self.closeclose()
62 
63  def start_test(self, test):
64  self._separator_separator('TEST')
65  self._start_start('TEST', test.name)
66  self._separator_separator('TEST')
67 
68  def end_test(self, test):
69  self._separator_separator('TEST')
70  self._end_end('TEST', test.name, test.elapsedtime)
71  self._separator_separator('TEST')
72 
73  def start_keyword(self, kw):
74  if self._kw_level_kw_level == 0:
75  self._separator_separator('KEYWORD')
76  self._start_start(kw.type, kw.name, kw.args)
77  self._kw_level_kw_level += 1
78 
79  def end_keyword(self, kw):
80  self._end_end(kw.type, kw.name, kw.elapsedtime)
81  self._kw_level_kw_level -= 1
82 
83  def log_message(self, msg):
84  if self._is_logged_is_logged(msg.level):
85  self._write_write(msg.message, level=msg.level, timestamp=msg.timestamp)
86 
87  def close(self):
88  if not self._outfile_outfile.closed:
89  self._outfile_outfile.close()
90 
91  def _start(self, type_, name, args=''):
92  args = ' ' + seq2str2(args)
93  self._write_write('+%s START %s: %s%s' % ('-'*self._indent_indent, type_, name, args))
94  self._indent_indent += 1
95 
96  def _end(self, type_, name, elapsed):
97  self._indent_indent -= 1
98  self._write_write('+%s END %s: %s (%s)' % ('-'*self._indent_indent, type_, name, elapsed))
99 
100  def _separator(self, type_):
101  self._write_write(self._separators_separators[type_] * 78, separator=True)
102 
103  def _write(self, text, separator=False, level='INFO', timestamp=None):
104  if separator and self._separator_written_last_separator_written_last:
105  return
106  if not separator:
107  text = '%s - %s - %s' % (timestamp or get_timestamp(), level, text)
108  self._outfile_outfile.write(text.rstrip() + '\n')
109  self._outfile_outfile.flush()
110  self._separator_written_last_separator_written_last = separator
def _end(self, type_, name, elapsed)
Definition: debugfile.py:96
def _start(self, type_, name, args='')
Definition: debugfile.py:91
def _write(self, text, separator=False, level='INFO', timestamp=None)
Definition: debugfile.py:103
def write(msg, level='INFO', html=False)
Writes the message to the log file using the given level.
Definition: logger.py:84
def seq2str2(sequence)
Returns sequence in format [ item 1 | item 2 | ...
Definition: misc.py:90
def file_writer(path=None, encoding='UTF-8', newline=None, usage=None)
Definition: robotio.py:25
def get_timestamp(daysep='', daytimesep=' ', timesep=':', millissep='.')
Definition: robottime.py:335