Skip to content

Commit

Permalink
add ostream operator<<() for LuaErr
Browse files Browse the repository at this point in the history
Signed-off-by: shewer <[email protected]>
  • Loading branch information
shewer committed May 7, 2024
1 parent 43229d7 commit dede424
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 40 deletions.
7 changes: 7 additions & 0 deletions src/lib/lua.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "lua.h"
#include <iostream>
#include "lua_templates.h"

namespace LuaImpl {
Expand Down Expand Up @@ -215,6 +216,12 @@ namespace LuaImpl {
}
}

// ostream << LuaErr
std::ostream& operator<<(std::ostream& os, const LuaErr& err) {
os << " Error: (" << err.status << ") : " << err.e << ".";
return os;
}

Lua::Lua() {
L_ = luaL_newstate();
if (L_) {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class LuaObj {
};

struct LuaErr { int status; std::string e; };

std::ostream& operator<<(std::ostream& os, const LuaErr& err);

template <typename T>
using LuaResult = Result<T, LuaErr>;

Expand Down
43 changes: 24 additions & 19 deletions src/lua_gears.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bool LuaTranslation::Next() {
if (!r.ok()) {
LuaErr e = r.get_err();
if (e.e != "")
LOG(ERROR) << "LuaTranslation::Next error(" << e.status << "): " << e.e;
LOG(ERROR) << "LuaTranslation::Next" << e;
set_exhausted(true);
return false;
} else {
Expand Down Expand Up @@ -79,12 +79,11 @@ static void raw_init(lua_State *L, const Ticket &t,
lua_pushstring(L, _vec_klass.at(0).c_str());
int status = lua_pcall(L, 1, 1, 0);
if (status != LUA_OK) {
const char *e = lua_tostring(L, -1);
const string e = lua_tostring(L, -1);
LOG(ERROR) << "Lua Compoment of autoload error:("
<< " module: "<< t.klass
<< " name_space: " << t.name_space
<< " status: " << status
<< " ): " << e;
<< LuaErr({status, e});
}
} else {
lua_getglobal(L, _vec_klass.at(0).c_str());
Expand All @@ -100,12 +99,11 @@ static void raw_init(lua_State *L, const Ticket &t,
LuaObj::pushdata(L, *env);
int status = lua_pcall(L, 1, 1, 0);
if (status != LUA_OK) {
const char *e = lua_tostring(L, -1);
const string e = lua_tostring(L, -1);
LOG(ERROR) << "Lua Compoment of initialize error:("
<< " module: "<< t.klass
<< " name_space: " << t.name_space
<< " status: " << status
<< " ): " << e;
<< LuaErr({status, e});
}
}
lua_pop(L, 1);
Expand Down Expand Up @@ -151,12 +149,24 @@ an<Translation> LuaFilter::Apply(
return New<LuaTranslation>(lua_, f);
}

bool LuaFilter::AppliesToSegment(Segment* segment) {
if ( ! tags_match_ )
return TagsMatch(segment);

auto r = lua_->call<bool, an<LuaObj>, Segment *, an<LuaObj>>(tags_match_, segment, env_);
if (!r.ok()) {
LOG(ERROR) << "LuaFilter::AppliesToSegment of " << name_space_ << r.get_err();
return false;
}
else
return r.get();
}

LuaFilter::~LuaFilter() {
if (fini_) {
auto r = lua_->void_call<an<LuaObj>, an<LuaObj>>(fini_, env_);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LuaFilter::~LuaFilter of "<< name_space_ << " error(" << e.status << "): " << e.e;
LOG(ERROR) << "LuaFilter::~LuaFilter of "<< name_space_ << r.get_err();
}
}
}
Expand All @@ -182,8 +192,7 @@ LuaTranslator::~LuaTranslator() {
if (fini_) {
auto r = lua_->void_call<an<LuaObj>, an<LuaObj>>(fini_, env_);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LuaTranslator::~LuaTranslator of "<< name_space_ << " error(" << e.status << "): " << e.e;
LOG(ERROR) << "LuaTranslator::~LuaTranslator of "<< name_space_ << r.get_err();
}
}
}
Expand All @@ -198,8 +207,7 @@ bool LuaSegmentor::Proceed(Segmentation* segmentation) {
auto r = lua_->call<bool, an<LuaObj>, Segmentation &,
an<LuaObj>>(func_, *segmentation, env_);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LuaSegmentor::Proceed of "<< name_space_ << " error(" << e.status << "): " << e.e;
LOG(ERROR) << "LuaSegmentor::Proceed of "<< name_space_ << r.get_err();
return true;
} else
return r.get();
Expand All @@ -209,8 +217,7 @@ LuaSegmentor::~LuaSegmentor() {
if (fini_) {
auto r = lua_->void_call<an<LuaObj>, an<LuaObj>>(fini_, env_);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LuaSegmentor::~LuaSegmentor of "<< name_space_ << " error(" << e.status << "): " << e.e;
LOG(ERROR) << "LuaSegmentor::~LuaSegmentor of "<< name_space_ << r.get_err();
}
}
}
Expand All @@ -225,8 +232,7 @@ ProcessResult LuaProcessor::ProcessKeyEvent(const KeyEvent& key_event) {
auto r = lua_->call<int, an<LuaObj>, const KeyEvent&,
an<LuaObj>>(func_, key_event, env_);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LuaProcessor::ProcessKeyEvent of "<< name_space_ << " error(" << e.status << "): " << e.e;
LOG(ERROR) << "LuaProcessor::ProcessKeyEvent of "<< name_space_ << r.get_err();
return kNoop;
} else
switch (r.get()) {
Expand All @@ -240,8 +246,7 @@ LuaProcessor::~LuaProcessor() {
if (fini_) {
auto r = lua_->void_call<an<LuaObj>, an<LuaObj>>(fini_, env_);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LuaProcessor::~LuaProcessor of "<< name_space_ << " error(" << e.status << "): " << e.e;
LOG(ERROR) << "LuaProcessor::~LuaProcessor of "<< name_space_ << r.get_err();
}
}
}
Expand Down
14 changes: 1 addition & 13 deletions src/lua_gears.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,7 @@ class LuaFilter : public Filter, TagMatching {
virtual an<Translation> Apply(an<Translation> translation,
CandidateList* candidates);

virtual bool AppliesToSegment(Segment* segment) {
if ( ! tags_match_ )
return TagsMatch(segment);

auto r = lua_->call<bool, an<LuaObj>, Segment *, an<LuaObj>>(tags_match_, segment, env_);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LuaFilter::AppliesToSegment of " << name_space_ << " error(" << e.status << "): " << e.e;
return false;
}
else
return r.get();
}
virtual bool AppliesToSegment(Segment* segment);

private:
Lua *lua_;
Expand Down
3 changes: 1 addition & 2 deletions src/script_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ namespace ScriptTranslatorReg {
auto r = lua_->call<bool, an<LuaObj>, LScriptTranslator*, const CommitEntry&>(
memorize_callback_, this, commit_entry);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LScriptTranslator of " << name_space_
<< ": memorize_callback error(" << e.status << "): " << e.e;
<< ": memorize_callback" << r.get_err();
return false;
}
return r.get();
Expand Down
3 changes: 1 addition & 2 deletions src/table_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ namespace TableTranslatorReg {
auto r = lua_->call<bool, an<LuaObj>, LTableTranslator*, const CommitEntry&>(
memorize_callback_, this, commit_entry);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LTableTranslator of " << name_space_
<< ": memorize_callback error(" << e.status << "): " << e.e;
<< ": memorize_callback" << r.get_err();
return false;
}
return r.get();
Expand Down
6 changes: 2 additions & 4 deletions src/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1336,8 +1336,7 @@ static int raw_connect(lua_State *L) {
auto f = [lua, o](I... i) {
auto r = lua->void_call<an<LuaObj>, Context *>(o, i...);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "Context::Notifier error(" << e.status << "): " << e.e;
LOG(ERROR) << "Context::Notifier" << r.get_err();
}
};

Expand Down Expand Up @@ -1824,8 +1823,7 @@ namespace MemoryReg {

auto r = lua_->call<bool, an<LuaObj>, const CommitEntry &>(memorize_callback, commit_entry);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LuaMemory::Memorize error(" << e.status << "): " << e.e;
LOG(ERROR) << "LuaMemory::Memorize" << r.get_err();
return false;
} else
return r.get();
Expand Down

0 comments on commit dede424

Please sign in to comment.