00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "bblogger_plugin.h"
00024 #include "log_thread.h"
00025
00026 #include <utils/time/time.h>
00027
00028 #include <set>
00029
00030 #include <cstring>
00031 #include <cerrno>
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034 #include <unistd.h>
00035
00036 using namespace fawkes;
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 BlackBoardLoggerPlugin::BlackBoardLoggerPlugin(Configuration *config)
00050 : Plugin(config)
00051 {
00052 std::set<std::string> ifaces;
00053
00054 std::string prefix = "/fawkes/bblogger/";
00055 std::string replay_prefix = "/fawkes/bblogreplay/";
00056
00057 std::string scenario = "";
00058 try {
00059 scenario = config->get_string((prefix + "scenario").c_str());
00060 } catch (Exception &e) {
00061 e.append("No scenario defined, configure %sscenario", prefix.c_str());
00062 throw;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072 std::string scenario_prefix = prefix + scenario + "/";
00073 std::string ifaces_prefix = scenario_prefix + "interfaces/";
00074
00075 std::string logdir = LOGDIR;
00076 bool buffering = true;
00077 bool flushing = false;
00078 try {
00079 logdir = config->get_string((scenario_prefix + "logdir").c_str());
00080 } catch (Exception &e) { }
00081 try {
00082 buffering = config->get_bool((scenario_prefix + "buffering").c_str());
00083 } catch (Exception &e) { }
00084 try {
00085 flushing = config->get_bool((scenario_prefix + "flushing").c_str());
00086 } catch (Exception &e) { }
00087
00088 struct stat s;
00089 int err = stat(logdir.c_str(), &s);
00090 if (err != 0) {
00091 char buf[1024];
00092 Exception se ("Cannot access logdir %s (%s)",
00093 logdir.c_str(), strerror_r(errno, buf, 1024));
00094 if (mkdir(logdir.c_str(), 0755) != 0) {
00095 se.append("Failed to create log directory (%s)",
00096 strerror_r(errno, buf, 1024));
00097 throw se;
00098 }
00099 } else if ( ! S_ISDIR(s.st_mode) ) {
00100 throw Exception("Logdir path %s is not a directory", logdir.c_str());
00101 }
00102
00103
00104
00105 Time start;
00106
00107 char date[21];
00108 Time now;
00109 struct tm *tmp = localtime(&(now.get_timeval()->tv_sec));
00110 strftime(date, 21, "%F-%H-%M-%S", tmp);
00111 std::string replay_cfg_prefix = replay_prefix + scenario + "-" + date + "/logs/";
00112
00113 Configuration::ValueIterator *i = config->search(ifaces_prefix.c_str());
00114 while (i->next()) {
00115 std::string iface_name = std::string(i->path()).substr(ifaces_prefix.length());
00116 iface_name = iface_name.substr(0, iface_name.find("/"));
00117
00118
00119 BBLoggerThread *log_thread = new BBLoggerThread(i->get_string().c_str(),
00120 logdir.c_str(),
00121 buffering, flushing,
00122 scenario.c_str(), &start);
00123
00124 std::string filename = log_thread->get_filename();
00125 config->set_string((replay_cfg_prefix + iface_name + "/file").c_str(), filename);
00126
00127 thread_list.push_back(log_thread);
00128 }
00129 delete i;
00130
00131 if ( thread_list.empty() ) {
00132 throw Exception("No interfaces configured for logging, aborting");
00133 }
00134
00135 BBLoggerThread *bblt = dynamic_cast<BBLoggerThread *>(thread_list.front());
00136 bblt->set_threadlist(thread_list);
00137 }
00138
00139 PLUGIN_DESCRIPTION("Write BlackBoard interface data to files")
00140 EXPORT_PLUGIN(BlackBoardLoggerPlugin)