log included/loaded files by instruction

This commit is contained in:
john30
2016-03-20 12:25:47 +01:00
parent 6e6e05c10e
commit adcf3e0d33
3 changed files with 74 additions and 28 deletions
+32 -5
View File
@@ -1275,16 +1275,43 @@ result_t Instruction::create(const string contextPath, const string& defaultDest
} else {
path = contextPath.substr(0, pos+1);
}
returnValue = new LoadInstruction(condition, singleton, path+(*it), defaultDest, defaultCircuit, defaultSuffix);
returnValue = new LoadInstruction(condition, singleton, defaultDest, defaultCircuit, defaultSuffix, path+(*it));
return RESULT_OK;
}
// unknown instruction
return RESULT_ERR_INVALID_ARG;
}
string Instruction::getDestination()
{
// ZZ.circuit[.suffix]
string ret;
if (!m_defaultDest.empty())
ret = m_defaultDest;
if (!m_defaultCircuit.empty() || !m_defaultSuffix.empty()) {
if (!ret.empty())
ret += ".";
if (m_defaultCircuit.empty())
ret += "*";
else
ret += m_defaultCircuit;
if (!m_defaultSuffix.empty())
ret += "."+m_defaultSuffix;
}
return ret;
}
result_t LoadInstruction::execute(MessageMap* messages) {
return messages->readFromFile(m_filename, false, m_defaultDest, m_defaultCircuit, m_defaultSuffix);
result_t LoadInstruction::execute(MessageMap* messages, ostringstream& log) {
result_t result = messages->readFromFile(m_filename, false, m_defaultDest, m_defaultCircuit, m_defaultSuffix);
if (result!=RESULT_OK) {
if (log.tellp()>0)
log << ", ";
log << "error " << (isSingleton() ? "loading " : "including ") << m_filename << " for \"" << getDestination() << "\": " << getResultCode(result);
return result;
}
log << (isSingleton() ? "loaded " : "included ") << m_filename << " for \"" << getDestination() << "\"";
return result;
}
@@ -1556,7 +1583,7 @@ result_t MessageMap::resolveCondition(Condition* condition) {
return result;
}
result_t MessageMap::executeInstructions(bool verbose) {
result_t MessageMap::executeInstructions(bool verbose, ostringstream& log) {
result_t overallResult = RESULT_OK;
for (map<string, vector<Instruction*> >::iterator it = m_instructions.begin(); it != m_instructions.end(); it++) {
vector<Instruction*> instructions = it->second;
@@ -1582,7 +1609,7 @@ result_t MessageMap::executeInstructions(bool verbose) {
if (instruction->isSingleton()) {
removeSingletons = true;
}
result_t result = instruction->execute(this);
result_t result = instruction->execute(this, log);
if (result!=RESULT_OK) {
overallResult = result;
}
+34 -21
View File
@@ -943,9 +943,12 @@ public:
* Construct a new instance.
* @param condition the @a Condition this instruction requires, or null.
* @param singleton whether this @a Instruction belongs to a set of instructions of which only the first one may be executed for the same source file.
* @param defaultDest the default destination address, or empty.
* @param defaultCircuit the default circuit name, or empty.
* @param defaultSuffix the default circuit name suffix (starting with a "."), or empty.
*/
Instruction(Condition* condition, const bool singleton)
: m_condition(condition), m_singleton(singleton) { }
Instruction(Condition* condition, const bool singleton, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix)
: m_condition(condition), m_singleton(singleton), m_defaultDest(defaultDest), m_defaultCircuit(defaultCircuit), m_defaultSuffix(defaultSuffix) { }
/**
* Destructor.
@@ -955,9 +958,9 @@ public:
/**
* Factory method for creating a new instance.
* @param contextPath the path and/or filename context being loaded.
* @param defaultDest the default destination address (may be overwritten by file name), or empty.
* @param defaultCircuit the default circuit name (may be overwritten by file name), or empty.
* @param defaultSuffix the default circuit name suffix (starting with a ".", may be overwritten by file name, or empty.
* @param defaultDest the default destination address, or empty.
* @param defaultCircuit the default circuit name, or empty.
* @param defaultSuffix the default circuit name suffix (starting with a "."), or empty.
* @param condition the @a Condition for the instruction, or NULL.
* @param type the type of the instruction.
* @param it the iterator to traverse for the definition parts.
@@ -980,12 +983,19 @@ public:
*/
bool isSingleton() { return m_singleton; }
/**
* Return a string describing the destination from the stored default values.
* @return a string describing the destination.
*/
string getDestination();
/**
* Execute the instruction.
* @param messages the @a MessageMap.
* @param log the @a ostringstream to log success messages to (if necessary).
* @return @a RESULT_OK on success, or an error code.
*/
virtual result_t execute(MessageMap* messages) = 0;
virtual result_t execute(MessageMap* messages, ostringstream& log) = 0;
private:
@@ -995,6 +1005,17 @@ private:
/** whether this @a Instruction belongs to a set of instructions of which only the first one may be executed for the same source file. */
bool m_singleton;
protected:
/** the default destination address, or empty. */
const string m_defaultDest;
/** the default circuit name, or empty. */
const string m_defaultCircuit;
/** the default circuit name suffix (starting with a "."), or empty. */
const string m_defaultSuffix;
};
@@ -1009,13 +1030,13 @@ public:
* Construct a new instance.
* @param condition the @a Condition this instruction requires, or null.
* @param singleton whether this @a Instruction belongs to a set of instructions of which only the first one may be executed for the same source file.
* @param filename the name of the file to load.
* @param defaultDest the default destination address (may be overwritten by file name), or empty.
* @param defaultCircuit the default circuit name (may be overwritten by file name), or empty.
* @param defaultSuffix the default circuit name suffix (starting with a ".", may be overwritten by file name, or empty.
* @param defaultSuffix the default circuit name suffix (starting with a ".", may be overwritten by file name), or empty.
* @param filename the name of the file to load.
*/
LoadInstruction(Condition* condition, const bool singleton, const string filename, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix)
: Instruction(condition, singleton), m_filename(filename), m_defaultDest(defaultDest), m_defaultCircuit(defaultCircuit), m_defaultSuffix(defaultSuffix) { }
LoadInstruction(Condition* condition, const bool singleton, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix, const string filename)
: Instruction(condition, singleton, defaultDest, defaultCircuit, defaultSuffix), m_filename(filename) { }
/**
* Destructor.
@@ -1023,22 +1044,13 @@ public:
virtual ~LoadInstruction() { }
// @copydoc
virtual result_t execute(MessageMap* messages);
virtual result_t execute(MessageMap* messages, ostringstream& log);
private:
/** the name of the file to load. */
const string m_filename;
/** the default destination address (may be overwritten by file name), or empty. */
const string m_defaultDest;
/** the default circuit name (may be overwritten by file name), or empty. */
const string m_defaultCircuit;
/** the default circuit name suffix (starting with a ".", may be overwritten by file name, or empty. */
const string m_defaultSuffix;
};
@@ -1119,9 +1131,10 @@ public:
/**
* Run all executable @a Instruction instances.
* @param verbose whether to verbosely add all problems to the error message.
* @param log the @a ostringstream to log success messages to (if necessary).
* @return @a RESULT_OK on success, or an error code.
*/
result_t executeInstructions(bool verbose);
result_t executeInstructions(bool verbose, ostringstream& log);
/**
* Get the stored @a Message instances for the key.