// Both MSVC6 and 7 seem to use the non-standard vsnprintf // so we are using vscprintf to determine buffer size, however // only MSVC7 headers include vscprintf for some reason. std::string vformat(const char *fmt, va_list argPtr) { // We draw the line at a 1MB string. const int maxSize = 1000000; // If the string is less than 161 characters, // allocate it on the stack because this saves // the malloc/free time. const int bufSize = 161; char stackBuffer[bufSize]; int actualSize = _vscprintf(fmt, argPtr) + 1; if (actualSize > bufSize) { // Now use the heap. char* heapBuffer = NULL; if (actualSize < maxSize) { heapBuffer = (char*)System::malloc(maxSize + 1); vsnprintf(heapBuffer, maxSize, fmt, argPtr); heapBuffer[maxSize] = '\0'; } else { heapBuffer = (char*)System::malloc(actualSize); vsprintf(heapBuffer, fmt, argPtr); } std::string formattedString(heapBuffer); System::free(heapBuffer); return formattedString; } else { vsprintf(stackBuffer, fmt, argPtr); return std::string(stackBuffer); } }
void FileAppender::append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, const char* function, const QString& message) { QMutexLocker locker(&m_logFileMutex); openFile(); m_logStream << formattedString(timeStamp, logLevel, file, line, function, message); m_logStream.flush(); m_logFile.flush(); }
void LogWriter::vprintLog(int logLevel, const TCHAR *fmt, va_list argList) { if (m_logger != 0) { // Format the original string. int count = _vsctprintf(fmt, argList); std::vector<TCHAR> formattedString(count + 1); _vstprintf(&formattedString.front(), fmt, argList); m_logger->print(logLevel, &formattedString.front()); } }
void ReplaceSpaces(StrPtrLen *sourcePtr, StrPtrLen *destPtr, char *replaceStr) { if ((NULL != destPtr) && (NULL != destPtr->Ptr) && (0 < destPtr->Len)) destPtr->Ptr[0] = 0; do { if ((NULL == sourcePtr) || (NULL == destPtr) || (NULL == sourcePtr->Ptr) || (NULL == destPtr->Ptr) || (0 == sourcePtr->Len) || (0 == destPtr->Len) ) break; if (0 == sourcePtr->Ptr[0]) { destPtr->Len = 0; break; } const StrPtrLen replaceValue(replaceStr); StringFormatter formattedString(destPtr->Ptr, destPtr->Len); StringParser sourceStringParser(sourcePtr); StrPtrLen preStopChars; do { sourceStringParser.ConsumeUntil(&preStopChars, StringParser::sEOLWhitespaceMask); if (preStopChars.Len > 0) { formattedString.Put(preStopChars);// copy the string up to the space or eol. it will be truncated if there's not enough room. if (sourceStringParser.Expect(' ') && (formattedString.GetSpaceLeft() > replaceValue.Len)) { formattedString.Put(replaceValue.Ptr, replaceValue.Len); } else //no space character or no room for replacement { break; } } } while (preStopChars.Len != 0); destPtr->Set(formattedString.GetBufPtr(), formattedString.GetBytesWritten()); } while (false); }
std::string sqlFormat(char c) { std::string formattedString("\'"); formattedString.append(1, c); formattedString += '\''; return formattedString; }
/** * \sa AbstractStringAppender::format() */ void ConsoleAppender::append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, const char* function, const QString& category, const QString& message) { std::cerr << qPrintable(formattedString(timeStamp, logLevel, file, line, function, category, message)); }