VirtualBox

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

Last change on this file since 78403 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: testboxcommons.py 76553 2019-01-01 01:45:53Z 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-2019 Oracle Corporation
14
15This file is part of VirtualBox Open Source Edition (OSE), as
16available from http://www.virtualbox.org. This file is free software;
17you can redistribute it and/or modify it under the terms of the GNU
18General Public License (GPL) as published by the Free Software
19Foundation, in version 2 as it comes in the "COPYING" file of the
20VirtualBox OSE distribution. VirtualBox OSE is distributed in the
21hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
22
23The contents of this file may alternatively be used under the terms
24of the Common Development and Distribution License Version 1.0
25(CDDL) only, as it comes in the "COPYING.CDDL" file of the
26VirtualBox OSE distribution, in which case the provisions of the
27CDDL are applicable instead of those of the GPL.
28
29You may elect to license modified versions of this file under the
30terms and conditions of either the GPL or the CDDL or both.
31"""
32__version__ = "$Revision: 76553 $"
33
34
35# Standard python imports.
36import sys
37import traceback
38
39# Validation Kit imports.
40from common import utils;
41
42#
43# Exceptions.
44#
45
46class TestBoxException(Exception):
47 """
48 Custom exception class
49 """
50 pass
51
52#
53# Logging.
54#
55
56def 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
65def log2(sMessage, sCaller = None, sTsPrf = None):
66 """
67 Debug logging, will later be disabled by default.
68 """
69 if True is True:
70 if sTsPrf is None: sTsPrf = utils.getTimePrefix();
71 print('[%s] %s' % (sTsPrf, sMessage,));
72 sys.stdout.flush()
73 _ = sCaller;
74
75def _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 asInfo:
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
129def log1Xcpt(sText = None, cFrames = 1):
130 """Logs an exception."""
131 return _logXcptWorker(log, '', sText, cFrames);
132
133def log2Xcpt(sText = None, cFrames = 1):
134 """Debug logging of an exception."""
135 return _logXcptWorker(log2, '', sText, cFrames);
136
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