Skip to content

Commit

Permalink
Merge pull request OpenMW#2336 from akortunov/logging
Browse files Browse the repository at this point in the history
Use the logging system for compiler errors
  • Loading branch information
psi29a authored Apr 24, 2019
2 parents df94bfb + bd2188a commit b43eb56
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 29 deletions.
6 changes: 2 additions & 4 deletions apps/openmw/mwdialogue/dialoguemanagerimp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ namespace MWDialogue
DialogueManager::DialogueManager (const Compiler::Extensions& extensions, Translation::Storage& translationDataStorage) :
mTranslationDataStorage(translationDataStorage)
, mCompilerContext (MWScript::CompilerContext::Type_Dialogue)
, mErrorStream(std::cout.rdbuf())
, mErrorHandler(mErrorStream)
, mErrorHandler()
, mTalkedTo(false)
, mTemporaryDispositionChange(0.f)
, mPermanentDispositionChange(0.f)
Expand Down Expand Up @@ -204,8 +203,7 @@ namespace MWDialogue

if (!success)
{
Log(Debug::Warning)
<< "Warning: compiling failed (dialogue script)\n" << cmd << "\n\n";
Log(Debug::Error) << "Error: compiling failed (dialogue script): \n" << cmd << "\n";
}

return success;
Expand Down
1 change: 0 additions & 1 deletion apps/openmw/mwdialogue/dialoguemanagerimp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace MWDialogue

Translation::Storage& mTranslationDataStorage;
MWScript::CompilerContext mCompilerContext;
std::ostream mErrorStream;
Compiler::StreamErrorHandler mErrorHandler;

MWWorld::Ptr mActor;
Expand Down
6 changes: 2 additions & 4 deletions apps/openmw/mwdialogue/scripttest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ void test(const MWWorld::Ptr& actor, int &compiled, int &total, const Compiler::

MWScript::CompilerContext compilerContext(MWScript::CompilerContext::Type_Dialogue);
compilerContext.setExtensions(extensions);
std::ostream errorStream(std::cout.rdbuf());
Compiler::StreamErrorHandler errorHandler(errorStream);
Compiler::StreamErrorHandler errorHandler;
errorHandler.setWarningsMode (warningsMode);

const MWWorld::Store<ESM::Dialogue>& dialogues = MWBase::Environment::get().getWorld()->getStore().get<ESM::Dialogue>();
Expand Down Expand Up @@ -84,8 +83,7 @@ void test(const MWWorld::Ptr& actor, int &compiled, int &total, const Compiler::

if (!success)
{
Log(Debug::Warning)
<< "compiling failed (dialogue script)\n" << info->mResultScript << "\n\n";
Log(Debug::Error) << "Error: compiling failed (dialogue script): \n" << info->mResultScript << "\n";
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions apps/openmw/mwscript/scriptmanagerimp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace MWScript
ScriptManager::ScriptManager (const MWWorld::ESMStore& store,
Compiler::Context& compilerContext, int warningsMode,
const std::vector<std::string>& scriptBlacklist)
: mErrorHandler (std::cerr), mStore (store),
: mErrorHandler(), mStore (store),
mCompilerContext (compilerContext), mParser (mErrorHandler, mCompilerContext),
mOpcodesInstalled (false), mGlobalScripts (store)
{
Expand Down Expand Up @@ -72,8 +72,7 @@ namespace MWScript

if (!Success)
{
Log(Debug::Warning)
<< "Warning: compiling failed: " << name;
Log(Debug::Error) << "Error: script compiling failed: " << name;
}

if (Success)
Expand Down
45 changes: 32 additions & 13 deletions components/compiler/streamerrorhandler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "streamerrorhandler.hpp"

#include <sstream>

#include <components/debug/debuglog.hpp>

#include "tokenloc.hpp"

namespace Compiler
Expand All @@ -9,38 +13,53 @@ namespace Compiler
void StreamErrorHandler::report (const std::string& message, const TokenLoc& loc,
Type type)
{
Debug::Level logLevel = Debug::Info; // Usually script warnings are not too important
if (type == ErrorMessage)
logLevel = Debug::Error;

std::stringstream text;

if (type==ErrorMessage)
mStream << "error ";
text << "Error: ";
else
mStream << "warning ";
text << "Warning: ";

if (!mContext.empty())
mStream << mContext << " ";
text << mContext << " ";

text << "line " << loc.mLine+1 << ", column " << loc.mColumn+1
<< " (" << loc.mLiteral << "): " << message;

mStream
<< "line " << loc.mLine+1 << ", column " << loc.mColumn+1
<< " (" << loc.mLiteral << ")" << std::endl
<< " " << message << std::endl;
Log(logLevel) << text.str();
}

// Report a file related error

void StreamErrorHandler::report (const std::string& message, Type type)
{
Debug::Level logLevel = Debug::Info;
if (type==ErrorMessage)
mStream << "error ";
logLevel = Debug::Error;

std::stringstream text;

if (type==ErrorMessage)
text << "Error: ";
else
mStream << "warning ";
text << "Warning: ";

if (!mContext.empty())
text << mContext << " ";

text << "file: " << message << std::endl;

mStream
<< "file:" << std::endl
<< " " << message << std::endl;
Log(logLevel) << text.str();
}

void StreamErrorHandler::setContext(const std::string &context)
{
mContext = context;
}

StreamErrorHandler::StreamErrorHandler (std::ostream& ErrorStream) : mStream (ErrorStream) {}
StreamErrorHandler::StreamErrorHandler() {}
}
6 changes: 2 additions & 4 deletions components/compiler/streamerrorhandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

namespace Compiler
{
/// \brief Error handler implementation: Write errors into stream
/// \brief Error handler implementation: Write errors into logging stream

class StreamErrorHandler : public ErrorHandler
{
std::ostream& mStream;

std::string mContext;

// not implemented
Expand All @@ -32,7 +30,7 @@ namespace Compiler

// constructors

StreamErrorHandler (std::ostream& ErrorStream);
StreamErrorHandler ();
///< constructor
};
}
Expand Down

0 comments on commit b43eb56

Please sign in to comment.