c++ - 如何在 C++ 中连接函数 m_stream 中的字符串?

标签 c++ logging concatenation

我正在用 C++ 为我的程序编写记录器。

我有这段代码:

void Log::Write(char* logline)
{
    time_t rawtime;
    struct tm * timeinfo;
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    m_stream << asctime (timeinfo) << logline << endl;
}

这是一个日志

2011 年 10 月 11 日星期二 13:07:28

我想像这样在一行中输出不同的内容: 2011 年 10 月 11 日星期二 13:07:28 - 这是日志

我该怎么做? 谢谢!

最佳答案

docs 中所述:

The string result produced by asctime contains exactly 26 characters and has the form Wed Jan 02 02:03:55 1980\n\0

所以如果你不想写行结束符号,你可以使用'write'函数来指定确切的字符数,这样写

time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
m_stream.write( asctime (timeinfo), 24 );
m_stream << " - " << logline << std::endl;

当然在那种情况下你应该写评论解释为什么使用“24”...

关于c++ - 如何在 C++ 中连接函数 m_stream 中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7723794/

相关文章:

c++ - 列表迭代器不可取消引用

c++ - std::map - 无异常且无插入的元素访问

c++ - 在构造函数成员初始化之前调用成员函数的语法

logging - 我可以将日志写入/添加到位于hdfs中的压缩文件中吗?

logging - 启动 tomcat 作为服务不登录 catalina.out

mysql - 在坏表上加入 MySQL 表

c++ - 有没有办法到达最后的盒子 - GRAPH

database - Logback dbAppender 自定义 SQL

MySQL:连接两个信息,返回一个虚拟列并依次与该列执行 INNER JOIN

PHP 连接变为 NULL