Index: ext/libicpf/src/log.cpp =================================================================== diff -u -r9a6c943d8b2737422c3652980c8cce2c03679422 -raea5c1302fae74ec41e9d7649f8b8b2b8138aee1 --- ext/libicpf/src/log.cpp (.../log.cpp) (revision 9a6c943d8b2737422c3652980c8cce2c03679422) +++ ext/libicpf/src/log.cpp (.../log.cpp) (revision aea5c1302fae74ec41e9d7649f8b8b2b8138aee1) @@ -37,7 +37,10 @@ #endif #ifdef WIN32 - #define vsnprintf _vsnprintf; + #undef vsnprintf + #define vsnprintf _vsnprintf + #undef snprintf + #define snprintf _snprintf #endif BEGIN_ICPF_NAMESPACE @@ -528,4 +531,95 @@ va_end(va); } +/** Logs a formatted error message to a log file(also outputs to stderr). + * As an addition the first string %err is replaced with a given error + * followed by the error description (system-based). + * \param[in] pszStr - format string for the given parameters + * \param[in] iSysErr - system error to be shown + */ +void log_file::logerr(const char_t* pszStr, int iSysErr, ...) +{ + char_t szNewFmt[2048]; + if (prepare_fmt(pszStr, iSysErr, szNewFmt)) + { + va_list va; + va_start(va, iSysErr); + logv(LT_ERROR, false, szNewFmt, va); + va_end(va); + } + else + { + va_list va; + va_start(va, iSysErr); + logv(LT_ERROR, false, pszStr, va); + va_end(va); + } +} + +/** Logs a formatted error message to a log file(also outputs to stderr). + * As an addition the first string %err is replaced with a given error + * followed by the error description (system-based). + * This function differ from logerr() with logging the output string + * also to the stderr. + * \param[in] pszStr - format string for the given parameters + * \param[in] iSysErr - system error to be shown + */ +void log_file::logerrs(const char_t* pszStr, int iSysErr, ...) +{ + char_t szNewFmt[2048]; + if (prepare_fmt(pszStr, iSysErr, szNewFmt)) + { + va_list va; + va_start(va, iSysErr); + logv(LT_ERROR, true, szNewFmt, va); + va_end(va); + } + else + { + va_list va; + va_start(va, iSysErr); + logv(LT_ERROR, true, pszStr, va); + va_end(va); + } +} + +/** Function prepares a format string with error number and an error message + * for use with logerr() and logerrs() functions. + * \param[in] pszStr - input format string (%err will be replaced with a 0x%lx (error message) + * \param[in] iSysError - system error to parse + * \param[out] pszOut - pointer to a buffer that will receive the data (must be 2048 bytes in size) + * \return If the %err string was found and replaced within a given format string. + */ +bool log_file::prepare_fmt(const char_t* pszStr, int iSysErr, char_t* pszOut) +{ + // find the %err in pszStr + char_t* pszFnd=strstr(pszStr, "%err"); + if (pszFnd) + { + // find an error description for the error + char_t* pszErrDesc=NULL; +#ifdef _WIN32 + char_t szErrDesc[512]; + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, (DWORD)iSysErr, 0, szErrDesc, 512, NULL); + pszErrDesc=szErrDesc; +#else + pszErrDesc=strerror(iSysErr); +#endif + + // format a string with err no and desc + char_t szError[1024]; + snprintf(szError, 1024, "0x%lx (%s)", iSysErr, pszErrDesc); + + // replace %err with the new data + pszOut[0]='\0'; + strncat(pszOut, pszStr, pszFnd-pszStr); + strcat(pszOut, szError); + strcat(pszOut, pszFnd+4); + + return true; + } + else + return false; +} + END_ICPF_NAMESPACE Index: ext/libicpf/src/log.h =================================================================== diff -u -r01f412dddb725bcd82c0dce8755af7f113a101d8 -raea5c1302fae74ec41e9d7649f8b8b2b8138aee1 --- ext/libicpf/src/log.h (.../log.h) (revision 01f412dddb725bcd82c0dce8755af7f113a101d8) +++ ext/libicpf/src/log.h (.../log.h) (revision aea5c1302fae74ec41e9d7649f8b8b2b8138aee1) @@ -141,6 +141,9 @@ void loge(const char_t* pszStr, ...); ///< Logs an error message with formatting void loges(const char_t* pszStr, ...); ///< Logs an error message with formatting(also prints to stderr) + + void logerr(const char_t* pszStr, int iSysErr, ...); ///< Logs an error message with system error number and error description + void logerrs(const char_t* pszStr, int iSysErr, ...); ///< Logs an error message with system error number and error description (also prints to stderr) /**@}*/ /// Gets the global instance of the log file @@ -155,6 +158,10 @@ bool truncate(int_t iAdd); /// Returns the size of a log file int_t size(); + +private: + /// Prepares a new format string for logerr(s) functions + bool prepare_fmt(const char_t* pszStr, int iSysErr, char_t* pszOut); public: char_t* m_pszPath; ///< Path to the log file