diff --git a/src/main/cpp/inc/plugin.h b/src/main/cpp/inc/plugin.h index 9e5bca1..8925775 100644 --- a/src/main/cpp/inc/plugin.h +++ b/src/main/cpp/inc/plugin.h @@ -30,6 +30,7 @@ public: virtual std::map getEvents(); virtual ~CppPlugin(); void printStacktrace(); + void log(std::string msg); }; bool registerCmd(JNIEnv* env, CppPlugin* plugin, std::string& cname, cmdfptr function); diff --git a/src/main/cpp/src/event.cpp b/src/main/cpp/src/event.cpp index 6c667ac..fd99fa3 100644 --- a/src/main/cpp/src/event.cpp +++ b/src/main/cpp/src/event.cpp @@ -59,7 +59,7 @@ void Event::reapply(JNIEnv* env, jobject jevent) { env->CallObjectMethod(map, MAPCLEAR); for(auto it : data) { - std::cout << "write back to java: " << it.first << " Value: " << it.second << std::endl; + DEB(CYELLOW << "write back to java: " << it.first << " Value: " << CGREEN<< it.second); jstring key = env->NewStringUTF(it.first.c_str()); env->CallObjectMethod(map, MAPPUT, key, it.second); diff --git a/src/main/cpp/src/plugin.cpp b/src/main/cpp/src/plugin.cpp index a5f83ac..d70b9d1 100644 --- a/src/main/cpp/src/plugin.cpp +++ b/src/main/cpp/src/plugin.cpp @@ -2,6 +2,8 @@ #include #include //backtrace +#include //time logging +#include //snprintf for time logging #define STACKTRACEBUFFER 100 @@ -51,4 +53,18 @@ void CppPlugin::printStacktrace() { } } +void CppPlugin::log(std::string msg) { + //create time stamp + time_t rawt; + struct tm *timeinfo; + time(&rawt); //get time + timeinfo = localtime(&rawt); + size_t length = name.length() + 22; + char *timebuf = new char[length]; + snprintf(timebuf, length, "\r[%02d:%02d:%02d INFO]: [%s] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, name.c_str()); + std::cout << timebuf << msg << std::endl; + + free(timebuf); +} + } //namespace diff --git a/testplugins/01verysimpleplugin/src/plugin.cpp b/testplugins/01verysimpleplugin/src/plugin.cpp index 618dce7..0f7b574 100644 --- a/testplugins/01verysimpleplugin/src/plugin.cpp +++ b/testplugins/01verysimpleplugin/src/plugin.cpp @@ -17,15 +17,15 @@ public: }; void SimplePlugin::onLoad(JNIEnv*) { - std::cout << "SimplePLugin::onLoad();" << std::endl; + log("SimplePLugin::onLoad();"); } void SimplePlugin::onEnable(JNIEnv*) { - std::cout << "SimplePLugin::onEnable();" << std::endl; + log("SimplePLugin::onEnable();"); } void SimplePlugin::onDisable(JNIEnv*) { - std::cout << "SimplePLugin::onDisable();" << std::endl; + log("SimplePLugin::onDisable();"); } CppPlugin* init() { diff --git a/testplugins/02simpleEvents/src/plugin.cpp b/testplugins/02simpleEvents/src/plugin.cpp index acbe3c9..18803ee 100644 --- a/testplugins/02simpleEvents/src/plugin.cpp +++ b/testplugins/02simpleEvents/src/plugin.cpp @@ -17,15 +17,15 @@ public: }; void EventsPlugin::onLoad(JNIEnv*) { - std::cout << "Events::onLoad();" << std::endl; + log("Events::onLoad();"); } void EventsPlugin::onEnable(JNIEnv*) { - std::cout << "Events::onEnable();" << std::endl; + log("Events::onEnable();"); } void EventsPlugin::onDisable(JNIEnv*) { - std::cout << "Events::onDisable();" << std::endl; + log("Events::onDisable();"); } CppPlugin* init() { @@ -35,17 +35,17 @@ CppPlugin* init() { void eventJoin(JNIEnv* env, CppPlugin* pl, Event* e) { jstring msg = (jstring) e->getData(env, "joinMessage"); std::string jmsg(env->GetStringUTFChars(msg, 0)); - std::cout << "join " << e->getName() << " old join message: " << jmsg << std::endl; + pl->log("join " + e->getName() + " old join message: " + jmsg); jstring newmsg = env->NewStringUTF("Ein Spieler ist dem Spiel beigetreten!"); e->setData("joinMessage", newmsg); } void eventQuit(JNIEnv* env, CppPlugin* pl, Event* e) { - std::cout << "quit " << e->getName() << std::endl; + pl->log("quit " + e->getName()); } std::map EventsPlugin::getEvents() { - std::cout << "EventsPlugin::getEvents" << std::endl; + log("EventsPlugin::getEvents"); std::map out; out.insert({"PlayerJoinEvent", eventJoin}); out.insert({"PlayerQuitEvent", eventQuit}); diff --git a/testplugins/03simpleCmd/src/plugin.cpp b/testplugins/03simpleCmd/src/plugin.cpp index 98ee7ac..1408b4c 100644 --- a/testplugins/03simpleCmd/src/plugin.cpp +++ b/testplugins/03simpleCmd/src/plugin.cpp @@ -10,7 +10,7 @@ using namespace Plugin; extern "C" { bool hicmd(JNIEnv* env, CppPlugin* plugin, jobject sender, jobject cmd, std::string& label, std::vector& args) { - std::cout << "HICMD::hi " << label << " argsize: " << args.size() << std::endl; + plugin->log("HICMD::hi " + label + " argsize: " + std::to_string(args.size())); for(std::string& arg : args) { std::cout << arg << std::endl; } @@ -28,16 +28,16 @@ public: }; void CmdPlugin::onLoad(JNIEnv*) { - std::cout << "CmdPlugin::onLoad();" << std::endl; + log("CmdPlugin::onLoad();"); } void CmdPlugin::onEnable(JNIEnv* env) { - std::cout << "CmdPlugin::onEnable();" << std::endl; + log("CmdPlugin::onEnable();"); registerCmd(env, this, "hi", hicmd); } void CmdPlugin::onDisable(JNIEnv*) { - std::cout << "CmdPlugin::onDisable();" << std::endl; + log("CmdPlugin::onDisable();"); } CppPlugin* init() { diff --git a/testplugins/04crashtest/Makefile b/testplugins/04crashtest/Makefile new file mode 100644 index 0000000..3ba1cf1 --- /dev/null +++ b/testplugins/04crashtest/Makefile @@ -0,0 +1,11 @@ + +CFLAGS = -Wall -pedantic-errors -std=c++17 -g +SRCDIR = src/ +INCDIR = ../../src/main/cpp/inc +LDFLAGS = -I/usr/lib/jvm/java-8-openjdk-amd64/include/ -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux/ -I$(INCDIR) +NAME = crashtest.so + +all: + g++ -fPIC -shared $(CFLAGS) $(SRCDIR)*.cpp $(LDFLAGS) -o $(NAME) -lplugin + + #/home/yannis/git/Cppplugin/testserver/plugins/CppPlugins/libplugin.so diff --git a/testplugins/04crashtest/src/plugin.cpp b/testplugins/04crashtest/src/plugin.cpp new file mode 100644 index 0000000..e543603 --- /dev/null +++ b/testplugins/04crashtest/src/plugin.cpp @@ -0,0 +1,41 @@ +#include "plugin.h" + +#include +#include +#include +#include + +using namespace Plugin; + +extern "C" { + +void sub(CppPlugin* plugin) { + plugin->printStacktrace(); + plugin->log("asdf"); + throw std::string("Nope"); +} + +bool throwcmd(JNIEnv* env, CppPlugin* plugin, jobject sender, jobject cmd, std::string& label, std::vector& args) { + sub(plugin); + return true; +} + +class CmdPlugin : public CppPlugin { +public: + //virtual void onLoad(JNIEnv*); + virtual void onEnable(JNIEnv*); + //virtual void onDisable(JNIEnv*); + //virtual std::map getEvents(); +}; + +void CmdPlugin::onEnable(JNIEnv* env) { + log("CmdPlugin::onEnable();"); + registerCmd(env, this, "throw", throwcmd); +} + + +CppPlugin* init() { + return new CmdPlugin(); +} + +}//extern "C"