fw4spl
fwLog/src/fwLog/Logger.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwLog/Logger.hpp"
8 
9 #include <boost/foreach.hpp>
10 
11 namespace fwLog
12 {
13 
15 {
16 }
17 
18 //-----------------------------------------------------------------------------
19 
21 {
22 }
23 
24 //-----------------------------------------------------------------------------
25 
26 void Logger::information(const std::string& message)
27 {
28  ::fwLog::Log log(::fwLog::Log::INFORMATION, message);
29  m_logContainer.push_back(log);
30  SLM_INFO(message);
31 }
32 
33 //-----------------------------------------------------------------------------
34 
35 void Logger::warning(const std::string& message)
36 {
37  ::fwLog::Log log(::fwLog::Log::WARNING, message);
38  m_logContainer.push_back(log);
39  SLM_WARN(message);
40 }
41 
42 //-----------------------------------------------------------------------------
43 
44 void Logger::critical(const std::string& message)
45 {
46  ::fwLog::Log log(::fwLog::Log::CRITICAL, message);
47  m_logContainer.push_back(log);
48  SLM_ERROR(message);
49 }
50 
51 //-----------------------------------------------------------------------------
52 
53 ::fwLog::Log Logger::getLog(unsigned int index)
54 {
55  SLM_ASSERT("Please be sure to provide an index lower than the number of items.", index < this->count());
56  return m_logContainer[index];
57 }
58 
59 //-----------------------------------------------------------------------------
60 
61 std::size_t Logger::count() const
62 {
63  return m_logContainer.size();
64 }
65 
66 //-----------------------------------------------------------------------------
67 
68 std::size_t Logger::count(::fwLog::Log::LevelType level) const
69 {
70  std::size_t count = 0;
71  for(const ::fwLog::Log& log: m_logContainer)
72  {
73  if(log.getLevel() == level)
74  {
75  ++count;
76  }
77  }
78  return count;
79 }
80 
81 //-----------------------------------------------------------------------------
82 
84 {
85  std::sort(m_logContainer.begin(), m_logContainer.end(), Logger::logSorter);
86 }
87 
88 //-----------------------------------------------------------------------------
89 
91 {
92  m_logContainer.clear();
93 }
94 
95 //-----------------------------------------------------------------------------
96 
97 bool Logger::logSorter(const ::fwLog::Log& logA, const ::fwLog::Log& logB)
98 {
99  return logA.getLevel() > logB.getLevel();
100 }
101 
102 //-----------------------------------------------------------------------------
103 
104 } //namespace fwLog
105 
106 
FWLOG_API std::size_t count() const
Returns the number of logs.
FWLOG_API ~Logger()
Destructor.
FWLOG_API Logger()
Constructor.
#define SLM_WARN(message)
Definition: spyLog.hpp:261
FWLOG_API void critical(const std::string &message)
Add critical log into the logger.
fwLog contains classes used to manage logs.
Definition: Log.hpp:16
#define SLM_ERROR(message)
Definition: spyLog.hpp:272
FWLOG_API void sort()
Sort logs according to log levels.
static bool logSorter(const ::fwLog::Log &logA, const ::fwLog::Log &logB)
Function used to sort logs.
LogContainerType m_logContainer
Log container.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
FWLOG_API void clear()
Clear logs.
FWLOG_API void information(const std::string &message)
Add information log into the logger.
FWLOG_API void warning(const std::string &message)
Add warning log into the logger.
#define SLM_INFO(message)
Definition: spyLog.hpp:250
Log.
Definition: Log.hpp:23
FWLOG_API::fwLog::Log getLog(unsigned int index)
Return the log matching the specified index.