summaryrefslogtreecommitdiff
path: root/2004/i/nono
diff options
context:
space:
mode:
authorschodet2004-03-21 17:15:46 +0000
committerschodet2004-03-21 17:15:46 +0000
commit1be0aec720c1e3f1ffebc2844245933ac7d1c0a9 (patch)
tree25377ef2e1b1d64bc17a830ea213f67f7d41f0a0 /2004/i/nono
parent677ad16bf96efd9487e7659b15dafc718cd53b46 (diff)
Add: Logger.
Diffstat (limited to '2004/i/nono')
-rw-r--r--2004/i/nono/src/GNUmakefile2
-rw-r--r--2004/i/nono/src/config/config.cc18
-rw-r--r--2004/i/nono/src/config/config.h2
-rw-r--r--2004/i/nono/src/logger/Makefile.defs8
-rw-r--r--2004/i/nono/src/logger/log.cc108
-rw-r--r--2004/i/nono/src/logger/log.h72
-rw-r--r--2004/i/nono/src/logger/logger.cc78
-rw-r--r--2004/i/nono/src/logger/logger.h46
-rw-r--r--2004/i/nono/src/logger/test_logger.cc47
-rw-r--r--2004/i/nono/src/utils/hexa.cc2
-rw-r--r--2004/i/nono/src/utils/hexa.h2
11 files changed, 381 insertions, 4 deletions
diff --git a/2004/i/nono/src/GNUmakefile b/2004/i/nono/src/GNUmakefile
index a946254..4b3ac24 100644
--- a/2004/i/nono/src/GNUmakefile
+++ b/2004/i/nono/src/GNUmakefile
@@ -1,4 +1,4 @@
-SUBDIRS = camera config date erreur motor serial vision fake_serial utils
+SUBDIRS = camera config date erreur motor serial vision fake_serial utils logger
TARGETS =
diff --git a/2004/i/nono/src/config/config.cc b/2004/i/nono/src/config/config.cc
index 4755677..9a88494 100644
--- a/2004/i/nono/src/config/config.cc
+++ b/2004/i/nono/src/config/config.cc
@@ -39,7 +39,7 @@ Config::Config (const char *filename)
#else
if (config_yyopen (filename, 0, 0) == -1)
#endif
- throw ErreurConfig (filename, "Erreur d'ouverture.\n");
+ m_type = 0;
}
// Destructeur.
@@ -69,6 +69,16 @@ Config::getId (void)
return config_yylval.id;
}
+void
+Config::getId (std::string &s)
+{
+ if (m_type == -1) m_type = config_yylex ();
+ if (m_type != ID)
+ throw ErreurConfig (m_filename.c_str (), "Identificateur attendu.\n");
+ m_type = -1;
+ s = config_yylval.id;
+}
+
bool
Config::getBool (void)
{
@@ -143,6 +153,12 @@ Config::noId (void)
"Identificateur inconnu.\n");
}
+void
+Config::throwError (const char *msg)
+{
+ throw ErreurConfig (m_filename.c_str (), msg);
+}
+
// Appellé par le lexer pour changer de fichier.
bool
Config::wrap_handler (void *p)
diff --git a/2004/i/nono/src/config/config.h b/2004/i/nono/src/config/config.h
index f56d558..f6f22df 100644
--- a/2004/i/nono/src/config/config.h
+++ b/2004/i/nono/src/config/config.h
@@ -41,6 +41,7 @@ class Config
int getNum (void);
Config &operator>> (int &num) { num = getNum (); return *this; }
const char *getId (void);
+ void getId (std::string &s);
bool getBool (void);
Config &operator>> (bool &b) { b = getBool (); return *this; }
double getFloat (void);
@@ -59,6 +60,7 @@ class Config
bool eof (void);
// Lance une erreur, on a pas trouvé ce qu'on voulais.
void noId (void);
+ void throwError (const char *msg);
private:
// Appellé par le lexer pour changer de fichier.
static bool wrap_handler (void *p);
diff --git a/2004/i/nono/src/logger/Makefile.defs b/2004/i/nono/src/logger/Makefile.defs
new file mode 100644
index 0000000..4ae4ab8
--- /dev/null
+++ b/2004/i/nono/src/logger/Makefile.defs
@@ -0,0 +1,8 @@
+TARGETS += test_logger
+LIBS += logger.a
+test_logger_SOURCES = test_logger.cc logger.a config.a erreur.a
+logger_a_SOURCES = logger.cc log.cc
+
+test_logger: $(test_logger_SOURCES:%.cc=%.o)
+
+logger.a: ${logger_a_SOURCES:%.cc=logger.a(%.o)}
diff --git a/2004/i/nono/src/logger/log.cc b/2004/i/nono/src/logger/log.cc
new file mode 100644
index 0000000..a5c4c83
--- /dev/null
+++ b/2004/i/nono/src/logger/log.cc
@@ -0,0 +1,108 @@
+// log.cc
+// nono - programme du robot 2004. {{{
+//
+// Copyright (C) 2004 Nicolas Schodet
+//
+// 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 "log.h"
+#include "logger.h"
+
+Logger Log::logger_;
+
+/// Constructeur.
+Log::Log (const std::string &module)
+ : std::ostream ((std::streambuf *) this),
+ module_ (module),
+ curLevel_ (info)
+{
+ init ();
+}
+
+/// Constructeur.
+Log::Log (const std::string &module, const std::string &instance)
+ : std::ostream ((std::streambuf *) this),
+ module_ (module), instance_ (instance),
+ curLevel_ (info)
+{
+ init ();
+}
+
+/// Paramètre le niveau de log pour la prochaine sortie.
+Log &
+Log::operator() (Level level)
+{
+ curLevel_ = level;
+ return *this;
+}
+
+static const char *levelTab[] =
+{
+ "none",
+ "fatal",
+ "error",
+ "warning",
+ "info",
+ "debug"
+};
+
+/// Traduit le niveau de log.
+void
+Log::toString (Level level, std::string &s)
+{
+ s = levelTab[static_cast<int> (level)];
+}
+
+/// Traduit le niveau de log.
+Log::Level
+Log::toLevel (const std::string &level)
+{
+ for (int i = 0; i < static_cast<int> (nbLevel); ++i)
+ {
+ if (level == levelTab[i])
+ return static_cast<Level> (i);
+ }
+ return nbLevel;
+}
+
+/// Appellé lorsque le tampon streambuf déborde.
+int
+Log::overflow (int c)
+{
+ if (c == '\n')
+ {
+ if (curLevel_ <= maxLevel_)
+ logger_.log (module_, instance_, curLevel_, buffer_);
+ buffer_.erase ();
+ }
+ else
+ {
+ buffer_ += c;
+ }
+ return c;
+}
+
+/// Partie commune des constructeurs.
+void
+Log::init (void)
+{
+ maxLevel_ = logger_.maxLevel (module_);
+}
+
diff --git a/2004/i/nono/src/logger/log.h b/2004/i/nono/src/logger/log.h
new file mode 100644
index 0000000..d089a3b
--- /dev/null
+++ b/2004/i/nono/src/logger/log.h
@@ -0,0 +1,72 @@
+#ifndef log_h
+#define log_h
+// log.h
+// nono - programme du robot 2004. {{{
+//
+// Copyright (C) 2004 Nicolas Schodet
+//
+// 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 <string>
+#include <iostream>
+
+class Logger;
+
+class Log : public std::ostream, protected std::streambuf
+{
+ public:
+ enum Level
+ {
+ none,
+ fatal,
+ error,
+ warning,
+ info,
+ debug,
+ nbLevel
+ };
+ private:
+ std::string module_;
+ std::string instance_;
+ std::string buffer_;
+ static Logger logger_;
+ Level maxLevel_;
+ Level curLevel_;
+ public:
+ /// Constructeur.
+ Log (const std::string &module);
+ /// Constructeur.
+ Log (const std::string &module, const std::string &instance);
+ /// Paramètre le niveau de log pour la prochaine sortie.
+ Log &operator() (Level level);
+ /// Traduit le niveau de log.
+ static void toString (Level level, std::string &s);
+ /// Traduit le niveau de log.
+ static Level toLevel (const std::string &level);
+ protected:
+ /// Appellé lorsque le tampon streambuf déborde.
+ int overflow (int c);
+ private:
+ /// Partie commune des constructeurs.
+ void init (void);
+};
+
+#endif // log_h
diff --git a/2004/i/nono/src/logger/logger.cc b/2004/i/nono/src/logger/logger.cc
new file mode 100644
index 0000000..60cb013
--- /dev/null
+++ b/2004/i/nono/src/logger/logger.cc
@@ -0,0 +1,78 @@
+// logger.cc
+// nono - programme du robot 2004. {{{
+//
+// Copyright (C) 2004 Nicolas Schodet
+//
+// 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.h"
+#include "config/config.h"
+
+/// Constructeur par default.
+Logger::Logger (void)
+{
+ // Constructeur utilisé en global.
+ try
+ {
+ // Lit la conf.
+ Config rc ("rc/logger");
+ std::string module, level;
+ Log::Level l;
+ while (!rc.eof ())
+ {
+ rc.getId (module);
+ rc.getId (level);
+ l = Log::toLevel (level);
+ if (l != Log::nbLevel)
+ logLevels_[module] = l;
+ else
+ rc.throwError ("Log level expected.\n");
+ }
+ }
+ catch (const std::exception &e)
+ {
+ std::cerr << e.what () << std::endl;
+ throw;
+ }
+}
+
+/// Récupère le level maximum pour un module.
+Log::Level
+Logger::maxLevel (const std::string &module) const
+{
+ LogLevels::const_iterator i;
+ i = logLevels_.find (module);
+ if (i != logLevels_.end ())
+ return i->second;
+ else
+ return Log::none;
+}
+
+/// Loggue un message.
+void
+Logger::log (const std::string &module, const std::string &instance,
+ Log::Level level, const std::string &msg)
+{
+ std::clog << module << ": ";
+ if (!instance.empty ())
+ std::clog << instance << ": ";
+ std::clog << msg << std::endl;
+}
+
diff --git a/2004/i/nono/src/logger/logger.h b/2004/i/nono/src/logger/logger.h
new file mode 100644
index 0000000..ced2765
--- /dev/null
+++ b/2004/i/nono/src/logger/logger.h
@@ -0,0 +1,46 @@
+#ifndef logger_h
+#define logger_h
+// logger.h
+// nono - programme du robot 2004. {{{
+//
+// Copyright (C) 2004 Nicolas Schodet
+//
+// 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 "log.h"
+
+#include <string>
+#include <map>
+
+class Logger
+{
+ typedef std::map<std::string, Log::Level> LogLevels;
+ LogLevels logLevels_;
+ public:
+ /// Constructeur par default.
+ Logger (void);
+ /// Récupère le level maximum pour un module.
+ Log::Level maxLevel (const std::string &module) const;
+ /// Loggue un message.
+ void log (const std::string &module, const std::string &instance,
+ Log::Level level, const std::string &msg);
+};
+
+#endif // logger_h
diff --git a/2004/i/nono/src/logger/test_logger.cc b/2004/i/nono/src/logger/test_logger.cc
new file mode 100644
index 0000000..09c4923
--- /dev/null
+++ b/2004/i/nono/src/logger/test_logger.cc
@@ -0,0 +1,47 @@
+// test_logger.cc
+// nono - programme du robot 2004. {{{
+//
+// Copyright (C) 2004 Nicolas Schodet
+//
+// 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 "log.h"
+
+#include <exception>
+#include "erreur/erreur.h"
+
+int
+main (void)
+{
+ try
+ {
+ Log lmotor ("motor");
+ Log lasserv ("asserv");
+ lmotor << "speed " << 4 << ' ' << 5 << std::endl;
+ lasserv << "speed 0x" << hex << 40 << " 0x" << hex << 50 << std::endl;
+ lasserv (Log::debug) << "send !v????" << std::endl;
+ return 0;
+ }
+ catch (const std::exception &e)
+ {
+ std::cerr << e.what () << std::endl;
+ return 1;
+ }
+}
diff --git a/2004/i/nono/src/utils/hexa.cc b/2004/i/nono/src/utils/hexa.cc
index 8a68aab..247b44c 100644
--- a/2004/i/nono/src/utils/hexa.cc
+++ b/2004/i/nono/src/utils/hexa.cc
@@ -57,7 +57,7 @@ hex2digit (char c)
/// Converti un chiffre (0-15) en hexa (0-9a-f).
char
-digit2hex (char d)
+digit2hex (int d)
{
return digit2hexTbl_[d];
}
diff --git a/2004/i/nono/src/utils/hexa.h b/2004/i/nono/src/utils/hexa.h
index 38a4ae5..2bcf81d 100644
--- a/2004/i/nono/src/utils/hexa.h
+++ b/2004/i/nono/src/utils/hexa.h
@@ -29,6 +29,6 @@
int hex2digit (char c);
/// Converti un chiffre (0-15) en hexa (0-9a-f).
-char digit2hex (char d);
+char digit2hex (int d);
#endif // hexa_h