1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: testboxcommons.py 56295 2015-06-09 14:29:55Z 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-2015 Oracle Corporation
|
---|
14 |
|
---|
15 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
16 | available from http://www.virtualbox.org. This file is free software;
|
---|
17 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
18 | General Public License (GPL) as published by the Free Software
|
---|
19 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
20 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
21 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
22 |
|
---|
23 | The contents of this file may alternatively be used under the terms
|
---|
24 | of the Common Development and Distribution License Version 1.0
|
---|
25 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
26 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
27 | CDDL are applicable instead of those of the GPL.
|
---|
28 |
|
---|
29 | You may elect to license modified versions of this file under the
|
---|
30 | terms and conditions of either the GPL or the CDDL or both.
|
---|
31 | """
|
---|
32 | __version__ = "$Revision: 56295 $"
|
---|
33 |
|
---|
34 |
|
---|
35 | # Standard python imports.
|
---|
36 | import sys
|
---|
37 | import traceback
|
---|
38 |
|
---|
39 | # Validation Kit imports.
|
---|
40 | from common import utils;
|
---|
41 |
|
---|
42 | #
|
---|
43 | # Exceptions.
|
---|
44 | #
|
---|
45 |
|
---|
46 | class TestBoxException(Exception):
|
---|
47 | """
|
---|
48 | Custom exception class
|
---|
49 | """
|
---|
50 | pass
|
---|
51 |
|
---|
52 | #
|
---|
53 | # Logging.
|
---|
54 | #
|
---|
55 |
|
---|
56 | def log(sMessage, sCaller = None, sTsPrf = None):
|
---|
57 | """
|
---|
58 | Print out a message and flush stdout
|
---|
59 | """
|
---|
60 | if sTsPrf is None: sTsPrf = utils.getTimePrefix();
|
---|
61 | print('[%s] %s' % (sTsPrf, sMessage,));
|
---|
62 | sys.stdout.flush();
|
---|
63 | _ = sCaller;
|
---|
64 |
|
---|
65 | def log2(sMessage, sCaller = None, sTsPrf = None):
|
---|
66 | """
|
---|
67 | Debug logging, will later be disabled by default.
|
---|
68 | """
|
---|
69 | if True:
|
---|
70 | if sTsPrf is None: sTsPrf = utils.getTimePrefix();
|
---|
71 | print('[%s] %s' % (sTsPrf, sMessage,));
|
---|
72 | sys.stdout.flush()
|
---|
73 | _ = sCaller;
|
---|
74 |
|
---|
75 | def _logXcptWorker(fnLogger, sPrefix = '', sText = None, cFrames = 1, fnLogger1 = log):
|
---|
76 | """
|
---|
77 | Log an exception, optionally with a preceeding message and more than one
|
---|
78 | call frame.
|
---|
79 | """
|
---|
80 | ## @todo skip all this if iLevel is too high!
|
---|
81 |
|
---|
82 | # Try get exception info.
|
---|
83 | sTsPrf = utils.getTimePrefix();
|
---|
84 | try:
|
---|
85 | oType, oValue, oTraceback = sys.exc_info();
|
---|
86 | except:
|
---|
87 | oType = oValue = oTraceback = None;
|
---|
88 | if oType is not None:
|
---|
89 |
|
---|
90 | # Try format the info
|
---|
91 | try:
|
---|
92 | rc = 0;
|
---|
93 | sCaller = utils.getCallerName(oTraceback.tb_frame);
|
---|
94 | if sText is not None:
|
---|
95 | rc = fnLogger('%s%s' % (sPrefix, sText), sCaller, sTsPrf);
|
---|
96 | asInfo = [];
|
---|
97 | try:
|
---|
98 | asInfo = asInfo + traceback.format_exception_only(oType, oValue);
|
---|
99 | if cFrames is not None and cFrames <= 1:
|
---|
100 | asInfo = asInfo + traceback.format_tb(oTraceback, 1);
|
---|
101 | else:
|
---|
102 | asInfo.append('Traceback:')
|
---|
103 | asInfo = asInfo + traceback.format_tb(oTraceback, cFrames);
|
---|
104 | asInfo.append('Stack:')
|
---|
105 | asInfo = asInfo + traceback.format_stack(oTraceback.tb_frame.f_back, cFrames);
|
---|
106 | except:
|
---|
107 | fnLogger1('internal-error: Hit exception #2! %s' % (traceback.format_exc()), sCaller, sTsPrf);
|
---|
108 |
|
---|
109 | if len(asInfo) > 0:
|
---|
110 | # Do the logging.
|
---|
111 | for sItem in asInfo:
|
---|
112 | asLines = sItem.splitlines();
|
---|
113 | for sLine in asLines:
|
---|
114 | rc = fnLogger('%s%s' % (sPrefix, sLine), sCaller, sTsPrf);
|
---|
115 |
|
---|
116 | else:
|
---|
117 | fnLogger('No exception info...', sCaller, sTsPrf);
|
---|
118 | rc = -3;
|
---|
119 | except:
|
---|
120 | fnLogger1('internal-error: Hit exception! %s' % (traceback.format_exc()), None, sTsPrf);
|
---|
121 | rc = -2;
|
---|
122 | else:
|
---|
123 | fnLogger1('internal-error: No exception! %s' % (utils.getCallerName(iFrame=3)), utils.getCallerName(iFrame=3), sTsPrf);
|
---|
124 | rc = -1;
|
---|
125 |
|
---|
126 | return rc;
|
---|
127 |
|
---|
128 |
|
---|
129 | def log1Xcpt(sText = None, cFrames = 1):
|
---|
130 | """Logs an exception."""
|
---|
131 | return _logXcptWorker(log, '', sText, cFrames);
|
---|
132 |
|
---|
133 | def log2Xcpt(sText = None, cFrames = 1):
|
---|
134 | """Debug logging of an exception."""
|
---|
135 | return _logXcptWorker(log2, '', sText, cFrames);
|
---|
136 |
|
---|