1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: testboxcommons.py 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
3 |
|
---|
4 | """
|
---|
5 | TestBox Script - Common Functions and Classes.
|
---|
6 |
|
---|
7 | This module contains constants and functions that are useful for all
|
---|
8 | the files in this (testbox) directory.
|
---|
9 | """
|
---|
10 |
|
---|
11 | __copyright__ = \
|
---|
12 | """
|
---|
13 | Copyright (C) 2012-2023 Oracle and/or its affiliates.
|
---|
14 |
|
---|
15 | This file is part of VirtualBox base platform packages, as
|
---|
16 | available from https://www.virtualbox.org.
|
---|
17 |
|
---|
18 | This program is free software; you can redistribute it and/or
|
---|
19 | modify it under the terms of the GNU General Public License
|
---|
20 | as published by the Free Software Foundation, in version 3 of the
|
---|
21 | License.
|
---|
22 |
|
---|
23 | This program is distributed in the hope that it will be useful, but
|
---|
24 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
26 | General Public License for more details.
|
---|
27 |
|
---|
28 | You should have received a copy of the GNU General Public License
|
---|
29 | along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
30 |
|
---|
31 | The contents of this file may alternatively be used under the terms
|
---|
32 | of the Common Development and Distribution License Version 1.0
|
---|
33 | (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
34 | in the VirtualBox distribution, in which case the provisions of the
|
---|
35 | CDDL are applicable instead of those of the GPL.
|
---|
36 |
|
---|
37 | You may elect to license modified versions of this file under the
|
---|
38 | terms and conditions of either the GPL or the CDDL or both.
|
---|
39 |
|
---|
40 | SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
41 | """
|
---|
42 | __version__ = "$Revision: 98103 $"
|
---|
43 |
|
---|
44 |
|
---|
45 | # Standard python imports.
|
---|
46 | import sys
|
---|
47 | import traceback
|
---|
48 |
|
---|
49 | # Validation Kit imports.
|
---|
50 | from common import utils;
|
---|
51 |
|
---|
52 | #
|
---|
53 | # Exceptions.
|
---|
54 | #
|
---|
55 |
|
---|
56 | class TestBoxException(Exception):
|
---|
57 | """
|
---|
58 | Custom exception class
|
---|
59 | """
|
---|
60 | pass; # pylint: disable=unnecessary-pass
|
---|
61 |
|
---|
62 | #
|
---|
63 | # Logging.
|
---|
64 | #
|
---|
65 |
|
---|
66 | def 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 |
|
---|
75 | def 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
|
---|
80 | if sTsPrf is None: sTsPrf = utils.getTimePrefix();
|
---|
81 | print('[%s] %s' % (sTsPrf, sMessage,));
|
---|
82 | sys.stdout.flush()
|
---|
83 | _ = sCaller;
|
---|
84 |
|
---|
85 | def _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 |
|
---|
139 | def log1Xcpt(sText = None, cFrames = 1):
|
---|
140 | """Logs an exception."""
|
---|
141 | return _logXcptWorker(log, '', sText, cFrames);
|
---|
142 |
|
---|
143 | def log2Xcpt(sText = None, cFrames = 1):
|
---|
144 | """Debug logging of an exception."""
|
---|
145 | return _logXcptWorker(log2, '', sText, cFrames);
|
---|
146 |
|
---|