aricpp icon indicating copy to clipboard operation
aricpp copied to clipboard

Allow custom logging function in aricpp::Client

Open daniele77 opened this issue 4 years ago • 0 comments

Discussed in https://github.com/daniele77/aricpp/discussions/57

Originally posted by ferchor2003 June 23, 2021 In my existing application I already have a custom mechanism to log information. I want to use that in my aricpp application instead of simply writing to std::out. To that effect I have modified client.h and websocket.h to use a function of my choosing to log events with an added severity parameter. Hopefully you will find this useful.

websocket.h

Add the LoggingSeverity and pLogInfoFnType definitions and calls to the new logging function:

namespace aricpp
{
enum class LoggingSeverity { Info, Warning, Error };
using pLogInfoFnType = std::function< void(const std::string& msg, LoggingSeverity sev)>;

class WebSocket
{
public:

    using ConnectHandler = std::function< void( const boost::system::error_code& ) >;
    using ReceiveHandler = std::function< void( const std::string&, const boost::system::error_code& ) >;

    WebSocket( boost::asio::io_service& _ios, std::string _host, std::string _port, pLogInfoFnType pLogInfoFn) :
        ios(_ios), 
        host(std::move(_host)), 
        port(std::move(_port)), 
        resolver(ios), 
        socket( new socket_type(ios)),
        websocket( new socket_stream_type( *socket)),
        pingTimer(ios),
        LogCallback(pLogInfoFn)
    {}

Where LogCallback is a private member of the class:

	pLogInfoFnType LogCallback;

I can then log events like in the following, replacing std::cout:

    void Received( boost::system::error_code ec )
    {
        std::string s( (std::istreambuf_iterator<char>(&rxData)), std::istreambuf_iterator<char>() );
#ifdef ARICPP_TRACE_WEBSOCKET
        if ( ec ) LogCallback("*** websocket error: " + ec.message() + '\n', LoggingSeverity::Info);
        else LogCallback("*** <== " + s + '\n', LoggingSeverity::Info);
#endif
        if ( ec ) 
			onReceive( std::string(), ec );
        else 
			onReceive( s, ec );
        rxData.consume( rxData.size() );
        if ( ec != boost::asio::error::eof && ec != boost::asio::error::operation_aborted ) Read();
    }

client.h

Add the ability to pass the custom logging function as a parameter to the constructor:

Client(boost::asio::io_service& ios, const std::string& host, const std::string& port,
	std::string _user, std::string _password, std::string _application, pLogInfoFnType pLogInfoFn) :
	user(std::move(_user)), password(std::move(_password)),
	application(std::move(_application)),
	websocket(ios, host, port, pLogInfoFn),
	httpclient(ios, host, port, user, password, pLogInfoFn),
	LogCallback(pLogInfoFn)
{
	assert(LogCallback != nullptr);
}

~Client() noexcept
{
    LogCallback("~Client() - Close websocket", LoggingSeverity::Info);
    Close();
}

void Connect( ConnectHandler h, std::size_t connectionRetrySeconds )
{
    onConnection = std::move(h);
    LogCallback("Sending websocket connect request", LoggingSeverity::Info);
    websocket.Connect("/ari/events?api_key="+user+":"+password+"&app="+application+"&subscribeAll=false", 
	[this](auto e)
	{
		if (e)
		{
			std::string errMsg = "websocket connect failure: " + e.message();					
			LogCallback(errMsg, LoggingSeverity::Error);
			onConnection(e);
		}
                else this->WebsocketConnected(); // gcc requires "this"
            },
	connectionRetrySeconds );
}

Application code

// 
// Allows the aricpp::client class to log information with the application
//
void LogInPlugin(const string& msg, aricpp::LoggingSeverity sev)
{
	switch (sev) {
	case aricpp::LoggingSeverity::Info:
		MyLogInfo(msg);
		break;
	case aricpp::LoggingSeverity::Warning:
		MyLogWarning(msg);
		break;
	case aricpp::LoggingSeverity::Error:
		MyLogError(msg);
		break;
	default:
		assert(true);	// Don't know this severity setting
	}
}
...
client = make_unique<aricpp::Client>(ios, astHost, astPort, astUser, astPassword, astAppName, LogInPlugin);

```</div>

daniele77 avatar Jun 24 '21 12:06 daniele77