我需要一个类来包装对 std::clog
的调用这样:
- 每条消息都带有标题前缀,其中包括时间和生成消息的实体的名称。
- 消息根据错误类型(例如调试、警告、错误)进行着色。
- 使用方式完全等同于
std::clog << "..."
对于它的所有特性(即具有隐式基本类型到字符串转换、流操纵器、刷新等的能力)
我的尝试是基于这个论坛 (*) 中找到的许多示例,但我以一种错误的方式猜测,因为我的类有点错误。
本质上我尝试的是扩展 std::streambuf
通过覆盖 overflow
和 xputn
最终调用 clog
的方法的 operator<<
.
注意:我发现很难保留我的问题 complete(**), minimal and verifiable所有在同一时间,所以任何建议/评论将不胜感激。不过,对我来说最重要的是我采用的方法,而不是具体的错误或实现缺陷。
class LogStream : public std::streambuf
{
public:
enum class Color { RED_BRIGHT, RED_DARK, /* ...others */ NO_COLOR };
LogStream(void);
LogStream(std::basic_ostream<char>& out, std::string cname, char c, Color icon_color, Color text_color = Color::NO_COLOR);
/* Non-copiable. */
LogStream(const LogStream&) = delete;
LogStream& operator=(const LogStream&) = delete;
static void setNameLength(int l);
protected:
int_type overflow(int_type c = traits_type::eof());
std::streamsize xsputn(const char* s, std::streamsize n);
private:
/* Important stuff: */
std::basic_ostream<char>& m_out;
bool m_new_line;
void conditionalPrintHeader(void);
void endLine(void);
/* For message decoration purposes: */
Color m_color_icon;
Color m_color_text;
char m_icon;
std::string m_cname;
static std::map<Color, const std::string> m_color_lut;
};
/* A macro to create static instances of a logger later in each CPP file. */
#define CREATE_LOGGER(klass) \
namespace Log { \
static std::ostream dbg(new LogStream( \
std::clog, \
#klass, \
'>', \
LogStream::Color::RED_BRIGHT, \
LogStream::Color::NO_COLOR)); \
static std::ostream err(new LogStream( \
std::clog, \
#klass, \
'e', \
LogStream::Color::RED_BRIGHT, \
LogStream::Color::RED_DARK)); \
}
我重写的函数是这样实现的:
std::streamsize LogStream::xsputn(const char* s, std::streamsize n)
{
conditionalPrintHeader();
if(s[n - 1] == '\n') {
m_new_line = true;
endLine();
}
m_out << s;
return n;
}
LogStream::int_type LogStream::overflow(int_type c)
{
if(c == traits_type::eof()) {
return traits_type::eof();
} else {
char_type ch = traits_type::to_char_type(c);
return xsputn(&ch, 1) == 1 ? c : traits_type::eof();
}
}
void LogStream::conditionalPrintHeader(void)
{
if(m_new_line) {
m_out << "... header and color escape codes ...";
m_new_line = false;
}
}
void LogStream::endLine(void)
{
m_out << "color escape code for no color.";
}
函数conditionalPrintHeader
和 endLine
本质上尝试实现这个基本结构:
[header string] [ANSI color code] [<the log message>] [end color code]
所以当我这样做时:
Log::warn << "Integer: " << 123 << ", Bool: " << std::boolalpha << true << ", Float: " << 3.1415f << "\n";
终端输出:
HEADER + [START COLOR] Integer: 123, Bool: true, Float: 3.1415 [END COLOR]
大多数时候一切都很好,除非我需要打印整数值。我得到的不是数字,而是额外的垃圾,如下所示:
[ ... ] Integer: 123�~c, Bool: true, Float: 3.1415
注释:
(*) 启发或直接促成我的解决方案的类似问题:
- https://stackoverflow.com/a/10921803/1876268 --我在其中采用了大部分实现概念。
- Add time stamp with std::cout
- Correct way to declare/define custom cout-like object
- https://codereview.stackexchange.com/a/187373/176115
- Correct implementation of std::streambuf::overflow
(**) 我粘贴了整个头文件和源文件,以便尽可能完整,以防万一我在其他地方遗漏了错误:Log.hpp , Log.cpp .
最佳答案
现在我认为问题出在 xsputn
的字符串参数上(我假设它不是以 null 结尾的)。我用整数解决了我的问题,如下所示,但我仍然不清楚这种方法是否好。
std::streamsize LogStream::xsputn(const char* s, std::streamsize n)
{
conditionalPrintHeader();
if(s[n - 1] == '\n') {
m_new_line = true;
endLine();
}
std::string str;
for(int c = 0; c < n; c++) {
str += s[c];
}
m_out << str;
return n;
}
关于c++ - 带有颜色和标题的 std::clog 包装器无法正确打印整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52488259/