bblogger_plugin.cpp

00001 
00002 /***************************************************************************
00003  *  bblogger_plugin.cpp - Fawkes BlackBoard Logger Plugin
00004  *
00005  *  Created: Sat Nov 07 23:21:36 2009
00006  *  Copyright  2009  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
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 /** @class BlackBoardLoggerPlugin "bblogger_plugin.h"
00039  * BlackBoard logger plugin.
00040  * This plugin logs one or more (or even all) interfaces to data files
00041  * for later replay or analyzing.
00042  *
00043  * @author Tim Niemueller
00044  */
00045 
00046 /** Constructor.
00047  * @param config Fawkes configuration
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) {} // ignored, use default set above
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) { /* ignored, use default set above */ }
00079   try {
00080     buffering = config->get_bool((scenario_prefix + "buffering").c_str());
00081   } catch (Exception &e) { /* ignored, use default set above */ }
00082   try {
00083     flushing = config->get_bool((scenario_prefix + "flushing").c_str());
00084   } catch (Exception &e) { /* ignored, use default set above */ }
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   // We do not have the framework clock available at this point, but for the start
00102   // time of the log we are only interested in the system time anyway
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     //printf("Adding sync thread for peer %s\n", peer.c_str());
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)