<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Trying to get file, function, line number]]></title><description><![CDATA[<p dir="auto">I'm trying to get file, function and line number to display in a message. I found a technique here (<a href="https://forum.micropython.org/viewtopic.php?t=7811" target="_blank" rel="noopener noreferrer nofollow ugc">https://forum.micropython.org/viewtopic.php?t=7811</a>), and tried to adapt it. It seemed to be working for a bit, but then somehow it stopped. The idea was to force an exception, which should dump a stack trace, then rummage through that. But for some reason, it is only showing the current function:</p>
<pre><code>Traceback (most recent call last):
  File "lib/logging.py", line 46, in log
ValueError: foo
'Traceback (most recent call last):\n  File "lib/logging.py", line 46, in log\nValueError: foo\n'
line= Traceback (most recent call last):
line=   File "lib/logging.py", line 46, in log
INFO:Traceback (most recent call last)::ShowError (2000-1-1 0:0:14): error='WiFi connected'
</code></pre>
<p dir="auto">Here's my code (borrowed in part from <a href="https://github.com/m5stack/M5Stack_MicroPython/blob/master/MicroPython_BUILD/components/micropython/esp32/modules/logging.py" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/m5stack/M5Stack_MicroPython/blob/master/MicroPython_BUILD/components/micropython/esp32/modules/logging.py</a>). Note that I also tried the seek(0) readline() thing from the example I'm copying, but it doesn't matter. In fact, just throwing a straight <code>sys.print_exception(err)</code> and letting it print out <em>still</em> doesn't show the stack.</p>
<pre><code>    def log(self, level, msg, *args):
        if level &gt;= (self.level or _level):
            redirect = io.StringIO()
            try:
              raise ValueError('foo')
            except ValueError as err:
              sys.print_exception(err, redirect)
            print('%r' % redirect.getvalue())
            caller = 'unknown'
            for new_line in redirect.getvalue().splitlines():
              print('line=',new_line)
              if 'logging.py' in new_line:
                break
              caller = new_line
            if caller != 'unknown':
              caller = caller_re.sub('[\\1:\\2](\\3)', caller)
            _stream.write("%s:%s:" % (self._level_str(level), caller))
            if not args:
                print(msg, file=_stream)
            else:
                print(msg % args, file=_stream)
</code></pre>
<p dir="auto">I know from other code that I have that <code>sys.print_exception(err)</code> normally prints the stack, so I'm a bit flummoxed!</p>
<p dir="auto">Any ideas where the stack trace went?</p>
]]></description><link>https://community.m5stack.com/topic/4931/trying-to-get-file-function-line-number</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 08:20:02 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/4931.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 31 Dec 2022 23:01:49 GMT</pubDate><ttl>60</ttl></channel></rss>