[C++-sig] calling python logger module from C++

Andreas Beyer beyer at imb-jena.de
Sat May 14 00:17:20 CEST 2005


OK, once again without formatting. Sorry for that.

Maybe I am totally wrong, but what about parsing the vargs inside C/C++, 
translating them into a std::string and then passing that string over to 
Python?
Thus, you end up with an actual implementation of virtual void Log() in 
C++ (as opposed the abstract declaration). Internally it might call an 
abstract method, which only accepts the log-level and one string as 
arguments. This second method can then be implemented in Python.

e.g.

virtual void _Log( LogLevel level, std::wstring msg ) = 0;
virtual void Log( LogLevel level, std::wstring msg, ... )
{
    // handle vargs here
    ...
    _Log(level, msg);
}


Andreas

Petrucio wrote:

>I don't think it is possible.
>
>I usually export 'instances' of my varargs functions to python. This could 
>be an issue if the types of your args are also variable, like the standard 
>printf-like functions.
>
>You can in this case just export the most common uses, of a string and some 
>other variable after, something like:
>
>Your real vararged function:
>virtual void Log( LogLevel level, std::wstring msg, ... ) = 0;
>
>Exported 'instances'
>virtual void Log( LogLevel level, std::wstring msg) = 0;
>virtual void Log( LogLevel level, std::wstring msg, int int_arg) = 0;
>virtual void Log( LogLevel level, std::wstring msg, std::wstring str_arg) = 0;
>
>Although I can't immediately see the need for this, since that better place 
>to format your string than python?
>
>Beware of performance issues when using an inter-language polimorphic 
>solution this this for something that will probably be called a lot like a 
>logger.
>
>Petrucio - Game Programmer
>Hoplon Infotainment - www.taikodom.com.br
>
>At 00:19 12/5/2005, you wrote:
>  
>
>>I've been wondering how best to provide support for calling my python logger
>>(which uses the standard logger module) from within my C++ extensions built
>>using Boost.Python. My first thought was to do this:
>>
>>enum LogLevel {
>>  LogLevelCritical = 50,
>>    LogLevelError = 40,
>>    LogLevelWarning = 30,
>>    LogLevelInfo = 20,
>>    LogLevelDebug = 10
>>};
>>
>>
>>class PiLogger {
>>public:
>>
>>  virtual void Log( LogLevel level, std::wstring msg ) = 0;
>>
>>};
>>
>>and then implement this class in python, setting it as a static variable in
>>my C++. This works pretty well, but I'd like to be able to support format
>>strings, which requires varargs. I couldn't figure out how to wrap a varargs
>>function - is that even possible? If it's not, is there a better way to
>>accomplish what I want?
>>
>>Thanks,
>>
>>Eric
>>    
>>




More information about the Cplusplus-sig mailing list