Hello,
i have simple[1] function like this:
def log_msg(msg , file , line):
print "%s:%s %s" % (file,line,msg)
the file and line arguments should be the filename and linenumber of
the source file where the function is called. If this were C I would
have used the __FILE__ and __LINE__ macros as:
log_msg(msg , __FILE__ , __LINE__)
Is there a way to emulate this behaviour in Python?
Best Regards
Joakim Hove
[1]: It is not *that* simple, but you get the point.
--
Joakim Hove
hove AT ntnu.no /
Tlf: +47 (73 5)9 34 27 / Stabburveien 18
Fax: ............... .. / N-5231 Paradis http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04 4 9344
Le dimanche 13 août 2006 13:31, Joakim Hove a écrit*:
Hello,
i have simple[1] function like this:
def log_msg(msg , file , line):
print "%s:%s %s" % (file,line,msg)
the file and line arguments should be the filename and linenumber of
the source file where the function is called. If this were C I would
have used the __FILE__ and __LINE__ macros as:
log_msg(msg , __FILE__ , __LINE__)
Is there a way to emulate this behaviour in Python?
Sure, try :
In [46]: import inspect
In [47]: c=inspect.curre ntframe()
In [48]: c.f_lineno
Out[48]: 1
In [49]: c.f_code.co_fil ename
Out[49]: '<ipython console>'
Of course, in the console, these are not truly relevant.
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
Joakim Hove wrote:
Hello,
i have simple[1] function like this:
def log_msg(msg , file , line):
print "%s:%s %s" % (file,line,msg)
the file and line arguments should be the filename and linenumber of
the source file where the function is called. If this were C I would
have used the __FILE__ and __LINE__ macros as:
log_msg(msg , __FILE__ , __LINE__)
Is there a way to emulate this behaviour in Python?
It's better in Python not to emulate that but to let the caller do the
work:
C:\junk>type caller_id.py
import inspect
def logger(msg):
print "logger:", msg
print "called from %s:%d" % inspect.stack()[1][1:3]
print
def client1():
logger("one")
def client2():
logger("two")
client1()
client2()
C:\junk>caller_ id.py
logger: one
called from C:\junk\caller_ id.py:9
logger: two
called from C:\junk\caller_ id.py:12
If you care to search for __LINE__ in this newsgroup, there's a slight
chance you might find a thread or two or twenty-two on the topic :-)
I don't usually go for one-liners, especially ugly ones like
"inspect.stack( )[1][1:3]" but it avoids the risk of hanging on to a
reference to the frame object -- see the warning in the inspect docs.
You can get the name of the calling function or method (and,
indirectly, the method's class) if you want to log the whole dossier --
details left as an exercise :-)
HTH,
John
Maric Michaud <ma***@aristote .infowrites:
Sure, try :
In [46]: import inspect
In [47]: c=inspect.curre ntframe()
In [48]: c.f_lineno
Out[48]: 1
In [49]: c.f_code.co_fil ename
Out[49]: '<ipython console>'
Thanks a lot - that was just what I wanted.
Regards - Joakim
--
Joakim Hove
hove AT ntnu.no /
Tlf: +47 (73 5)9 34 27 / Stabburveien 18
Fax: ............... .. / N-5231 Paradis http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
Le dimanche 13 août 2006 14:18, John Machin a écrit*:
I don't usually go for one-liners, especially ugly ones like
"inspect.stack( )[1][1:3]" but it avoids the risk of hanging on to a
reference to the frame object -- see the warning in the inspect docs.
Yes, my mistake, thanks for pointing this out, my suggestion should have been
more accurate :
import inspect
try :
c=inspect.curre ntframe()
log_msg(c.f_lin eno, c.f_code.co_fil ename)
finally :
del c
or shorter ;
from inspect import currentframe
log_msg(current frame().f_linen o,
currentframe(). f_code.co_filen ame)
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Spry |
last post by:
Hi,
I wanted to write macros for finding the number of memory allocations and
deallocations also wanted to find the locations.
The code I have is a pretty big one. I have a wrapper on top of malloc(int
x) as Xmalloc(int x)
Now I want to write a macro for Xmalloc which can log the location of the
file and the line and the number of bytes allocated.
|
by: qazmlp |
last post by:
How exactly __FILE__ and __LINE__ macros are defined? Or, Is the
definition of these macros implementation dependent ?
I am wondering how easily they can get the file name and line number
respectively.
|
by: Paul Shipley |
last post by:
Hi,
Does anyone know a way of converting the __LINE__ macro to a string at
compile time? The reason I ask it because I want to put some debug
information in to tell me if memory is not being allocated. For
example, this function will return the status of my system:
static char* get_status (void)
{
char* str_ptr;
|
by: jake1138 |
last post by:
I couldn't find an example of this anywhere so I post it in the hope
that someone finds it useful. I believe this is compiler specific (I'm
using gcc), as C99 defines __VA_ARGS__. Comments are welcome.
This will print the file name and line number followed by a format
string and a variable number of arguments. The key here is that you
MUST have a space between __LINE__ and the last comma, otherwise
__LINE__ gets eaten by the ## if...
|
by: baumann.Pan |
last post by:
where are these macros defined? can I use it a release ver code?
| |
by: wsq |
last post by:
in C++ we can use __FILE__ and __LINE__ to help us locating the bug, is
there anything like these in C# ?
thanks,
wsq
|
by: Kenneth Brody |
last post by:
Am I correct that using __FILE__ and __LINE__ within a macro will expand
to the filename and line in which the macro is invoked, rather than where
it is defined?
For example, in a header file:
#ifdef DEBUG
#define LOGIT(note) DoLogging(__FILE__,__LINE__,note)
#else
#define LOGIT(note)
|
by: Neo |
last post by:
Hie,
Can I put __FILE__ and __LINE__ macros inside the class function which
may not be inline. And
void function()
{
classobject::LogInfo(...);
}
Internally LogInfo should log file name and line number. Here I dont
|
by: Neo |
last post by:
Hi Friends,
I am planning to use "__FILE__,__LINE__,__FUNCTION__ " for a logging
component in my class.
In debug build I gets all information. I tried with release mode also
and it works. But I want to verify that will I able to get this
information at any kind of build with all possible optimizations?
Regards
Vikram S
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |