static void LogToConsole(const char *msg, LogLevel level, bool color) { FILE *output_file = stdout; // Messages should ALL go to stdout else they are disordered struct tm now; time_t now_seconds = time(NULL); localtime_r(&now_seconds, &now); if (color) { fprintf(output_file, "%s", LogLevelToColor(level)); } if (level >= LOG_LEVEL_INFO && VPREFIX[0]) { fprintf(stdout, "%s ", VPREFIX); } if (TIMESTAMPS) { char formatted_timestamp[64]; LoggingFormatTimestamp(formatted_timestamp, 64, &now); fprintf(stdout, "%s ", formatted_timestamp); } fprintf(stdout, "%8s: %s\n", LogLevelToString(level), msg); if (color) { // Turn off the color again. fprintf(output_file, "\x1b[0m"); } }
void LogManager::Log(const Logger& Log, LogLevel Level, const std::string& Message) { //assert(Sinks.size() > 0 && "No sinks to log to, possible leak after LogManager shutdown?"); if (Sinks.size() == 0) { return; } LogMessage LoggedMessage(Log); LoggedMessage.Time = time(NULL); LoggedMessage.Level = Level; LoggedMessage.RawMessage = Message; struct std::tm* tm = localtime(&LoggedMessage.Time); char dateTime[20] = ""; std::strftime(dateTime, 20, "%Y/%m/%d %H:%M:%S", tm); std::stringstream format_stream; format_stream << dateTime << " [" << LogLevelToString(LoggedMessage.Level) << "] " << LoggedMessage.Log.GetName() << " - " << LoggedMessage.RawMessage; LoggedMessage.FormattedMessage = format_stream.str(); for (auto it = Sinks.begin(); it != Sinks.end(); ++it) { (*it)->Append(LoggedMessage); } }
/** * @brief Get the Output Stream */ std::ostringstream& Get( const LogLevel& level = LogLevel::INFO ){ os << "- " << NowTime(); os << " " << LogLevelToString(level) << ": "; os << std::string((int)level > (int)LogLevel::DEBUG ? (int)level - (int)LogLevel::DEBUG : 0, '\t'); return os; }
void LogManager::Log(LogLevel level, const std::string& logMsg) { if (level < mLogFilter) return; mLogStream << mLogPrefix << " - " + LogLevelToString(level) + " - " << logMsg << std::endl; }
static void LogToConsole(const char *msg, LogLevel level, bool color) { FILE *output_file = stdout; // Messages should ALL go to stdout else they are disordered struct tm now; time_t now_seconds = time(NULL); localtime_r(&now_seconds, &now); char formatted_timestamp[64]; LoggingFormatTimestamp(formatted_timestamp, 64, &now); if (MACHINE_OUTPUT) { const char *string_level = LogLevelToString(level); if (color) { fprintf(output_file, "%s%s %8s: %s\x1b[0m\n", LogLevelToColor(level), formatted_timestamp, string_level, msg); } else { fprintf(output_file, "%s %8s: %s\n", formatted_timestamp, string_level, msg); } } else { if (level >= LOG_LEVEL_INFO && VPREFIX[0]) { fprintf(stdout, "%s ", VPREFIX); } if (!BePretty) { fprintf(stdout, "%s ", formatted_timestamp); } if (level <= LOG_LEVEL_INFO) { fprintf(stdout, "%s %s\n", LogLevelToString(level), msg); } else { fprintf(stdout, "%s\n", msg); } } }
void SetupLogger(const char* name, int level) { static bool setup = false; if (setup) return; setup = true; std::string loggerName = name; // The logger is set to log all messages. We use the appenders to restrict the output(s). log4cplus::Logger logger = log4cplus::Logger::getInstance(loggerName); logger.setLogLevel(level); logger.setAdditivity(true); // The console appender log4cplus::SharedAppenderPtr consoleAppender(new log4cplus::ConsoleAppender); std::string consoleAppName = loggerName + ".console"; consoleAppender->setName(consoleAppName); consoleAppender->setThreshold(log4cplus::INFO_LOG_LEVEL); std::string consolePattern = "%-5p (%d{%q}) [%b:%L] %m%n"; std::auto_ptr<log4cplus::Layout> layout(new log4cplus::PatternLayout(consolePattern)); consoleAppender->setLayout(layout); logger.addAppender(consoleAppender); // Generate the name of the rolling file in $HOME/Library/Logs std::string homeDir = getenv("HOME"); std::string logFileName = loggerName + ".log"; std::string logFilePath = homeDir + "/Library/Logs/" + logFileName; std::string logFileAppName = loggerName + ".file"; log4cplus::SharedAppenderPtr logFileApp(new log4cplus::RollingFileAppender(logFilePath, 1000000, 3, false)); logFileApp->setName(logFileAppName); std::string filePattern = "%-5p [%d{%y-%m-%d %H:%M:%S:%q}][%b:%L] %m%n"; std::auto_ptr<log4cplus::Layout> fileLayout(new log4cplus::PatternLayout(filePattern)); logFileApp->setLayout(fileLayout); logFileApp->setThreshold(log4cplus::TRACE_LOG_LEVEL); logger.addAppender(logFileApp); // Send this to the console. LOG4CPLUS_INFO(logger, "Logging to file: " << logFilePath << ", Level: " << LogLevelToString(level)); }
void LogToStdout(const char *msg, LogLevel level, bool color) { if (LEGACY_OUTPUT) { if (level >= LOG_LEVEL_VERBOSE) { printf("%s> %s\n", VPREFIX, msg); } else { printf("%s\n", msg); } } else { struct tm now; time_t now_seconds = time(NULL); localtime_r(&now_seconds, &now); char formatted_timestamp[25]; if (strftime(formatted_timestamp, 25, "%Y-%m-%dT%H:%M:%S%z", &now) == 0) { // There was some massacre formating the timestamp. Wow strlcpy(formatted_timestamp, "<unknown>", sizeof(formatted_timestamp)); } const char *string_level = LogLevelToString(level); if (color) { printf("%s%-24s %8s: %s\x1b[0m\n", LogLevelToColor(level), formatted_timestamp, string_level, msg); } else { printf("%-24s %8s: %s\n", formatted_timestamp, string_level, msg); } } }
void Logger::operator () (LogLevel level, const mpt::ustring &text) //----------------------------------------------------------------- { DoLog(context, LogLevelToString(level) + MPT_USTRING(": ") + text); }
void clConsole::PrintLogLevelC(const LString& Params) { (void)Params; Env->Console->Display( LString("Current log level: ") + LogLevelToString( Env->GetLogLevel() ) ); }