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