Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95668 > unrolled thread

Re: Logging to a file from a C-extension

Started byStefan Behnel <stefan_ml@behnel.de>
First post2015-08-26 18:39 +0200
Last post2015-08-26 18:39 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Logging to a file from a C-extension Stefan Behnel <stefan_ml@behnel.de> - 2015-08-26 18:39 +0200

#95668 — Re: Logging to a file from a C-extension

FromStefan Behnel <stefan_ml@behnel.de>
Date2015-08-26 18:39 +0200
SubjectRe: Logging to a file from a C-extension
Message-ID<mailman.58.1440607201.11709.python-list@python.org>
Hi,

welcome to Python (and to this list). The usual way to reply here is
inline, after stripping anything that's not relevant to your reply.


AllanPfalzgraf schrieb am 25.08.2015 um 15:03:
> From: Stefan Behnel:
>> Al Pfalzgraf schrieb am 18.08.2015 um 15:07:
>>> If a logging file is opened at the level of a Python application,
>>> how would the log file name be communicated to a C-extension so that
>>>  logging from the extension would be sent to the same log file?
>> 
>> Writing to the file directly (as was suggested) may not be a good idea
>> as it would bypass the log filtering and formatting. Instead, I'd
>> suggest sending output to a normal Python Logger object instead.
>> 
>> This is obviously trivial in Cython (where you can just implement it
>> in Python code), but you can do the same in C with just the usual
>> C-API overhead.
> 
> You have understood my question.  I'm new to Python.  Could I use a
> Cython solution to get suggestions on just how to go about this in the C
> extension?  Otherwise could you suggest which C-API functions I should
> be looking at?

Well, my suggestion would be to write the extension in Cython instead of C,
simply because it allows you to care about what you want to achieve instead
of having to concentrate on C-API details like this which try to get in
your way and are difficult to master.

However, if you really want to use the CPython C-API directly, you have two
choices: write the logging setup in Python and execute it from a C string
using PyRun_SimpleString(), or reimplement what Python would do in C using
PyImport_AddModule, PyObject_GetAttr and PyObject_Call*(). The latter is
also what Cython does, except that it generates faster code.

Stefan

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web