简体   繁体   中英

How to use same wide char string format for windows and linux

Currently, I have some code that will running on Windows/Linux. Now I have a problem when output some information.
On windows, I use _vsnwprintf_s() to support variable parameter. So it support below format.

Log0(L"111");
Log1(L"%s", L"22");
Log2(L"%s, %d", L"33", 44);

On Linux, I can not use vswprintf to format string, but it need use %ls to format wide string.

Debug0(L"111");
Debug1(L"%ls", L"22");

Currently, I want to wrapper a unified function InfoX() to support cross-platform, so it internally will use LogX() or DebugX() base on current OS type.
As you can see, on windows, I will use %s to format wide string, but will use %ls on linux. I do not known how to input at ??? in Info2() function.

Info2(L"???", L"22");

Why don't you use stl library ? It is the better tool to answer to your problem. You use wide char so std::wstring is the best solution. To create your "wstring" value you use "wstringstream" and you don't need to format your charaters.

#include <sstream>
#include <iostream>

void LOG(std::wstring _sLog)
{
    std::wcout << _sLog << '\n';
    //Your LOG code
}

int main()
{

    std::wstringstream oss;
    std::wstring wideValue(L"my wide value :");
    oss << wideValue << 101;
    std::wstring s = oss.str();
    LOG(s);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM