1 // FILE logger.h : Declaration of class logger
2 //////////////////////////////////////////////////////////////////////////
3 //
4 // Copyright 1990-2012 Marcus Mo
5 //
6 // This file is part of the eclib package.
7 //
8 // eclib is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2 of the License, or (at your
11 // option) any later version.
12 //
13 // eclib is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 // for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with eclib; if not, write to the Free Software Foundation,
20 // Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 //
22 //////////////////////////////////////////////////////////////////////////
23 
24 /**
25  * logger.h
26  *
27  * Declarations for logger class.
28  * Verbosity levels are integers:
29  *    -1 : Error. Always print
30  *     0 : Print if verbose > 0
31  *     1 : Print if verbose > 1
32  *    ... etc ...
33  */
34 
35 #ifndef _ECLIB_LOGGER_H
36 #define _ECLIB_LOGGER_H
37 
38 // Include headers
39 #include <iostream>
40 #include <iomanip>
41 #include <cstdio>
42 #include <sstream>
43 
44 // Disable multithreading
45 // #undef ECLIB_MULTITHREAD
46 
47 #ifdef ECLIB_MULTITHREAD
48 #include <boost/thread/thread.hpp>
49 
50 // Uncomment if multithreading debug messages required
51 //#define ECLIB_MULTITHREAD_DEBUG
52 #endif
53 
54 // Logging configurations
55 #ifndef ECLOG1
56 #define ECLOG1(v) if(eclogger::level() <= (v)) ; else eclogger().stream()
57 #endif
58 #ifndef ECLOG2
59 #define ECLOG2(v) if(eclogger::level() <= (v)) ; else eclogger().stream(__FILE__,__LINE__)
60 #endif
61 
62 // Expose logging macro
63 #ifndef ECLOG
64 #define ECLOG ECLOG1
65 #endif
66 
67 class eclogger {
68   public:
69     eclogger();
70     ~eclogger();
71 
72     std::ostringstream& stream();
73     std::ostringstream& stream( const char *file, const unsigned long line );
74 
75     static int  level();
76     static void setLevel( const int verbose );
77 
78   private:
79     static int level_;
80     std::ostringstream s;
81 };
82 
83 #endif
84