summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/logger
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/src/logger
parent677ad16bf96efd9487e7659b15dafc718cd53b46 (diff)
Add: Logger.
Diffstat (limited to '2004/i/nono/src/logger')
-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
6 files changed, 359 insertions, 0 deletions
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;
+ }
+}