cout/cerr包装ostream只需最少的努力
cout/cerr wrapper ostream with minimal effort
我想更好地学习C++(目前我的C++仅限于它的C子集*咳嗽*…),因此我决定尝试"C++-ify"我从C到C++的一个有用的日志记录函数,(我认为)最好用代码来解释
#include <stdarg.h>
#include <stdio.h>
enum msg_type {
LOG_DBG,
LOG_INF,
LOG_WRN,
LOG_ERR
};
int printf_log(enum msg_type mt, const char *fmt_string, ...)
{
va_list args;
va_start(args, fmt_string);
switch (mt) {
case LOG_DBG:
printf("[D] ");
break;
case LOG_INF:
printf("[I] ");
break;
case LOG_WRN:
printf("[W] ");
break;
case LOG_ERR:
printf("[E] ");
break;
default:
break;
}
int res = vprintf(fmt_string, args);
va_end(args);
return res;
}
int main()
{
int i = 0;
printf_log(LOG_DBG, "i is %dn", i);
i++;
printf_log(LOG_INF, "i is %dn", i);
i++;
printf_log(LOG_WRN, "i is %dn", i);
}
这应该输出:
[D] i is 0
[I] i is 1
[W] i is 2
我对C++版本的想法是有这样的东西:
#include <log.h>
int main()
{
int i = 0;
log::log(log::dbg)<<"i is " << i << "n";
i++;
log::log(log::inf)<< "i is " << i << "n";
i++;
log::log(log::wrn)<< "i is " << i << "n";
}
结果相同。
我特别希望避免对参数进行任何解析(可能除了log::inf
或类似的),而是让它们直接传递到cout
上。
但我真的不知道从哪里开始,所有关于这方面的事情要么期望更多的C++知识,要么希望你实现自己的streambuf
或类似的东西。
我的想法基本上是一样的,所以我尽了最大努力,这是我在互联网上使用不同资源生成的代码:
#include <iostream>
using std::cout;
class Log {
public:
enum msg_type {
dbg =1,
inf,
wrn,
err
};
Log(enum msg_type mt)
{
switch (mt) {
case dbg:
cout << "[D] ";
break;
case inf:
cout << "[I] ";
break;
case wrn:
cout << "[W] ";
break;
case err:
cout << "[E] ";
break;
default:
break;
}
}
template<typename T>
const Log& operator<<(const T& t)
{
std::cout << t;
return *this;
}
};
int main()
{
int i = 0;
Log(Log::dbg)<< "i is " << i++ << "n";
Log(Log::inf)<< "i is " << i++ << "n";
Log(Log::inf)<< "i is " << i++ << "n";
}
显然它不起作用,但我不知道错误消息试图告诉我什么。
G++:
main.cc: In function ‘int main()’:
main.cc:46:34: error: passing ‘const Log’ as ‘this’ argument discards qualifiers [-fpermissive]
Log(Log::dbg)<< "i is " << i++ << "n";
^
main.cc:35:14: note: in call to ‘const Log& Log::operator<<(const T&) [with T = int]’
const Log& operator<<(const T& t)
^
main.cc:46:40: error: passing ‘const Log’ as ‘this’ argument discards qualifiers [-fpermissive]
Log(Log::dbg)<< "i is " << i++ << "n";
^
main.cc:35:14: note: in call to ‘const Log& Log::operator<<(const T&) [with T = char [2]]’
const Log& operator<<(const T& t)
^
Clang:
main.cc:46:30: error: invalid operands to binary expression ('const Log' and 'int')
Log(Log::dbg)<< "i is " << i++ << "n";
~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~
main.cc:35:14: note: candidate function not viable: 'this' argument has type
'const Log', but method is not marked const
const Log& operator<<(const T& t)
^
1 error generated.
当然,最简单的方法就是做一个宏,用例如cout << [I] <<
来替换我记录的任何"伪ostream",但这远没有那么灵活。
有人对如何正确地做到这一点有什么想法吗?我可以向详细描述这一点的资源提示一下吗?
感谢您的帮助!
const Log& operator<<(const T& t)
这意味着,每次在语句中对日志对象使用<<
时,都会返回一个const Log&
。同一语句中的下一个链接的<<
对该const Log&
进行操作,该操作无法工作,因为operator<<
本身未标记为const
。
由于您是在"修改日志",所以在这里设置constness没有多大意义因此,我建议完全删除const
:
Log& operator<<(const T& t)
现在它看起来像是std::ostream
中定义的operator<<
(std::cout
的类型),这是有意义的,因为您实际上只是在为std::cout
创建一个传递。
(现场演示)
Log
对象是常量,因此只能调用常量成员函数。
通过在声明后添加const
关键字,可以说成员函数是常量。
所以你的课应该是这样的(缩写):
class Log {
public:
...
template<typename T>
const Log& operator<<(const T& t) const
// ^^^^^
// Note const keyword here
{
...
}
...
};
- 努力将整数转换为链表。不知道我在这里做错了什么
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- ConstexPR :GCC比Clang更努力地评估ConstexPR
- 我在解决此错误时遇到问题.我正在努力在主函数中传递数组
- 努力将指向成员函数的指针绑定到类模板的T成员,然后在槽中调用
- Cout和Cerr未同步
- 努力使用 C# 从本机 DLL 调用该方法
- 我正在努力处理多维数组输入和输出,我管理输入,但是打印无法按计划工作
- 我正在努力在随机数组中查找最小值,有人知道为什么我的代码不起作用?C++
- 尝试将 std::cerr 重定向到文件时出现访问冲突异常
- 如何将"cout"、"cerr"、"stdout"和"stderr"从C++程序定向到单个文件
- 如何在 std::ofstream 和 std::cerr 之间切换
- 防止 cin、cout、cerr 被实例化
- 努力用链接列表实施设置
- 我应该使用 std::cerr 和 exit() 而不是抛出异常
- 我是否应该努力负责单一责任或最大程度地减少成员变量的数量
- 努力理解一个以十六进制、十进制、bin 和 dec 计算数字总和的程序
- cout 和 cerr 的控制台输出顺序错误
- 努力将在一个函数中创建的数组传递到一个排序函数
- cout/cerr包装ostream只需最少的努力