summaryrefslogtreecommitdiff
path: root/i/marvin/src
diff options
context:
space:
mode:
Diffstat (limited to 'i/marvin/src')
-rw-r--r--i/marvin/src/log/Makefile.defs5
-rw-r--r--i/marvin/src/log/data_circular_buffer_factory.cc61
-rw-r--r--i/marvin/src/log/data_circular_buffer_factory.hh53
-rw-r--r--i/marvin/src/log/logger.cc3
-rw-r--r--i/marvin/src/log/logger_ram.cc98
-rw-r--r--i/marvin/src/log/logger_ram.hh63
-rw-r--r--i/marvin/src/log/test_log.cc2
7 files changed, 283 insertions, 2 deletions
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 <stdexcept>
@@ -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 <stdexcept>
+#include <sstream> // 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<const uint8_t *> (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<const uint8_t *> (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<const uint8_t *> (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<const uint8_t *> (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<const uint8_t *> (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 <string>
+
+/// 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 <fstream>
int
main (int argc, char **argv)