I'm using following code to format HRESULT
to message and write the message to file only if HRESULT
is an error.
The code compiles and works fine, except that I'm getting following compiler warning:
Warning C6031 Return value ignored: 'wcsrchr'.
I don't want to disable warning but to resolve it, but I'm not able to figure out how? Here is a minimum compilable code:
// compile with: /Wall
#include <Windows.h>
#include <cwchar> // std::wcsrchr
#include <comdef.h> // _com_error
#include <iostream> // std::cin
// Show only file name instead of full path wide version
#define __FILENAME__ (std::wcsrchr(TEXT(__FILE__), L'\\') ? std::wcsrchr(TEXT(__FILE__), L'\\') + 1 : TEXT(__FILE__))
// Writes a sprintf-formatted string to the logging file.
#define TRACE(...) DebugLogTrace(__VA_ARGS__)
// Log HRESULTs if failed.
#define LOG_IF_FAILED(file_name, line, hr) if constexpr (FAILED(hr)) \
{ TRACE((TEXT("%s %i %s"), file_name, line, _com_error(hr).ErrorMessage())); }
// Writes a sprintf-formatted string to the logging file.
void DebugLogTrace(PCTSTR format_string, ...) noexcept
{
// implementation not important
}
int main()
{
// generate example failure
LOG_IF_FAILED(__FILENAME__, __LINE__, E_FAIL);
std::cin.get();
return 0;
}
Example file output for E_FAIL
error code:
7:50:11 Unspecified error