如何获取boost :: asio :: io_service当前动作编号 [英] How to get boost::asio::io_service current action number
问题描述
Boost :: asio :: io_service为调试目的提供了处理程序跟踪,它通过定义BOOST_ASIO_ENABLE_HANDLER_TRACKING启用,但将其数据记录到stderr。我想在我的应用程序中使用此跟踪信息。我的问题是什么是最好的方式访问< action>
在我的应用程序?
Boost::asio::io_service provides "handler tracking" for debugging purposes, it is enabled by defining BOOST_ASIO_ENABLE_HANDLER_TRACKING but logs its data to stderr. I'd like to use this tracking information in my application. My question is what is the best way to get access to the <action>
inside my application?
有关为什么的更多上下文,我想这样做;我想将< action>
作为参数附加到其他异步操作,以便我可以跟踪源请求来自哪里。
For more context as to why I want to do this; I would like to attach the <action>
as a parameter to other async operations so that I can track where the originating request came from.
推荐答案
一个模仿asio调试处理程序跟踪的示例。注意事项:
An example that mimics asio debug handler tracking. Caveats:
- 假设ioService仅从单一主题执行。
- 非线程安全访问
std :: cerr
code> - 将此留作练习。
- Assumes ioService only run from a single thread. I never use any other way so I'm not sure what needs to change to fix this limitation.
- Non-thread safe access to
std::cerr
- fixing this left as an exercise.
代码:
#include <boost/asio.hpp>
#include <boost/atomic.hpp>
#include <iostream>
class HandlerTracking
{
public:
HandlerTracking()
:
mCount(1)
{ }
template <class Handler>
class WrappedHandler
{
public:
WrappedHandler(HandlerTracking& t, Handler h, std::uint64_t id)
:
mHandlerTracking(t),
mHandler(h),
mId(id)
{ }
WrappedHandler(const WrappedHandler& other)
:
mHandlerTracking(other.mHandlerTracking),
mHandler(other.mHandler),
mId(other.mId),
mInvoked(other.mInvoked)
{
other.mInvoked = true;
}
~WrappedHandler()
{
if (!mInvoked)
std::cerr << '~' << mId << std::endl;
}
template <class... Args>
void operator()(Args... args)
{
mHandlerTracking.mCurrHandler = mId;
std::cerr << '>' << mId << std::endl;
try
{
mInvoked = true;
mHandler(args...);
}
catch(...)
{
std::cerr << '!' << mId << std::endl;
throw;
}
std::cerr << '<' << mId << std::endl;
}
const std::uint64_t id() { return mId; }
private:
HandlerTracking& mHandlerTracking;
Handler mHandler;
const std::uint64_t mId;
mutable bool mInvoked = false;
};
template <class Handler>
WrappedHandler<Handler> wrap(Handler handler)
{
auto next = mCount.fetch_add(1);
std::cerr << mCurrHandler << '*' << next << std::endl;
return WrappedHandler<Handler>(*this, handler, next);
}
boost::atomic<std::uint64_t> mCount;
std::uint64_t mCurrHandler = 0; // Note: If ioService run on multiple threads we need a curr handler per thread
};
// Custom invokation hook for wrapped handlers
//template <typename Function, typename Handler>
//void asio_handler_invoke(Function f, HandlerTracking::WrappedHandler<Handler>* h)
//{
// std::cerr << "Context: " << h << ", " << h->id() << ", " << f.id() << std::endl;
// f();
//}
// Class to demonstrate callback with arguments
class MockSocket
{
public:
MockSocket(boost::asio::io_service& ioService) : mIoService(ioService) {}
template <class Handler>
void async_read(Handler h)
{
mIoService.post([h]() mutable { h(42); }); // we always read 42 bytes
}
private:
boost::asio::io_service& mIoService;
};
int main(int argc, char* argv[])
{
boost::asio::io_service ioService;
HandlerTracking tracking;
MockSocket socket(ioService);
std::function<void()> f1 = [&]() { std::cout << "Handler1" << std::endl; };
std::function<void()> f2 = [&]() { std::cout << "Handler2" << std::endl; ioService.post(tracking.wrap(f1)); };
std::function<void()> f3 = [&]() { std::cout << "Handler3" << std::endl; ioService.post(tracking.wrap(f2)); };
std::function<void()> f4 = [&]() { std::cout << "Handler4" << std::endl; ioService.post(tracking.wrap(f3)); };
std::function<void(int)> s1 = [](int s) { std::cout << "Socket read " << s << " bytes" << std::endl; };
socket.async_read(tracking.wrap(s1));
ioService.post(tracking.wrap(f1));
ioService.post(tracking.wrap(f2));
ioService.post(tracking.wrap(f3));
auto tmp = tracking.wrap(f4); // example handler destroyed without invocation
ioService.run();
return 0;
}
输出:
0*1
0*2
0*3
0*4
0*5
>1
Socket read 42 bytes
<1
>2
Handler1
<2
>3
Handler2
3*6
<3
>4
Handler3
4*7
<4
>6
Handler1
<6
>7
Handler2
7*8
<7
>8
Handler1
<8
~5
这篇关于如何获取boost :: asio :: io_service当前动作编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!