Index: ext/libicpf/src/log.cpp =================================================================== diff -u -N -r2446443341715955423610c01b43fe7841a10e3e -r6dae57f5e7aeeb965bc018024d8360069f6e15c1 --- ext/libicpf/src/log.cpp (.../log.cpp) (revision 2446443341715955423610c01b43fe7841a10e3e) +++ ext/libicpf/src/log.cpp (.../log.cpp) (revision 6dae57f5e7aeeb965bc018024d8360069f6e15c1) @@ -21,52 +21,37 @@ * \brief Contains the implamentation of a log class. */ #include "log.h" +#include "exception.h" #include #include #include #include #include "macros.h" -#ifdef WIN32 +#if defined(_WIN32) || defined(_WIN64) #include #include #include #else #include #endif -#ifdef WIN32 - #undef vsnprintf - #define vsnprintf _vsnprintf - #undef snprintf - #define snprintf _snprintf -#endif - BEGIN_ICPF_NAMESPACE /// Table of strings representing the log message types -const char_t* __logtype_str[] = { "debug", "info", "warning", "error" }; +const tchar_t* __logtype_str[] = { _t("debug"), _t("info"), _t("warning"), _t("error") }; -/// Global variable initialized when constructing log_file object -log_file* __g_log=NULL; - /** Constructs a log_file object. * \param[in] bGlobal - states if this should be treates as a global instance of the log_file. * Only one global log_file instance could exist in the application. */ -log_file::log_file(bool bGlobal) : +log_file::log_file() : m_pszPath(NULL), m_iMaxSize(262144), m_bLogStd(false), - m_iLogLevel(LT_DEBUG), - m_bGlobal(bGlobal), + m_iLogLevel(level_debug), m_lock() { - if (m_bGlobal) - { - assert(__g_log == NULL); // there is another instance of a global log running - __g_log=this; - } #ifdef WIN32 _fmode=_O_BINARY; #endif @@ -76,58 +61,33 @@ */ log_file::~log_file() { - if (m_bGlobal) - __g_log=NULL; delete [] m_pszPath; } -/** Creates a global instance of a log file. - * \param[in] pszPath - path to a log file to write to - * \param[in] iMaxSize - maximum size of a log file - * \param[in] iLogLevel - minimum log level of the messages to log - * \param[in] bLogStd - log the messages also to stdout/stderr - * \param[in] bClean - cleans the log file upon opening - * \return True if the log file has been successfully initialized or false if not. - */ -bool log_file::create_log(const char_t* pszPath, int_t iMaxSize, int_t iLogLevel, bool bLogStd, bool bClean) -{ - assert(__g_log == NULL); - - __g_log=new log_file(true); - if (!__g_log->init(pszPath, iMaxSize, iLogLevel, bLogStd, bClean)) - { - delete __g_log; - __g_log=NULL; - return false; - } - - return true; -} - /** Initializes the constructed log file. * \param[in] pszPath - path to a log file to write to * \param[in] iMaxSize - maximum size of a log file * \param[in] iLogLevel - minimum log level of the messages to log * \param[in] bLogStd - log the messages also to stdout/stderr * \param[in] bClean - cleans the log file upon opening - * \return True if the log file has been successfully initialized or false if not. */ -bool log_file::init(const char_t* pszPath, int_t iMaxSize, int_t iLogLevel, bool bLogStd, bool bClean) +void log_file::init(const tchar_t* pszPath, int_t iMaxSize, int_t iLogLevel, bool bLogStd, bool bClean) { + // store the path and other params delete [] m_pszPath; - m_pszPath=new char_t[strlen(pszPath)+1]; - strcpy(m_pszPath, pszPath); + m_pszPath=new tchar_t[_tcslen(pszPath)+1]; + _tcscpy(m_pszPath, pszPath); m_iMaxSize=iMaxSize; m_bLogStd=bLogStd; m_iLogLevel=iLogLevel; - FILE* pFile=fopen(pszPath, bClean ? "w" : "a"); + // try to open a file + FILE* pFile=_tfopen(pszPath, bClean ? _t("w") : _t("a")); if (pFile == NULL) - return false; + THROW(exception::format(_t("[log_file::init()] Could not open the specified file (") TSTRFMT _t(")")), 0, 0, 0); fclose(pFile); - return true; } /** Retrieves the current size of a log file. @@ -139,7 +99,7 @@ assert(m_pszPath); int_t iSize=-1; - FILE* pFile=fopen(m_pszPath, "r"); + FILE* pFile=_tfopen(m_pszPath, _t("r")); if (pFile != NULL) { if (fseek(pFile, 0, SEEK_END) == 0) @@ -171,7 +131,7 @@ return false; // establish the new file size (1/3rd of the current size or max_size-add_size) - int_t iNewSize=minval((int_t)(iSize*0.66), m_iMaxSize-iAdd); + int_t iNewSize=minval((int_t)(iSize*0.66), m_iMaxSize-iAdd) & ~1; #ifdef _WIN32 // win32 does not have the ftruncate function, so we have to make some API calls @@ -183,22 +143,23 @@ { // read the string to the eol DWORD dwRD; - char_t szBuffer[4096]; + tchar_t szBuffer[4096/sizeof(tchar_t)]; if (ReadFile(hFile, szBuffer, 4096, &dwRD, NULL)) { - szBuffer[(dwRD > 0) ? dwRD-1 : 0]='\0'; + dwRD/=sizeof(tchar_t); + szBuffer[(dwRD > 0) ? dwRD-1 : 0]=_t('\0'); // replace the /r and /n in the log to the \0 for (DWORD i=0;i LT_DEBUG) + if (m_iLogLevel > level_debug) return; va_list va; va_start(va, pszStr); - logv(LT_DEBUG, false, pszStr, va); + logv(level_debug, false, pszStr, va); va_end(va); } /** Logs a formatted debug message to a log file(also outputs to stdout). * \param[in] pszStr - format string for the given parameters */ -void log_file::logds(const char_t* pszStr, ...) +void log_file::logds(const tchar_t* pszStr, ...) { - if (m_iLogLevel > LT_DEBUG) + if (m_iLogLevel > level_debug) return; va_list va; va_start(va, pszStr); - logv(LT_DEBUG, true, pszStr, va); + logv(level_debug, true, pszStr, va); va_end(va); } #else -void log_file::logd(const char_t* pszStr, ...) +void log_file::logd(const tchar_t* /*pszStr*/, ...) { } -void log_file::logds(const char_t* pszStr, ...) +void log_file::logds(const tchar_t* /*pszStr*/, ...) { } #endif -#if _LOG_LEVEL <= LT_INFO +#ifdef SKIP_LEVEL_INFO /** Logs a formatted informational message to a log file. * \param[in] pszStr - format string for the given parameters */ -void log_file::logi(const char_t* pszStr, ...) +void log_file::logi(const tchar_t* pszStr, ...) { - if (m_iLogLevel > LT_INFO) + if (m_iLogLevel > level_info) return; va_list va; va_start(va, pszStr); - logv(LT_INFO, false, pszStr, va); + logv(level_info, false, pszStr, va); va_end(va); } /** Logs a formatted informational message to a log file(also outputs to stdout). * \param[in] pszStr - format string for the given parameters */ -void log_file::logis(const char_t* pszStr, ...) +void log_file::logis(const tchar_t* pszStr, ...) { - if (m_iLogLevel > LT_INFO) + if (m_iLogLevel > level_info) return; va_list va; va_start(va, pszStr); - logv(LT_INFO, true, pszStr, va); + logv(level_info, true, pszStr, va); va_end(va); } #else -void log_file::logi(const char_t* pszStr, ...) +void log_file::logi(const tchar_t* /*pszStr*/, ...) { } -void log_file::logis(const char_t* pszStr, ...) +void log_file::logis(const tchar_t* /*pszStr*/, ...) { } #endif -#if _LOG_LEVEL <= LT_WARNING +#ifndef SKIP_LEVEL_WARNING /** Logs a formatted warning message to a log file. * \param[in] pszStr - format string for the given parameters */ -void log_file::logw(const char_t* pszStr, ...) +void log_file::logw(const tchar_t* pszStr, ...) { - if (m_iLogLevel > LT_WARNING) + if (m_iLogLevel > level_warning) return; va_list va; va_start(va, pszStr); - logv(LT_WARNING, false, pszStr, va); + logv(level_warning, false, pszStr, va); va_end(va); } /** Logs a formatted warning message to a log file(also outputs to stdout). * \param[in] pszStr - format string for the given parameters */ -void log_file::logws(const char_t* pszStr, ...) +void log_file::logws(const tchar_t* pszStr, ...) { - if (m_iLogLevel > LT_WARNING) + if (m_iLogLevel > level_warning) return; va_list va; va_start(va, pszStr); - logv(LT_WARNING, true, pszStr, va); + logv(level_warning, true, pszStr, va); va_end(va); } #else -void log_file::logw(const char_t* pszStr, ...) +void log_file::logw(const tchar_t* /*pszStr*/, ...) { } -void log_file::logws(const char_t* pszStr, ...) +void log_file::logws(const tchar_t* /*pszStr*/, ...) { } @@ -516,22 +465,22 @@ /** Logs a formatted error message to a log file. * \param[in] pszStr - format string for the given parameters */ -void log_file::loge(const char_t* pszStr, ...) +void log_file::loge(const tchar_t* pszStr, ...) { va_list va; va_start(va, pszStr); - logv(LT_ERROR, false, pszStr, va); + logv(level_error, false, pszStr, va); va_end(va); } /** Logs a formatted error message to a log file(also outputs to stderr). * \param[in] pszStr - format string for the given parameters */ -void log_file::loges(const char_t* pszStr, ...) +void log_file::loges(const tchar_t* pszStr, ...) { va_list va; va_start(va, pszStr); - logv(LT_ERROR, true, pszStr, va); + logv(level_error, true, pszStr, va); va_end(va); } @@ -541,21 +490,21 @@ * \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, ...) +void log_file::logerr(const tchar_t* pszStr, int iSysErr, ...) { - char_t szNewFmt[2048]; + tchar_t szNewFmt[2048]; if (prepare_fmt(pszStr, iSysErr, szNewFmt)) { va_list va; va_start(va, iSysErr); - logv(LT_ERROR, false, szNewFmt, va); + logv(level_error, false, szNewFmt, va); va_end(va); } else { va_list va; va_start(va, iSysErr); - logv(LT_ERROR, false, pszStr, va); + logv(level_error, false, pszStr, va); va_end(va); } } @@ -568,21 +517,21 @@ * \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, ...) +void log_file::logerrs(const tchar_t* pszStr, int iSysErr, ...) { - char_t szNewFmt[2048]; + tchar_t szNewFmt[2048]; if (prepare_fmt(pszStr, iSysErr, szNewFmt)) { va_list va; va_start(va, iSysErr); - logv(LT_ERROR, true, szNewFmt, va); + logv(level_error, true, szNewFmt, va); va_end(va); } else { va_list va; va_start(va, iSysErr); - logv(LT_ERROR, true, pszStr, va); + logv(level_error, true, pszStr, va); va_end(va); } } @@ -594,31 +543,31 @@ * \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) const +bool log_file::prepare_fmt(const tchar_t* pszStr, int iSysErr, tchar_t* pszOut) const { // find the %err in pszStr - const char_t* pszFnd=strstr(pszStr, "%err"); + const tchar_t* pszFnd=_tcsstr(pszStr, _t("%err")); if (pszFnd) { // find an error description for the error - char_t* pszErrDesc=NULL; + tchar_t* pszErrDesc=NULL; #ifdef _WIN32 - char_t szErrDesc[512]; + tchar_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); + tchar_t szError[1024]; + _sntprintf(szError, 1024, _t("0x%lx (%s)"), iSysErr, pszErrDesc); // replace %err with the new data - pszOut[0]='\0'; - strncat(pszOut, pszStr, (size_t)(pszFnd-pszStr)); - strcat(pszOut, szError); - strcat(pszOut, pszFnd+4); + pszOut[0]=_t('\0'); + _tcsncat(pszOut, pszStr, (size_t)(pszFnd-pszStr)); + _tcscat(pszOut, szError); + _tcscat(pszOut, pszFnd+4); return true; }