You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

59 lines
1.8 KiB

/*
MediaMtx (C++) - logger
Line format of MediaMTX: "2026/12/02 16:43:01 INF [path stream_3] [RTSP source] ready: 1 track (H264)"
la_mojo (kernelHelperMediaMTX) parses these lines (ERR / INF, "[path ", "RTSP source", "online").
*/
#pragma once
#include <cstdarg>
#include <functional>
#include <mutex>
#include <string>
namespace mtx {
enum class LogLevel { Debug = 0, Info = 1, Warn = 2, Error = 3 };
class Logger
{
public:
static Logger & instance();
void setLevel(LogLevel level);
LogLevel level() const { return m_level; }
// stdout and / or a file. The file is opened in append mode on every line, so it can be truncated
// by another process (la_mojo cleans the log above 40 KB).
void setDestinations(bool toStdout, const std::string & file);
// Every line (without the time stamp and the new line) also goes to this function, e.g. to the
// application's log (la_mojo: basicLogCollection). Empty: none.
typedef std::function<void(LogLevel, const char *)> Sink;
void setSink(Sink sink);
void log(LogLevel level, const char * fmt, ...) __attribute__((format(printf, 3, 4)));
void vlog(LogLevel level, const char * fmt, va_list ap);
private:
Logger() = default;
std::mutex m_mutex;
LogLevel m_level = LogLevel::Info;
bool m_toStdout = true;
std::string m_file;
Sink m_sink;
};
// Prefixes every line with a tag, e.g. "[path stream_3] [RTSP source] "
class LogParent
{
public:
explicit LogParent(std::string prefix = std::string()) : m_prefix(std::move(prefix)) {}
void setLogPrefix(const std::string & prefix) { m_prefix = prefix; }
const std::string & logPrefix() const { return m_prefix; }
void log(LogLevel level, const char * fmt, ...) const __attribute__((format(printf, 3, 4)));
private:
std::string m_prefix;
};
LogLevel parseLogLevel(const std::string & s, bool * ok = nullptr);
} // namespace mtx