diff --git a/src/lua_gears.cc b/src/lua_gears.cc index beb0fdc..54de4ba 100644 --- a/src/lua_gears.cc +++ b/src/lua_gears.cc @@ -3,6 +3,9 @@ namespace rime { +std::ostream &operator<<(std::ostream &os, const LuaErr &e) { + return os << " error(" << e.status << "): " << e.e; +} //--- LuaTranslation bool LuaTranslation::Next() { if (exhausted()) { @@ -11,8 +14,8 @@ bool LuaTranslation::Next() { auto r = lua_->resume>(f_); if (!r.ok()) { LuaErr e = r.get_err(); - if (e.e != "") - LOG(ERROR) << "LuaTranslation::Next error(" << e.status << "): " << e.e; + LOG_IF(ERROR, e.e != "" ) << typeid(*this).name() <<"::" << __FUNCTION__ + << "[" << name_space_ << "]" << e; set_exhausted(true); return false; } else { @@ -70,7 +73,7 @@ static void raw_init(lua_State *L, const Ticket &t, *fini = LuaObj::todata(L, -1); } lua_pop(L, 1); - + if (tags_match) { lua_getfield(L, -1, "tags_match"); if (lua_type(L, -1) == LUA_TFUNCTION) { @@ -92,6 +95,14 @@ static void raw_init(lua_State *L, const Ticket &t, *func = LuaObj::todata(L, -1); lua_pop(L, 1); } +#define VOID_CALL(func, fname) \ +do { \ + if (func) { \ + auto r = lua_->void_call, an>(fini_, env_); \ + LOG_IF(ERROR, !r.ok()) << typeid(*this).name() <<"::" << __FUNCTION__ \ + << "[" << name_space_ << ": " #fname "]" << r.get_err(); \ + } \ +} while(0) //--- LuaFilter LuaFilter::LuaFilter(const Ticket& ticket, Lua* lua) @@ -99,22 +110,24 @@ LuaFilter::LuaFilter(const Ticket& ticket, Lua* lua) lua->to_state([&](lua_State *L) {raw_init(L, ticket, &env_, &func_, &fini_, &tags_match_);}); } +bool LuaFilter::AppliesToSegment(Segment* segment) { + if ( ! tags_match_ ) + return TagsMatch(segment); + + auto r = lua_->call, Segment *, an>(tags_match_, segment, env_); + LOG_IF(ERROR, !r.ok()) << typeid(*this).name() << "::" << __FUNCTION__ + << "[" << name_space_ << "]" << r.get_err(); + return (r.ok()) ? r.get() : false; +} + an LuaFilter::Apply( an translation, CandidateList* candidates) { auto f = lua_->newthread, an, an, CandidateList *>(func_, translation, env_, candidates); - return New(lua_, f); + return New(lua_, f, name_space_ + ":\"func\""); } -LuaFilter::~LuaFilter() { - if (fini_) { - auto r = lua_->void_call, an>(fini_, env_); - if (!r.ok()) { - auto e = r.get_err(); - LOG(ERROR) << "LuaFilter::~LuaFilter of "<< name_space_ << " error(" << e.status << "): " << e.e; - } - } -} +LuaFilter::~LuaFilter() { VOID_CALL(fini_, "fini"); } //--- LuaTranslator LuaTranslator::LuaTranslator(const Ticket& ticket, Lua* lua) @@ -126,22 +139,14 @@ an LuaTranslator::Query(const string& input, const Segment& segment) { auto f = lua_->newthread, const string &, const Segment &, an>(func_, input, segment, env_); - an t = New(lua_, f); + an t = New(lua_, f, name_space_ + ": \"func\""); if (t->exhausted()) return an(); else return t; } -LuaTranslator::~LuaTranslator() { - if (fini_) { - auto r = lua_->void_call, an>(fini_, env_); - if (!r.ok()) { - auto e = r.get_err(); - LOG(ERROR) << "LuaTranslator::~LuaTranslator of "<< name_space_ << " error(" << e.status << "): " << e.e; - } - } -} +LuaTranslator::~LuaTranslator() { VOID_CALL(fini_, "fini"); } //--- LuaSegmentor LuaSegmentor::LuaSegmentor(const Ticket& ticket, Lua *lua) @@ -152,23 +157,12 @@ LuaSegmentor::LuaSegmentor(const Ticket& ticket, Lua *lua) bool LuaSegmentor::Proceed(Segmentation* segmentation) { auto r = lua_->call, Segmentation &, an>(func_, *segmentation, env_); - if (!r.ok()) { - auto e = r.get_err(); - LOG(ERROR) << "LuaSegmentor::Proceed of "<< name_space_ << " error(" << e.status << "): " << e.e; - return true; - } else - return r.get(); + LOG_IF(ERROR, !r.ok()) << typeid(*this).name() << "::" << __FUNCTION__ + << "[" << name_space_ << "]" << r.get_err(); + return (r.ok()) ? r.get() : true ; } -LuaSegmentor::~LuaSegmentor() { - if (fini_) { - auto r = lua_->void_call, an>(fini_, env_); - if (!r.ok()) { - auto e = r.get_err(); - LOG(ERROR) << "LuaSegmentor::~LuaSegmentor of "<< name_space_ << " error(" << e.status << "): " << e.e; - } - } -} +LuaSegmentor::~LuaSegmentor() { VOID_CALL(fini_, "fini"); } //--- LuaProcessor LuaProcessor::LuaProcessor(const Ticket& ticket, Lua* lua) @@ -180,8 +174,8 @@ ProcessResult LuaProcessor::ProcessKeyEvent(const KeyEvent& key_event) { auto r = lua_->call, const KeyEvent&, an>(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_IF(ERROR, !r.ok()) << typeid(*this).name() << "::" << __FUNCTION__ + << "[" << name_space_ << "]" << r.get_err(); return kNoop; } else switch (r.get()) { @@ -191,14 +185,6 @@ ProcessResult LuaProcessor::ProcessKeyEvent(const KeyEvent& key_event) { } } -LuaProcessor::~LuaProcessor() { - if (fini_) { - auto r = lua_->void_call, an>(fini_, env_); - if (!r.ok()) { - auto e = r.get_err(); - LOG(ERROR) << "LuaProcessor::~LuaProcessor of "<< name_space_ << " error(" << e.status << "): " << e.e; - } - } -} +LuaProcessor::~LuaProcessor() { VOID_CALL(fini_, "fini"); } } // namespace rime diff --git a/src/lua_gears.h b/src/lua_gears.h index d763970..8774f19 100644 --- a/src/lua_gears.h +++ b/src/lua_gears.h @@ -11,10 +11,14 @@ namespace rime { +std::ostream& operator<<(std::ostream &os, const LuaErr &e); + class LuaTranslation : public Translation { public: LuaTranslation(Lua *lua, an f) - : lua_(lua), f_(f) { + : LuaTranslation(lua, f, "") {}; + LuaTranslation(Lua *lua, an f, string name_space) + : lua_(lua), f_(f), name_space_(name_space) { Next(); } @@ -23,11 +27,14 @@ class LuaTranslation : public Translation { an Peek() { return c_; } + string name_space() const { return name_space_; } + void set_name_space(const string& ns) { name_space_ = ns;} private: Lua *lua_; an c_; an f_; + string name_space_; }; class LuaFilter : public Filter, TagMatching { @@ -38,19 +45,7 @@ class LuaFilter : public Filter, TagMatching { virtual an Apply(an translation, CandidateList* candidates); - virtual bool AppliesToSegment(Segment* segment) { - if ( ! tags_match_ ) - return TagsMatch(segment); - - auto r = lua_->call, Segment *, an>(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_; diff --git a/src/types.cc b/src/types.cc index a8b73c4..3f32aa0 100644 --- a/src/types.cc +++ b/src/types.cc @@ -217,7 +217,7 @@ namespace TranslationReg { return 0; auto o = lua->newthreadx(L, n); - an r = New(lua, o); + an r = New(lua, o,"=lua="); LuaType>::pushdata(L, r); return 1; } @@ -236,6 +236,20 @@ namespace TranslationReg { lua_pushvalue(L, 1); return 2; } + bool set_name_space(T &t, const string &ns) { + if (auto tp = dynamic_cast(&t)) { + tp->set_name_space(ns); + return true; + } + return false; + } + + string name_space(T &t) { + if (auto tp = dynamic_cast(&t)) { + return tp->name_space(); + } + return ""; + } static const luaL_Reg funcs[] = { { "Translation", raw_make }, @@ -244,14 +258,17 @@ namespace TranslationReg { static const luaL_Reg methods[] = { { "iter", raw_iter }, + { "set_name_space", WRAP(set_name_space)}, { NULL, NULL }, }; static const luaL_Reg vars_get[] = { + { "name_space", WRAP(name_space)}, { NULL, NULL }, }; static const luaL_Reg vars_set[] = { + { "name_space", WRAP(set_name_space)}, { NULL, NULL }, }; } @@ -1212,10 +1229,7 @@ static int raw_connect(lua_State *L) { auto c = t.connect ([lua, o](I... i) { auto r = lua->void_call, Context *>(o, i...); - if (!r.ok()) { - auto e = r.get_err(); - LOG(ERROR) << "Context::Notifier error(" << e.status << "): " << e.e; - } + LOG_IF(ERROR, !r.ok()) << "Context::Notifier" << r.get_err(); }); LuaType::pushdata(L, c); @@ -1484,8 +1498,7 @@ namespace MemoryReg { auto r = lua_->call, 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();