跨平台日志宏不在Windows上工作

时间:2012-02-10 23:33:03

标签: android c++ windows logging printf

我正在使用宏在不同平台上进行简单日志记录。这是我在android上使用的一些内容:

#include <android/log.h>
#define __ENGINE_LOG_INFO(msg, argptr) __android_log_vprint(ANDROID_LOG_INFO, __ENGINE_LOG_TAG, msg, argptr);

以下是我在Windows上试过的相似内容:

#elif defined _WIN32 || _WIN64
#include <stdarg.h>
#include <stdio.h>
#define __ENGINE_LOG_INFO(msg, argptr)  printf ("%s:%s",__ENGINE_LOG_TAG,"DEBUG:"); printf(msg, argptr); printf("\n");

在此函数中调用宏:

void LogManagerImpl::LogInfo(const char* msg, ...)
{
    va_list argptr;
    va_start(argptr, msg);
    __ENGINE_LOG_INFO(msg, argptr);
    va_end(argptr);
}

例如,我这样使用它:

engine->GetLogger()->LogInfo("TEST: MemoryManagerTest:AllocateWithMemPool: Loop time: %d msec", timeStop - timeStart);

这在Android上运行良好,但由于某种原因,它似乎在Windows中打印伪造值(每次都是完全相同的值 - 一个非常大的值)。我开始认为它看起来像一个地址,但我不确定为什么它不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您希望在Windows版本中使用vprintf(msg, argptr);而不是printf(msg, argptr);vprintf()函数设计为使用va_list类型作为实际参数值的容器,该值将与输入字符串中指示的值匹配,其中-printf()为不