Frage

Why does this code fragment:

 if (iErr) {
    std::stringstream ss;
    string serror("error");
    ss << "lua error code " << iErr << ": " << lua_tostring(lua, -1);
    ss >> serror;

    Log *log= StandardLog::getInstance();
    log->logError("Lua error following in next line");
    log->logError(serror);

    //lua_pop(lua, 1);/* pop error message from the stack */
    return 1;
 }

just print:

Lua error following in next line
lua

instead of

Lua error following in next line
lua error code 3: lua/generator/generator_example_nodenavigator.lua:10: attempt to call global 'require' (a nil value)

The Logger lines look like this:

void Logger::log(char message[]){
    string smessage(message);
    log(smessage);
}

void Logger::log(string smessage){
    ...
}

but it appears the logger is not at fault, since i get the same output using cerr.

War es hilfreich?

Lösung

The input operator >> extracts only until the next whitespace. Either use std::getline or the stringstream::str function to get the string.

My recommendation is to change to std::ostringstream and use std::ostringstream::str:

std::ostringstream oss;

oss << "lua error code " << iErr << ": " << lua_tostring(lua, -1);

Log *log= StandardLog::getInstance();
log->logError("Lua error following in next line");
log->logError(oss.str());

Andere Tipps

The extraction operator you're using reads a space-delimited string from the input stream. So, it stops when it hits the first space in our input.

Just replace

log->logError(serror);

with

log->logError(ss.str());

(and you don't need serror at all).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top