void Log::ReadLoggersFromConfig() { std::list<std::string> keys = sConfigMgr->GetKeysByString("Logger."); while (!keys.empty()) { CreateLoggerFromConfig(keys.front()); keys.pop_front(); } // Bad config configuration, creating default config if (loggers.find(LOGGER_ROOT) == loggers.end()) { fprintf(stderr, "Wrong Loggers configuration. Review your Logger config section.\n" "Creating default loggers [root (Error), server (Info)] to console\n"); Close(); // Clean any Logger or Appender created AppenderConsole* appender = new AppenderConsole(NextAppenderId(), "Console", LOG_LEVEL_DEBUG, APPENDER_FLAGS_NONE, ExtraAppenderArgs()); appenders[appender->getId()] = appender; Logger& rootLogger = loggers[LOGGER_ROOT]; rootLogger.Create(LOGGER_ROOT, LOG_LEVEL_ERROR); rootLogger.addAppender(appender->getId(), appender); Logger& serverLogger = loggers["server"]; serverLogger.Create("server", LOG_LEVEL_INFO); serverLogger.addAppender(appender->getId(), appender); } }
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 Log::CreateAppenderFromConfig(const char* name) { if (!name || *name == '\0') return; // Format=type,level,flags,optional1,optional2 // if type = File. optional1 = file and option2 = mode // if type = Console. optional1 = Color std::string options = "Appender."; options.append(name); options = ConfigMgr::GetStringDefault(options.c_str(), ""); Tokenizer tokens(options, ','); Tokenizer::const_iterator iter = tokens.begin(); if (tokens.size() < 2) { fprintf(stderr, "Log::CreateAppenderFromConfig: Wrong configuration for appender %s. Config line: %s\n", name, options.c_str()); return; } AppenderFlags flags = APPENDER_FLAGS_NONE; AppenderType type = AppenderType(atoi(*iter)); ++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); return; } if (++iter != tokens.end()) flags = AppenderFlags(atoi(*iter)); switch (type) { case APPENDER_CONSOLE: { AppenderConsole* appender = new AppenderConsole(NextAppenderId(), name, level, flags); appenders[appender->getId()] = appender; if (++iter != tokens.end()) appender->InitColors(*iter); //fprintf(stdout, "Log::CreateAppenderFromConfig: Created Appender %s (%u), Type CONSOLE, Mask %u\n", appender->getName().c_str(), appender->getId(), appender->getLogLevel()); // DEBUG - RemoveMe break; } case APPENDER_FILE: { std::string filename; std::string mode = "a"; if (++iter == tokens.end()) { fprintf(stderr, "Log::CreateAppenderFromConfig: Missing file name for appender %s\n", name); return; } filename = *iter; if (++iter != tokens.end()) mode = *iter; if (flags & APPENDER_FLAGS_USE_TIMESTAMP) { size_t dot_pos = filename.find_last_of("."); if (dot_pos != filename.npos) filename.insert(dot_pos, m_logsTimestamp); else filename += m_logsTimestamp; } uint8 id = NextAppenderId(); appenders[id] = new AppenderFile(id, name, level, filename.c_str(), m_logsDir.c_str(), mode.c_str(), flags); //fprintf(stdout, "Log::CreateAppenderFromConfig: Created Appender %s (%u), Type FILE, Mask %u, File %s, Mode %s\n", name, id, level, filename.c_str(), mode.c_str()); // DEBUG - RemoveMe break; } case APPENDER_DB: { uint8 id = NextAppenderId(); appenders[id] = new AppenderDB(id, name, level, realm); break; } default: fprintf(stderr, "Log::CreateAppenderFromConfig: Unknown type %d for appender %s\n", type, name); break; } }
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++)); switch (type) { case APPENDER_CONSOLE: { AppenderConsole* appender = new AppenderConsole(NextAppenderId(), name, level, flags); appenders[appender->getId()] = appender; if (size > 3) appender->InitColors(*iter++); //fprintf(stdout, "Log::CreateAppenderFromConfig: Created Appender %s (%u), Type CONSOLE, Mask %u\n", appender->getName().c_str(), appender->getId(), appender->getLogLevel()); break; } case APPENDER_FILE: { std::string filename; std::string mode = "a"; if (size < 4) { fprintf(stderr, "Log::CreateAppenderFromConfig: Missing file name for appender %s\n", name.c_str()); return; } filename = *iter++; if (size > 4) mode = *iter++; if (flags & APPENDER_FLAGS_USE_TIMESTAMP) { size_t dot_pos = filename.find_last_of("."); if (dot_pos != filename.npos) filename.insert(dot_pos, m_logsTimestamp); else filename += m_logsTimestamp; } uint64 maxFileSize = 0; if (size > 5) maxFileSize = atoi(*iter++); uint8 id = NextAppenderId(); appenders[id] = new AppenderFile(id, name, level, filename.c_str(), m_logsDir.c_str(), mode.c_str(), flags, maxFileSize); //fprintf(stdout, "Log::CreateAppenderFromConfig: Created Appender %s (%u), Type FILE, Mask %u, File %s, Mode %s\n", name.c_str(), id, level, filename.c_str(), mode.c_str()); break; } case APPENDER_DB: { uint8 id = NextAppenderId(); appenders[id] = new AppenderDB(id, name, level); break; } default: fprintf(stderr, "Log::CreateAppenderFromConfig: Unknown type %d for appender %s\n", type, name.c_str()); break; } }