簡體   English   中英

如何將模板可變參數保存到std :: ofstream中?

[英]How to store template variadic arguments into std::ofstream?

我正在用C ++實現一個小的Log

class Log
    {
    public:
        enum LogLevel
        {
            TRACE,
            DEBUG,
            INFO,
            WARNING,
            ERROR,
            FATAL
        };

        Log(): m_log(DEBUG) {} ;
        ~Log() {};

        int SetLogFilePath(const std::string& p_directory, const std::string& p_prefix);

        void SetLogLevel(const LogLevel p_logLvl);

        LogLevel& GetLogLevel();

        template<typename... Args>
        int Logging(const std::string& p_logLevel, const std::string& p_srcFile, const std::string& p_function, const int& p_line,
            const Args&& ... p_args);

    private:
        LogLevel m_log;
        std::string m_logFilePath;
    };

這是我的Log類,我將目錄和前綴存儲到std::string m_logFilePath

template <typename ... Args>
    int Log::Logging(const std::string& p_logLevel, const std::string& p_srcFile, const std::string& p_function, const int& p_line,
        const Args&&... p_args)
    {
        std::ofstream log;
        log.open(m_logFilePath.c_str(), std::ios::out);
        if(!log.good())
        {
            std::cout << "Couldn't create file : " << m_logFilePath.c_str() << std::endl;
            return 0;
        }
        switch(p_logLevel)
        {
        case "TRACE":
            log << "(TRACE) ";
            break;

        case "DEBUG":
            log << "(DEBUG) ";
            break;

        case "INFO":
            log << "(INFO) ";
            break;

        case "WARNING":
            log << "(WARNING) ";
            break;

        case "ERROR":
            log << "(ERROR) ";
            break;

        case "FATAL":
            log << "(FATAL) ";
            break;

        default: break;
        }
        log << "Log Created at (Local Time): " << std::put_time(std::localtime(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now())), "%c") << " ";
        log << "File Name: " << p_srcFile.c_str() << " " << "Function Name: " << p_function.c_str() << " " << "Line: " << p_line << " " << std::forward<Args>(p_args) << std::endl;
        log.close();
        return 1;
    }

以上是我對int Logging(...)函數的實現。

基本上,我存儲日志記錄級別,當前時間,源文件路徑,函數名稱,代碼行以及一些可變參數。

我一直在尋找一種將多個可變參數存儲到std::ostream log但是找不到任何答案。

請幫忙!

如果我理解正確,那么您的問題是您不知道如何管理可變參量列表(基於變量模板)(在這種情況下:將其發送到輸出流)。

我提出了一個使用示例,該示例在(未使用的)數組初始化的上下文中使用逗號運算符的功能

using unused = int[];

int  cnt { -1 };

(void)unused { 0, (oss << ", extra val "
                       << ++cnt << " [" << args << ']', 0)... };

以下是一個簡化但完整的示例

#include <sstream>
#include <iostream>

template <typename ... Args>
std::string  logging (std::string const & valA, std::string const & valB,
                      std::string const & valC, Args const & ... args)
 {
   std::ostringstream oss;

   using unused = int[];

   oss << "valA [" << valA << "], "
       << "valB [" << valB << "], "
       << "valC [" << valC << ']';

   int  cnt { -1 };

   (void)unused { 0, (oss << ", extra val "
                          << ++cnt << " [" << args << ']', 0)... };

   return oss.str();
 }

int main()
 {
   auto l = logging("val1", "val2", "val3", 0, 1L, 2LL, 3U, 4UL, 5ULL,
                    6.0, 7.0f, "eight");

   std::cout << l << std::endl;
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM