跨平台日志宏在 Windows 上不起作用

发布于 2025-01-04 03:27:32 字数 961 浏览 3 评论 0原文

我正在使用宏在不同平台上进行简单的日志记录。这是我在 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 中打印虚假值(每次都是完全相同的值 - 一个非常大的值)。我开始认为它看起来像一个地址,但我不确定为什么它不起作用。有什么想法吗?

I am using a macro to do simple logging on different platforms. Here's some of what I use on android:

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

And here's the corresonding I've tried on 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");

The macro is invoked in this function:

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

For example, I use it like this:

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

This works fine on Android, but for some reason it seems to print bogus values in Windows (it is the exact same value everytime - a very big value). I am starting to think it looks like an address, but I'm not sure why its not working. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油焖大侠 2025-01-11 03:27:32

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

You want to use vprintf(msg, argptr); rather than printf(msg, argptr); in your windows version. The vprintf() function was designed to work with a va_list type as the container for the actual argument values that will be matched against the values indicated in the input character string, where-as printf() was not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文