Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / engine / Logger.cxx
1 //  Copyright (C) 2006-2008  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.
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 #include "Logger.hxx"
20 #include "LogRecord.hxx"
21
22 #include <iostream>
23
24 //#define _DEVDEBUG_
25 #include "YacsTrace.hxx"
26
27 using namespace YACS::ENGINE;
28
29 Logger::Logger(const std::string& name):_name(name)
30 {
31 }
32
33 Logger::~Logger()
34 {
35   reset();
36 }
37
38 const std::string& Logger::getName()
39 {
40   return _name;
41 }
42
43 void Logger::log(int level,const std::string& message,const char* filename,int line)
44 {
45   LogRecord* record=makeRecord(getName(),level,message,filename,line);
46   handle(record);
47 }
48
49 void Logger::error(const std::string& message,const char* filename,int line)
50 {
51   log(LogRecord::ERROR,message,filename,line);
52 }
53
54 void Logger::fatal(const std::string& message,const char* filename,int line)
55 {
56   log(LogRecord::FATAL,message,filename,line);
57 }
58
59 void Logger::warning(const std::string& message,const char* filename,int line)
60 {
61   log(LogRecord::WARNING,message,filename,line);
62 }
63
64 LogRecord* Logger::makeRecord(const std::string& name,int level,const std::string& message,const char* filename,int line)
65 {
66   return new LogRecord(name,level,message,filename,line);
67 }
68
69 void Logger::handle(LogRecord* record)
70 {
71   _records.push_back(record);
72 }
73
74 void Logger::reset()
75 {
76   std::vector<LogRecord*>::iterator it;
77   for( it = _records.begin(); it != _records.end(); it++)
78     {
79       delete (*it);
80     }
81   _records.clear();
82 }
83
84 std::string Logger::getStr()
85 {
86   std::vector<LogRecord*>::iterator it;
87   std::string msg;
88   for( it = _records.begin(); it != _records.end(); it++)
89     {
90       msg=msg + (*it)->getStr() + '\n';
91     }
92   return msg;
93 }
94
95 bool Logger::isEmpty()
96 {
97   return _records.size() == 0;
98 }
99
100 bool Logger::hasErrors()
101 {
102   bool ret=false;
103   std::vector<LogRecord*>::iterator it;
104   for( it = _records.begin(); it != _records.end(); it++)
105     {
106       if((*it)->_level > LogRecord::WARNING)
107         {
108           ret=true;
109           break;
110         }
111     }
112   return ret;
113 }