Salome HOME
2466d82754eafff46ba4ca0af831ad8fb7be20ea
[modules/yacs.git] / src / engine / Logger.cxx
1 // Copyright (C) 2006-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Logger.hxx"
21 #include "LogRecord.hxx"
22
23 #include <iostream>
24
25 //#define _DEVDEBUG_
26 #include "YacsTrace.hxx"
27
28 using namespace YACS::ENGINE;
29
30 Logger::Logger(const std::string& name):_name(name)
31 {
32 }
33
34 Logger::~Logger()
35 {
36   reset();
37 }
38
39 const std::string& Logger::getName()
40 {
41   return _name;
42 }
43
44 void Logger::log(int level,const std::string& message,const char* filename,int line)
45 {
46   LogRecord* record=makeRecord(getName(),level,message,filename,line);
47   handle(record);
48 }
49
50 void Logger::error(const std::string& message,const char* filename,int line)
51 {
52   log(LogRecord::ERROR,message,filename,line);
53 }
54
55 void Logger::fatal(const std::string& message,const char* filename,int line)
56 {
57   log(LogRecord::FATAL,message,filename,line);
58 }
59
60 void Logger::warning(const std::string& message,const char* filename,int line)
61 {
62   log(LogRecord::WARNING,message,filename,line);
63 }
64
65 LogRecord* Logger::makeRecord(const std::string& name,int level,const std::string& message,const char* filename,int line)
66 {
67   return new LogRecord(name,level,message,filename,line);
68 }
69
70 void Logger::handle(LogRecord* record)
71 {
72   _records.push_back(record);
73 }
74
75 void Logger::reset()
76 {
77   std::vector<LogRecord*>::iterator it;
78   for( it = _records.begin(); it != _records.end(); it++)
79     {
80       delete (*it);
81     }
82   _records.clear();
83 }
84
85 std::string Logger::getStr()
86 {
87   std::vector<LogRecord*>::iterator it;
88   std::string msg;
89   for( it = _records.begin(); it != _records.end(); it++)
90     {
91       msg=msg + (*it)->getStr() + '\n';
92     }
93   return msg;
94 }
95
96 bool Logger::isEmpty()
97 {
98   return _records.size() == 0;
99 }
100
101 bool Logger::hasErrors()
102 {
103   bool ret=false;
104   std::vector<LogRecord*>::iterator it;
105   for( it = _records.begin(); it != _records.end(); it++)
106     {
107       if((*it)->_level > LogRecord::WARNING)
108         {
109           ret=true;
110           break;
111         }
112     }
113   return ret;
114 }