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 bool generate_replay_config = false;
00066 try {
00067 generate_replay_config = config->get_bool((prefix + "generate_replay_config").c_str());
00068 } catch (Exception &e) {}
00069
00070 std::string scenario_prefix = prefix + scenario + "/";
00071 std::string ifaces_prefix = scenario_prefix + "interfaces/";
00072
00073 std::string logdir = LOGDIR;
00074 bool buffering = true;
00075 bool flushing = false;
00076 try {
00077 logdir = config->get_string((scenario_prefix + "logdir").c_str());
00078 } catch (Exception &e) { }
00079 try {
00080 buffering = config->get_bool((scenario_prefix + "buffering").c_str());
00081 } catch (Exception &e) { }
00082 try {
00083 flushing = config->get_bool((scenario_prefix + "flushing").c_str());
00084 } catch (Exception &e) { }
00085
00086 struct stat s;
00087 int err = stat(logdir.c_str(), &s);
00088 if (err != 0) {
00089 char buf[1024];
00090 Exception se ("Cannot access logdir %s (%s)",
00091 logdir.c_str(), strerror_r(errno, buf, 1024));
00092 if (mkdir(logdir.c_str(), 0755) != 0) {
00093 se.append("Failed to create log directory (%s)",
00094 strerror_r(errno, buf, 1024));
00095 throw se;
00096 }
00097 } else if ( ! S_ISDIR(s.st_mode) ) {
00098 throw Exception("Logdir path %s is not a directory", logdir.c_str());
00099 }
00100
00101
00102
00103 Time start;
00104
00105 char date[21];
00106 Time now;
00107 struct tm *tmp = localtime(&(now.get_timeval()->tv_sec));
00108 strftime(date, 21, "%F-%H-%M-%S", tmp);
00109 std::string replay_cfg_prefix = replay_prefix + scenario + "-" + date + "/logs/";
00110
00111 Configuration::ValueIterator *i = config->search(ifaces_prefix.c_str());
00112 while (i->next()) {
00113 std::string iface_name = std::string(i->path()).substr(ifaces_prefix.length());
00114 iface_name = iface_name.substr(0, iface_name.find("/"));
00115
00116
00117 BBLoggerThread *log_thread = new BBLoggerThread(i->get_string().c_str(),
00118 logdir.c_str(),
00119 buffering, flushing,
00120 scenario.c_str(), &start);
00121
00122 std::string filename = log_thread->get_filename();
00123 config->set_string((replay_cfg_prefix + iface_name + "/file").c_str(), filename);
00124
00125 thread_list.push_back(log_thread);
00126 }
00127 delete i;
00128
00129 if ( thread_list.empty() ) {
00130 throw Exception("No interfaces configured for logging, aborting");
00131 }
00132
00133 BBLoggerThread *bblt = dynamic_cast<BBLoggerThread *>(thread_list.front());
00134 bblt->set_threadlist(thread_list);
00135 }
00136
00137 PLUGIN_DESCRIPTION("Write BlackBoard interface data to files")
00138 EXPORT_PLUGIN(BlackBoardLoggerPlugin)