1 // FILE logger.cc : Implementation of member functions for 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.cc
26  *
27  * Simple thread-safe logger class. Prints to stdout.
28  */
29 
30 #include "eclib/logger.h"
31 
32 int eclogger::level_ = 0;
33 
34 /**
35  * eclogger()
36  *
37  * Main constructor. Defaults verbosity to 0.
38  */
eclogger()39 eclogger::eclogger() {}
40 
41 /**
42  * ~eclogger()
43  *
44  * Destructor. Appends newline character.
45  * Uses thread-safe fprintf, and flushes.
46  */
~eclogger()47 eclogger::~eclogger() {
48   //s << std::endl;
49   fprintf( stdout, "%s", s.str().c_str() );
50   fflush( stdout );
51 }
52 
53 /**
54  * level()
55  *
56  * Returns logger verbosity level.
57  */
level()58 int eclogger::level() {
59   return level_;
60 }
61 
62 /**
63  * setLevel()
64  *
65  * Allows for global change of verbosity level.
66  */
setLevel(const int verbose)67 void eclogger::setLevel( const int verbose ) {
68   level_ = verbose;
69 }
70 
71 /**
72  * stream()
73  *
74  * Returns stream object.
75  */
stream()76 std::ostringstream& eclogger::stream() {
77 #if defined(ECLIB_MULTITHREAD) && defined(ECLIB_MULTITHREAD_DEBUG)
78   s << "Thread " << boost::this_thread::get_id() << "\t";
79 #endif
80 
81   return s;
82 }
83 
84 /**
85  * stream()
86  *
87  * Returns stream object with FILE and LINE details.
88  */
stream(const char * file,const unsigned long line)89 std::ostringstream& eclogger::stream( const char *file, const unsigned long line ) {
90   std::string filename = file;
91   s << std::setw(20) << filename << std::setw(5) << line << " ";
92   return stream();
93 }
94