some bugs fixed, logging is quite good now

master
Marc Wäckerlin 10 years ago
parent 24d419d6b2
commit 0c6af413a9
  1. 44
      src/commands.hxx

@ -75,7 +75,7 @@ class Function;
class Logger { class Logger {
public: public:
Logger(Command* command, Script* script); Logger(Command* command, Script* script, bool showLines = true);
void operator[](QString txt); void operator[](QString txt);
void operator()(QString txt); void operator()(QString txt);
~Logger(); ~Logger();
@ -178,17 +178,16 @@ class Command: public QObject {
return commands; return commands;
} }
QStringList commaSeparatedList(QString value) { QStringList commaSeparatedList(QString value) {
if (!value.size()) return QStringList();
switch (value.size()>1&&value.at(0)==value.at(value.size()-1) switch (value.size()>1&&value.at(0)==value.at(value.size()-1)
?value.at(0).toLatin1():'\0') { ?value.at(0).toLatin1():'\0') {
case '"': case '\'': { case '"': case '\'': {
return value.mid(1, value.size()-2) return value.mid(1, value.size()-2)
.split(QRegularExpression(QString(value[0])+", *" .split(QRegularExpression(QString(value[0])+", *"
+QString(value[0])), +QString(value[0])));
QString::SkipEmptyParts);
} break; } break;
default: { default: {
return value.split(QRegularExpression(", *"), return value.split(QRegularExpression(", *"));
QString::SkipEmptyParts);
} }
} }
} }
@ -274,7 +273,11 @@ class Comment: public Command {
return cmd; return cmd;
} }
bool execute(Script* script, QWebFrame*) { bool execute(Script* script, QWebFrame*) {
this->log(false);
Logger log(this, script); Logger log(this, script);
this->log(true);
log(_comment);
this->log(false);
return true; return true;
} }
private: private:
@ -858,10 +861,10 @@ class Script: public QObject {
Command* cmd(command?command:_command); Command* cmd(command?command:_command);
if (cmd) if (cmd)
prefix += QString("%2:%3%1 ") prefix += QString("%2:%3%1 ")
.arg(QString(' ', cmd->indent())) .arg(QString(cmd->indent(), QChar(' ')))
.arg(cmd->file(), 20) .arg(cmd->file(), 20, QChar(' '))
.arg(cmd->line(), -4, 10); .arg(cmd->line(), -4, 10, QChar(' '));
text = prefix+text.split('\n').join("\n "+prefix); text = prefix+text.split('\n').join("\n"+prefix+" ");
logging(text); logging(text);
std::cout<<text<<std::endl<<std::flush; std::cout<<text<<std::endl<<std::flush;
_cout += text + "\n"; _cout += text + "\n";
@ -1953,7 +1956,7 @@ class Function: public Command {
int indent) { int indent) {
std::shared_ptr<Function> cmd(new Function()); std::shared_ptr<Function> cmd(new Function());
if (!args.size()) throw BadArgument(tag()+" requires a <name>"); if (!args.size()) throw BadArgument(tag()+" requires a <name>");
QStringList allargs(args.split(" ", QString::SkipEmptyParts)); QStringList allargs(args.split(" "));
cmd->_name = allargs.takeFirst().trimmed(); cmd->_name = allargs.takeFirst().trimmed();
cmd->_vars = commaSeparatedList(allargs.join(' ')); cmd->_vars = commaSeparatedList(allargs.join(' '));
script->function(cmd->_name, cmd); script->function(cmd->_name, cmd);
@ -1962,7 +1965,7 @@ class Function: public Command {
return cmd; return cmd;
} }
bool execute(Script* script, QWebFrame*) { bool execute(Script* script, QWebFrame*) {
Logger log(this, script); Logger log(this, script, false);
return true; return true;
} }
bool call(Command* parentCommand, QStringList args, bool call(Command* parentCommand, QStringList args,
@ -2000,7 +2003,7 @@ class Call: public Command {
QStringList&, QString, int, int) { QStringList&, QString, int, int) {
std::shared_ptr<Call> cmd(new Call()); std::shared_ptr<Call> cmd(new Call());
if (!args.size()) throw BadArgument(tag()+" requires a <name>"); if (!args.size()) throw BadArgument(tag()+" requires a <name>");
QStringList allargs(args.split(" ", QString::SkipEmptyParts)); QStringList allargs(args.split(" "));
cmd->_name = allargs.takeFirst().trimmed(); cmd->_name = allargs.takeFirst().trimmed();
cmd->_args = commaSeparatedList(allargs.join(' ')); cmd->_args = commaSeparatedList(allargs.join(' '));
return cmd; return cmd;
@ -2060,7 +2063,7 @@ class If: public Command {
return cmd; return cmd;
} }
bool execute(Script* script, QWebFrame* frame) { bool execute(Script* script, QWebFrame* frame) {
Logger log(this, script); Logger log(this, script, false);
QString value(script->replacevars(_value)); QString value(script->replacevars(_value));
bool check(false); bool check(false);
switch (_cmp) { switch (_cmp) {
@ -2253,11 +2256,15 @@ inline bool Screenshot::execute(Script* script, QWebFrame* frame) {
return true; return true;
} }
inline Logger::Logger(Command* command, Script* script): inline Logger::Logger(Command* command, Script* script, bool showLines):
_command(command), _script(script) { _command(command), _script(script) {
_previous = _script->command(); _previous = _script->command();
_script->command(command); _script->command(command);
if (_command->log()) _script->log("\\ "+_command->command(), _command); if (_command->log())
if (showLines)
_script->log("\\ "+_command->command(), _command);
else
_script->log("\\ "+_command->command().split('\n').first(), _command);
} }
inline void Logger::operator()(QString txt) { inline void Logger::operator()(QString txt) {
if (_command->log()) _script->log(" "+txt, _command); if (_command->log()) _script->log(" "+txt, _command);
@ -2266,7 +2273,7 @@ inline void Logger::operator[](QString txt) {
_script->plainlog(txt); _script->plainlog(txt);
} }
inline Logger::~Logger() { inline Logger::~Logger() {
if (_command->log()) _script->log("/ "+_command->command(), _command); if (_command->log()) _script->log("/ "+_command->tag(), _command);
_script->command(_previous); _script->command(_previous);
} }
@ -2280,8 +2287,11 @@ inline bool Command::runScript(Command* parentCommand,
if (args.size()!=vars.size()) if (args.size()!=vars.size())
throw WrongNumberOfArguments(vars, args); throw WrongNumberOfArguments(vars, args);
for (QStringList::iterator var(vars.begin()), arg(args.begin()); for (QStringList::iterator var(vars.begin()), arg(args.begin());
var<vars.end() && arg<args.end(); ++var, ++arg) var<vars.end() && arg<args.end(); ++var, ++arg) {
parent->log("argument: "+*var+" = "+parent->replacevars(*arg),
parentCommand);
scriptCopy.set(*var, parent->replacevars(*arg)); scriptCopy.set(*var, parent->replacevars(*arg));
}
try { try {
connect(&scriptCopy, SIGNAL(logging(QString)), connect(&scriptCopy, SIGNAL(logging(QString)),
parent, SLOT(parentlog(QString))); parent, SLOT(parentlog(QString)));

Loading…
Cancel
Save