summaryrefslogtreecommitdiff
path: root/i/marvin/src/log/logger_file.cc
diff options
context:
space:
mode:
Diffstat (limited to 'i/marvin/src/log/logger_file.cc')
-rw-r--r--i/marvin/src/log/logger_file.cc144
1 files changed, 144 insertions, 0 deletions
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;
+}