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

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. MediaMtx (C++) - logger
  3. Line format of MediaMTX: "2026/12/02 16:43:01 INF [path stream_3] [RTSP source] ready: 1 track (H264)"
  4. la_mojo (kernelHelperMediaMTX) parses these lines (ERR / INF, "[path ", "RTSP source", "online").
  5. */
  6. #pragma once
  7. #include <cstdarg>
  8. #include <functional>
  9. #include <mutex>
  10. #include <string>
  11. namespace mtx {
  12. enum class LogLevel { Debug = 0, Info = 1, Warn = 2, Error = 3 };
  13. class Logger
  14. {
  15. public:
  16. static Logger & instance();
  17. void setLevel(LogLevel level);
  18. LogLevel level() const { return m_level; }
  19. // stdout and / or a file. The file is opened in append mode on every line, so it can be truncated
  20. // by another process (la_mojo cleans the log above 40 KB).
  21. void setDestinations(bool toStdout, const std::string & file);
  22. // Every line (without the time stamp and the new line) also goes to this function, e.g. to the
  23. // application's log (la_mojo: basicLogCollection). Empty: none.
  24. typedef std::function<void(LogLevel, const char *)> Sink;
  25. void setSink(Sink sink);
  26. void log(LogLevel level, const char * fmt, ...) __attribute__((format(printf, 3, 4)));
  27. void vlog(LogLevel level, const char * fmt, va_list ap);
  28. private:
  29. Logger() = default;
  30. std::mutex m_mutex;
  31. LogLevel m_level = LogLevel::Info;
  32. bool m_toStdout = true;
  33. std::string m_file;
  34. Sink m_sink;
  35. };
  36. // Prefixes every line with a tag, e.g. "[path stream_3] [RTSP source] "
  37. class LogParent
  38. {
  39. public:
  40. explicit LogParent(std::string prefix = std::string()) : m_prefix(std::move(prefix)) {}
  41. void setLogPrefix(const std::string & prefix) { m_prefix = prefix; }
  42. const std::string & logPrefix() const { return m_prefix; }
  43. void log(LogLevel level, const char * fmt, ...) const __attribute__((format(printf, 3, 4)));
  44. private:
  45. std::string m_prefix;
  46. };
  47. LogLevel parseLogLevel(const std::string & s, bool * ok = nullptr);
  48. } // namespace mtx