How to pass const string parameter from perl to c++ using swig

Viewed 148

I am new to Swig and I need help in debugging this error.

My motive is to create a logging system with less time consuming using Swig, C++ , Perl. But i got error while passing the string parameter form Perl to C++ via swig.

I have placed my entire code and error below. Please help me to solve this issue. Thanks in future.

Logger.cpp

#include <stdexcept>

#include <string>
#include <ctime>
#include "Logger.h"

using namespace std;
Logger* Logger::pInstance = nullptr;




Logger& Logger::instance()
{
    lock_guard<mutex> guard(sMutex);
    printf("instance");
    if (pInstance == nullptr)
        pInstance = new Logger();
    return *pInstance;
}

void Logger::Cleanup()
{
    lock_guard<mutex> guard(Logger::sMutex);
    delete Logger::pInstance;
    Logger::pInstance = nullptr;
}


Logger::Logger()
{
    printf("Constructor\n");
    mOutputStream.open("log.out", ios_base::app);
    if (!mOutputStream.good()) {
        throw runtime_error("Unable to initialize the Logger!");
    } 

}
void Logger::log(const string& inMessage, const string& inLogLevel)
{
    lock_guard<mutex> guard(sMutex);
    logHelper(inMessage, inLogLevel);
}

void Logger::logHelper(const std::string& inMessage, const std::string& inLogLevel)
{
    mOutputStream << "[ " << current_time() << " ]" << inLogLevel << ": " << inMessage << endl;
}

string Logger::current_time()
{
    time_t rawtime;
  struct tm * timeinfo;
  char buffer[80];

  time (&rawtime);
  timeinfo = localtime(&rawtime);

  strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
  std::string str(buffer);
    return str;
}



Logger::~Logger(){
    printf("deconstructor\n");
    mOutputStream.close();
}  

Logger.h

#ifndef LOGGER_H_
#define LOGGER_H_
#include <stdio.h>

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <mutex>

class Logger
{
    public:
        Logger();
        ~Logger();
        Logger& instance();
        void log(const std::string& inMessage, 
        const std::string& inLogLevel);
    protected:
        static Logger* pInstance;
        std::ofstream mOutputStream;
        void Cleanup();
        void logHelper(const std::string& inMessage, 
        const std::string& inLogLevel);
    std::string current_time();
    private:


        std::mutex sMutex;
};
#endif

Logger.i

%module Logger 
%{
    #include "Logger.h"

%}

%include "Logger.h"

ch.pl

use strict;
use warnings;

use Cwd qw( abs_path );
use File::Basename qw( dirname );
use lib dirname(abs_path($0));

use Logger;  

my $p=new Logger::Logger();
$p->instance();
$p->log("test message");

Error message*

TypeError in method 'Logger_log', argument 2 of type 'std::string' at ch1.perl line 19.
1 Answers

You need to include a typemap for std::string in your interface file so SWIG knows how to handle the type, see chapter 9.4 in the manual:

9.4.1 std::string The std_string.i library provides typemaps for converting C++ std::string objects to and from strings in the target scripting language.

%module Logger 
%include "std_string.i"
%{
    #include "Logger.h"
%} 
%include "Logger.h"

If I use this and run ch.pl like this:

use strict;
use warnings;
use lib '.';
use Logger;      
my $p = Logger::Logger->new();
$p->instance();
$p->log("test message", "DEBUG");

I get output:

Constructor
instanceConstructor
deconstructor

The content of the log.out file is:

[ 22-05-2020 17:35:12 ]DEBUG: test message
Related