void StringHelper::toString(bool val, LogString& dst) { if (val) { dst.append(LOG4CXX_STR("true")); } else { dst.append(LOG4CXX_STR("false")); } }
virtual log4cxx_status_t decode(ByteBuffer& in, LogString& out) { enum { BUFSIZE = 256 }; logchar buf[BUFSIZE]; const apr_size_t initial_outbytes_left = BUFSIZE * sizeof(logchar); apr_status_t stat = APR_SUCCESS; if (in.remaining() == 0) { size_t outbytes_left = initial_outbytes_left; { synchronized sync(mutex); stat = apr_xlate_conv_buffer((apr_xlate_t*) convset, NULL, NULL, (char*) buf, &outbytes_left); } out.append(buf, (initial_outbytes_left - outbytes_left)/sizeof(logchar)); } else { while(in.remaining() > 0 && stat == APR_SUCCESS) { size_t inbytes_left = in.remaining(); size_t initial_inbytes_left = inbytes_left; size_t pos = in.position(); apr_size_t outbytes_left = initial_outbytes_left; { synchronized sync(mutex); stat = apr_xlate_conv_buffer((apr_xlate_t*) convset, in.data() + pos, &inbytes_left, (char*) buf, &outbytes_left); } out.append(buf, (initial_outbytes_left - outbytes_left)/sizeof(logchar)); in.position(pos + (initial_inbytes_left - inbytes_left)); } } return stat; }
void SimpleLayout::format(LogString& output, const spi::LoggingEventPtr& event, log4cxx::helpers::Pool&) const { output.append(event->getLevel()->toString()); output.append(LOG4CXX_STR(" - ")); output.append(event->getRenderedMessage()); output.append(LOG4CXX_EOL); }
void testBasic2() { Pool pool; RelativeTimeDateFormat relativeFormat; LogString expected; relativeFormat.format(expected, event->getTimeStamp(), pool); expected.append(LOG4CXX_STR(" INFO [")); expected.append(event->getThreadName()); expected.append(LOG4CXX_STR("] org.foobar - msg 1")); expected.append(LOG4CXX_EOL); assertFormattedEquals(LOG4CXX_STR("%relative %-5level [%thread] %logger - %m%n"), getFormatSpecifiers(), expected); }
virtual log4cxx_status_t decode(ByteBuffer& in, LogString& out) { const char* p = in.current(); size_t i = in.position(); #if !LOG4CXX_CHARSET_EBCDIC for (; i < in.limit() && ((unsigned int) *p) < 0x80; i++, p++) { out.append(1, *p); } in.position(i); #endif if (i < in.limit()) { Pool subpool; const char* enc = apr_os_locale_encoding(subpool.getAPRPool()); { synchronized sync(mutex); if (enc == 0) { if (decoder == 0) { encoding = "C"; decoder = new USASCIICharsetDecoder(); } } else if (encoding != enc) { encoding = enc; try { LogString e; Transcoder::decode(encoding, e); decoder = getDecoder(e); } catch (IllegalArgumentException& ex) { decoder = new USASCIICharsetDecoder(); } } } return decoder->decode(in, out); } return APR_SUCCESS; }
virtual log4cxx_status_t decode(ByteBuffer& in, LogString& out) { size_t remaining = in.remaining(); if( remaining > 0) { const logchar* src = (const logchar*) (in.data() + in.position()); size_t count = remaining / sizeof(logchar); out.append(src, count); in.position(in.position() + remaining); } return APR_SUCCESS; }
void PropertiesPatternConverter::format( const LoggingEventPtr& event, LogString& toAppendTo, Pool& /* p */) const { if (option.length() == 0) { toAppendTo.append(1, (logchar) 0x7B /* '{' */); LoggingEvent::KeySet keySet(event->getMDCKeySet()); for(LoggingEvent::KeySet::const_iterator iter = keySet.begin(); iter != keySet.end(); iter++) { toAppendTo.append(1, (logchar) 0x7B /* '{' */); toAppendTo.append(*iter); toAppendTo.append(1, (logchar) 0x2C /* ',' */); event->getMDC(*iter, toAppendTo); toAppendTo.append(1, (logchar) 0x7D /* '}' */); } toAppendTo.append(1, (logchar) 0x7D /* '}' */); } else { event->getMDC(option, toAppendTo); } }
bool NDC::get(LogString& dest) { ThreadSpecificData* data = ThreadSpecificData::getCurrentData(); if (data != 0) { Stack& stack = data->getStack(); if(!stack.empty()) { dest.append(getFullMessage(stack.top())); return true; } data->recycle(); } return false; }
void StringHelper::toString(log4cxx_int64_t n, Pool& pool, LogString& dst) { if (n >= INT_MIN && n <= INT_MAX) { toString((int) n, pool, dst); } else { const log4cxx_int64_t BILLION = APR_INT64_C(1000000000); int billions = (int) (n / BILLION); char* upper = pool.itoa(billions); int remain = (int) (n - billions * BILLION); if (remain < 0) remain *= -1; char* lower = pool.itoa(remain); Transcoder::decode(upper, dst); dst.append(9 - strlen(lower), 0x30 /* '0' */); Transcoder::decode(lower, dst); } }
LogString OptionConverter::convertSpecialChars(const LogString& s) { logchar c; LogString sbuf; LogString::const_iterator i = s.begin(); while(i != s.end()) { c = *i++; if (c == 0x5C /* '\\' */) { c = *i++; switch (c) { case 0x6E: //'n' c = 0x0A; break; case 0x72: //'r' c = 0x0D; break; case 0x74: //'t' c = 0x09; break; case 0x66: //'f' c = 0x0C; break; default: break; } } sbuf.append(1, c); } return sbuf; }
virtual log4cxx_status_t decode(ByteBuffer& in, LogString& out) { log4cxx_status_t stat = APR_SUCCESS; enum { BUFSIZE = 256 }; wchar_t buf[BUFSIZE]; mbstate_t mbstate; memset(&mbstate, 0, sizeof(mbstate)); while(in.remaining() > 0) { size_t requested = in.remaining(); if (requested > BUFSIZE - 1) { requested = BUFSIZE - 1; } memset(buf, 0, BUFSIZE*sizeof(wchar_t)); const char* src = in.current(); if(*src == 0) { out.append(1, (logchar) 0); in.position(in.position() + 1); } else { size_t converted = mbsrtowcs(buf, &src, requested, &mbstate); if (converted == (size_t) -1) { stat = APR_BADARG; in.position(src - in.data()); break; } else { stat = append(out, buf); in.position(in.position() + converted); } } } return stat; }
LogString OptionConverter::substVars(const LogString& val, Properties& props) { LogString sbuf; const logchar delimStartArray[] = { 0x24, 0x7B, 0 }; const LogString delimStart(delimStartArray); const logchar delimStop = 0x7D; // '}'; const size_t DELIM_START_LEN = 2; const size_t DELIM_STOP_LEN = 1; int i = 0; int j, k; while(true) { j = val.find(delimStart, i); if(j == -1) { // no more variables if(i==0) { // this is a simple string return val; } else { // add the tail string which contails no variables and return the result. sbuf.append(val.substr(i, val.length() - i)); return sbuf; } } else { sbuf.append(val.substr(i, j - i)); k = val.find(delimStop, j); if(k == -1) { LogString msg(1, (logchar) 0x22 /* '\"' */); msg.append(val); msg.append(LOG4CXX_STR("\" has no closing brace. Opening brace at position ")); Pool p; StringHelper::toString(j, p, msg); msg.append(1, (logchar) 0x2E /* '.' */); throw IllegalArgumentException(msg); } else { j += DELIM_START_LEN; LogString key = val.substr(j, k - j); // first try in System properties LogString replacement(getSystemProperty(key, LogString())); // then try props parameter if(replacement.empty()) { replacement = props.getProperty(key); } if(!replacement.empty()) { // Do variable substitution on the replacement string // such that we can solve "Hello ${x2}" as "Hello p1" // the where the properties are // x1=p1 // x2=${x1} LogString recursiveReplacement = substVars(replacement, props); sbuf.append(recursiveReplacement); } i = k + DELIM_STOP_LEN; } } } }
void ThreadPatternConverter::format( const LoggingEventPtr& event, LogString& toAppendTo, Pool& /* p */) const { toAppendTo.append(event->getThreadName()); }
void XMLLayout::format(LogString& output, const spi::LoggingEventPtr& event, Pool& p) const { output.append(LOG4CXX_STR("<log4j:event logger=\"")); Transform::appendEscapingTags(output, event->getLoggerName()); output.append(LOG4CXX_STR("\" timestamp=\"")); StringHelper::toString(event->getTimeStamp()/1000L, p, output); output.append(LOG4CXX_STR("\" level=\"")); Transform::appendEscapingTags(output, event->getLevel()->toString()); output.append(LOG4CXX_STR("\" thread=\"")); Transform::appendEscapingTags(output, event->getThreadName()); output.append(LOG4CXX_STR("\">")); output.append(LOG4CXX_EOL); output.append(LOG4CXX_STR("<log4j:message><![CDATA[")); // Append the rendered message. Also make sure to escape any // existing CDATA sections. Transform::appendEscapingCDATA(output, event->getRenderedMessage()); output.append(LOG4CXX_STR("]]></log4j:message>")); output.append(LOG4CXX_EOL); LogString ndc; if(event->getNDC(ndc)) { output.append(LOG4CXX_STR("<log4j:NDC><![CDATA[")); Transform::appendEscapingCDATA(output, ndc); output.append(LOG4CXX_STR("]]></log4j:NDC>")); output.append(LOG4CXX_EOL); } if(locationInfo) { output.append(LOG4CXX_STR("<log4j:locationInfo class=\"")); const LocationInfo& locInfo = event->getLocationInformation(); LOG4CXX_DECODE_CHAR(className, locInfo.getClassName()); Transform::appendEscapingTags(output, className); output.append(LOG4CXX_STR("\" method=\"")); LOG4CXX_DECODE_CHAR(method, locInfo.getMethodName()); Transform::appendEscapingTags(output, method); output.append(LOG4CXX_STR("\" file=\"")); LOG4CXX_DECODE_CHAR(fileName, locInfo.getFileName()); Transform::appendEscapingTags(output, fileName); output.append(LOG4CXX_STR("\" line=\"")); StringHelper::toString(locInfo.getLineNumber(), p, output); output.append(LOG4CXX_STR("\"/>")); output.append(LOG4CXX_EOL); } if (properties) { KeySet propertySet(event->getPropertyKeySet()); KeySet keySet(event->getMDCKeySet()); if (!(keySet.empty() && propertySet.empty())) { output.append(LOG4CXX_STR("<log4j:properties>")); output.append(LOG4CXX_EOL); for (KeySet::const_iterator i = keySet.begin(); i != keySet.end(); i++) { LogString key(*i); LogString value; if(event->getMDC(key, value)) { output.append(LOG4CXX_STR("<log4j:data name=\"")); Transform::appendEscapingTags(output, key); output.append(LOG4CXX_STR("\" value=\"")); Transform::appendEscapingTags(output, value); output.append(LOG4CXX_STR("\"/>")); output.append(LOG4CXX_EOL); } } for (KeySet::const_iterator i2 = propertySet.begin(); i2 != propertySet.end(); i2++) { LogString key(*i2); LogString value; if(event->getProperty(key, value)) { output.append(LOG4CXX_STR("<log4j:data name=\"")); Transform::appendEscapingTags(output, key); output.append(LOG4CXX_STR("\" value=\"")); Transform::appendEscapingTags(output, value); output.append(LOG4CXX_STR("\"/>")); output.append(LOG4CXX_EOL); } } output.append(LOG4CXX_STR("</log4j:properties>")); output.append(LOG4CXX_EOL); } } output.append(LOG4CXX_STR("</log4j:event>")); output.append(LOG4CXX_EOL); output.append(LOG4CXX_EOL); }
/** * Formats a millisecond count into a date/time string. * * @param now Number of milliseconds after midnight 1 Jan 1970 GMT. * @param sbuf the string buffer to write to */ void CachedDateFormat::format(LogString& buf, log4cxx_time_t now, Pool& p) const { // // If the current requested time is identical to the previously // requested time, then append the cache contents. // if (now == previousTime) { buf.append(cache); return; } // // If millisecond pattern was not unrecognized // (that is if it was found or milliseconds did not appear) // if (millisecondStart != UNRECOGNIZED_MILLISECONDS) { // Check if the cache is still valid. // If the requested time is within the same integral second // as the last request and a shorter expiration was not requested. if (now < slotBegin + expiration && now >= slotBegin && now < slotBegin + 1000000L) { // // if there was a millisecond field then update it // if (millisecondStart >= 0 ) { millisecondFormat((int) ((now - slotBegin)/1000), cache, millisecondStart); } // // update the previously requested time // (the slot begin should be unchanged) previousTime = now; buf.append(cache); return; } } // // could not use previous value. // Call underlying formatter to format date. cache.erase(cache.begin(), cache.end()); formatter->format(cache, now, p); buf.append(cache); previousTime = now; slotBegin = (previousTime / 1000000) * 1000000; if (slotBegin > previousTime) { slotBegin -= 1000000; } // // if the milliseconds field was previous found // then reevaluate in case it moved. // if (millisecondStart >= 0) { millisecondStart = findMillisecondStart(now, cache, formatter, p); } }
void MessagePatternConverter::format( const LoggingEventPtr& event, LogString& toAppendTo, Pool& /* p */) const { toAppendTo.append(event->getRenderedMessage()); }
void LevelPatternConverter::format( const LoggingEventPtr& event, LogString& toAppendTo, log4cxx::helpers::Pool& /* p */) const { toAppendTo.append(event->getLevel()->toString()); }
inline log4cxx_status_t append(LogString& out, const wchar_t* buf) { out.append(buf); return APR_SUCCESS; }