Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将ostream重定向到文件不工作_C++_Logging_File Io_Operator Overloading - Fatal编程技术网

C++ 将ostream重定向到文件不工作

C++ 将ostream重定向到文件不工作,c++,logging,file-io,operator-overloading,C++,Logging,File Io,Operator Overloading,我有一个自定义日志系统,它允许我根据当前选择的详细程度向日志文件和控制台发送信息。现在,我遇到的问题是文件的输出,控制台的输出工作正常 以下是一个例子: ilra_talk << "Local IP: " << systemIP() << " | Hostname: " << systemhostname() << endl; // the systemIP() and systemhostname() funct

我有一个自定义日志系统,它允许我根据当前选择的详细程度向日志文件和控制台发送信息。现在,我遇到的问题是文件的输出,控制台的输出工作正常

以下是一个例子:

     ilra_talk << "Local IP: " << systemIP() << " |  Hostname: " << systemhostname() << endl;
     // the systemIP() and systemhostname() functions have already been defined
类定义如下所示:

class ilra
{
    static int ilralevel_set; // properly initialized in my main .cpp
    static int ilralevel_passed; // properly initialized in my main .cpp
    static bool relay_enabled; // properly initialized in my main .cpp
    static bool log_enabled; // properly initialized in my main .cpp
    static ofstream logfile; // properly initialized in my main .cpp
public:
    // constructor / destructor
    ilra(const std::string &funcName, int toset)
    {
        ilralevel_passed = toset;
    }
    ~ilra(){};

    // enable / disable irla functions
    static void ilra_verbose_level(int toset){
        ilralevel_set = toset;
    }
    static void ilra_log_enabled(bool toset){
        log_enabled = toset;

        if (log_enabled == true){
            // get current time
            time_t rawtime;
            time ( &rawtime );

            // name of log file (based on time of application start)
            stringstream logname_s;
            string logname = "rclient-";
            logname_s << rawtime;
            logname.append(logname_s.str());

            // open a log file
            logfile.open(logname.c_str());
        }
    }

    // output
    template <class T>
    ilra &operator<<(const T &v)
    {
        if(log_enabled == true){ // log_enabled is set to true
            logfile << v; 
            logfile << "Test" << endl;  // test will show up, but intended information will not appear
            }
        if(ilralevel_passed <= ilralevel_set)
            std::cout << v;
        return *this;
    }

    ilra &operator<<(std::ostream&(*f)(std::ostream&))
    {
        if(log_enabled == true) // log_enabled is set to true
            logfile << *f;
        if(ilralevel_passed <= ilralevel_set)
            std::cout << *f;
        return *this;
    }
};  // end of the class
类ilra
{
static int ilralevel_set;//在my main.cpp中正确初始化
static int ilralevel_passed;//在my main.cpp中正确初始化
静态布尔继电器\u已启用;//在my main.cpp中正确初始化
静态bool log_enabled;//在my main.cpp中正确初始化
流日志文件的静态;//在my main.cpp中正确初始化
公众:
//构造函数/析构函数
ilra(常量标准::字符串和函数名,int-toset)
{
ilralevel_passed=toset;
}
~ilra(){};
//启用/禁用irla功能
静态无效ilra\U详细级别(int toset){
ilralevel_set=toset;
}
静态无效ilra_日志_已启用(bool-toset){
log_enabled=toset;
if(log_enabled==true){
//获取当前时间
时间与时间;
时间(&rawtime);
//日志文件的名称(基于应用程序启动的时间)
stringstream logname_s;
字符串logname=“rclient-”;

logname_s看起来像“Test”很可能是从其他地方打印出来的,事实上,
log\u enabled
并不是在向流中插入数据时设置的。您是否尝试过无条件地将数据插入
logfile
流中,或者每次
操作符都打印出
log\u enabled
,我看不出任何错误代码,但我个人会做两个更改:

  • 将logfile.flush()放入ilra::~ilra()。日志和缓冲不是朋友


  • static of Stream logfile
    更改为
    static of Stream*logfile
    :在
    ilra_log_enabled()
    中分配/删除它,并在日志文件输出看起来像什么?控制台输出看起来像什么?@Adam日志输出除了“测试”之外将不显示任何内容我为确保日志文件设置正确而添加的语句。控制台将显示此语句(对于上面的特定语句):Local IP:192.168.144.128 | Hostname:localhost.localdomain您是对的--代码没有问题。只是在文本文件的顶部创建了大量空白,导致实际内容移到底部。您有printf宏的示例吗?@BSchlinker:…嗯…常见t格式太糟糕了。不管怎样,这里是到GCC变量宏文档的链接——了解在其他编译器中查找什么
    class ilra
    {
        static int ilralevel_set; // properly initialized in my main .cpp
        static int ilralevel_passed; // properly initialized in my main .cpp
        static bool relay_enabled; // properly initialized in my main .cpp
        static bool log_enabled; // properly initialized in my main .cpp
        static ofstream logfile; // properly initialized in my main .cpp
    public:
        // constructor / destructor
        ilra(const std::string &funcName, int toset)
        {
            ilralevel_passed = toset;
        }
        ~ilra(){};
    
        // enable / disable irla functions
        static void ilra_verbose_level(int toset){
            ilralevel_set = toset;
        }
        static void ilra_log_enabled(bool toset){
            log_enabled = toset;
    
            if (log_enabled == true){
                // get current time
                time_t rawtime;
                time ( &rawtime );
    
                // name of log file (based on time of application start)
                stringstream logname_s;
                string logname = "rclient-";
                logname_s << rawtime;
                logname.append(logname_s.str());
    
                // open a log file
                logfile.open(logname.c_str());
            }
        }
    
        // output
        template <class T>
        ilra &operator<<(const T &v)
        {
            if(log_enabled == true){ // log_enabled is set to true
                logfile << v; 
                logfile << "Test" << endl;  // test will show up, but intended information will not appear
                }
            if(ilralevel_passed <= ilralevel_set)
                std::cout << v;
            return *this;
        }
    
        ilra &operator<<(std::ostream&(*f)(std::ostream&))
        {
            if(log_enabled == true) // log_enabled is set to true
                logfile << *f;
            if(ilralevel_passed <= ilralevel_set)
                std::cout << *f;
            return *this;
        }
    };  // end of the class