summaryrefslogtreecommitdiff
path: root/i/marvin
diff options
context:
space:
mode:
authordufourj2006-03-14 21:34:17 +0000
committerdufourj2006-03-14 21:34:17 +0000
commit3e6b45437f75bb4ad20ce5b42f1cf9d1a8029129 (patch)
tree023b25cc381ed8ae3da636385ff4be11f8ac111f /i/marvin
parent6b9bddbcd6973b71a6c378f7c92cf1fc226def63 (diff)
Log :
- adding a file logger.
Diffstat (limited to 'i/marvin')
-rw-r--r--i/marvin/runtime/rc/config1
-rw-r--r--i/marvin/src/log/Makefile.defs2
-rw-r--r--i/marvin/src/log/logger.cc7
-rw-r--r--i/marvin/src/log/logger_file.cc144
-rw-r--r--i/marvin/src/log/logger_file.hh70
-rw-r--r--i/marvin/src/log/logger_stdout.hh2
-rw-r--r--i/marvin/src/log/test_log.cc3
7 files changed, 223 insertions, 6 deletions
diff --git a/i/marvin/runtime/rc/config b/i/marvin/runtime/rc/config
index 397f4e7..3292279 100644
--- a/i/marvin/runtime/rc/config
+++ b/i/marvin/runtime/rc/config
@@ -37,6 +37,7 @@ motor.pStatMotor = 0
# Possible values are :
# - null : no log, optimized for time ;
# - stdout : to stdout ;
+# - file:/path/filename : to a file.
# log.logger.default = "stdout"
# log.logger.asserv = "null"
diff --git a/i/marvin/src/log/Makefile.defs b/i/marvin/src/log/Makefile.defs
index 7e2c328..43caab5 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 logger.o logger_stdout.o $(config_OBJECTS)
+log_OBJECTS = log.o log_message.o logger.o logger_stdout.o logger_file.o $(config_OBJECTS)
test_log_OBJECTS = test_log.o $(log_OBJECTS)
diff --git a/i/marvin/src/log/logger.cc b/i/marvin/src/log/logger.cc
index fc17070..c6fdb6e 100644
--- a/i/marvin/src/log/logger.cc
+++ b/i/marvin/src/log/logger.cc
@@ -25,6 +25,7 @@
#include "logger_null.hh"
#include "logger_stdout.hh"
+#include "logger_file.hh"
#include <stdexcept>
@@ -32,11 +33,15 @@
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 if (loggerName.substr (0, 4) == "file")
+ if (loggerName.size () < 6)
+ throw std::runtime_error ("Missing filename");
+ else
+ return new LoggerFile (loggerName.substr (5));
else
throw std::invalid_argument ("Unknown logger : " + loggerName);
return 0;
diff --git a/i/marvin/src/log/logger_file.cc b/i/marvin/src/log/logger_file.cc
new file mode 100644
index 0000000..011a470
--- /dev/null
+++ b/i/marvin/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 <stdexcept>
+#include <fstream>
+#include <iostream>
+
+LoggerFile::t_files_ LoggerFile::files_;
+
+/// Default constructor.
+LoggerFile::LoggerFile (const std::string &param)
+ : 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/marvin/src/log/logger_file.hh b/i/marvin/src/log/logger_file.hh
new file mode 100644
index 0000000..2cfd4d4
--- /dev/null
+++ b/i/marvin/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 <string>
+#include <map>
+#include <fstream>
+
+/// Logger to a file.
+class LoggerFile : public Logger
+{
+ /// The pair contains the number of log reference to the file.
+ typedef std::pair<unsigned int, std::ofstream *> t_pair_files_;
+ /// Map of opened file, to manage multiple module access to the same file.
+ typedef std::map<const std::string, t_pair_files_> 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 &param);
+ /// 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/marvin/src/log/logger_stdout.hh b/i/marvin/src/log/logger_stdout.hh
index 6147e76..eaa706a 100644
--- a/i/marvin/src/log/logger_stdout.hh
+++ b/i/marvin/src/log/logger_stdout.hh
@@ -29,7 +29,7 @@
#include <iostream>
#include <string>
-/// Null logger to prevent losing time
+/// Logger to stdout.
class LoggerStdout : public Logger
{
public:
diff --git a/i/marvin/src/log/test_log.cc b/i/marvin/src/log/test_log.cc
index fc80777..77146ef 100644
--- a/i/marvin/src/log/test_log.cc
+++ b/i/marvin/src/log/test_log.cc
@@ -33,9 +33,6 @@ main (int argc, char **argv)
try
{
Config config (argc, argv);
- // TODO
-// std::ofstream chier("/tmp/testLog");
-// Log::setOstLog(&chier);
Log log ("main");
Log log2 ("chier");
log ("foo") << "[Default] bar" << 4 << "foobar" << 5.6;