48 virtual void SetLogLevel(LogLevel level) = 0;
56 virtual void LogMessage(
const std::chrono::system_clock::time_point &time, LogLevel level,
const std::string msg)
const = 0;
61 static std::map<LogLevel, std::string> LogLevelNames = { { LogLevel::Trace,
"TRACE" }, { LogLevel::Debug,
"DEBUG" }, { LogLevel::Info,
"INFO" },
62 { LogLevel::Warn,
"WARN" }, { LogLevel::Error,
"ERROR" }, { LogLevel::Off,
"OFF" } };
69 std::string R5_SDK_CORE_EXPORT MapToStr(LogLevel logLevel);
78 LogLevel R5_SDK_CORE_EXPORT MapFromStr(
const std::string &logLevelStr,
bool *ok =
nullptr);
82 typedef std::shared_ptr<ILogger> ILoggerPtr;
100 template<
class... Args>
101 void trace(
const std::string format,
const Args &... args)
103 log(LogLevel::Trace, format, args...);
110 template<
class... Args>
111 void info(
const std::string format,
const Args &... args)
113 log(LogLevel::Info, format, args...);
120 template<
class... Args>
121 void warn(
const std::string format,
const Args &... args)
123 log(LogLevel::Warn, format, args...);
130 template<
class... Args>
131 void error(
const std::string format,
const Args &... args)
133 log(LogLevel::Error, format, args...);
140 template<
class... Args>
141 void debug(
const std::string format,
const Args &... args)
143 log(LogLevel::Debug, format, args...);
153 template<
class... Args>
154 void log(LogLevel level,
const std::string format,
const Args &... args)
156 log(std::chrono::system_clock::now(), level, format, args...);
165 template<
class... Args>
166 void log(
const std::chrono::system_clock::time_point &time, LogLevel level,
const std::string format,
const Args &... args)
172 auto formattedSize = std::snprintf(
nullptr, 0, format.c_str(), args...) + 1;
173 if (formattedSize <= 0) {
176 auto size =
static_cast<size_t>(formattedSize);
177 auto buf = std::make_unique<char[]>(size);
178 std::snprintf(buf.get(), size, format.c_str(), args...);
180 std::istringstream input(std::string(buf.get(), buf.get() + size - 1));
181 for (std::string line; std::getline(input, line);) {
182 mT->LogMessage(time, level, line);
194 static std::string ToStr(
const std::chrono::system_clock::time_point &time)
196 static const auto fmt =
"%Y-%m-%d %T";
199 char dateTime[30] = { 0 };
200 const auto t = std::chrono::system_clock::to_time_t(time);
201 if (std::strftime(dateTime,
sizeof(dateTime), fmt, localtime(&t))) {
204 std::stringstream ss;
205 ss << dateTime <<
"." << std::setw(3) << std::setfill(
'0')
206 << std::chrono::duration_cast<std::chrono::milliseconds>(time.time_since_epoch()).count() % 1000;
223 virtual void LogMessage(
const std::chrono::system_clock::time_point &time, LogLevel level,
const std::string msg)
const override 225 if (level < mLevel) {
229 std::stringstream ss;
230 ss << ToStr(time) <<
" ";
232 std::map<LogLevel, std::string>::iterator iter = LogLevelNames.find(level);
233 if (iter != LogLevelNames.end()) {
234 std::stringstream ssLevel;
235 ssLevel <<
"[" << iter->second <<
"]";
236 ss << std::setw(9) << std::left << ssLevel.str();
239 ss << msg << std::endl;
241 auto &os = (level == LogLevel::Error ? std::cerr : std::cout);
248 virtual void SetLogLevel(LogLevel level)
override { mLevel = level; }
251 LogLevel mLevel = LogLevel::Warn;
273 virtual void LogMessage(
const std::chrono::system_clock::time_point &time, LogLevel level,
const std::string msg)
const override 275 if (level < mLevel) {
279 std::lock_guard<std::mutex> lock(mMutex);
281 std::ofstream logFile(mFileName, std::ios_base::app);
282 if (logFile.is_open()) {
283 logFile << ToStr(time) <<
" ";
285 std::map<LogLevel, std::string>::iterator iter = LogLevelNames.find(level);
286 if (iter != LogLevelNames.end()) {
287 std::stringstream ssLevel;
288 ssLevel <<
"[" << iter->second <<
"]";
289 logFile << std::setw(9) << std::left << ssLevel.str();
292 logFile << msg << std::endl;
302 virtual void SetLogLevel(LogLevel level)
override { mLevel = level; }
305 LogLevel mLevel = LogLevel::Warn;
306 std::string mFileName;
307 mutable std::mutex mMutex;
virtual void SetLogLevel(LogLevel level) override
Implementatio of method to set the log level of object.
Definition: r5logger.h:302
virtual void SetLogLevel(LogLevel level) override
Implementatio of method to set the log level of object.
Definition: r5logger.h:248
void log(const std::chrono::system_clock::time_point &time, LogLevel level, const std::string format, const Args &... args)
Log message with specified log level and timestamp.
Definition: r5logger.h:166
LoggerWrapper(ILoggerPtr t)
Default constructor.
Definition: r5logger.h:94
void error(const std::string format, const Args &... args)
Log message with Error log level.
Definition: r5logger.h:131
Helper object that simplfies use of ILogger.
Definition: r5logger.h:86
ILoggerPtr mT
internal pointer to logger instance
Definition: r5logger.h:186
virtual void LogMessage(const std::chrono::system_clock::time_point &time, LogLevel level, const std::string msg) const override
Implementation of method to log message to stdout.
Definition: r5logger.h:223
void warn(const std::string format, const Args &... args)
Log message with Warn log level.
Definition: r5logger.h:121
virtual void LogMessage(const std::chrono::system_clock::time_point &time, LogLevel level, const std::string msg) const override
Implementation of method to log message to specified file.
Definition: r5logger.h:273
void debug(const std::string format, const Args &... args)
Log message with Debug log level.
Definition: r5logger.h:141
void log(LogLevel level, const std::string format, const Args &... args)
Log message with specified log level.
Definition: r5logger.h:154
Simple implementation of logger that prints messages to file.
Definition: r5logger.h:257
Interface for Red5Pro Core SDK internal logger access.
Definition: r5logger.h:39
Simple implementation of logger that prints messages to stdout.
Definition: r5logger.h:213
void trace(const std::string format, const Args &... args)
Log message with Trace log level.
Definition: r5logger.h:101
void info(const std::string format, const Args &... args)
Log message with Info log level.
Definition: r5logger.h:111
FileLogger(std::string fileName)
Constructor that specify output file name.
Definition: r5logger.h:265