C++ 如何在C+中自动刷新日志消息并解锁互斥锁+;?
这是我写的。它工作得很好,但有些事情我还没能做到C++ 如何在C+中自动刷新日志消息并解锁互斥锁+;?,c++,c++11,logging,mutex,flush,C++,C++11,Logging,Mutex,Flush,这是我写的。它工作得很好,但有些事情我还没能做到 uLOG(warning) << "Test log message " << 123 << uLOGE; uLOG(warning)您需要重新设计日志框架,以便uLOG是您实例化的类,其析构函数执行uLOGE宏的工作 非常简单的例子: struct uLOG { uLOG(std::string const& type) { std::cout << "
uLOG(warning) << "Test log message " << 123 << uLOGE;
uLOG(warning)您需要重新设计日志框架,以便uLOG
是您实例化的类,其析构函数执行uLOGE
宏的工作
非常简单的例子:
struct uLOG
{
uLOG(std::string const& type)
{
std::cout << "Log: " << type << " - ";
}
template<typename T>
uLOG& operator<<(T const& output)
{
std::cout << output;
return *this;
}
~uLOG()
{
std::cout << " (end of log)" << std::endl;
}
};
// ...
uLOG("warning") << "log message" << 123;
structurlog
{
uLOG(标准::字符串常量和类型)
{
std::cout第二种方法是正确的,如果实现正确,它不需要大括号。你是用宏实现的吗?这里不需要大括号
uLOG应该是一个返回日志编写器类的临时对象的函数。如您所述,它应该在ctor中锁定,在dtor中刷新和解锁,并且还具有模板化运算符。第二个示例中的大括号不是必需的,为什么您认为它们是?:)这可能是真实的issue@OMGtechy例如我把支架放好,以便临时的物体ct超出范围,其析构函数被调用。
struct uLOG
{
uLOG(std::string const& type)
{
std::cout << "Log: " << type << " - ";
}
template<typename T>
uLOG& operator<<(T const& output)
{
std::cout << output;
return *this;
}
~uLOG()
{
std::cout << " (end of log)" << std::endl;
}
};
// ...
uLOG("warning") << "log message" << 123;
Log: warning - log message123 (end of log)