C++变量函数和cout
C++ - variadic functions and cout
我有一个接受可变参数的日志记录函数。这对android日志记录和printf来说很好,但我想对std::cout和文件流也这样做。有简单的方法解决这个问题吗?
void LogManagerImpl::LogInfo(const char* msg, ...)
{
va_list argptr;
va_start(argptr, msg);
/* Log to stdout */
if (mLogToStdOut)
{
#ifdef ANDROID
__android_log_vprint(ANDROID_LOG_INFO, __ENGINE_LOG_TAG, msg, argptr);
#elif defined _WIN32 || _WIN64
//printf ("%s:%s",__ENGINE_LOG_TAG,"INFO:"); vprintf(msg, argptr); printf("n");
// how do I do the same as above except with for example std::cout?
#endif
}
/* Log to file */
if (mLogToFile)
{
// TODO
}
va_end(argptr);
}
不要尝试对C++流使用可变包装器,只需使用相应的C API,如vprintf
/vnsprintf
。以这种方式封装流只会浪费所有的好处,并导致额外的复杂性。
为什么不让你的包装器API使用流,并将它们映射到Android平台上的printf
。这样,你就可以获得流的所有好处,而只会在不支持流的平台上失去这些好处。
相关文章:
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 在 COUT 语句中使用 COUT 调用函数
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- C++代码停止工作错误使用cout内部函数
- 指针变量在 cout 函数中不起作用
- 如果我想使用 cout 为我的函数提供任何输入......我该如何给出
- 使用<<自有函数的运算符来"override" cout
- std::cout 在打印变量与函数表达式时的行为不同
- 是否可以在每次使用std::cout时执行一个函数
- 不能 std::cout 一个max_element函数
- 打印一个带有静态 int 的函数,有一个 std::cout 和多个 std::cout 有什么区别?
- cout 一个类的函数
- 为什么 cout 语句在传递指向函数的指针时没有执行?
- 将参数传递给成员函数,就像使用 std::cout 一样
- 函数不返回值,但 cout 显示它
- 如何使用 std::cout 或 std::ofstream 作为单个函数的输入?
- Std::cout函数作为参数
- 调试断言在 cout 函数上失败
- 简单(return:void) cout函数返回c++的内存位置
- 如何调用cout函数输出流文件