VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testboxscript/testboxcommons.py@ 101505

Last change on this file since 101505 was 98655, checked in by vboxsync, 22 months ago

ValKit: Pylint 2.16.2 adjustments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: testboxcommons.py 98655 2023-02-20 15:05:40Z vboxsync $
3
4"""
5TestBox Script - Common Functions and Classes.
6
7This module contains constants and functions that are useful for all
8the files in this (testbox) directory.
9"""
10
11__copyright__ = \
12"""
13Copyright (C) 2012-2023 Oracle and/or its affiliates.
14
15This file is part of VirtualBox base platform packages, as
16available from https://www.virtualbox.org.
17
18This program is free software; you can redistribute it and/or
19modify it under the terms of the GNU General Public License
20as published by the Free Software Foundation, in version 3 of the
21License.
22
23This program is distributed in the hope that it will be useful, but
24WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26General Public License for more details.
27
28You should have received a copy of the GNU General Public License
29along with this program; if not, see <https://www.gnu.org/licenses>.
30
31The contents of this file may alternatively be used under the terms
32of the Common Development and Distribution License Version 1.0
33(CDDL), a copy of it is provided in the "COPYING.CDDL" file included
34in the VirtualBox distribution, in which case the provisions of the
35CDDL are applicable instead of those of the GPL.
36
37You may elect to license modified versions of this file under the
38terms and conditions of either the GPL or the CDDL or both.
39
40SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
41"""
42__version__ = "$Revision: 98655 $"
43
44
45# Standard python imports.
46import sys
47import traceback
48
49# Validation Kit imports.
50from common import utils;
51
52#
53# Exceptions.
54#
55
56class TestBoxException(Exception):
57 """
58 Custom exception class
59 """
60 pass; # pylint: disable=unnecessary-pass
61
62#
63# Logging.
64#
65
66def log(sMessage, sCaller = None, sTsPrf = None):
67 """
68 Print out a message and flush stdout
69 """
70 if sTsPrf is None: sTsPrf = utils.getTimePrefix();
71 print('[%s] %s' % (sTsPrf, sMessage,));
72 sys.stdout.flush();
73 _ = sCaller;
74
75def log2(sMessage, sCaller = None, sTsPrf = None):
76 """
77 Debug logging, will later be disabled by default.
78 """
79 if True is True: # pylint: disable=comparison-with-itself,comparison-of-constants
80 if sTsPrf is None: sTsPrf = utils.getTimePrefix();
81 print('[%s] %s' % (sTsPrf, sMessage,));
82 sys.stdout.flush()
83 _ = sCaller;
84
85def _logXcptWorker(fnLogger, sPrefix = '', sText = None, cFrames = 1, fnLogger1 = log):
86 """
87 Log an exception, optionally with a preceeding message and more than one
88 call frame.
89 """
90 ## @todo skip all this if iLevel is too high!
91
92 # Try get exception info.
93 sTsPrf = utils.getTimePrefix();
94 try:
95 oType, oValue, oTraceback = sys.exc_info();
96 except:
97 oType = oValue = oTraceback = None;
98 if oType is not None:
99
100 # Try format the info
101 try:
102 rc = 0;
103 sCaller = utils.getCallerName(oTraceback.tb_frame);
104 if sText is not None:
105 rc = fnLogger('%s%s' % (sPrefix, sText), sCaller, sTsPrf);
106 asInfo = [];
107 try:
108 asInfo = asInfo + traceback.format_exception_only(oType, oValue);
109 if cFrames is not None and cFrames <= 1:
110 asInfo = asInfo + traceback.format_tb(oTraceback, 1);
111 else:
112 asInfo.append('Traceback:')
113 asInfo = asInfo + traceback.format_tb(oTraceback, cFrames);
114 asInfo.append('Stack:')
115 asInfo = asInfo + traceback.format_stack(oTraceback.tb_frame.f_back, cFrames);
116 except:
117 fnLogger1('internal-error: Hit exception #2! %s' % (traceback.format_exc()), sCaller, sTsPrf);
118
119 if asInfo:
120 # Do the logging.
121 for sItem in asInfo:
122 asLines = sItem.splitlines();
123 for sLine in asLines:
124 rc = fnLogger('%s%s' % (sPrefix, sLine), sCaller, sTsPrf);
125
126 else:
127 fnLogger('No exception info...', sCaller, sTsPrf);
128 rc = -3;
129 except:
130 fnLogger1('internal-error: Hit exception! %s' % (traceback.format_exc()), None, sTsPrf);
131 rc = -2;
132 else:
133 fnLogger1('internal-error: No exception! %s' % (utils.getCallerName(iFrame=3)), utils.getCallerName(iFrame=3), sTsPrf);
134 rc = -1;
135
136 return rc;
137
138
139def log1Xcpt(sText = None, cFrames = 1):
140 """Logs an exception."""
141 return _logXcptWorker(log, '', sText, cFrames);
142
143def log2Xcpt(sText = None, cFrames = 1):
144 """Debug logging of an exception."""
145 return _logXcptWorker(log2, '', sText, cFrames);
146
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette