Порадуйте log4cxx оператором ostream <<

Глядя на этот старый пост, я тоже пытаюсь собрать оператор ostream << (), но продолжаю работать, чтобы компилировать проблемы. Я пробовал предложения пространства имен, как std::, так и log4cxx::helpers, но ничего не получилось.

у меня есть vector<uint8_t> что я хотел бы сбросить в LOG4CXX_DEBUG() и т. д. У меня есть это до сих пор:

ostream& operator<<(ostream& os, const vector<uint8_t>& bs)
{
    ios_base::fmtflags oldFlags = os.flags();
    streamsize oldPrec = os.precision();
    char oldFill = os.fill();

    int s = bs.size();
    os << setfill('0') << uppercase << hex;
    for (int i = 0; i < s; ++i) {
        os << "0x" << setw(2) << (int)bs[i] << ' ';
    }

    os.flags(oldFlags);
    os.precision(oldPrec);
    os.fill(oldFill);

    return os;
}

Компилятор GNU g++ выплевывает что-то вроде следующего:

Compiling StationControllertest.cpp... In file included from /usr/include/log4cxx/logger.h:32:0,
                 from ./Configuration.h:15,
                 from StationController.h:4,
                 from StationControllertest.cpp:14:
/usr/include/log4cxx/helpers/messagebuffer.h: In function ‘std::basic_ostream<char>& log4cxx::helpers::operator<<(log4cxx::helpers::CharMessageBuffer&, const V&) [with V = std::vector<unsigned char>]’:
StationControllertest.cpp:474:5:   instantiated from here
/usr/include/log4cxx/helpers/messagebuffer.h:190:47: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
/usr/include/c++/4.6/ostream:581:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = std::vector<unsigned char>]’
makefile:120: recipe for target 'StationControllertest.o' failed
make: *** [StationControllertest.o] Error 1

РЕДАКТИРОВАТЬ: Вот пример программы

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <stdint.h>
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>

using namespace std;
using namespace log4cxx;
using namespace log4cxx::helpers;

static LoggerPtr logger(Logger::getLogger("log4test"));


ostream& operator<<(ostream& os, const vector<uint8_t>& bs)
{
    ios_base::fmtflags oldFlags = os.flags();
    streamsize oldPrec = os.precision();
    char oldFill = os.fill();

    int s = bs.size();
    os << setfill('0') << uppercase << hex;
    for (int i = 0; i < s; ++i) {
        os << "0x" << setw(2) << (int)bs[i] << ' ';
    }

    os.flags(oldFlags);
    os.precision(oldPrec);
    os.fill(oldFill);

    return os;
}


int main(int argc, char* argv[])
{
    PropertyConfigurator::configure("Log4cxxConfig.cfg");

    vector<uint8_t> v = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a };

    cout << v << endl;
    LOG4CXX_INFO(logger, v);
}

Оператор cout компилируется и работает, но не макрос LOG4CXX_INFO. Спасибо!

0 ответов

Другие вопросы по тегам