int main(int argc, const char * argv[]) { // Testing Cache LRUCache<int> cache(10); for (int i = 0; i < 20; i++) { cache.put(make_prefix(i), i*10); } for (int i = 0; i < 20; i++){ std::string key = make_prefix(i); std::cout << key << ": "; if (cache.exists(key)) { std::cout << cache.get(key); } else { std::cout << "<none>"; } std::cout << std::endl; } }
std::string concat_msg(level l, const std::vector<std::string>& args) { auto msg = make_prefix(l); const auto prefix_length = msg.length(); for(const auto& arg: args) { msg += replace_newlines(arg, prefix_length); } msg += '\n'; return msg; }
static void do_error( int level, int line, const char *fmt, va_list args ) { if(!error_stream) { error_stream = stderr; } if(error_level>=level) { fprintf(error_stream,"%s ",make_prefix(line)); vfprintf(error_stream,fmt,args); fprintf(error_stream,"\n"); fflush(error_stream); } }
int configure_output(char *arg) { static int prefix_flag = 1; logpool_init(LOGPOOL_DEFAULT); if(arg == NULL) { if(ltrace == NULL) ltrace = ltrace_open_string_data32(NULL); if(prefix_flag) make_prefix(); prefix_flag = 0; } else { char opt_log_stderr[] = "-log=stderr"; char opt_log_syslog[] = "-log=syslog"; char opt_log_file[] = "-log=file"; char opt_log_memcached[] = "-log=memcached"; char opt_prefix[] = "-prefix="; if(strncmp(arg, opt_log_stderr, OPT_LOG_STDERR_LEN) == 0) { if(ltrace == NULL) ltrace = ltrace_open_string_data32(NULL); } else if(strncmp(arg, opt_log_syslog, OPT_LOG_SYSLOG_LEN) == 0) { if(ltrace == NULL) ltrace = ltrace_open_syslog_data32(NULL); } else if(strncmp(arg, opt_log_file, OPT_LOG_FILE_LEN) == 0) { if(ltrace == NULL) ltrace = ltrace_open_file_data32(NULL, "LOG"); } else if(strncmp(arg, opt_log_memcached, OPT_LOG_MEMCACHED_LEN) == 0) { if(ltrace == NULL) { char *ip = get_server_ip(); char *host; int port; if(ip != NULL) { host = strtok(ip, ":"); port = atoi(strtok(NULL, ":")); } else { host = "localhost"; port = 11211; } ltrace = ltrace_open_memcache_data32(NULL, host, port); } } else if(strncmp(arg, opt_prefix, OPT_PREFIX_LEN) == 0) { arg += 8; if(prefix_flag) memcpy(prefix, arg, strlen(arg)); prefix_flag = 0; } else { return 1; } } return 0; }
std::string format_msg(level l, const std::string& format, std::vector<std::string> args) { const auto prefix = make_prefix(l); const auto length = prefix.length(); const auto fmt = replace_newlines(format, length); std::transform(args.begin(), args.end(), args.begin(), [=](const std::string& str){return replace_newlines(str, length);}); auto msg = prefix; auto arg_index = std::size_t{0}; auto it = fmt.begin(); const auto end = fmt.end(); while(it != end) { auto pos = std::find(it, end, '%'); msg.append(it, pos); if (pos == end) { break; } ++pos; if (pos == end) { throw std::invalid_argument{"Invalid formatstring (ends on single '%')"}; } switch(*pos) { case '%': msg.push_back('%'); break; case 's': if (arg_index >= args.size()) { throw std::invalid_argument{"Invalid formatstring (not enough arguments)"}; } msg.append(args[arg_index++]); break; default: throw std::invalid_argument{"Invalid formatstring (unknown format-character)"}; } it = std::next(pos); } msg.push_back('\n'); return msg; }
void printXMLBlock(ResXMLTree* block) { block->restart(); Vector<namespace_entry> namespaces; ResXMLTree::event_code_t code; int depth = 0; while ((code=block->next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { String8 prefix = make_prefix(depth); int i; if (code == ResXMLTree::START_TAG) { size_t len; const uint16_t* ns16 = block->getElementNamespace(&len); String8 elemNs = build_namespace(namespaces, ns16); const uint16_t* com16 = block->getComment(&len); if (com16) { printf("%s <!-- %s -->\n", prefix.string(), String8(com16).string()); } printf("%sE: %s%s (line=%d)\n", prefix.string(), elemNs.string(), String8(block->getElementName(&len)).string(), block->getLineNumber()); int N = block->getAttributeCount(); depth++; prefix = make_prefix(depth); for (i=0; i<N; i++) { uint32_t res = block->getAttributeNameResID(i); ns16 = block->getAttributeNamespace(i, &len); String8 ns = build_namespace(namespaces, ns16); String8 name(block->getAttributeName(i, &len)); printf("%sA: ", prefix.string()); if (res) { printf("%s%s(0x%08x)", ns.string(), name.string(), res); } else { printf("%s%s", ns.string(), name.string()); } Res_value value; block->getAttributeValue(i, &value); if (value.dataType == Res_value::TYPE_NULL) { printf("=(null)"); } else if (value.dataType == Res_value::TYPE_REFERENCE) { printf("=@0x%x", (int)value.data); } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) { printf("=?0x%x", (int)value.data); } else if (value.dataType == Res_value::TYPE_STRING) { printf("=\"%s\"", String8(block->getAttributeStringValue(i, &len)).string()); } else { printf("=(type 0x%x)0x%x", (int)value.dataType, (int)value.data); } const char16_t* val = block->getAttributeStringValue(i, &len); if (val != NULL) { printf(" (Raw: \"%s\")", String8(val).string()); } printf("\n"); } } else if (code == ResXMLTree::END_TAG) { depth--; } else if (code == ResXMLTree::START_NAMESPACE) { namespace_entry ns; size_t len; const uint16_t* prefix16 = block->getNamespacePrefix(&len); if (prefix16) { ns.prefix = String8(prefix16); } else { ns.prefix = "<DEF>"; } ns.uri = String8(block->getNamespaceUri(&len)); namespaces.push(ns); printf("%sN: %s=%s\n", prefix.string(), ns.prefix.string(), ns.uri.string()); depth++; } else if (code == ResXMLTree::END_NAMESPACE) { depth--; const namespace_entry& ns = namespaces.top(); size_t len; const uint16_t* prefix16 = block->getNamespacePrefix(&len); String8 pr; if (prefix16) { pr = String8(prefix16); } else { pr = "<DEF>"; } if (ns.prefix != pr) { prefix = make_prefix(depth); printf("%s*** BAD END NS PREFIX: found=%s, expected=%s\n", prefix.string(), pr.string(), ns.prefix.string()); } String8 uri = String8(block->getNamespaceUri(&len)); if (ns.uri != uri) { prefix = make_prefix(depth); printf("%s *** BAD END NS URI: found=%s, expected=%s\n", prefix.string(), uri.string(), ns.uri.string()); } namespaces.pop(); } else if (code == ResXMLTree::TEXT) { size_t len; printf("%sC: \"%s\"\n", prefix.string(), String8(block->getText(&len)).string()); } } block->restart(); }