From 8f486613be58ced269db1d437e560c16558604e8 Mon Sep 17 00:00:00 2001 From: becquet Date: Thu, 10 May 2007 18:49:20 +0000 Subject: Création de chuck, le programme du robot 2007. --- i/chuck/src/log/Makefile.defs | 12 ++ i/chuck/src/log/data_circular_buffer_factory.cc | 72 ++++++++++++ i/chuck/src/log/data_circular_buffer_factory.hh | 53 +++++++++ i/chuck/src/log/log.cc | 130 +++++++++++++++++++++ i/chuck/src/log/log.hh | 82 ++++++++++++++ i/chuck/src/log/log_message.cc | 74 ++++++++++++ i/chuck/src/log/log_message.hh | 55 +++++++++ i/chuck/src/log/log_message.tcc | 36 ++++++ i/chuck/src/log/log_server.cc | 54 +++++++++ i/chuck/src/log/log_server.hh | 50 ++++++++ i/chuck/src/log/logger.cc | 52 +++++++++ i/chuck/src/log/logger.hh | 68 +++++++++++ i/chuck/src/log/logger_file.cc | 144 ++++++++++++++++++++++++ i/chuck/src/log/logger_file.hh | 70 ++++++++++++ i/chuck/src/log/logger_null.hh | 51 +++++++++ i/chuck/src/log/logger_ram.cc | 98 ++++++++++++++++ i/chuck/src/log/logger_ram.hh | 63 +++++++++++ i/chuck/src/log/logger_stdout.cc | 68 +++++++++++ i/chuck/src/log/logger_stdout.hh | 54 +++++++++ i/chuck/src/log/test_log.cc | 53 +++++++++ i/chuck/src/log/test_log_server.cc | 45 ++++++++ 21 files changed, 1384 insertions(+) create mode 100644 i/chuck/src/log/Makefile.defs create mode 100644 i/chuck/src/log/data_circular_buffer_factory.cc create mode 100644 i/chuck/src/log/data_circular_buffer_factory.hh create mode 100644 i/chuck/src/log/log.cc create mode 100644 i/chuck/src/log/log.hh create mode 100644 i/chuck/src/log/log_message.cc create mode 100644 i/chuck/src/log/log_message.hh create mode 100644 i/chuck/src/log/log_message.tcc create mode 100644 i/chuck/src/log/log_server.cc create mode 100644 i/chuck/src/log/log_server.hh create mode 100644 i/chuck/src/log/logger.cc create mode 100644 i/chuck/src/log/logger.hh create mode 100644 i/chuck/src/log/logger_file.cc create mode 100644 i/chuck/src/log/logger_file.hh create mode 100644 i/chuck/src/log/logger_null.hh create mode 100644 i/chuck/src/log/logger_ram.cc create mode 100644 i/chuck/src/log/logger_ram.hh create mode 100644 i/chuck/src/log/logger_stdout.cc create mode 100644 i/chuck/src/log/logger_stdout.hh create mode 100644 i/chuck/src/log/test_log.cc create mode 100644 i/chuck/src/log/test_log_server.cc (limited to 'i/chuck/src/log') diff --git a/i/chuck/src/log/Makefile.defs b/i/chuck/src/log/Makefile.defs new file mode 100644 index 0000000..43aaed4 --- /dev/null +++ b/i/chuck/src/log/Makefile.defs @@ -0,0 +1,12 @@ +PROGRAMS += test_log test_log_server + +log_server_OBJECTS = log_server.o $(socket_databuffer_OBJECTS) + +log_OBJECTS = log.o log_message.o logger.o \ + logger_stdout.o logger_file.o logger_ram.o \ + data_circular_buffer_factory.o \ + $(log_server_OBJECTS) \ + $(config_OBJECTS) $(data_OBJECTS) + +test_log_OBJECTS = test_log.o $(log_OBJECTS) +test_log_server_OBJECTS = test_log_server.o $(log_server_OBJECTS) $(data_OBJECTS) diff --git a/i/chuck/src/log/data_circular_buffer_factory.cc b/i/chuck/src/log/data_circular_buffer_factory.cc new file mode 100644 index 0000000..a45d3bc --- /dev/null +++ b/i/chuck/src/log/data_circular_buffer_factory.cc @@ -0,0 +1,72 @@ +// 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" +#include "log_server.hh" + +#include + +/// 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. + try + { + LogServer(*dbBuffer_, "", 2442); + } + catch(std::exception & ex) + { + std::cout << ex.what() << std::endl; + } +// 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/chuck/src/log/data_circular_buffer_factory.hh b/i/chuck/src/log/data_circular_buffer_factory.hh new file mode 100644 index 0000000..8f0874f --- /dev/null +++ b/i/chuck/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/chuck/src/log/log.cc b/i/chuck/src/log/log.cc new file mode 100644 index 0000000..cd7bb32 --- /dev/null +++ b/i/chuck/src/log/log.cc @@ -0,0 +1,130 @@ +// log.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 "log.hh" + +#include "config/config.hh" +#include "logger.hh" + +#include + +/// Constructeur. +Log::Log (const char *module, const char *instance) + : module_ (module), instance_ (instance) +{ + // Get Config + Config &config = Config::getInstance (); + // Get default level log + std::string defaultLvl = config.get("log.level.default", + "info"); + level_ = toLevel (defaultLvl); + // Get private level if exist + level_ = toLevel (config.get + (std::string ("log.level.") + module_, defaultLvl)); + + // Create the null logger + loggerNull_ = Logger::create ("null"); + // Create the real logger + logger_ = Logger::create (config.get + (std::string ("log.logger.") + module_, + config.get + (std::string ("log.logger.default"), "stdout"))); +} + +Log::~Log (void) +{ + delete loggerNull_; + delete logger_; +} + +/// Crée un nouveau LogMessage. +LogMessage +Log::operator() (const char *msg, Level level/*info*/) const +{ + // Check level + if (level <= getLevel()) + // Use real logger + return LogMessage (*this, *logger_, msg, level); + else + // Use null logger + return LogMessage (*this, *loggerNull_, msg, level); +} + +/// Traduit le niveau de log. +std::string +Log::toString (Level level) +{ + switch (level) + { + case none: + return "none"; + case fatal: + return "fatal"; + case error: + return "error"; + case warning: + return "warning"; + case info: + return "info"; + case debug: + return "debug"; + case verydebug: + return "verydebug"; + default: + return "logunknown"; + } +} + +/// Traduit le niveau de log. +Log::Level +Log::toLevel (const std::string &level) +{ + switch (level[0]) + { + case 'n': + return none; + case 'f': + return fatal; + case 'e': + return error; + case 'w': + return warning; + case 'i': + return info; + case 'd': + return debug; + case 'v': + return verydebug; + default: + return levelReserved; + } +} + +/// Change un niveau de log en masque. +Log::Level +Log::toLevelMask (Log::Level level) +{ + return static_cast ((level - 1) | level); +} + diff --git a/i/chuck/src/log/log.hh b/i/chuck/src/log/log.hh new file mode 100644 index 0000000..1141962 --- /dev/null +++ b/i/chuck/src/log/log.hh @@ -0,0 +1,82 @@ +#ifndef log_hh +#define log_hh +// log.h +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 +#include + +class LogMessage; +class Logger; + +/// Classe de log. Permet de construire des LogMessage. +class Log +{ + public: + enum Level + { + none = 0x00, + fatal = 0x01, + error = 0x02, + warning = 0x04, + info = 0x08, + debug = 0x10, + verydebug = 0x20, + levelReserved = 0xffff + }; + private: + const char *module_; + const char *instance_; + Level level_; + /// Null logger. + Logger *loggerNull_; + /// Real logger. + Logger *logger_; + + public: + /// Constructeur. + Log (const char *module, const char *instance = 0); + /// Destructor. + ~Log (void); + /// Crée un nouveau LogMessage. + LogMessage operator() (const char *msg, Level level = info) const; + /// Récupère le module. + const char *getModule (void) const { return module_; } + /// Récupère l'instance. + const char *getInstance (void) const { return instance_; } + /// Traduit le niveau de log. + static std::string toString (Level level); + /// Traduit le niveau de log. + static Level toLevel (const std::string &level); + /// Change un niveau de log en masque. + static Level toLevelMask (Level level); + /// Récupère le niveau de log + Level getLevel(void) const { return level_; } + /// Set le niveau de log + void setLevel(Level level) { level_ = level; } +}; + +#include "log_message.hh" + +#endif // log_hh diff --git a/i/chuck/src/log/log_message.cc b/i/chuck/src/log/log_message.cc new file mode 100644 index 0000000..4305374 --- /dev/null +++ b/i/chuck/src/log/log_message.cc @@ -0,0 +1,74 @@ +// log_message.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 "log_message.hh" + +#include + +/// Constructeur. +LogMessage::LogMessage (const Log &log, Logger &logger, const char *msg, + Log::Level level) +: logger_ (logger) +{ + logger_.start (log, msg, level); +} + +/// Destructeur. +LogMessage::~LogMessage (void) +{ + logger_.stop (); +} + +/// Output a string or a variable name. +LogMessage & +LogMessage::operator<< (const char *s) +{ + logger_ << s; + return *this; +} + +/// Output a string or a variable name. +LogMessage & +LogMessage::operator<< (const std::string &s) +{ + logger_ << s; + return *this; +} + +/// Output a integer. +LogMessage & +LogMessage::operator<< (int i) +{ + logger_ << i; + return *this; +} + +/// Output a double. +LogMessage & +LogMessage::operator<< (double d) +{ + logger_ << d; + return *this; +} + diff --git a/i/chuck/src/log/log_message.hh b/i/chuck/src/log/log_message.hh new file mode 100644 index 0000000..718e6ae --- /dev/null +++ b/i/chuck/src/log/log_message.hh @@ -0,0 +1,55 @@ +#ifndef log_message_hh +#define log_message_hh +// log_message.hh +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 "log.hh" +#include "logger.hh" + +class LogMessage +{ + private: + Logger &logger_; + public: + /// Constructeur. + LogMessage (const Log &log, Logger &logger, const char *msg, Log::Level level); + /// Destructeur. + ~LogMessage (void); + /// Output a string or a variable name. + LogMessage &operator<< (const char *s); + /// Output a string or a variable name. + LogMessage &operator<< (const std::string &s); + /// Output a integer. + LogMessage &operator<< (int i); + /// Output a double. + LogMessage &operator<< (double d); + /// Output a OutputStreamable as a string. + template + LogMessage &operator<< (const OutputStreamable &o); +}; + +#include "log_message.tcc" + +#endif // log_message_hh diff --git a/i/chuck/src/log/log_message.tcc b/i/chuck/src/log/log_message.tcc new file mode 100644 index 0000000..4928d59 --- /dev/null +++ b/i/chuck/src/log/log_message.tcc @@ -0,0 +1,36 @@ +// log_message.tcc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 + +/// Output a OutputStreamable as a string. +template +LogMessage & +LogMessage::operator<< (const OutputStreamable &o) +{ + logger_ << o; + return *this; +} + diff --git a/i/chuck/src/log/log_server.cc b/i/chuck/src/log/log_server.cc new file mode 100644 index 0000000..126ed66 --- /dev/null +++ b/i/chuck/src/log/log_server.cc @@ -0,0 +1,54 @@ +// log_server.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Nicolas Haller +// +// Robot APB Team/Efrei 2005. +// 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 "log_server.hh" +#include "socket/socket_client.hh" + +LogServer::LogServer(DataInput & di, const Address & address) + :ss_(address),di_(di) +{ +} + +LogServer::LogServer(DataInput & di, const std::string & address, + int port) + :ss_(address, port),di_(di) +{ +} + +void +LogServer::listen(void) +{ + int charBuffer; + + const unsigned BUFFER_SIZE = 256; + // Buffer de taille completement arbitraire + uint8_t buffer[BUFFER_SIZE]; + // Ouvre les oreilles du socket + ss_.listen(1); + // Attend gentillement une connection + SocketClient sc(ss_); + // Paf on envoie la sauce + while((charBuffer = di_.read(buffer, BUFFER_SIZE)) != 0) + sc.write(buffer, charBuffer); +} diff --git a/i/chuck/src/log/log_server.hh b/i/chuck/src/log/log_server.hh new file mode 100644 index 0000000..a18de15 --- /dev/null +++ b/i/chuck/src/log/log_server.hh @@ -0,0 +1,50 @@ +#ifndef log_server_hh +#define log_server_hh +// log_server.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Nicolas Haller +// +// Robot APB Team/Efrei 2005. +// 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 "socket/socket_server.hh" +#include "data/data_input.hh" + +/// Classe pour récupérer les logs. +/// La classe construit un serveur en fin de match pour pouvoir récupérer les +/// logs de la pc104 +class LogServer +{ + private: + /// La socket serveur qui attend la ze connection + SocketServer ss_; + /// La dataBuffer à transmettre (avec les logs dedans c'est mieux) + DataInput & di_; + public: + /// Constructeur + LogServer(DataInput & di, const Address & address); + /// Constructeur + LogServer(DataInput & di, const std::string & address, int port); + /// Mise en écoute du serveur + void listen(void); +}; + +#endif // log_server_hh diff --git a/i/chuck/src/log/logger.cc b/i/chuck/src/log/logger.cc new file mode 100644 index 0000000..128aa16 --- /dev/null +++ b/i/chuck/src/log/logger.cc @@ -0,0 +1,52 @@ +// logger.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} +#include "logger.hh" + +#include "logger_null.hh" +#include "logger_stdout.hh" +#include "logger_file.hh" +#include "logger_ram.hh" + +#include + +/// Create a logger from the name. +Logger * +Logger::create (const std::string &loggerName) +{ + if (loggerName == "null") + return new LoggerNull; + else if (loggerName == "stdout") + return new LoggerStdout; + else if (loggerName.substr (0, 4) == "file") + if (loggerName.size () < 6) + throw std::runtime_error ("Missing filename"); + 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/chuck/src/log/logger.hh b/i/chuck/src/log/logger.hh new file mode 100644 index 0000000..cc47095 --- /dev/null +++ b/i/chuck/src/log/logger.hh @@ -0,0 +1,68 @@ +#ifndef logger_hh +#define logger_hh +// logger.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include "log.hh" + +#include + +class Logger +{ + public: + /// Empty destructor. + virtual ~Logger (void) { } + /// Create a logger. + static Logger *create (const std::string &loggerName); + /// Called when a message will be logged. + virtual void start (const Log &log, const char *msg, Log::Level level) = 0; + /// Called when a message has been logged. + virtual void stop (void) = 0; + /// Output a string or a variable name. + virtual Logger &operator<< (const char *s) = 0; + /// Output a string or a variable name. + virtual Logger &operator<< (const std::string &s) = 0; + /// Output a integer. + virtual Logger &operator<< (int i) = 0; + /// Output a double. + virtual Logger &operator<< (double d) = 0; + /// TODO vector + + protected: + /// Private empty constructor. + Logger (void) { } +}; + +#include + +template +Logger &operator<< (Logger &l, const T &v) +{ + std::ostringstream ss; + ss << v; + l << ss.str (); + return l; +} + +#endif // logger_hh diff --git a/i/chuck/src/log/logger_file.cc b/i/chuck/src/log/logger_file.cc new file mode 100644 index 0000000..011a470 --- /dev/null +++ b/i/chuck/src/log/logger_file.cc @@ -0,0 +1,144 @@ +// logger_file.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_file.hh" + +#include +#include +#include + +LoggerFile::t_files_ LoggerFile::files_; + +/// Default constructor. +LoggerFile::LoggerFile (const std::string ¶m) + : filename_ (param) +{ + if (param.empty ()) + throw std::invalid_argument ("Missing filename for file logger"); + file_ = getFile (); +} + +/// Default destructor. +LoggerFile::~LoggerFile (void) +{ + try + { + releaseFile (); + } + catch (std::runtime_error &) + { + } +} + +/// Get a ofstream associated to the filename. +/// This function manage multiple log to the same file by couting the number +/// reference to the file. +std::ofstream * +LoggerFile::getFile (void) +{ + t_files_::iterator i = files_.find (filename_); + // If file does not exist + if (i == files_.end ()) + { + // Create it + std::ofstream *file = new std::ofstream (filename_.c_str ()); + // Add it + files_[filename_] = t_pair_files_ (1, file); + return file; + } + else + { + // Increment number of reference + t_pair_files_ &pair_file = i->second; + pair_file.first++; + return pair_file.second; + } +} + +/// Release the file from the map if needed. Return true if deleted. +bool +LoggerFile::releaseFile (void) +{ + t_files_::iterator i = files_.find (filename_); + // If file does not exist + if (i == files_.end ()) + throw std::runtime_error ("Trying to release an unknown file ?!"); + else + { + t_pair_files_ &pair_file = i->second; + // Decrement number of reference + pair_file.first--; + // Delete it if needed + if (pair_file.first == 0) + { + delete pair_file.second; + files_.erase (i); + return true; + } + } + return false; +} + +/// Called at the begining of a message. +void +LoggerFile::start (const Log &log, const char *msg, Log::Level level) +{ + *file_ << log.getModule () << ':'; + const char *instance = log.getInstance (); + if (instance) + *file_ << ' ' << instance << ':'; + *file_ << " (" << msg << ')'; +} + +/// Output a string or a variable name. +LoggerFile & +LoggerFile::operator<< (const char *s) +{ + *file_ << ' ' << s; + return *this; +} + +/// Output a string or a variable name. +LoggerFile & +LoggerFile::operator<< (const std::string &s) +{ + *file_ << ' ' << s; + return *this; +} + +/// Output a integer. +LoggerFile & +LoggerFile::operator<< (int i) +{ + *file_ << ' ' << i; + return *this; +} + +/// Output a double. +LoggerFile & +LoggerFile::operator<< (double d) +{ + *file_ << ' ' << d; + return *this; +} diff --git a/i/chuck/src/log/logger_file.hh b/i/chuck/src/log/logger_file.hh new file mode 100644 index 0000000..2cfd4d4 --- /dev/null +++ b/i/chuck/src/log/logger_file.hh @@ -0,0 +1,70 @@ +#ifndef logger_file_hh +#define logger_file_hh +// logger_file.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 +#include +#include + +/// Logger to a file. +class LoggerFile : public Logger +{ + /// The pair contains the number of log reference to the file. + typedef std::pair t_pair_files_; + /// Map of opened file, to manage multiple module access to the same file. + typedef std::map t_files_; + static t_files_ files_; + /// Local file used by this instance of the logger file. + std::ofstream *file_; + /// Local file name. + std::string filename_; + public: + /// Default constructor. + LoggerFile (const std::string ¶m); + /// Default destructor. + ~LoggerFile (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) { *file_ << std::endl; } + /// Output a string or a variable name. + LoggerFile &operator<< (const char *s); + /// Output a string or a variable name. + LoggerFile &operator<< (const std::string &s); + /// Output a integer. + LoggerFile &operator<< (int i); + /// Output a double. + LoggerFile &operator << (double d); + private: + /// Get a ofstream associated to the filename. + std::ofstream *getFile (void); + /// Release the file from the map if needed. Return true if deleted. + bool releaseFile (void); +}; + +#endif // logger_file_hh diff --git a/i/chuck/src/log/logger_null.hh b/i/chuck/src/log/logger_null.hh new file mode 100644 index 0000000..03c71a6 --- /dev/null +++ b/i/chuck/src/log/logger_null.hh @@ -0,0 +1,51 @@ +#ifndef logger_null_hh +#define logger_null_hh +// logger_null.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include "logger.hh" + +/// Null logger to prevent losing time. +class LoggerNull : public Logger +{ + public: + /// Default empty constructor. + LoggerNull (void) { } + /// Default empty destructor. + ~LoggerNull (void) { } + /// Empty start. + void start (const Log &, const char *, Log::Level) { } + /// Empty stop. + void stop (void) { } + /// Empty << string or variable name. + LoggerNull &operator<< (const char *) { return *this; } + /// Empty << string or variable name. + LoggerNull &operator<< (const std::string &) { return *this; } + /// Empty << integer. + LoggerNull &operator<< (int) { return *this; } + /// Empty << double. + LoggerNull &operator << (double) { return *this; } +}; + +#endif // logger_null_hh diff --git a/i/chuck/src/log/logger_ram.cc b/i/chuck/src/log/logger_ram.cc new file mode 100644 index 0000000..720d0e8 --- /dev/null +++ b/i/chuck/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/chuck/src/log/logger_ram.hh b/i/chuck/src/log/logger_ram.hh new file mode 100644 index 0000000..03838c2 --- /dev/null +++ b/i/chuck/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/chuck/src/log/logger_stdout.cc b/i/chuck/src/log/logger_stdout.cc new file mode 100644 index 0000000..5956d67 --- /dev/null +++ b/i/chuck/src/log/logger_stdout.cc @@ -0,0 +1,68 @@ +// logger_stdout.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} +#include "logger_stdout.hh" + +/// Called at the begining of a message. +void +LoggerStdout::start (const Log &log, const char *msg, Log::Level level) +{ + std::cout << log.getModule () << ':'; + const char *instance = log.getInstance (); + if (instance) + std::cout << ' ' << instance << ':'; + std::cout << " (" << msg << ')'; + +} + +/// Output a string or a variable name. +LoggerStdout & +LoggerStdout::operator<< (const char *s) +{ + std::cout << ' ' << s; + return *this; +} + +/// Output a string or a variable name. +LoggerStdout & +LoggerStdout::operator<< (const std::string &s) +{ + std::cout << ' ' << s; + return *this; +} + +/// Output a integer. +LoggerStdout & +LoggerStdout::operator<< (int i) +{ + std::cout << ' ' << i; + return *this; +} + +/// Output a double. +LoggerStdout & +LoggerStdout::operator<< (double d) +{ + std::cout << ' ' << d; + return *this; +} diff --git a/i/chuck/src/log/logger_stdout.hh b/i/chuck/src/log/logger_stdout.hh new file mode 100644 index 0000000..eaa706a --- /dev/null +++ b/i/chuck/src/log/logger_stdout.hh @@ -0,0 +1,54 @@ +#ifndef logger_stdout_hh +#define logger_stdout_hh +// logger_stdout.hh +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Dufour Jérémy +// +// 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. +// +// Contact : +// Web: %WEB% +// Email: +// }}} + +#include "logger.hh" + +#include +#include + +/// Logger to stdout. +class LoggerStdout : public Logger +{ + public: + /// Default empty constructor. + LoggerStdout (void) { } + /// Default empty destructor. + ~LoggerStdout (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) { std::cout << std::endl; } + /// Output a string or a variable name. + LoggerStdout &operator<< (const char *s); + /// Output a string or a variable name. + LoggerStdout &operator<< (const std::string &s); + /// Output a integer. + LoggerStdout &operator<< (int i); + /// Output a double. + LoggerStdout &operator << (double d); +}; + +#endif // logger_stdout_hh diff --git a/i/chuck/src/log/test_log.cc b/i/chuck/src/log/test_log.cc new file mode 100644 index 0000000..64989b5 --- /dev/null +++ b/i/chuck/src/log/test_log.cc @@ -0,0 +1,53 @@ +// test_log.cc +// robert - programme du robot 2005. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// Robot APB Team/Efrei 2005. +// 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 "log.hh" +#include "data_circular_buffer_factory.hh" + +#include "config/config.hh" + +int +main (int argc, char **argv) +{ + try + { + Config config (argc, argv); + Log log ("main"); + Log log2 ("chier"); + log ("foo") << "[Default] bar" << 4 << "foobar" << 5.6; + log ("bar", Log::error) << "[Error] foo" << 5; + log ("bar", Log::info) << "[Info] Chier" << 3 << "Info" << 2.3; + log ("bar", Log::debug) << "[Debug] Partout" << 3 << "Debug" << 2.3; + log2 ("foo") << "[Default] bar" << 4 << "foobar" << 5.6; + log2 ("bar", Log::error) << "[Error] foo" << 5; + log2 ("bar", Log::info) << "[Info] Chier" << 3 << "Info" << 2.3; + log2 ("bar", Log::debug) << "[Debug] Partout" << 3 << "Debug" << 2.3; + } + catch (const std::exception &e) + { + std::cerr << e.what () << std::endl; + return 1; + } + return 0; +} diff --git a/i/chuck/src/log/test_log_server.cc b/i/chuck/src/log/test_log_server.cc new file mode 100644 index 0000000..70792f6 --- /dev/null +++ b/i/chuck/src/log/test_log_server.cc @@ -0,0 +1,45 @@ +// test_log_server.cc +// marvin - programme du robot 2006. {{{ +// +// Copyright (C) 2006 Nicolas Haller +// +// Robot APB Team/Efrei 2005. +// 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_buffer.hh" +#include "log_server.hh" +#include "socket/address.hh" + +#include + +int main (void) +{ + // On fait un zolie DataBuffer + DataBuffer db_; + // On ecrit des betises dedans + char * betise = "J'aime bien la choucroute traditionnelle.\n"; + db_.write(reinterpret_cast(betise), strlen(betise)); + // On crée le serveur de la mort qui tue + LogServer ls(db_, std::string(), 2042); + // On écoute tranquillement + ls.listen(); + + return 0; +} -- cgit v1.2.3