Domanda

Come posso registrare più di una singola stringa con log4cpp?

E.G.Se voglio registrare tutto Argv a Main:

#include <iostream>
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/PatternLayout.hh>

using namespace std;

int main(int argc, char* argv[]) {
    log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender","mylog");
    log4cpp::PatternLayout *layout = new log4cpp::PatternLayout();
    layout->setConversionPattern("%d: %p - %m %n");
    log4cpp::Category& category = log4cpp::Category::getInstance("Category");
    appender->setLayout(layout);
    category.setAppender(appender);
    category.setPriority(log4cpp::Priority::INFO); 

    category.info("program started"); // this works fine, I see it in the logfile

    for(int i=0; i<argc; ++i) {
        // next line does not compile:
        category.info("argv["<<i<<"] = '"<<argv[i]<<"'");
    }

    return 0;
}   
.

La riga

category.info("argv["<<i<<"] = '"<<argv[i]<<"'");
.

non è compilato.Ovviamente il logger non funziona come un Östream .Qual è il modo Log4Cpp Way per registrare qualcosa del genere, preferibile contemporaneamente?

È stato utile?

Soluzione

You have two options:

  • Use printf-style formatting:

    for (int i = 0; i < argc; ++i)
    {   
        category.info("argv[%d] = '%s'", i, argv[i]);
    }  
    
  • Use infoStream():

    for (int i = 0; i < argc; ++i)
    {
        category.infoStream() << "argv[" << i << "] = '" << argv[i] << "'";
    }  
    

I'd go with the latter.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top