void decode2() { char buf[BUFSIZE + 6]; memset(buf, 'A', BUFSIZE); buf[BUFSIZE - 3] = 0; #if defined(__STDC_LIB_EXT1__) || defined(__STDC_SECURE_LIB__) strcat_s(buf, sizeof buf, "Hello"); #else strcat(buf, "Hello"); #endif ByteBuffer src(buf, strlen(buf)); CharsetDecoderPtr dec(CharsetDecoder::getDefaultDecoder()); LogString greeting; log4cxx_status_t stat = dec->decode(src, greeting); LOGUNIT_ASSERT_EQUAL(APR_SUCCESS, stat); LOGUNIT_ASSERT_EQUAL((size_t) 0, src.remaining()); stat = dec->decode(src, greeting); LOGUNIT_ASSERT_EQUAL(APR_SUCCESS, stat); LogString manyAs(BUFSIZE - 3, LOG4CXX_STR('A')); LOGUNIT_ASSERT_EQUAL(manyAs, greeting.substr(0, BUFSIZE - 3)); LOGUNIT_ASSERT_EQUAL(LogString(LOG4CXX_STR("Hello")), greeting.substr(BUFSIZE - 3)); }
LevelPtr OptionConverter::toLevel(const LogString& value, const LevelPtr& defaultValue) { size_t hashIndex = value.find(LOG4CXX_STR("#")); if (hashIndex == LogString::npos) { if (value.empty()) { return defaultValue; } else { LogLog::debug( ((LogString) LOG4CXX_STR("OptionConverter::toLevel: no class name specified, level=[")) + value +LOG4CXX_STR("]")); // no class name specified : use standard Level class return Level::toLevelLS(value, defaultValue); } } LogString clazz = value.substr(hashIndex + 1); LogString levelName = value.substr(0, hashIndex); LogLog::debug(((LogString) LOG4CXX_STR("OptionConverter::toLevel: class=[")) + clazz + LOG4CXX_STR("], level=[") + levelName + LOG4CXX_STR("]")); // This is degenerate case but you never know. if (levelName.empty()) { return Level::toLevelLS(value, defaultValue); } try { Level::LevelClass& levelClass = (Level::LevelClass&)Loader::loadClass(clazz); return levelClass.toLevel(levelName); } catch (ClassNotFoundException&) { LogLog::warn(((LogString) LOG4CXX_STR("custom level class [")) + clazz + LOG4CXX_STR("] not found.")); } catch(Exception& oops) { LogLog::warn( LOG4CXX_STR("class [") + clazz + LOG4CXX_STR("], level [") + levelName + LOG4CXX_STR("] conversion) failed."), oops); } catch(...) { LogLog::warn( LOG4CXX_STR("class [") + clazz + LOG4CXX_STR("], level [") + levelName + LOG4CXX_STR("] conversion) failed.")); } return defaultValue; }
void decode7() { // // normal characters striding over a buffer boundary // std::string longMsg(BUFSIZE - 2, 'A'); longMsg.append("Hello"); LogString decoded; Transcoder::decode(longMsg, decoded); LOGUNIT_ASSERT_EQUAL((size_t) BUFSIZE + 3, decoded.length()); LOGUNIT_ASSERT_EQUAL(LogString(BUFSIZE -2, LOG4CXX_STR('A')), decoded.substr(0, BUFSIZE - 2)); LOGUNIT_ASSERT_EQUAL((LogString) LOG4CXX_STR("Hello"), decoded.substr(BUFSIZE -2 )); }
void PropertySetter::setProperties(helpers::Properties& properties, const LogString& prefix, Pool& p) { int len = prefix.length(); std::vector<LogString> names = properties.propertyNames(); std::vector<LogString>::iterator it; for (it = names.begin(); it != names.end(); it++) { LogString key = *it; // handle only properties that start with the desired frefix. if (key.find(prefix) == 0) { // ignore key if it contains dots after the prefix if (key.find(0x2E /* '.' */, len + 1) != LogString::npos) { continue; } LogString value = OptionConverter::findAndSubst(key, properties); key = key.substr(len); if (key == LOG4CXX_STR("layout") && obj != 0 && obj->instanceof(Appender::getStaticClass())) { continue; } setProperty(key, value, p); } } activate(p); }
bool OptionConverter::toBoolean(const LogString& value, bool dEfault) { if (value.length() >= 4) { if (StringHelper::equalsIgnoreCase(value.substr(0,4), LOG4CXX_STR("TRUE"), LOG4CXX_STR("true"))) { return true; } } if (dEfault && value.length() >= 5) { if (StringHelper::equalsIgnoreCase(value.substr(0,5), LOG4CXX_STR("FALSE"), LOG4CXX_STR("false"))) { return false; } } return dEfault; }
LogString StringHelper::trim(const LogString& s) { LogString::size_type pos = s.find_first_not_of(' '); if (pos == std::string::npos) { return LogString(); } LogString::size_type n = s.find_last_not_of(' ') - pos + 1; return s.substr(pos, n); }
void Hierarchy::updateParents(LoggerPtr logger) { synchronized sync(mutex); const LogString name(logger->getName()); int length = name.size(); bool parentFound = false; // if name = "w.x.y.z", loop thourgh "w.x.y", "w.x" and "w", but not "w.x.y.z" for(size_t i = name.find_last_of(0x2E /* '.' */, length-1); i != LogString::npos; i = name.find_last_of(0x2E /* '.' */, i-1)) { LogString substr = name.substr(0, i); LoggerMap::iterator it = loggers->find(substr); if(it != loggers->end()) { parentFound = true; logger->parent = it->second; break; // no need to update the ancestors of the closest ancestor } else { ProvisionNodeMap::iterator it2 = provisionNodes->find(substr); if (it2 != provisionNodes->end()) { it2->second.push_back(logger); } else { ProvisionNode node(1, logger); provisionNodes->insert( ProvisionNodeMap::value_type(substr, node)); } } } // If we could not find any existing parents, then link with root. if(!parentFound) { logger->parent = root; } }
long OptionConverter::toFileSize(const LogString& s, long dEfault) { if(s.empty()) { return dEfault; } size_t index = s.find_first_of(LOG4CXX_STR("bB")); if (index != LogString::npos && index > 0) { long multiplier = 1; index--; if (s[index] == 0x6B /* 'k' */ || s[index] == 0x4B /* 'K' */) { multiplier = 1024; } else if(s[index] == 0x6D /* 'm' */ || s[index] == 0x4D /* 'M' */) { multiplier = 1024*1024; } else if(s[index] == 0x67 /* 'g'*/ || s[index] == 0x47 /* 'G' */) { multiplier = 1024*1024*1024; } return toInt(s.substr(0, index), 1) * multiplier; } return toInt(s, 1); }
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; } } } }
const TimeZonePtr TimeZone::getTimeZone( const LogString & id ) { const logchar gmt[] = { 0x47, 0x4D, 0x54, 0 }; if ( id == gmt ) { return log4cxx::helpers::TimeZoneImpl::GMTTimeZone::getInstance(); } if ( id.length() >= 5 && id.substr( 0, 3 ) == gmt ) { int hours = 0; int minutes = 0; int sign = 1; if (id[3] == 0x2D /* '-' */) { sign = -1; } LogString off( id.substr( 4 ) ); if ( id.length() >= 7 ) { size_t colonPos = off.find( 0x3A /* ':' */); if ( colonPos == LogString::npos ) { minutes = StringHelper::toInt(off.substr(off.length() - 2)); hours = StringHelper::toInt(off.substr(0, off.length() - 2)); } else { minutes = StringHelper::toInt(off.substr(colonPos + 1)); hours = StringHelper::toInt(off.substr(0, colonPos)); } } else { hours = StringHelper::toInt(off); } LogString s(gmt); Pool p; LogString hh; StringHelper::toString(hours, p, hh); if (sign > 0) { s.append(1, (logchar) 0x2B /* '+' */); } else { s.append(1, (logchar) 0x2D /* '-' */); } if (hh.length() == 1) { s.append(1, (logchar) 0x30 /* '0' */); } s.append(hh); s.append(1, (logchar) 0x3A /*' :' */); LogString mm; StringHelper::toString(minutes, p, mm); if (mm.length() == 1) { s.append(1, (logchar) 0x30 /* '0' */); } s.append(mm); apr_int32_t offset = sign * (hours * 3600 + minutes * 60); return new log4cxx::helpers::TimeZoneImpl::FixedTimeZone( s, offset ); } const TimeZonePtr & ltz = getDefault(); if ( ltz->getID() == id ) { return ltz; } return getGMT(); }