From e5996fa7074b11bce48b125756051d09de438533 Mon Sep 17 00:00:00 2001 From: dufourj Date: Sun, 16 Apr 2006 17:38:16 +0000 Subject: Log : - rajout du logger en ram avec factory ; TODO : - rajouter le serveur pour récupérer les données à la fin du programme ; - voir pour un paramêtre pour la taille du buffer depuis le fichier de config. --- i/marvin/runtime/rc/config | 4 +- i/marvin/src/log/Makefile.defs | 5 +- i/marvin/src/log/data_circular_buffer_factory.cc | 61 +++++++++++++++ i/marvin/src/log/data_circular_buffer_factory.hh | 53 +++++++++++++ i/marvin/src/log/logger.cc | 3 + i/marvin/src/log/logger_ram.cc | 98 ++++++++++++++++++++++++ i/marvin/src/log/logger_ram.hh | 63 +++++++++++++++ i/marvin/src/log/test_log.cc | 2 +- 8 files changed, 286 insertions(+), 3 deletions(-) create mode 100644 i/marvin/src/log/data_circular_buffer_factory.cc create mode 100644 i/marvin/src/log/data_circular_buffer_factory.hh create mode 100644 i/marvin/src/log/logger_ram.cc create mode 100644 i/marvin/src/log/logger_ram.hh (limited to 'i/marvin') diff --git a/i/marvin/runtime/rc/config b/i/marvin/runtime/rc/config index 3292279..de0bf14 100644 --- a/i/marvin/runtime/rc/config +++ b/i/marvin/runtime/rc/config @@ -37,7 +37,9 @@ motor.pStatMotor = 0 # Possible values are : # - null : no log, optimized for time ; # - stdout : to stdout ; -# - file:/path/filename : to a file. +# - file:/path/filename : to a file ; +# - ram : in a circular buffer for getting it at the end of the program by +# network. # log.logger.default = "stdout" # log.logger.asserv = "null" diff --git a/i/marvin/src/log/Makefile.defs b/i/marvin/src/log/Makefile.defs index 43caab5..ad0b76b 100644 --- a/i/marvin/src/log/Makefile.defs +++ b/i/marvin/src/log/Makefile.defs @@ -1,5 +1,8 @@ PROGRAMS += test_log -log_OBJECTS = log.o log_message.o logger.o logger_stdout.o logger_file.o $(config_OBJECTS) +log_OBJECTS = log.o log_message.o logger.o \ + logger_stdout.o logger_file.o logger_ram.o \ + data_circular_buffer_factory.o \ + $(config_OBJECTS) $(data_OBJECTS) test_log_OBJECTS = test_log.o $(log_OBJECTS) diff --git a/i/marvin/src/log/data_circular_buffer_factory.cc b/i/marvin/src/log/data_circular_buffer_factory.cc new file mode 100644 index 0000000..231f57b --- /dev/null +++ b/i/marvin/src/log/data_circular_buffer_factory.cc @@ -0,0 +1,61 @@ +// data_circular_buffer_factory.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "data_circular_buffer_factory.hh" + +/// Initialisation of static member. +DataCircularBuffer *DataCircularBufferFactory::dbBuffer_ = 0; +unsigned DataCircularBufferFactory::refCount_ = 0; + +/// Default empty constructor. +DataCircularBufferFactory::DataCircularBufferFactory (void) +{ + refCount_++; +} +/// Destructor. +DataCircularBufferFactory::~DataCircularBufferFactory (void) +{ + refCount_--; + // End of use ? + if (!refCount_ && dbBuffer_) + { + // XXX Create a server in a try/catch block for removing exception. +// uint8_t c; +// while (dbBuffer_-> read (&c, 1)) +// std::cout << c; + delete dbBuffer_; + dbBuffer_ = 0; + } +} + +/// Get a DataCircularBuffer. +DataCircularBuffer & +DataCircularBufferFactory::getDataCircularBuffer (void) +{ + if (!dbBuffer_) + // 100Ko + // TODO: config ? + dbBuffer_ = new DataCircularBuffer (100000); + return *dbBuffer_; +} diff --git a/i/marvin/src/log/data_circular_buffer_factory.hh b/i/marvin/src/log/data_circular_buffer_factory.hh new file mode 100644 index 0000000..8f0874f --- /dev/null +++ b/i/marvin/src/log/data_circular_buffer_factory.hh @@ -0,0 +1,53 @@ +#ifndef data_circular_buffer_factory_hh +#define data_circular_buffer_factory_hh +// data_circular_buffer_factory.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} + +#include "data/data_circular_buffer.hh" + +/// The purpose of this class is to provide a factory of data_circular_buffer. +/// In fact, we will create only one data_circula_buffer and we provide a +/// reference to this one to logger_ram. When the data_circular_buffer will be +/// deleted, we will launch a socket server for fetching data remotely with a +/// netcat. + +class DataCircularBufferFactory +{ + friend class LoggerRam; + friend class Tester; + private: + /// The only DataCircularBuffer. + static DataCircularBuffer *dbBuffer_; + /// Reference counter. + static unsigned refCount_; + /// Default empty constructor. + DataCircularBufferFactory (void); + /// Destructor. + ~DataCircularBufferFactory (void); + /// Get a DataCircularBuffer. + DataCircularBuffer & getDataCircularBuffer (void); +}; + +#endif // data_circular_buffer_factory_hh diff --git a/i/marvin/src/log/logger.cc b/i/marvin/src/log/logger.cc index c6fdb6e..128aa16 100644 --- a/i/marvin/src/log/logger.cc +++ b/i/marvin/src/log/logger.cc @@ -26,6 +26,7 @@ #include "logger_null.hh" #include "logger_stdout.hh" #include "logger_file.hh" +#include "logger_ram.hh" #include @@ -43,6 +44,8 @@ Logger::create (const std::string &loggerName) else return new LoggerFile (loggerName.substr (5)); else + if (loggerName.substr (0, 3) == "ram") + return new LoggerRam; throw std::invalid_argument ("Unknown logger : " + loggerName); return 0; } diff --git a/i/marvin/src/log/logger_ram.cc b/i/marvin/src/log/logger_ram.cc new file mode 100644 index 0000000..720d0e8 --- /dev/null +++ b/i/marvin/src/log/logger_ram.cc @@ -0,0 +1,98 @@ +// logger_ram.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "logger_ram.hh" + +#include +#include // Convertion from num to string + +/// Default constructor. +LoggerRam::LoggerRam (void) + : buffer_ (bufferFactory_.getDataCircularBuffer ()) +{ +} + +/// Called at the begining of a message. +void +LoggerRam::start (const Log &log, const char *msg, Log::Level level) +{ + strTmp_ = log.getModule (); + strTmp_.append (":"); + + const char *instance = log.getInstance (); + if (instance) + { + strTmp_.append (" "); + strTmp_.append (instance); + strTmp_.append (":"); + } + strTmp_.append (" ("); + strTmp_.append (msg); + strTmp_.append (")"); + + buffer_.write (reinterpret_cast (strTmp_.data ()), strTmp_.size ()); +} + +/// Output a string or a variable name. +LoggerRam & +LoggerRam::operator<< (const char *s) +{ + strTmp_ = " "; + strTmp_.append (s); + buffer_.write (reinterpret_cast (strTmp_.data ()), strTmp_.size ()); + return *this; +} + +/// Output a string or a variable name. +LoggerRam & +LoggerRam::operator<< (const std::string &s) +{ + strTmp_ = " " + s; + buffer_.write (reinterpret_cast (strTmp_.data ()), strTmp_.size ()); + return *this; +} + +/// Output a integer. +LoggerRam & +LoggerRam::operator<< (int i) +{ + strTmp_ = " "; + std::ostringstream oStr; + oStr << i; + strTmp_.append (oStr.str ()); + buffer_.write (reinterpret_cast (strTmp_.data ()), strTmp_.size ()); + return *this; +} + +/// Output a double. +LoggerRam & +LoggerRam::operator<< (double d) +{ + strTmp_ = " "; + std::ostringstream oStr; + oStr << d; + strTmp_.append (oStr.str ()); + buffer_.write (reinterpret_cast (strTmp_.data ()), strTmp_.size ()); + return *this; +} diff --git a/i/marvin/src/log/logger_ram.hh b/i/marvin/src/log/logger_ram.hh new file mode 100644 index 0000000..03838c2 --- /dev/null +++ b/i/marvin/src/log/logger_ram.hh @@ -0,0 +1,63 @@ +#ifndef logger_ram_hh +#define logger_ram_hh +// logger_ram.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} + +#include "logger.hh" +#include "data_circular_buffer_factory.hh" +#include "data/data_circular_buffer.hh" + +#include + +/// Logger into ram (with a data circular buffer). +class LoggerRam : public Logger +{ + /// Factory for creating DataCircularBuffer. + DataCircularBufferFactory bufferFactory_; + /// Reference to the current DataCircularBuffer used. + DataCircularBuffer &buffer_; + /// Temporary string. + std::string strTmp_; + public: + /// Default constructor. + LoggerRam (void); + /// Default destructor. + ~LoggerRam (void) { } + /// Called at the begining of a message. + void start (const Log &log, const char *msg, Log::Level level); + /// Called at the end of a message. + void stop (void) { uint8_t cr = '\n'; buffer_.write (&cr, 1); } + /// Output a string or a variable name. + LoggerRam &operator<< (const char *s); + /// Output a string or a variable name. + LoggerRam &operator<< (const std::string &s); + /// Output a integer. + LoggerRam &operator<< (int i); + /// Output a double. + LoggerRam &operator << (double d); + private: +}; + +#endif // logger_ram_hh diff --git a/i/marvin/src/log/test_log.cc b/i/marvin/src/log/test_log.cc index 77146ef..64989b5 100644 --- a/i/marvin/src/log/test_log.cc +++ b/i/marvin/src/log/test_log.cc @@ -23,9 +23,9 @@ // // }}} #include "log.hh" +#include "data_circular_buffer_factory.hh" #include "config/config.hh" -#include int main (int argc, char **argv) -- cgit v1.2.3