summaryrefslogtreecommitdiff
path: root/i/marvin/src
diff options
context:
space:
mode:
authordufourj2006-03-14 01:47:30 +0000
committerdufourj2006-03-14 01:47:30 +0000
commitd9a38657d3f97a2543ea1528732cc6abe5e525c9 (patch)
tree744bf9b3587a3d2f234e2d6fc074c84eb1cb0362 /i/marvin/src
parent8a4df37115ce2baf734b2bf6b6c5ef394e8731e4 (diff)
Log :
- Découpage en différents types de logger ; - Création d'un logger null utilisé pour les messages non traités ; - Création d'un logger stdout pour la sortie sur stdout. TODO : - Créer un logger pour fichier ; - Créer un logger en ram.
Diffstat (limited to 'i/marvin/src')
-rw-r--r--i/marvin/src/log/Makefile.defs2
-rw-r--r--i/marvin/src/log/log.cc50
-rw-r--r--i/marvin/src/log/log.hh15
-rw-r--r--i/marvin/src/log/log_message.cc29
-rw-r--r--i/marvin/src/log/log_message.hh9
-rw-r--r--i/marvin/src/log/log_message.tcc2
-rw-r--r--i/marvin/src/log/logger.cc44
-rw-r--r--i/marvin/src/log/logger.hh57
-rw-r--r--i/marvin/src/log/logger_null.hh51
-rw-r--r--i/marvin/src/log/logger_stdout.cc68
-rw-r--r--i/marvin/src/log/logger_stdout.hh54
-rw-r--r--i/marvin/src/log/test_log.cc11
12 files changed, 321 insertions, 71 deletions
diff --git a/i/marvin/src/log/Makefile.defs b/i/marvin/src/log/Makefile.defs
index ab5e1e5..7e2c328 100644
--- a/i/marvin/src/log/Makefile.defs
+++ b/i/marvin/src/log/Makefile.defs
@@ -1,5 +1,5 @@
PROGRAMS += test_log
-log_OBJECTS = log.o log_message.o $(config_OBJECTS)
+log_OBJECTS = log.o log_message.o logger.o logger_stdout.o $(config_OBJECTS)
test_log_OBJECTS = test_log.o $(log_OBJECTS)
diff --git a/i/marvin/src/log/log.cc b/i/marvin/src/log/log.cc
index dd2227b..6a4f685 100644
--- a/i/marvin/src/log/log.cc
+++ b/i/marvin/src/log/log.cc
@@ -25,11 +25,10 @@
#include "log.hh"
#include "config/config.hh"
+#include "logger.hh"
#include <string>
-std::ostream * Log::ostLog_ = &std::cout;
-
/// Constructeur.
Log::Log (const char *module, const char *instance)
: module_ (module), instance_ (instance)
@@ -40,16 +39,30 @@ Log::Log (const char *module, const char *instance)
std::string defaultLvl = config.get<std::string>("log.level.default",
"info");
level_ = toLevel (defaultLvl);
-
// Get private level if exist
- level_ = toLevel (config.get<std::string>(std::string ("log.level.") + module, defaultLvl));
+ level_ = toLevel (config.get<std::string>
+ (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>
+ (std::string ("log.logger.") + module_,
+ config.get<std::string>
+ (std::string ("log.logger.default"), "stdout")));
}
/// Cre un nouveau LogMessage.
LogMessage
Log::operator() (const char *msg, Level level/*info*/) const
{
- return LogMessage (*this, msg, level);
+ // 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.
@@ -109,30 +122,3 @@ Log::toLevelMask (Log::Level level)
return static_cast <Level> ((level - 1) | level);
}
-/// Rcupre le niveau de log
-Log::Level
-Log::getLevel(void) const
-{
- return level_;
-}
-
-/// Set le niveau de log
-void
-Log::setLevel(Level level)
-{
- level_ = level;
-}
-
-/// Rcupre le ostream o sera envoy les logs
-std::ostream *
-Log::getOstLog(void)
-{
- return ostLog_;
-}
-
-/// Set le ostream o sera envoy les logs
-void
-Log::setOstLog(std::ostream * ost)
-{
- ostLog_ = ost;
-}
diff --git a/i/marvin/src/log/log.hh b/i/marvin/src/log/log.hh
index 2713166..4e240a8 100644
--- a/i/marvin/src/log/log.hh
+++ b/i/marvin/src/log/log.hh
@@ -28,6 +28,7 @@
#include <iostream>
class LogMessage;
+class Logger;
/// Classe de log. Permet de construire des LogMessage.
class Log
@@ -48,7 +49,11 @@ class Log
const char *module_;
const char *instance_;
Level level_;
- static std::ostream * ostLog_;
+ /// Null logger.
+ Logger *loggerNull_;
+ /// Real logger.
+ Logger *logger_;
+
public:
/// Constructeur.
Log (const char *module, const char *instance = 0);
@@ -65,13 +70,9 @@ class Log
/// Change un niveau de log en masque.
static Level toLevelMask (Level level);
/// Rcupre le niveau de log
- Level getLevel(void) const;
+ Level getLevel(void) const { return level_; }
/// Set le niveau de log
- void setLevel(Level level);
- /// Rcupre le ostream o sera envoy les logs
- static std::ostream * getOstLog(void);
- /// Set le ostream o sera envoy les logs
- static void setOstLog(std::ostream * ost);
+ void setLevel(Level level) { level_ = level; }
};
#include "log_message.hh"
diff --git a/i/marvin/src/log/log_message.cc b/i/marvin/src/log/log_message.cc
index 4443e13..4305374 100644
--- a/i/marvin/src/log/log_message.cc
+++ b/i/marvin/src/log/log_message.cc
@@ -27,32 +27,24 @@
#include <iostream>
/// Constructeur.
-LogMessage::LogMessage (const Log &log, const char *msg, Log::Level level)
+LogMessage::LogMessage (const Log &log, Logger &logger, const char *msg,
+ Log::Level level)
+: logger_ (logger)
{
- writeAllowed_ = level <= log.getLevel();
-
- if(writeAllowed_)
- {
- *Log::getOstLog() << log.getModule () << ':';
- if (log.getInstance ())
- *Log::getOstLog() << ' ' << log.getInstance () << ':';
- *Log::getOstLog() << ' ' << msg;
- }
+ logger_.start (log, msg, level);
}
/// Destructeur.
LogMessage::~LogMessage (void)
{
- if(writeAllowed_)
- *Log::getOstLog() << std::endl;
+ logger_.stop ();
}
/// Output a string or a variable name.
LogMessage &
LogMessage::operator<< (const char *s)
{
- if(writeAllowed_)
- *Log::getOstLog() << ' ' << s;
+ logger_ << s;
return *this;
}
@@ -60,8 +52,7 @@ LogMessage::operator<< (const char *s)
LogMessage &
LogMessage::operator<< (const std::string &s)
{
- if(writeAllowed_)
- *Log::getOstLog() << ' ' << s;
+ logger_ << s;
return *this;
}
@@ -69,8 +60,7 @@ LogMessage::operator<< (const std::string &s)
LogMessage &
LogMessage::operator<< (int i)
{
- if(writeAllowed_)
- *Log::getOstLog() << ' ' << i;
+ logger_ << i;
return *this;
}
@@ -78,8 +68,7 @@ LogMessage::operator<< (int i)
LogMessage &
LogMessage::operator<< (double d)
{
- if(writeAllowed_)
- *Log::getOstLog() << ' ' << d;
+ logger_ << d;
return *this;
}
diff --git a/i/marvin/src/log/log_message.hh b/i/marvin/src/log/log_message.hh
index 1786551..718e6ae 100644
--- a/i/marvin/src/log/log_message.hh
+++ b/i/marvin/src/log/log_message.hh
@@ -26,16 +26,15 @@
// }}}
#include "log.hh"
+#include "logger.hh"
class LogMessage
{
- private:
- /// Niveau de log suffisant?
- bool writeAllowed_;
-
+ private:
+ Logger &logger_;
public:
/// Constructeur.
- LogMessage (const Log &log, const char *msg, Log::Level level);
+ LogMessage (const Log &log, Logger &logger, const char *msg, Log::Level level);
/// Destructeur.
~LogMessage (void);
/// Output a string or a variable name.
diff --git a/i/marvin/src/log/log_message.tcc b/i/marvin/src/log/log_message.tcc
index 98ac01b..4928d59 100644
--- a/i/marvin/src/log/log_message.tcc
+++ b/i/marvin/src/log/log_message.tcc
@@ -30,7 +30,7 @@ template<typename OutputStreamable>
LogMessage &
LogMessage::operator<< (const OutputStreamable &o)
{
- std::cout << ' ' << o;
+ logger_ << o;
return *this;
}
diff --git a/i/marvin/src/log/logger.cc b/i/marvin/src/log/logger.cc
new file mode 100644
index 0000000..fc17070
--- /dev/null
+++ b/i/marvin/src/log/logger.cc
@@ -0,0 +1,44 @@
+// logger.cc
+// marvin - programme du robot 2006. {{{
+//
+// Copyright (C) 2006 Dufour Jrmy
+//
+// 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: <dufourj@efrei.fr>
+// }}}
+#include "logger.hh"
+
+#include "logger_null.hh"
+#include "logger_stdout.hh"
+
+#include <stdexcept>
+
+/// Create a logger from the name.
+Logger *
+Logger::create (const std::string &loggerName)
+{
+ // TODO Manages "file:my_file.txt"
+ if (loggerName == "null")
+ return new LoggerNull;
+ else if (loggerName == "stdout")
+ return new LoggerStdout;
+ else
+ throw std::invalid_argument ("Unknown logger : " + loggerName);
+ return 0;
+}
+
diff --git a/i/marvin/src/log/logger.hh b/i/marvin/src/log/logger.hh
new file mode 100644
index 0000000..d03fba7
--- /dev/null
+++ b/i/marvin/src/log/logger.hh
@@ -0,0 +1,57 @@
+#ifndef logger_hh
+#define logger_hh
+// logger.hh
+// marvin - programme du robot 2006. {{{
+//
+// Copyright (C) 2006 Dufour Jrmy
+//
+// 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: <dufourj@efrei.fr>
+// }}}
+
+#include "log.hh"
+
+#include <string>
+
+class Logger
+{
+ public:
+ /// 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) { }
+ /// Empty destructor.
+ virtual ~Logger (void) { }
+};
+
+#endif // logger_hh
diff --git a/i/marvin/src/log/logger_null.hh b/i/marvin/src/log/logger_null.hh
new file mode 100644
index 0000000..03c71a6
--- /dev/null
+++ b/i/marvin/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 Jrmy
+//
+// 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: <dufourj@efrei.fr>
+// }}}
+
+#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/marvin/src/log/logger_stdout.cc b/i/marvin/src/log/logger_stdout.cc
new file mode 100644
index 0000000..5956d67
--- /dev/null
+++ b/i/marvin/src/log/logger_stdout.cc
@@ -0,0 +1,68 @@
+// logger_stdout.cc
+// marvin - programme du robot 2006. {{{
+//
+// Copyright (C) 2006 Dufour Jrmy
+//
+// 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: <dufourj@efrei.fr>
+// }}}
+#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/marvin/src/log/logger_stdout.hh b/i/marvin/src/log/logger_stdout.hh
new file mode 100644
index 0000000..6147e76
--- /dev/null
+++ b/i/marvin/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 Jrmy
+//
+// 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: <dufourj@efrei.fr>
+// }}}
+
+#include "logger.hh"
+
+#include <iostream>
+#include <string>
+
+/// Null logger to prevent losing time
+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/marvin/src/log/test_log.cc b/i/marvin/src/log/test_log.cc
index 75b36f7..fc80777 100644
--- a/i/marvin/src/log/test_log.cc
+++ b/i/marvin/src/log/test_log.cc
@@ -33,15 +33,16 @@ main (int argc, char **argv)
try
{
Config config (argc, argv);
- std::ofstream chier("/tmp/testLog");
- Log::setOstLog(&chier);
+ // TODO
+// std::ofstream chier("/tmp/testLog");
+// Log::setOstLog(&chier);
Log log ("main");
- Log log2 ("Chier");
- log ("foo") << "bar" << 4 << "[Default] foobar" << 5.6;
+ 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") << "bar" << 4 << "[Default] foobar" << 5.6;
+ 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;