Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom logging function in aricpp::Client #58

Open
daniele77 opened this issue Jun 24, 2021 Discussed in #57 · 0 comments
Open

Allow custom logging function in aricpp::Client #58

daniele77 opened this issue Jun 24, 2021 Discussed in #57 · 0 comments
Assignees

Comments

@daniele77
Copy link
Owner

Discussed in #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 daniele77 self-assigned this Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant