CppPlugins/src/main/cpp/src/plugin.cpp

71 lines
1.5 KiB
C++

#include <plugin.h>
#include <iostream>
#include <execinfo.h> //backtrace
#include <time.h> //time logging
#include <stdio.h> //snprintf for time logging
#define STACKTRACEBUFFER 100
namespace Plugin {
void CppPlugin::onLoad(JNIEnv*) {
}
void CppPlugin::onEnable(JNIEnv*) {
}
void CppPlugin::onDisable(JNIEnv*) {
}
std::map<std::string, eventfptr> CppPlugin::getEvents() {
//std::cout << "Defaults::getEvents()" << std::endl;
return std::map<std::string, eventfptr>();
}
CppPlugin::~CppPlugin() {
}
void CppPlugin::printStacktrace() {
//create stacktrace
void *buffer[STACKTRACEBUFFER];
int count = backtrace(buffer, STACKTRACEBUFFER)-1; //dont print current frame
std::cout << "Plugin: " << name << " Creating Stacktrace: ";
std::cout << count << " Frames traced\n";
char** strings = nullptr;
strings = backtrace_symbols(buffer+1, count);
if(strings == nullptr) {
std::cout << "Error, could not read symbols - compiled with \"-g\"?" << std::endl;
} else {
for(int i = 0; i < count; i++) {
std::cout << strings[i] << "\n";
}
free(strings);
std::cout << std::endl;
}
}
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