void configure_default_logger(log4cxx::LoggerPtr logger, log4cxx::LevelPtr level, std::string fname, bool dual) { if (fname.empty() && dual) throw std::logic_error("dual log mode requires a filename"); logger->setLevel(level); if (fname.empty() || dual) { log4cxx::AppenderPtr app = logger_write_to_cout(logger); app->setName("COUT"); } if (!fname.empty()) { log4cxx::AppenderPtr app = logger_write_to_file(fname, logger); app->setName("FILE"); } }
void ProxyServer::run() { TRACE(std::cout, ""); zmq::context_t context(1); zmq::socket_t socket(context, ZMQ_REP); std::string addr = m_protocol + "://*:" + m_port; socket.bind(addr.c_str()); while (true) { TRACE(std::cout, ""); zmq::message_t z_req; socket.recv(&z_req); std::string s_req((char *)z_req.data(), z_req.size()); Request req; req.ParseFromString(s_req); // if trace is in Request_Header, then change log level of appender // to trace if (m_msgDriver->requestHasTrace(req)) { TRACE(std::cout, ""); logger->setLevel(log4cxx::Level::getTrace()); } if (m_reqPrinter) { TRACE(std::cout, ""); LOG4CXX_TRACE(logger, "Request: "<<(m_reqPrinter(req))); } Response *resp = NULL; // All the anlytics will be logged in this function switch (req.header().type()) { case Request_Header_Type_LOOKUP: { TRACE(std::cout, ""); try { m_worker->lookup(req); } catch (KeyNotFoundException &e) { TRACE(std::cout, ""); LOG4CXX_DEBUG(logger, "Key not found in store: " <<e.what()); } resp = m_worker->response(); break; } case Request_Header_Type_INSERT: { TRACE(std::cout, ""); try { m_worker->insert(req); } catch (KeyPresentException &e) { TRACE(std::cout, ""); LOG4CXX_DEBUG(logger, "Key already present in" " store: "<<e.what()); } catch (InsertionException &e) { TRACE(std::cout, ""); LOG4CXX_DEBUG(logger, "Insertion error: " <<e.what()); } resp = m_worker->response(); break; } case Request_Header_Type_REMOVE: { TRACE(std::cout, ""); try { m_worker->remove(req); } catch (KeyNotFoundException &e) { TRACE(std::cout, ""); LOG4CXX_DEBUG(logger, "Key not found in store: " <<e.what()); } catch (DeletionException &e) { TRACE(std::cout, ""); LOG4CXX_DEBUG(logger, "Deletion error: " <<e.what()); } resp = m_worker->response(); break; } default: { TRACE(std::cout, ""); break; } }; if (m_respPrinter) { TRACE(std::cout, ""); LOG4CXX_TRACE(logger, "Response : "<<(m_respPrinter(*resp))); } std::string s_resp; resp->SerializeToString(&s_resp); delete resp; zmq::message_t reply(s_resp.size()); memcpy((void *)reply.data(), (void *)s_resp.c_str(), s_resp.size()); socket.send(reply); } }
int main(int argc, char** argv) { log4cxx::PropertyConfigurator::configure("logger.conf"); Util::SyncHandler spipe(SIGPIPE, [](int, siginfo_t *, void *){;}); po::options_description desc("Program options"); desc.add_options() ("help,h", "show usage information") ("port", po::value<int>(&opt_port), "bind port (2080)") ("perf", po::bool_switch(&opt_perf), "turn off logging") ; //("coro", po::bool_switch(&opt_coro), "enable coro server") //("time", po::value<int>(&opt_time), "time to perform call, us (250000)") //("threads", po::value<int>(&opt_threads), "number of worker threads (no threads)") po::variables_map vm; try { po::store(po::parse_command_line(argc, argv, desc), vm); } catch (const std::exception& e) { ERROR(e.what()); return -1; } po::notify(vm); if (vm.count("help")) { std::cout << desc << std::endl; return 0; } Server s; RPC::Library impl(s); boost::asio::io_service io; Util::ThreadGroup tg; boost::asio::signal_set signals(io, SIGINT, SIGTERM); signals.async_wait([&io](auto error, auto number){ if (!error) { INFO("terminating server"); io.stop(); } }); Util::Server server(io); server.run(opt_port, [&impl](boost::asio::streambuf& data, Util::FramedSocket ptr) { cbor::binary result; impl.process(data.data(), result); Util::FramedMessage reply(std::move(result)); ptr->write(std::move(reply)); }); // start event loop tg.run([&io](){ log4cxx::NDC ndc("server"); TRACE("starting ... "); io.run(); TRACE("terminated"); }); if (opt_perf) logger->setLevel(log4cxx::Level::getFatal()); tg.wait(); return 0; }