1 /**
2  *   SFCGAL
3  *
4  *   Copyright (C) 2012-2013 Oslandia <infos@oslandia.com>
5  *   Copyright (C) 2012-2013 IGN (http://www.ign.fr)
6  *
7  *   This library is free software; you can redistribute it and/or
8  *   modify it under the terms of the GNU Library General Public
9  *   License as published by the Free Software Foundation; either
10  *   version 2 of the License, or (at your option) any later version.
11  *
12  *   This library is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *   Library General Public License for more details.
16 
17  *   You should have received a copy of the GNU Library General Public
18  *   License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef _SFCGAL_LOG_H_
22 #define _SFCGAL_LOG_H_
23 
24 #include <SFCGAL/config.h>
25 
26 #include <string>
27 #include <boost/format.hpp>
28 
29 /**
30  *
31  * Helper method to log debug message
32  *
33  * \code
34  * SFCGAL_DEBUG( "start new method" ) ;
35  * \endcode
36  */
37 #define SFCGAL_DEBUG( message ) SFCGAL::Logger::get()->log( SFCGAL::Logger::Debug, message, __FILE__, __LINE__ )
38 /**
39  *
40  * Helper method to log information message
41  *
42  * \code
43  * SFCGAL_INFO( "start new method" ) ;
44  * \endcode
45  */
46 #define SFCGAL_INFO( message ) SFCGAL::Logger::get()->log( SFCGAL::Logger::Info, message, __FILE__, __LINE__ )
47 /**
48  *
49  * Helper method to log warning message
50  *
51  * \code
52  * SFCGAL_WARNING( "start new method" ) ;
53  * \endcode
54  */
55 #define SFCGAL_WARNING( message ) SFCGAL::Logger::get()->log( SFCGAL::Logger::Warning, message, __FILE__, __LINE__ )
56 /**
57  *
58  * Helper method to log error message
59  *
60  * \code
61  * SFCGAL_ERROR( "invalid geometry" ) ;
62  * \endcode
63  */
64 #define SFCGAL_ERROR( message ) SFCGAL::Logger::get()->log( SFCGAL::Logger::Info, message, __FILE__, __LINE__ )
65 /**
66  *
67  * Helper method to log critical message
68  *
69  * \code
70  * SFCGAL_ERROR( "unexpected behavior in triangulate" ) ;
71  * \endcode
72  */
73 #define SFCGAL_CRITICAL( message ) SFCGAL::Logger::get()->log( SFCGAL::Logger::Critical, message, __FILE__, __LINE__ )
74 
75 
76 namespace SFCGAL {
77 
78 /**
79  * [Singleton]Logger class
80  *
81  * @warning saved_lines and co removed (dangerous for memory and could be done in a LogWriter).
82  */
83 class SFCGAL_API Logger {
84 public:
85     /**
86      * destructor
87      */
88     ~Logger();
89 
90     /**
91      * log level
92      */
93     typedef enum {
94         Debug,
95         Info,
96         Warning,
97         Error,
98         Critical
99     } Level ;
100 
101     /**
102      * singleton accessor
103      */
104     static Logger* get() ;
105 
106     /**
107      * log a message using boost format
108      * @param level the log level
109      * @param message the message to log
110      * @param filename the filename (optional)
111      * @param lineNumber the line number in the file (optional)
112      */
113     void log(
114         const Level& level,
115         const boost::format& message,
116         const std::string& filename = "",
117         const int& lineNumber = -1
118     );
119 
120     /**
121      * log a message
122      * @param level the log level
123      * @param message the message to log
124      * @param filename the filename (optional)
125      * @param lineNumber the line number in the file (optional)
126      */
127     void log(
128         const Level& level,
129         const std::string& message,
130         const std::string& filename = "",
131         const int& lineNumber = -1
132     );
133 
134     /**
135      * get the current log level
136      */
137     const Level& logLevel() const ;
138     /**
139      * set the log level
140      */
141     void setLogLevel( const Level& logLevel ) ;
142 
143 private:
144     /**
145      * current log level
146      */
147     Level _logLevel ;
148     /**
149      * display file position?
150      */
151     bool _displayFilePosition ;
152 
153     /**
154      * private constructor
155      */
156     Logger( std::ostream& );
157     /**
158      * no copy constructor
159      */
160     Logger( const Logger& other );
161 
162     std::ostream _out;
163 };
164 
165 /**
166  * get the logger
167  */
168 SFCGAL_API Logger& logger() ;
169 
170 
171 }//SFCGAL
172 
173 #define SFCGAL_LOG( level, msg ) do { SFCGAL::Logger::get() << "[" << (level) << " " << __FILE__ << ":" << __LINE__ << "] " << msg << std::endl; } while (0)
174 
175 #ifndef NDEBUG
176 #  define LOG_DEBUG( msg ) do { SFCGAL_LOG( "DEBUG", msg ); } while(0)
177 #else
178 #  define LOG_DEBUG( msg ) do {} while(0)
179 #endif
180 #define LOG_NOTICE( msg ) do { SFCGAL_LOG( "NOTICE", msg ); } while(0)
181 #define LOG_ERROR( msg ) do { SFCGAL_LOG( "ERROR", msg ); } while(0)
182 
183 #endif
184