blob: 0d6f07ec7ad09b84c3ea41223ac5c5766a3b08ea [file] [log] [blame]
Tobin Ehlisfde2fc32015-06-12 12:49:01 -06001#!/usr/bin/env python3
2#
3# Copyright (C) 2015 LunarG, Inc.
4#
5# Permission is hereby granted, free of charge, to any person obtaining a
6# copy of this software and associated documentation files (the "Software"),
7# to deal in the Software without restriction, including without limitation
8# the rights to use, copy, modify, merge, publish, distribute, sublicense,
9# and/or sell copies of the Software, and to permit persons to whom the
10# Software is furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included
13# in all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21# DEALINGS IN THE SOFTWARE.
22
23from inspect import currentframe, getframeinfo
24
25# This is a wrapper class for inspect module that returns a formatted line
26# with details of the source file and line number of python code who called
27# into this class. The result can them be added to codegen to simplify
28# debug as it shows where code was generated from.
29class sourcelineinfo():
30 def __init__(self):
31 self.general_prefix = "// CODEGEN : "
32 self.file_prefix = "file "
33 self.line_prefix = "line #"
34 self.enabled = True
35
36 def get(self):
37 if self.enabled:
38 frameinfo = getframeinfo(currentframe().f_back)
39 return "%s%s%s %s%s" % (self.general_prefix, self.file_prefix, frameinfo.filename, self.line_prefix, frameinfo.lineno)
40 return ""