zhuizhu 8 mēneši atpakaļ
vecāks
revīzija
c387e449d9

+ 0 - 1
AvRecorder/basic/basic.h

@@ -60,7 +60,6 @@ void Free(T*& ptr, Func&& func)
     if (ptr == nullptr) {
         return;
     }
-
     func();
     ptr = nullptr;
 }

+ 4 - 4
AvRecorder/recorder/audio_recorder.cpp

@@ -136,10 +136,10 @@ void AudioRecorder::PullAndProcessAudio()
         while (true) {
             char buf[1024];
             int bytes = m_audioCapturers[index]->readAudioData(buf, sizeof(buf));
-            static int debugCounter = 0;
-            if (++debugCounter % 100 == 0) { // 每100次打印一次,避免日志过多
-                qDebug() << "Capturer" << index << "read" << bytes << "bytes";
-            }
+            // static int debugCounter = 0;
+            // if (++debugCounter % 100 == 0) { // 每100次打印一次,避免日志过多
+            //     qDebug() << "Capturer" << index << "read" << bytes << "bytes";
+            // }
             if (bytes <= 0) {
                 break;
             }

+ 0 - 1
AvRecorder/ui/av_recorder.h

@@ -9,7 +9,6 @@
 
 #include "audio_widget.h"
 #include "qcombobox.h"
-#include "qobjectdefs.h"
 #include "qstatusbar.h"
 #include "recorder/audio_recorder.h"
 #include "recorder/video_recorder.h"

+ 166 - 0
tlogger.cpp

@@ -0,0 +1,166 @@
+#include "tlogger.h"
+
+#include <iomanip>
+#include <iostream>
+
+namespace TC {
+// ConsoleOutput实现
+void ConsoleOutput::write(LogLevel level, const std::string& message)
+{
+    std::ostream& stream = (level >= LogLevel::WARNING) ? std::cerr : std::cout;
+    stream << message << std::endl;
+    stream.flush();
+}
+
+// FileOutput实现
+FileOutput::FileOutput(const std::string& filename)
+{
+    m_file.open(filename, std::ios::app);
+    if (!m_file.is_open()) {
+        std::cerr << "Failed to open log file: " << filename << std::endl;
+    }
+}
+
+FileOutput::~FileOutput()
+{
+    if (m_file.is_open()) {
+        m_file.close();
+    }
+}
+
+void FileOutput::write(LogLevel level, const std::string& message)
+{
+    std::lock_guard<std::mutex> lock(m_mutex);
+    if (m_file.is_open()) {
+        m_file << message << std::endl;
+        m_file.flush();
+    }
+}
+
+// Logger实现
+Logger& Logger::instance()
+{
+    static Logger instance;
+    return instance;
+}
+
+void Logger::setLevel(LogLevel level)
+{
+    std::lock_guard<std::mutex> lock(m_mutex);
+    m_level = level;
+}
+
+LogLevel Logger::getLevel() const
+{
+    std::lock_guard<std::mutex> lock(m_mutex);
+    return m_level;
+}
+
+void Logger::addOutput(std::unique_ptr<LogOutput> output)
+{
+    std::lock_guard<std::mutex> lock(m_mutex);
+    m_outputs.push_back(std::move(output));
+}
+
+void Logger::clearOutputs()
+{
+    std::lock_guard<std::mutex> lock(m_mutex);
+    m_outputs.clear();
+}
+
+void Logger::debug(const std::string& message)
+{
+    if (shouldLog(LogLevel::DEBUG)) {
+        log(LogLevel::DEBUG, message);
+    }
+}
+
+void Logger::info(const std::string& message)
+{
+    if (shouldLog(LogLevel::INFO)) {
+        log(LogLevel::INFO, message);
+    }
+}
+
+void Logger::warning(const std::string& message)
+{
+    if (shouldLog(LogLevel::WARNING)) {
+        log(LogLevel::WARNING, message);
+    }
+}
+
+void Logger::error(const std::string& message)
+{
+    if (shouldLog(LogLevel::ERROR)) {
+        log(LogLevel::ERROR, message);
+    }
+}
+
+void Logger::initialize(const std::string& logFile,
+                        LogLevel level,
+                        bool enableConsole,
+                        bool enableQt)
+{
+    Logger& logger = instance();
+    logger.clearOutputs();
+    logger.setLevel(level);
+
+    // 添加文件输出
+    if (!logFile.empty()) {
+        logger.addOutput(std::make_unique<FileOutput>(logFile));
+    }
+
+    // 添加控制台输出
+    if (enableConsole) {
+        logger.addOutput(std::make_unique<ConsoleOutput>());
+    }
+
+    logger.info("AV Logger initialized");
+}
+
+void Logger::log(LogLevel level, const std::string& message)
+{
+    std::lock_guard<std::mutex> lock(m_mutex);
+
+    std::string formattedMessage = getCurrentTime() + " [" + levelToString(level) + "] " + message;
+
+    for (auto& output : m_outputs) {
+        if (output) {
+            output->write(level, formattedMessage);
+        }
+    }
+}
+
+bool Logger::shouldLog(LogLevel level) const
+{
+    return level >= m_level;
+}
+
+std::string Logger::getCurrentTime() const
+{
+    auto now = std::chrono::system_clock::now();
+    auto time_t = std::chrono::system_clock::to_time_t(now);
+    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
+
+    std::ostringstream oss;
+    oss << std::put_time(std::localtime(&time_t), "%Y-%m-%d %H:%M:%S");
+    oss << '.' << std::setfill('0') << std::setw(3) << ms.count();
+    return oss.str();
+}
+
+std::string Logger::levelToString(LogLevel level) const
+{
+    switch (level) {
+    case LogLevel::DEBUG:
+        return "DEBUG";
+    case LogLevel::INFO:
+        return "INFO";
+    case LogLevel::WARNING:
+        return "WARN";
+    case LogLevel::ERROR:
+        return "ERROR";
+    default:
+        return "UNKNOWN";
+    }
+}
+} // namespace TC

+ 174 - 0
tlogger.h

@@ -0,0 +1,174 @@
+#ifndef TLOGGER_H
+#define TLOGGER_H
+
+#include <fstream>
+#include <memory>
+#include <mutex>
+#include <sstream>
+#include <string>
+#include <vector>
+
+// 前向声明Qt类型,避免在头文件中包含Qt
+class QString;
+
+namespace TC {
+// 日志级别枚举
+enum class LogLevel { DEBUG = 0, INFO = 1, WARNING = 2, ERROR = 3 };
+
+// 日志输出接口
+class LogOutput
+{
+public:
+    virtual ~LogOutput() = default;
+    virtual void write(LogLevel level, const std::string& message) = 0;
+};
+
+// 控制台输出
+class ConsoleOutput : public LogOutput
+{
+public:
+    void write(LogLevel level, const std::string& message) override;
+};
+
+// 文件输出
+class FileOutput : public LogOutput
+{
+public:
+    explicit FileOutput(const std::string& filename);
+    ~FileOutput();
+    void write(LogLevel level, const std::string& message) override;
+
+private:
+    std::ofstream m_file;
+    std::mutex m_mutex;
+};
+
+// 主日志类
+class Logger
+{
+public:
+    static Logger& instance();
+
+    // 设置日志级别
+    void setLevel(LogLevel level);
+    LogLevel getLevel() const;
+
+    // 添加输出目标
+    void addOutput(std::unique_ptr<LogOutput> output);
+    void clearOutputs();
+
+    // 日志记录方法
+    void debug(const std::string& message);
+    void info(const std::string& message);
+    void warning(const std::string& message);
+    void error(const std::string& message);
+
+    // 格式化日志记录
+    template<typename... Args>
+    void debugf(const std::string& format, Args&&... args)
+    {
+        if (shouldLog(LogLevel::DEBUG)) {
+            log(LogLevel::DEBUG, formatString(format, std::forward<Args>(args)...));
+        }
+    }
+
+    template<typename... Args>
+    void infof(const std::string& format, Args&&... args)
+    {
+        if (shouldLog(LogLevel::INFO)) {
+            log(LogLevel::INFO, formatString(format, std::forward<Args>(args)...));
+        }
+    }
+
+    template<typename... Args>
+    void warningf(const std::string& format, Args&&... args)
+    {
+        if (shouldLog(LogLevel::WARNING)) {
+            log(LogLevel::WARNING, formatString(format, std::forward<Args>(args)...));
+        }
+    }
+
+    template<typename... Args>
+    void errorf(const std::string& format, Args&&... args)
+    {
+        if (shouldLog(LogLevel::ERROR)) {
+            log(LogLevel::ERROR, formatString(format, std::forward<Args>(args)...));
+        }
+    }
+
+#ifdef QT_CORE_LIB
+    // Qt兼容接口
+    void qtDebug(const QString& message);
+    void qtInfo(const QString& message);
+    void qtWarning(const QString& message);
+    void qtError(const QString& message);
+#endif
+
+    // 初始化方法(全局初始化时调用)
+    static void initialize(const std::string& logFile = "av_log.txt",
+                           LogLevel level = LogLevel::INFO,
+                           bool enableConsole = true,
+                           bool enableQt = true);
+
+private:
+    Logger() = default;
+    ~Logger() = default;
+    Logger(const Logger&) = delete;
+    Logger& operator=(const Logger&) = delete;
+
+    void log(LogLevel level, const std::string& message);
+    bool shouldLog(LogLevel level) const;
+    std::string getCurrentTime() const;
+    std::string levelToString(LogLevel level) const;
+
+    template<typename... Args>
+    std::string formatString(const std::string& format, Args&&... args)
+    {
+        std::ostringstream oss;
+        formatImpl(oss, format, std::forward<Args>(args)...);
+        return oss.str();
+    }
+
+    // 处理无参数情况的formatImpl重载
+    void formatImpl(std::ostringstream& oss, const std::string& format) { oss << format; }
+
+    template<typename T>
+    void formatImpl(std::ostringstream& oss, const std::string& format, T&& value)
+    {
+        size_t pos = format.find("{}");
+        if (pos != std::string::npos) {
+            oss << format.substr(0, pos) << std::forward<T>(value) << format.substr(pos + 2);
+        } else {
+            oss << format;
+        }
+    }
+
+    template<typename T, typename... Args>
+    void formatImpl(std::ostringstream& oss, const std::string& format, T&& value, Args&&... args)
+    {
+        size_t pos = format.find("{}");
+        if (pos != std::string::npos) {
+            oss << format.substr(0, pos) << std::forward<T>(value);
+            formatImpl(oss, format.substr(pos + 2), std::forward<Args>(args)...);
+        } else {
+            oss << format;
+        }
+    }
+
+    LogLevel m_level = LogLevel::INFO;
+    std::vector<std::unique_ptr<LogOutput>> m_outputs;
+    mutable std::mutex m_mutex;
+};
+} // namespace TC
+
+#define T_LOGGER_DEBUG(msg) TC::Logger::instance().debug(msg)
+#define T_LOGGER_INFO(msg) TC::Logger::instance().info(msg)
+#define T_LOGGER_WARNING(msg) TC::Logger::instance().warning(msg)
+#define T_LOGGER_ERROR(msg) TC::Logger::instance().error(msg)
+
+#define T_LOGGER_DEBUGF(fmt, ...) TC::Logger::instance().debugf(fmt, __VA_ARGS__)
+#define T_LOGGER_INFOF(fmt, ...) TC::Logger::instance().infof(fmt, __VA_ARGS__)
+#define T_LOGGER_WARNINGF(fmt, ...) TC::Logger::instance().warningf(fmt, __VA_ARGS__)
+#define T_LOGGER_ERRORF(fmt, ...) TC::Logger::instance().errorf(fmt, __VA_ARGS__)
+
+#endif // TLOGGER_H