summaryrefslogtreecommitdiff
path: root/i
diff options
context:
space:
mode:
authorhaller2005-11-17 14:04:39 +0000
committerhaller2005-11-17 14:04:39 +0000
commite2a7a2c1b460626983377b2203346c6cabc429d6 (patch)
tree79fac5ae25131c82d131104356e2c93ace0c218b /i
parent621a1efccc170b84584dc375a225c19bd2911179 (diff)
Amélioration des logs
* Prise en compte des niveau de log * Paramétrage de la cible de log(ostream)
Diffstat (limited to 'i')
-rw-r--r--i/marvin/src/log/log.cc18
-rw-r--r--i/marvin/src/log/log.hh8
-rw-r--r--i/marvin/src/log/log_message.cc16
-rw-r--r--i/marvin/src/log/test_log.cc8
4 files changed, 40 insertions, 10 deletions
diff --git a/i/marvin/src/log/log.cc b/i/marvin/src/log/log.cc
index e1799b0..5b700a7 100644
--- a/i/marvin/src/log/log.cc
+++ b/i/marvin/src/log/log.cc
@@ -24,8 +24,10 @@
// }}}
#include "log.hh"
+std::ostream * Log::ostLog_ = &std::cout;
+
/// Constructeur.
-Log::Log (const char *module, const char *instance, Level level)
+Log::Log (const char *module, Level level, const char *instance)
: module_ (module), instance_ (instance), level_(level)
{
}
@@ -107,3 +109,17 @@ Log::setLevel(Level level)
{
level_ = level;
}
+
+/// Récupère 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 4dbaf16..b05d3a0 100644
--- a/i/marvin/src/log/log.hh
+++ b/i/marvin/src/log/log.hh
@@ -25,6 +25,7 @@
//
// }}}
#include <string>
+#include <iostream>
class LogMessage;
@@ -47,9 +48,10 @@ class Log
const char *module_;
const char *instance_;
Level level_;
+ static std::ostream * ostLog_;
public:
/// Constructeur.
- Log (const char *module, const char *instance = 0, Level level = info);
+ Log (const char *module, Level level = info, const char *instance = 0);
/// Crée un nouveau LogMessage.
LogMessage operator() (const char *msg, Level level = info) const;
/// Récupère le module.
@@ -66,6 +68,10 @@ class Log
Level getLevel(void) const;
/// Set le niveau de log
void setLevel(Level level);
+ /// Récupère 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);
};
#include "log_message.hh"
diff --git a/i/marvin/src/log/log_message.cc b/i/marvin/src/log/log_message.cc
index 054ef78..4443e13 100644
--- a/i/marvin/src/log/log_message.cc
+++ b/i/marvin/src/log/log_message.cc
@@ -33,10 +33,10 @@ LogMessage::LogMessage (const Log &log, const char *msg, Log::Level level)
if(writeAllowed_)
{
- std::cout << log.getModule () << ':';
+ *Log::getOstLog() << log.getModule () << ':';
if (log.getInstance ())
- std::cout << ' ' << log.getInstance () << ':';
- std::cout << ' ' << msg;
+ *Log::getOstLog() << ' ' << log.getInstance () << ':';
+ *Log::getOstLog() << ' ' << msg;
}
}
@@ -44,7 +44,7 @@ LogMessage::LogMessage (const Log &log, const char *msg, Log::Level level)
LogMessage::~LogMessage (void)
{
if(writeAllowed_)
- std::cout << std::endl;
+ *Log::getOstLog() << std::endl;
}
/// Output a string or a variable name.
@@ -52,7 +52,7 @@ LogMessage &
LogMessage::operator<< (const char *s)
{
if(writeAllowed_)
- std::cout << ' ' << s;
+ *Log::getOstLog() << ' ' << s;
return *this;
}
@@ -61,7 +61,7 @@ LogMessage &
LogMessage::operator<< (const std::string &s)
{
if(writeAllowed_)
- std::cout << ' ' << s;
+ *Log::getOstLog() << ' ' << s;
return *this;
}
@@ -70,7 +70,7 @@ LogMessage &
LogMessage::operator<< (int i)
{
if(writeAllowed_)
- std::cout << ' ' << i;
+ *Log::getOstLog() << ' ' << i;
return *this;
}
@@ -79,7 +79,7 @@ LogMessage &
LogMessage::operator<< (double d)
{
if(writeAllowed_)
- std::cout << ' ' << d;
+ *Log::getOstLog() << ' ' << d;
return *this;
}
diff --git a/i/marvin/src/log/test_log.cc b/i/marvin/src/log/test_log.cc
index 3be9b68..11bfe7c 100644
--- a/i/marvin/src/log/test_log.cc
+++ b/i/marvin/src/log/test_log.cc
@@ -23,14 +23,22 @@
//
// }}}
#include "log.hh"
+#include <fstream>
int
main (void)
{
+ std::ofstream chier("/tmp/testLog");
+ Log::setOstLog(&chier);
Log log ("main");
+ Log log2 ("Chier");
log ("foo") << "bar" << 4 << "foobar" << 5.6;
log ("bar", Log::error) << "foo" << 5;
log ("bar", Log::info) << "Chier" << 3 << "Info" << 2.3;
log ("bar", Log::debug) << "Partout" << 3 << "Debug" << 2.3;
+ log2 ("foo") << "bar" << 4 << "foobar" << 5.6;
+ log2 ("bar", Log::error) << "foo" << 5;
+ log2 ("bar", Log::info) << "Chier" << 3 << "Info" << 2.3;
+ log2 ("bar", Log::debug) << "Partout" << 3 << "Debug" << 2.3;
return 0;
}