Appender * NetAppender::getAppender(com_device_t &dev) { char name[1024]; snprintf(name, sizeof(name), "NET:%s/%s", dev.host, dev.file); Appender *app = (Appender *)comlog_get_nameMg()->get(type_appender, name); if (app == NULL) { _com_debug("create net appender %s", name); app = new NetAppender; app->setDeviceInfo(dev); _com_debug("try to open the netappender[%s]", name); if (app->open(NULL) != 0) { _com_error("sorry failt to open %s", name); delete app; return NULL; } com_device_t dev2; if (deviceNetToFile(dev, dev2) == 0) { app->_bkappender = FileAppender::getAppender(dev2); //assert (app->_bkappender); // if (app->_bkappender != NULL) { app->_bkappender->setLayout(dev2.layout); app->_bkappender->_open = dev2.open; } } comlog_get_nameMg()->set(name, app); } return app; }
int CommInpCfg :: redirectLog () { time_t curr_t; struct tm *ptm; char log_file_name[32]; time (&curr_t); ptm = gmtime (&curr_t); strftime (log_file_name, 32, "%Y%m%d_%H%M%S.log", ptm); string AppLogFile(dataOpt__.WorkingPath); AppLogFile.append("/") .append(log_file_name); Category& rootLogCategory = Category::getRoot(); rootLogCategory.removeAllAppenders(); Appender* appender = new FileAppender("Log", AppLogFile); PatternLayout* patternLayout = new PatternLayout(); patternLayout->setConversionPattern("[%d{%Y:%m:%d %H:%M:%S.%I}] %p %c %x: %m%n"); Layout* layout = dynamic_cast<Layout*> (patternLayout); appender->setLayout(layout); cout << "Redirecting logging from stdout to : " << AppLogFile << endl; rootLogCategory.addAppender(appender); return 0; }
bool Log::SetLogLevel(std::string const& name, const char* newLevelc, bool isLogger /* = true */) { LogLevel newLevel = LogLevel(atoi(newLevelc)); if (newLevel < 0) return false; if (isLogger) { LoggerMap::iterator it = loggers.begin(); while (it != loggers.end() && it->second.getName() != name) ++it; if (it == loggers.end()) return false; it->second.setLogLevel(newLevel); if (newLevel != LOG_LEVEL_DISABLED && newLevel < lowestLogLevel) lowestLogLevel = newLevel; } else { Appender* appender = GetAppenderByName(name); if (!appender) return false; appender->setLogLevel(newLevel); } return true; }
Appender * AppenderManager::getNextAppender() { int s = m_appenders.size(); if ( s == 1 ) return (*m_appenders.begin()); if ( s == 0 ) return NULL; int n = m_curAppender++; int m = -1; Appender * p = NULL; while( n != m_curAppender ) { if ( n >= s ) n = 0; p = m_appenders[n]; if (!p->isFail() ) { if ( !p->isFull() ) { m_curAppender = n; return p; } if ( m == -1 ) m = n; m = n; } ++n; } m_curAppender = m; return m_appenders[m]; }
Logger& Logger::get_instance( const std::string& filename, Level::LogLevel level, long size, int index ) { if ( !logger ) { Appender* appender = new RollFileAppender(filename.c_str(), size, index); Filter* filter = new LevelFilter( level ); appender->tie_filter( filter ); logger = new Logger(); logger->add_appender( appender ); } return *logger; }
void Log::CreateAppenderFromConfig(std::string const& appenderName) { if (appenderName.empty()) return; // Format=type, level, flags, optional1, optional2 // if type = File. optional1 = file and option2 = mode // if type = Console. optional1 = Color std::string options = sConfigMgr->GetStringDefault(appenderName.c_str(), ""); Tokenizer tokens(options, ','); Tokenizer::const_iterator iter = tokens.begin(); size_t size = tokens.size(); std::string name = appenderName.substr(9); if (size < 2) { fprintf(stderr, "Log::CreateAppenderFromConfig: Wrong configuration for appender %s. Config line: %s\n", name.c_str(), options.c_str()); return; } AppenderFlags flags = APPENDER_FLAGS_NONE; AppenderType type = AppenderType(atoi(*iter++)); LogLevel level = LogLevel(atoi(*iter++)); if (level > LOG_LEVEL_FATAL) { fprintf(stderr, "Log::CreateAppenderFromConfig: Wrong Log Level %d for appender %s\n", level, name.c_str()); return; } if (size > 2) flags = AppenderFlags(atoi(*iter++)); auto factoryFunction = appenderFactory.find(type); if (factoryFunction == appenderFactory.end()) { fprintf(stderr, "Log::CreateAppenderFromConfig: Unknown type %d for appender %s\n", type, name.c_str()); return; } try { Appender* appender = factoryFunction->second(NextAppenderId(), name, level, flags, ExtraAppenderArgs(iter, tokens.end())); appenders[appender->getId()] = appender; } catch (InvalidAppenderArgsException const& iaae) { fprintf(stderr, "%s", iaae.what()); } }
void HttpLog::init() { s_pLogger = Logger::getRootLogger() ; new(achAccessLog) AccessLog(""); Appender *appender = Appender::getAppender("stderr", "appender.ps"); Layout *layout = Layout::getLayout(ERROR_LOG_PATTERN, "layout.pattern"); layout->setUData(s_logPattern); appender->setLayout(layout); logger()->setLevel(Level::DEBUG); logger()->setAppender(appender); }
void initLog4Qt(Level level) { Log4Qt::Layout* layout; Appender* appender = LogManager::rootLogger()->appender("A1"); if (appender) { logger()->debug("initLogQt: using existing appender A1"); if (level != Level::INFO_INT) { LogManager::rootLogger()->setLevel(level); } layout = appender->layout(); } else { LogManager::rootLogger()->setLevel(level); // Create console layout TTCCLayout* tccLayout = new TTCCLayout(); tccLayout->setName("Layout"); tccLayout->setDateFormat(TTCCLayout::ABSOLUTEDATE); tccLayout->setThreadPrinting(false); tccLayout->activateOptions(); // Create appender A1 for console if level set through cmd line option if (level != Level::INFO_INT) { ConsoleAppender* a = new ConsoleAppender(tccLayout, ConsoleAppender::STDERR_TARGET); a->setName("A1"); a->activateOptions(); // Set appender on root logger LogManager::rootLogger()->addAppender(a); } layout = tccLayout; } // Let Log4Qt handle qDebug(), qWarning(), qCritical() and qFatal() LogManager::setHandleQtMessages(true); LogManager::qtLogger()->setLevel(Logger::rootLogger()->level()); // Create appender A2 for log window Gui::TLogWindow::appender = new Gui::TLogWindowAppender(layout); Gui::TLogWindow::appender->setName("A2"); Gui::TLogWindow::appender->activateOptions(); // Set log window appender on root logger LogManager::rootLogger()->addAppender(Gui::TLogWindow::appender); logger()->info("initLog4Qt: root logger initialized on level %1", LogManager::rootLogger()->level().toString()); }
static log4cxx::Logger* initLogger(const char* logFile, const char* logLevel) { static char s_parttern[] = "%d [%p] [%c] %m"; using namespace LOG4CXX_NS; Logger *pLogger = Logger::getRootLogger() ; Appender * appender = Appender::getAppender( logFile, "appender.ps" ); Layout* layout = Layout::getLayout( "patternErrLog", "layout.pattern" ); layout->setUData( s_parttern ); appender->setLayout( layout ); log4cxx::Level::setDefaultLevel(Level::toInt(logLevel)); pLogger->setLevel( logLevel ); pLogger->setAppender( appender ); return pLogger; }
CommInpCfg :: CommInpCfg() { // Set up logging with stdout as destination Category& rootLogCategory = Category::getRoot(); rootLogCategory.setPriority(Priority::INFO); rootLogCategory.removeAllAppenders(); Appender* appender = new FileAppender("_", dup(fileno(stdout))); PatternLayout* patternLayout = new PatternLayout(); patternLayout->setConversionPattern("[%d{%Y:%m:%d %H:%M:%S.%I}] %p %c %x: %m%n"); Layout* layout = dynamic_cast<Layout*> (patternLayout); appender->setLayout(layout); rootLogCategory.addAppender(appender); }
void Writer::writeArray(Appender& appender, const Value& value, Size indent) { appender.append("[" + lineBreak()); for (Size i = 0; i < value.getLength(); i++) { appender.append(space(indent + 1)); write(appender, value.get(i), indent + 1); if (i + 1 != value.getLength()) { appender.append(","); } appender.append(lineBreak()); } appender.append(space(indent) + "]"); }
void MainThreadAppender::append(const LoggingEvent &rEvent) { QReadLocker locker(&mAppenderGuard); Appender *pAppender; Q_FOREACH(pAppender, mAppenders) { if (QThread::currentThread() != qApp->thread()) { LoggingEvent *event = new LoggingEvent(rEvent); qApp->postEvent(pAppender, event); } else pAppender->doAppend(rEvent); } }
bool Logger::readLog(std::string &target) { bool success = false; LockGuard lock(m_mutex); for (size_t i=0; i<m_appenders.size(); ++i) { Appender *appender = m_appenders[i]; if (appender->getClass()->derivesFrom(MTS_CLASS(StreamAppender))) { StreamAppender *streamAppender = static_cast<StreamAppender *>(appender); if (streamAppender->logsToFile()) { streamAppender->readLog(target); success = true; break; } } } return success; }
void Writer::writeObject(Appender& appender, const Value& value, Size indent) { appender.append("{" + lineBreak()); auto propNames = value.getPropertyNames(); for (Size i = 0; i < propNames.size(); i++) { const std::string& name = propNames[i]; appender.append(space(indent + 1) + '"' + name + "\": "); write(appender, value.get(name), indent + 1); if (i + 1 != propNames.size()) { appender.append(","); } appender.append(lineBreak()); } appender.append(space(indent) + "}"); }
int HttpLog::setErrorLogFile(const char *pFileName) { Appender *appender = Appender::getAppender(pFileName, "appender.ps"); if (appender) { Appender *pOld = logger()->getAppender(); if ((pOld) && (pOld != appender)) pOld->close(); Layout *layout = Layout::getLayout(ERROR_LOG_PATTERN, "layout.pattern"); appender->setLayout(layout); logger()->setAppender(appender); return 0; } else return LS_FAIL; }
// gather command from serial input // this may be performed in multiple passes - only a newline (not stored) marks command ready to process void processSerialCommands() { int inch; // append any newly arrived chars & execute command if return received while ((inch = Serial.read()) >= 0) { if (inch == '\n') { // end of line if(doCommand(commandBuffer)) { showPrompt(); } commandAp.reset(); return; // process at most one command per loop } else { // continue line commandAp.append((char) inch); } } }
void initialize_log(const char* name, int verbose, string const& file_log = "") { // 1 instantiate an appender object that // will append to a log file Appender* app; if (file_log.empty()) app = new OstreamAppender(name, &std::cout); else app = new FileAppender(name, file_log); // 2. Instantiate a layout object // Two layouts come already available in log4cpp // unless you create your own. // BasicLayout includes a time stamp Layout* layout = new BasicLayout(); // 3. attach the layout object to the // appender object app->setLayout(layout); // 4. Instantiate the category object // you may extract the root category, but it is // usually more practical to directly instance // a child category Category * main_cat = &Category::getInstance(name); // 5. Step 1 // an Appender when added to a category becomes // an additional output destination unless // Additivity is set to false when it is false, // the appender added to the category replaces // all previously existing appenders main_cat->setAdditivity(false); // 5. Step 2 // this appender becomes the only one main_cat->setAppender(app); // 6. Set up the priority for the category // and is given INFO priority // attempts to log DEBUG messages will fail main_cat->setPriority( verbose*100 ); }
size_t dispatch_to_string(const Arg& arg, Appender& app, FmtFlags flags, integral_constant<bool, false> has_overload2_opt, integral_constant<bool, true> has_overload1_opt, integral_constant<bool, b1> has_overload2, integral_constant<bool, b2> has_overload1) { auto string = to_string(arg, flags); app.append(string); return string.size(); }
Appender * Appender::getAppender(com_device_t &dev) { xAutoLock lock(&comlog_appender_getlock); //devµÄ²ÎÊý¼ì²é __CHECKINT(dev.log_size, 1, COM_MAXLOGFILE, COM_MAXLOGFILE); Appender *app = NULL; switch (dev.type) { case COM_FILEDEVICE: _com_debug("create file appender"); app = FileAppender::getAppender(dev); break; case COM_NETDEVICE: _com_debug("create net appender"); app = NetAppender::getAppender(dev); break; case COM_TTYDEVICE: _com_debug("create tty appender"); app = TtyAppender::getAppender(dev); break; case COM_ULLOGDEVICE: case COM_ULNETDEVICE: _com_debug("create ul appender"); app = UlLogAppender::getAppender(dev); break; case COM_AFILEDEVICE: _com_debug("create async file appender"); app = AsyncFileAppender::getAppender(dev); break; default: _com_error("sorry i can't know the type[%d]", dev.type); break; } if (app != NULL) { app->setLayout(dev.layout); _core_debug("dev =%lx appender layout=%lx", dev.layout, app->_layout); app->_open = dev.open; } return app; }
void runScript(char *script) { char *lineStart = script; commandAp.reset(); // break response into individual lines to feed to command parser while(*lineStart != '\0') { commandAp.appendLine(lineStart); Serial.print(F("script> ")); doCommand(commandBuffer); lineStart += commandAp.len(); commandAp.reset(); // skip any control chars at & after line break eg when \r\n linebreaks while(*lineStart && *lineStart < ' ') { lineStart++; } } }
void generic_append(char const *begin,char const *end,Appender &a) { a.append('"'); char const *i,*last; char buf[8] = "\\u00"; for(i=begin,last = begin;i!=end;) { char const *addon = 0; unsigned char c=*i; switch(c) { case 0x22: addon = "\\\""; break; case 0x5C: addon = "\\\\"; break; case '\b': addon = "\\b"; break; case '\f': addon = "\\f"; break; case '\n': addon = "\\n"; break; case '\r': addon = "\\r"; break; case '\t': addon = "\\t"; break; default: if(c<=0x1F) { static char const tohex[]="0123456789abcdef"; buf[4]=tohex[c >> 4]; buf[5]=tohex[c & 0xF]; buf[6]=0; addon = buf; } }; if(addon) { a.append(last,i-last); a.append(addon); i++; last = i; } else { i++; } } a.append(last,i-last); a.append('"'); }
int mtssrv(int argc, char **argv) { int optchar; char *end_ptr = NULL; try { /* Default settings */ int nprocs = getCoreCount(), listenPort = MTS_DEFAULT_PORT; std::string nodeName = getHostName(), networkHosts = ""; bool quietMode = false; ELogLevel logLevel = EInfo; std::string hostName = getFQDN(); FileResolver *fileResolver = Thread::getThread()->getFileResolver(); bool hostNameSet = false; optind = 1; /* Parse command-line arguments */ while ((optchar = getopt(argc, argv, "a:c:s:n:p:i:l:L:qhv")) != -1) { switch (optchar) { case 'a': { std::vector<std::string> paths = tokenize(optarg, ";"); for (int i=(int)paths.size()-1; i>=0; --i) fileResolver->prependPath(paths[i]); } break; case 'c': networkHosts = networkHosts + std::string(";") + std::string(optarg); break; case 'i': hostName = optarg; hostNameSet = true; break; case 's': { std::ifstream is(optarg); if (is.fail()) SLog(EError, "Could not open host file!"); std::string host; while (is >> host) { if (host.length() < 1 || host.c_str()[0] == '#') continue; networkHosts = networkHosts + std::string(";") + host; } } break; case 'n': nodeName = optarg; break; case 'p': nprocs = strtol(optarg, &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Could not parse the processor count!"); break; case 'v': logLevel = EDebug; break; case 'L': { std::string arg = boost::to_lower_copy(std::string(optarg)); if (arg == "trace") logLevel = ETrace; else if (arg == "debug") logLevel = EDebug; else if (arg == "info") logLevel = EInfo; else if (arg == "warn") logLevel = EWarn; else if (arg == "error") logLevel = EError; else SLog(EError, "Invalid log level!"); } break; case 'l': if (!strcmp("s", optarg)) { listenPort = -1; quietMode = true; } else { listenPort = strtol(optarg, &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Could not parse the port number"); } break; case 'q': quietMode = true; break; case 'h': default: cout << "Mitsuba version " << Version(MTS_VERSION).toStringComplete() << ", Copyright (c) " MTS_YEAR " Wenzel Jakob" << endl; cout << "Usage: mtssrv [options]" << endl; cout << "Options/Arguments:" << endl; cout << " -h Display this help text" << endl << endl; cout << " -a p1;p2;.. Add one or more entries to the resource search path" << endl << endl; cout << " -p count Override the detected number of processors. Useful for reducing" << endl; cout << " the load or creating scheduling-only nodes in conjunction with" << endl; cout << " the -c and -s parameters, e.g. -p 0 -c host1;host2;host3,..." << endl << endl; cout << " -q Quiet mode - do not print any log messages to stdout" << endl << endl; cout << " -c hosts Nesting: connect to additional mtssrv instances over a network." << endl; cout << " Requires a semicolon-separated list of host names of the form" << endl; cout << " host.domain[:port] for a direct connection" << endl; cout << " or" << endl; cout << " [email protected][:/path] for a SSH connection (where" << endl; cout << " 'path' denotes the place where Mitsuba is checked" << endl; cout << " out -- by default, \"~/mitsuba\" is used)" << endl << endl; cout << " -s file Connect to additional Mitsuba servers specified in a file" << endl; cout << " with one name per line (same format as in -c)" << endl<< endl; cout << " -i name IP address / host name on which to listen for connections" << endl << endl; cout << " -l port Listen for connections on a certain port (Default: " << MTS_DEFAULT_PORT << ")." << endl; cout << " To listen on stdin, specify \"-ls\" (implies -q)" << endl << endl; cout << " -n name Assign a node name to this instance (Default: host name)" << endl << endl; cout << " -v Be more verbose (can be specified twice)" << endl << endl; cout << " -L level Explicitly specify the log level (trace/debug/info/warn/error)" << endl << endl; cout << " For documentation, please refer to http://www.mitsuba-renderer.org/docs.html" << endl; return 0; } } /* Configure the logging subsystem */ ref<Logger> log = Thread::getThread()->getLogger(); log->setLogLevel(logLevel); /* Initialize OpenMP */ Thread::initializeOpenMP(nprocs); /* Disable the default appenders */ for (size_t i=0; i<log->getAppenderCount(); ++i) { Appender *appender = log->getAppender(i); if (appender->getClass()->derivesFrom(MTS_CLASS(StreamAppender))) log->removeAppender(appender); } log->addAppender(new StreamAppender(formatString("mtssrv.%s.log", nodeName.c_str()))); if (!quietMode) log->addAppender(new StreamAppender(&std::cout)); SLog(EInfo, "Mitsuba version %s, Copyright (c) " MTS_YEAR " Wenzel Jakob", Version(MTS_VERSION).toStringComplete().c_str()); #if defined(__WINDOWS__) /* Custom handler for Ctrl-C signals */ SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE); #endif /* Configure the scheduling subsystem */ Scheduler *scheduler = Scheduler::getInstance(); for (int i=0; i<nprocs; ++i) scheduler->registerWorker(new LocalWorker(i, formatString("wrk%i", i))); std::vector<std::string> hosts = tokenize(networkHosts, ";"); /* Establish network connections to nested servers */ for (size_t i=0; i<hosts.size(); ++i) { const std::string &hostName = hosts[i]; ref<Stream> stream; if (hostName.find("@") == std::string::npos) { int port = MTS_DEFAULT_PORT; std::vector<std::string> tokens = tokenize(hostName, ":"); if (tokens.size() == 0 || tokens.size() > 2) { SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } else if (tokens.size() == 2) { port = strtol(tokens[1].c_str(), &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } stream = new SocketStream(tokens[0], port); } else { std::string path = "~/mitsuba"; std::vector<std::string> tokens = tokenize(hostName, "@/:"); if (tokens.size() < 2 || tokens.size() > 3) { SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } else if (tokens.size() == 3) { path = tokens[2]; } std::vector<std::string> cmdLine; cmdLine.push_back(formatString("bash -c 'cd %s; . setpath.sh; mtssrv -ls'", path.c_str())); stream = new SSHStream(tokens[0], tokens[1], cmdLine); } try { scheduler->registerWorker(new RemoteWorker(formatString("net%i", i), stream)); } catch (std::runtime_error &e) { if (hostName.find("@") != std::string::npos) { #if defined(__WINDOWS__) SLog(EWarn, "Please ensure that passwordless authentication " "using plink.exe and pageant.exe is enabled (see the documentation for more information)"); #else SLog(EWarn, "Please ensure that passwordless authentication " "is enabled (e.g. using ssh-agent - see the documentation for more information)"); #endif } throw e; } } scheduler->start(); if (listenPort == -1) { ref<StreamBackend> backend = new StreamBackend("con0", scheduler, nodeName, new ConsoleStream(), false); backend->start(); backend->join(); return 0; } /* Allocate a socket of the proper type (IPv4/IPv6) */ struct addrinfo hints, *servinfo, *p = NULL; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_flags = AI_PASSIVE; hints.ai_socktype = SOCK_STREAM; char portName[8]; int rv, one = 1; sock = INVALID_SOCKET; snprintf(portName, sizeof(portName), "%i", listenPort); if ((rv = getaddrinfo(hostNameSet ? hostName.c_str() : NULL, portName, &hints, &servinfo)) != 0) SLog(EError, "Error in getaddrinfo(%s:%i): %s", hostName.c_str(), listenPort, gai_strerror(rv)); for (p = servinfo; p != NULL; p = p->ai_next) { /* Allocate a socket */ sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if (sock == -1) SocketStream::handleError("none", "socket"); /* Avoid "bind: socket already in use" */ if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(int)) < 0) SocketStream::handleError("none", "setsockopt"); /* Bind the socket to the port number */ if (bind(sock, p->ai_addr, (socklen_t) p->ai_addrlen) == -1) { SocketStream::handleError("none", formatString("bind(%s:%i)", hostName.c_str(), listenPort), EError); #if defined(__WINDOWS__) closesocket(sock); #else close(sock); #endif continue; } break; } if (p == NULL) SLog(EError, "Failed to bind to port %i!", listenPort); freeaddrinfo(servinfo); if (listen(sock, CONN_BACKLOG) == -1) SocketStream::handleError("none", "bind"); SLog(EInfo, "Enter mtssrv -h for more options"); #if defined(__WINDOWS__) SLog(EInfo, "%s: Listening on port %i.. Send Ctrl-C to stop.", hostName.c_str(), listenPort); #else /* Avoid zombies processes */ struct sigaction sa; sa.sa_handler = collect_zombies; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) SLog(EError, "Error in sigaction(): %s!", strerror(errno)); sa.sa_handler = sigterm_handler; sa.sa_flags = 0; // we want SIGINT/SIGTERM to interrupt accept() if (sigaction(SIGTERM, &sa, NULL) == -1) SLog(EError, "Error in sigaction(): %s!", strerror(errno)); if (sigaction(SIGINT, &sa, NULL) == -1) SLog(EError, "Error in sigaction(): %s!", strerror(errno)); /* Ignore SIGPIPE */ signal(SIGPIPE, SIG_IGN); SLog(EInfo, "%s: Listening on port %i.. Send Ctrl-C or SIGTERM to stop.", hostName.c_str(), listenPort); #endif int connectionIndex = 0; /* Wait for connections */ while (running) { socklen_t addrlen = sizeof(sockaddr_storage); struct sockaddr_storage sockaddr; memset(&sockaddr, 0, addrlen); SOCKET newSocket = accept(sock, (struct sockaddr *) &sockaddr, &addrlen); if (newSocket == INVALID_SOCKET) { #if defined(__WINDOWS__) if(!running) break; #else if (errno == EINTR) continue; #endif SocketStream::handleError("none", "accept", EWarn); continue; } ref<StreamBackend> backend = new StreamBackend(formatString("con%i", connectionIndex++), scheduler, nodeName, new SocketStream(newSocket), true); backend->start(); } #if defined(__WINDOWS__) SLog(EInfo, "Caught signal - shutting down.."); #else close(sock); #endif } catch (const std::exception &e) { std::cerr << "Caught a critical exception: " << e.what() << endl; } catch (...) { std::cerr << "Caught a critical exception of unknown type!" << endl; } /* Shutdown */ Statistics::getInstance()->printStats(); return 0; }
QDebug operator<<(QDebug debug, const Appender &rAppender) { return rAppender.debug(debug); }
int main(int argc, char *argv[]) { int retval; /* Initialize Xerces-C */ try { XMLPlatformUtils::Initialize(); } catch(const XMLException &toCatch) { fprintf(stderr, "Error during Xerces initialization: %s", XMLString::transcode(toCatch.getMessage())); return -1; } /* Initialize the core framework */ Class::staticInitialization(); PluginManager::staticInitialization(); Statistics::staticInitialization(); Thread::staticInitialization(); Thread::initializeOpenMP(getProcessorCount()); Logger::staticInitialization(); Spectrum::staticInitialization(); Scheduler::staticInitialization(); SHVector::staticInitialization(); #if defined(__LINUX__) XInitThreads(); #endif #if defined(__OSX__) MTS_AUTORELEASE_BEGIN() /* Required for the mouse relocation in GLWidget */ CGSetLocalEventsSuppressionInterval(0.0f); MTS_AUTORELEASE_END() #endif #ifdef WIN32 /* Initialize WINSOCK2 */ WSADATA wsaData; if (WSAStartup(MAKEWORD(2,2), &wsaData)) SLog(EError, "Could not initialize WinSock2!"); if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) SLog(EError, "Could not find the required version of winsock.dll!"); #endif #if !defined(WIN32) /* Avoid zombies processes when running the server */ struct sigaction sa; sa.sa_handler = collect_zombies; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) SLog(EWarn, "Error in sigaction(): %s!", strerror(errno)); #endif qRegisterMetaType<ELogLevel>("ELogLevel"); qRegisterMetaType<fs::path>("fs::path"); MitsubaApplication app(argc, argv); try { QFile stylesheet(":/resources/stylesheet.css"); if (!stylesheet.open(QFile::ReadOnly)) { QMessageBox::critical(NULL, "Internal error", "Could not open stylesheet!"); exit(-1); } app.setStyleSheet(QTextStream(&stylesheet).readAll().toAscii()); #if defined(__OSX__) app.setAttribute(Qt::AA_DontShowIconsInMenus); #endif /* Disable the default appenders */ ref<Logger> logger = Thread::getThread()->getLogger(); for (size_t i=0; i<logger->getAppenderCount(); ++i) { Appender *appender = logger->getAppender(i); if (appender->getClass()->derivesFrom(MTS_CLASS(StreamAppender))) logger->removeAppender(appender); } #if defined(__OSX__) /* Create a log file inside the application bundle */ MTS_AUTORELEASE_BEGIN() logger->addAppender(new StreamAppender(formatString("%s/mitsuba.%s.log", __ubi_bundlepath().c_str(), getHostName().c_str()))); MTS_AUTORELEASE_END() #else /* Create a log file inside the current working directory */ logger->addAppender(new StreamAppender(formatString("mitsuba.%s.log", getHostName().c_str()))); #endif #if !defined(WIN32) /* Correct number parsing on some locales (e.g. ru_RU) */ setlocale(LC_NUMERIC, "C"); #endif mainWindow = new MainWindow(); mainWindow->initWorkers(); retval = app.exec(); delete mainWindow; } catch (const std::exception &e) { SLog(EWarn, "Critical exception during startup: %s", e.what()); QMessageBox::critical(NULL, QString("Critical exception"), e.what(), QMessageBox::Ok); retval = -1; } Statistics::getInstance()->printStats(); XMLPlatformUtils::Terminate(); #ifdef WIN32 /* Shut down WINSOCK2 */ WSACleanup(); #endif /* Shutdown the core framework */ SHVector::staticShutdown(); Scheduler::staticShutdown(); Spectrum::staticShutdown(); Logger::staticShutdown(); Thread::staticShutdown(); Statistics::staticShutdown(); PluginManager::staticShutdown(); Class::staticShutdown(); return retval; }
void assertString(const vespalib::string &input, const vespalib::string &output) { std::vector<char> buf; _appender.append(buf, input.c_str(), input.size()); EXPECT_EQUAL(output, vespalib::string(&buf[0], buf.size())); }
void Repository::log(const Category& category, const Level& level, const char* format, va_list args) { Appender* appender = static_cast<const CategoryNodeData*>(category.m_categoryNode)->m_appender; if (appender != NULL) { appender->write(level, format, args); } }
Appender* PropertyConfiguratorImpl::instantiateAppender(const std::string& appenderName) { Appender* appender = NULL; std::string appenderPrefix = std::string("appender.") + appenderName; // determine the type by the appenderName Properties::iterator key = _properties.find(appenderPrefix); if (key == _properties.end()) throw ConfigureFailure(std::string("Appender '") + appenderName + "' not defined"); std::string::size_type length = (*key).second.find_last_of("."); std::string appenderType = (length == std::string::npos) ? (*key).second : (*key).second.substr(length+1); // and instantiate the appropriate object if (appenderType == "ConsoleAppender") { appender = new OstreamAppender(appenderName, &std::cout); } else if (appenderType == "FileAppender") { std::string fileName = _properties.getString(appenderPrefix + ".fileName", "foobar"); bool append = _properties.getBool(appenderPrefix + ".append", true); appender = new FileAppender(appenderName, fileName, append); } else if (appenderType == "RollingFileAppender") { std::string fileName = _properties.getString(appenderPrefix + ".fileName", "foobar"); size_t maxFileSize = _properties.getInt(appenderPrefix + ".maxFileSize", 10*1024*1024); int maxBackupIndex = _properties.getInt(appenderPrefix + ".maxBackupIndex", 1); bool append = _properties.getBool(appenderPrefix + ".append", true); appender = new RollingFileAppender(appenderName, fileName, maxFileSize, maxBackupIndex, append); } else if (appenderType == "GenerationalFileAppender") { std::string fileName = _properties.getString(appenderPrefix + ".fileName", "foobar"); bool append = _properties.getBool(appenderPrefix + ".append", true); appender = new GenerationalFileAppender(appenderName, fileName, append); } else if (appenderType == "SyslogAppender") { std::string syslogName = _properties.getString(appenderPrefix + ".syslogName", "syslog"); std::string syslogHost = _properties.getString(appenderPrefix + ".syslogHost", "localhost"); int facility = _properties.getInt(appenderPrefix + ".facility", -1) * 8; // * 8 to get LOG_KERN, etc. compatible values. int portNumber = _properties.getInt(appenderPrefix + ".portNumber", -1); appender = new RemoteSyslogAppender(appenderName, syslogName, syslogHost, facility, portNumber); } #ifdef LOG4CPP_HAVE_SYSLOG else if (appenderType == "LocalSyslogAppender") { std::string syslogName = _properties.getString(appenderPrefix + ".syslogName", "syslog"); int facility = _properties.getInt(appenderPrefix + ".facility", -1) * 8; // * 8 to get LOG_KERN, etc. compatible values. appender = new SyslogAppender(appenderName, syslogName, facility); } #endif // LOG4CPP_HAVE_SYSLOG else if (appenderType == "AbortAppender") { appender = new AbortAppender(appenderName); } #ifdef LOG4CPP_HAVE_LIBIDSA else if (appenderType == "IdsaAppender") { // default idsa name ??? std::string idsaName = _properties.getString(appenderPrefix + ".idsaName", "foobar"); appender = new IdsaAppender(appenderName, idsaname); } #endif // LOG4CPP_HAVE_LIBIDSA #ifdef WIN32 // win32 debug appender else if (appenderType == "Win32DebugAppender") { appender = new Win32DebugAppender(appenderName); } // win32 NT event log appender else if (appenderType == "NTEventLogAppender") { std::string source = _properties.getString(appenderPrefix + ".source", "foobar"); appender = new NTEventLogAppender(appenderName, source); } #endif // WIN32 else { throw ConfigureFailure(std::string("Appender '") + appenderName + "' has unknown type '" + appenderType + "'"); } if (appender->requiresLayout()) { setLayout(appender, appenderName); } // set threshold std::string thresholdName = _properties.getString(appenderPrefix + ".threshold", ""); try { if (thresholdName != "") { appender->setThreshold(Priority::getPriorityValue(thresholdName)); } } catch(std::invalid_argument& e) { throw ConfigureFailure(std::string(e.what()) + " for threshold of appender '" + appenderName + "'"); } return appender; }
int mtsutil(int argc, char **argv) { char optchar, *end_ptr = NULL; try { /* Default settings */ int nprocs = getProcessorCount(); std::string nodeName = getHostName(), networkHosts = "", destFile=""; bool quietMode = false; ELogLevel logLevel = EInfo; FileResolver *fileResolver = Thread::getThread()->getFileResolver(); bool testCaseMode = false; if (argc < 2) { help(); return 0; } optind = 1; /* Parse command-line arguments */ while ((optchar = getopt(argc, argv, "+a:c:s:n:p:qhvt")) != -1) { switch (optchar) { case 'a': { std::vector<std::string> paths = tokenize(optarg, ";"); for (unsigned int i=0; i<paths.size(); ++i) fileResolver->addPath(paths[i]); } break; case 'c': networkHosts = networkHosts + std::string(";") + std::string(optarg); break; case 't': testCaseMode = true; break; case 's': { std::ifstream is(optarg); if (is.fail()) SLog(EError, "Could not open host file!"); std::string host; while (is >> host) { if (host.length() < 1 || host.c_str()[0] == '#') continue; networkHosts = networkHosts + std::string(";") + host; } } break; case 'n': nodeName = optarg; break; case 'v': logLevel = EDebug; break; case 'p': nprocs = strtol(optarg, &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Could not parse the processor count!"); break; case 'q': quietMode = true; break; case 'h': default: help(); return 0; } } /* Configure the logging subsystem */ ref<Logger> log = Thread::getThread()->getLogger(); log->setLogLevel(logLevel); /* Initialize OpenMP */ Thread::initializeOpenMP(nprocs); /* Disable the default appenders */ for (size_t i=0; i<log->getAppenderCount(); ++i) { Appender *appender = log->getAppender(i); if (appender->getClass()->derivesFrom(MTS_CLASS(StreamAppender))) log->removeAppender(appender); } log->addAppender(new StreamAppender(formatString("mitsuba.%s.log", nodeName.c_str()))); if (!quietMode) log->addAppender(new StreamAppender(&std::cout)); SLog(EInfo, "Mitsuba version %s, Copyright (c) " MTS_YEAR " Wenzel Jakob", Version(MTS_VERSION).toStringComplete().c_str()); /* Configure the scheduling subsystem */ Scheduler *scheduler = Scheduler::getInstance(); for (int i=0; i<nprocs; ++i) scheduler->registerWorker(new LocalWorker(formatString("wrk%i", i))); std::vector<std::string> hosts = tokenize(networkHosts, ";"); /* Establish network connections to nested servers */ for (size_t i=0; i<hosts.size(); ++i) { const std::string &hostName = hosts[i]; ref<Stream> stream; if (hostName.find("@") == std::string::npos) { int port = MTS_DEFAULT_PORT; std::vector<std::string> tokens = tokenize(hostName, ":"); if (tokens.size() == 0 || tokens.size() > 2) { SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } else if (tokens.size() == 2) { port = strtol(tokens[1].c_str(), &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } stream = new SocketStream(tokens[0], port); } else { std::string path = "~/mitsuba"; // default path if not specified std::vector<std::string> tokens = tokenize(hostName, "@:"); if (tokens.size() < 2 || tokens.size() > 3) { SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } else if (tokens.size() == 3) { path = tokens[2]; } std::vector<std::string> cmdLine; cmdLine.push_back(formatString("bash -c 'cd %s; . setpath.sh; mtssrv -ls'", path.c_str())); stream = new SSHStream(tokens[0], tokens[1], cmdLine); } try { scheduler->registerWorker(new RemoteWorker(formatString("net%i", i), stream)); } catch (std::runtime_error &e) { if (hostName.find("@") != std::string::npos) { #if defined(WIN32) SLog(EWarn, "Please ensure that passwordless authentication " "using plink.exe and pageant.exe is enabled (see the documentation for more information)"); #else SLog(EWarn, "Please ensure that passwordless authentication " "is enabled (e.g. using ssh-agent - see the documentation for more information)"); #endif } throw e; } } scheduler->start(); if (testCaseMode) { std::vector<fs::path> dirPaths = fileResolver->resolveAll("plugins"); std::set<std::string> seen; int executed = 0, succeeded = 0; for (size_t i=0; i<dirPaths.size(); ++i) { fs::path dirPath = fs::complete(dirPaths[i]); if (!fs::exists(dirPath) || !fs::is_directory(dirPath)) break; fs::directory_iterator end, it(dirPath); for (; it != end; ++it) { if (!fs::is_regular_file(it->status())) continue; std::string extension(boost::to_lower_copy(it->path().extension())); #if defined(WIN32) if (extension != ".dll") continue; #elif defined(__OSX__) if (extension != ".dylib") continue; #elif defined(__LINUX__) if (extension != ".so") continue; #else #error Unknown operating system! #endif std::string shortName = it->path().stem(); if (seen.find(shortName) != seen.end() || !boost::starts_with(shortName, "test_")) continue; seen.insert(shortName); Plugin plugin(shortName, it->path()); if (!plugin.isUtility()) continue; ref<Utility> utility = plugin.createUtility(); TestCase *testCase = static_cast<TestCase *>(utility.get()); if (!utility->getClass()->derivesFrom(MTS_CLASS(TestCase))) SLog(EError, "This is not a test case!"); if (testCase->run(argc-optind, argv+optind) != 0) SLog(EError, "Testcase unexpectedly returned with a nonzero value."); executed += testCase->getExecuted(); succeeded += testCase->getSucceeded(); } } SLog(EInfo, "Ran %i tests, %i succeeded, %i failed.", executed, succeeded, executed-succeeded); } else { if (argc <= optind) { std::cerr << "A utility name must be supplied!" << endl; return -1; } fs::path pluginName(argv[optind]); /* Build the full plugin file name */ #if defined(WIN32) pluginName.replace_extension(".dll"); #elif defined(__OSX__) pluginName.replace_extension(".dylib"); #elif defined(__LINUX__) pluginName.replace_extension(".so"); #else #error Unknown operating system! #endif fs::path fullName = fileResolver->resolve(fs::path("plugins") / pluginName); if (!fs::exists(fullName)) { /* Plugin not found! */ SLog(EError, "Utility \"%s\" not found (run \"mtsutil\" without arguments to " "see a list of available utilities)", fullName.file_string().c_str()); } SLog(EInfo, "Loading utility \"%s\" ..", argv[optind]); Plugin *plugin = new Plugin(argv[optind], fullName); if (!plugin->isUtility()) SLog(EError, "This plugin does not implement the 'Utility' interface!"); Statistics::getInstance()->logPlugin(argv[optind], plugin->getDescription()); ref<Utility> utility = plugin->createUtility(); int retval = utility->run(argc-optind, argv+optind); scheduler->pause(); utility = NULL; delete plugin; return retval; } } catch (const std::exception &e) { std::cerr << "Caught a critical exeption: " << e.what() << std::endl; } catch (...) { std::cerr << "Caught a critical exeption of unknown type!" << endl; } return 0; }
int mitsuba_app(int argc, char **argv) { int optchar; char *end_ptr = NULL; try { /* Default settings */ int nprocs_avail = getCoreCount(), nprocs = nprocs_avail; int numParallelScenes = 1; std::string nodeName = getHostName(), networkHosts = "", destFile=""; bool quietMode = false, progressBars = true, skipExisting = false; ELogLevel logLevel = EInfo; ref<FileResolver> fileResolver = Thread::getThread()->getFileResolver(); bool treatWarningsAsErrors = false; std::map<std::string, std::string, SimpleStringOrdering> parameters; int blockSize = 32; int flushTimer = -1; if (argc < 2) { help(); return 0; } optind = 1; /* Parse command-line arguments */ while ((optchar = getopt(argc, argv, "a:c:D:s:j:n:o:r:b:p:qhzvtwx")) != -1) { switch (optchar) { case 'a': { std::vector<std::string> paths = tokenize(optarg, ";"); for (int i=(int) paths.size()-1; i>=0; --i) fileResolver->prependPath(paths[i]); } break; case 'c': networkHosts = networkHosts + std::string(";") + std::string(optarg); break; case 'w': treatWarningsAsErrors = true; break; case 'D': { std::vector<std::string> param = tokenize(optarg, "="); if (param.size() != 2) SLog(EError, "Invalid parameter specification \"%s\"", optarg); parameters[param[0]] = param[1]; } break; case 's': { std::ifstream is(optarg); if (is.fail()) SLog(EError, "Could not open host file!"); std::string host; while (is >> host) { if (host.length() < 1 || host.c_str()[0] == '#') continue; networkHosts = networkHosts + std::string(";") + host; } } break; case 'n': nodeName = optarg; break; case 'o': destFile = optarg; break; case 'v': if (logLevel != EDebug) logLevel = EDebug; else logLevel = ETrace; break; case 'x': skipExisting = true; break; case 'p': nprocs = strtol(optarg, &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Could not parse the processor count!"); break; case 'j': numParallelScenes = strtol(optarg, &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Could not parse the parallel scene count!"); break; case 'r': flushTimer = strtol(optarg, &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Could not parse the '-r' parameter argument!"); break; case 'b': blockSize = strtol(optarg, &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Could not parse the block size!"); if (blockSize < 2 || blockSize > 128) SLog(EError, "Invalid block size (should be in the range 2-128)"); break; case 'z': progressBars = false; break; case 'q': quietMode = true; break; case 'h': default: help(); return 0; } } ProgressReporter::setEnabled(progressBars); /* Initialize OpenMP */ Thread::initializeOpenMP(nprocs); /* Configure the logging subsystem */ ref<Logger> log = Thread::getThread()->getLogger(); log->setLogLevel(logLevel); log->setErrorLevel(treatWarningsAsErrors ? EWarn : EError); /* Disable the default appenders */ for (size_t i=0; i<log->getAppenderCount(); ++i) { Appender *appender = log->getAppender(i); if (appender->getClass()->derivesFrom(MTS_CLASS(StreamAppender))) log->removeAppender(appender); } log->addAppender(new StreamAppender(formatString("mitsuba.%s.log", nodeName.c_str()))); if (!quietMode) log->addAppender(new StreamAppender(&std::cout)); SLog(EInfo, "Mitsuba version %s, Copyright (c) " MTS_YEAR " Wenzel Jakob", Version(MTS_VERSION).toStringComplete().c_str()); /* Configure the scheduling subsystem */ Scheduler *scheduler = Scheduler::getInstance(); bool useCoreAffinity = nprocs == nprocs_avail; for (int i=0; i<nprocs; ++i) scheduler->registerWorker(new LocalWorker(useCoreAffinity ? i : -1, formatString("wrk%i", i))); std::vector<std::string> hosts = tokenize(networkHosts, ";"); /* Establish network connections to nested servers */ for (size_t i=0; i<hosts.size(); ++i) { const std::string &hostName = hosts[i]; ref<Stream> stream; if (hostName.find("@") == std::string::npos) { int port = MTS_DEFAULT_PORT; std::vector<std::string> tokens = tokenize(hostName, ":"); if (tokens.size() == 0 || tokens.size() > 2) { SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } else if (tokens.size() == 2) { port = strtol(tokens[1].c_str(), &end_ptr, 10); if (*end_ptr != '\0') SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } stream = new SocketStream(tokens[0], port); } else { std::string path = "~/mitsuba"; // default path if not specified std::vector<std::string> tokens = tokenize(hostName, "@:"); if (tokens.size() < 2 || tokens.size() > 3) { SLog(EError, "Invalid host specification '%s'!", hostName.c_str()); } else if (tokens.size() == 3) { path = tokens[2]; } std::vector<std::string> cmdLine; cmdLine.push_back(formatString("bash -c 'cd %s; . setpath.sh; mtssrv -ls'", path.c_str())); stream = new SSHStream(tokens[0], tokens[1], cmdLine); } try { scheduler->registerWorker(new RemoteWorker(formatString("net%i", i), stream)); } catch (std::runtime_error &e) { if (hostName.find("@") != std::string::npos) { #if defined(__WINDOWS__) SLog(EWarn, "Please ensure that passwordless authentication " "using plink.exe and pageant.exe is enabled (see the documentation for more information)"); #else SLog(EWarn, "Please ensure that passwordless authentication " "is enabled (e.g. using ssh-agent - see the documentation for more information)"); #endif } throw e; } } scheduler->start(); #if !defined(__WINDOWS__) /* Initialize signal handlers */ struct sigaction sa; sa.sa_handler = signalHandler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGHUP, &sa, NULL)) SLog(EError, "Could not install a custom signal handler!"); if (sigaction(SIGFPE, &sa, NULL)) SLog(EError, "Could not install a custom signal handler!"); #endif /* Prepare for parsing scene descriptions */ SAXParser* parser = new SAXParser(); fs::path schemaPath = fileResolver->resolveAbsolute("data/schema/scene.xsd"); /* Check against the 'scene.xsd' XML Schema */ parser->setDoSchema(true); parser->setValidationSchemaFullChecking(true); parser->setValidationScheme(SAXParser::Val_Always); parser->setExternalNoNamespaceSchemaLocation(schemaPath.c_str()); /* Set the handler */ SceneHandler *handler = new SceneHandler(parameters); parser->setDoNamespaces(true); parser->setDocumentHandler(handler); parser->setErrorHandler(handler); renderQueue = new RenderQueue(); ref<FlushThread> flushThread; if (flushTimer > 0) { flushThread = new FlushThread(flushTimer); flushThread->start(); } int jobIdx = 0; for (int i=optind; i<argc; ++i) { fs::path filename = fileResolver->resolve(argv[i]), filePath = fs::absolute(filename).parent_path(), baseName = filename.stem(); ref<FileResolver> frClone = fileResolver->clone(); frClone->prependPath(filePath); Thread::getThread()->setFileResolver(frClone); SLog(EInfo, "Parsing scene description from \"%s\" ..", argv[i]); parser->parse(filename.c_str()); ref<Scene> scene = handler->getScene(); scene->setSourceFile(filename); scene->setDestinationFile(destFile.length() > 0 ? fs::path(destFile) : (filePath / baseName)); scene->setBlockSize(blockSize); if (scene->destinationExists() && skipExisting) continue; ref<RenderJob> thr = new RenderJob(formatString("ren%i", jobIdx++), scene, renderQueue, -1, -1, -1, true, flushTimer > 0); thr->start(); renderQueue->waitLeft(numParallelScenes-1); if (i+1 < argc && numParallelScenes == 1) Statistics::getInstance()->resetAll(); } /* Wait for all render processes to finish */ renderQueue->waitLeft(0); if (flushThread) flushThread->quit(); renderQueue = NULL; delete handler; delete parser; Statistics::getInstance()->printStats(); } catch (const std::exception &e) { std::cerr << "Caught a critical exception: " << e.what() << endl; return -1; } catch (...) { std::cerr << "Caught a critical exception of unknown type!" << endl; return -1; } return 0; }
size_t dispatch_to_string(const basic_string_view<CharT, Traits>& arg, Appender& app, FmtFlags flags) { app.append(arg); return arg.size(); }