ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
Logger.cpp
Go to the documentation of this file.
1 /*
2  * Logger.cpp
3  *
4  * Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <iostream>
22 #include <new>
23 #include <cstdlib>
24 
25 #include "Logger.hpp"
26 
27 namespace onposix {
28 
29 // Definition (and initialization) of static attributes
30 Logger* Logger::m_ = 0;
31 
32 #ifdef LOG_MULTITHREAD
33 
34 PosixMutex Logger::lock_ ;
35 
36 inline void Logger::lock()
37 {
38  lock_.lock();
39 }
40 
41 inline void Logger::unlock()
42 {
43  lock_.unlock();
44 }
45 #else
46 inline void Logger::lock(){}
47 inline void Logger::unlock(){}
48 #endif
49 
50 
51 
52 
53 
54 /**
55  * \brief Constructor.
56  *
57  * It is a private constructor, called only by getInstance() and only the
58  * first time. It is called inside a lock, so lock inside this method
59  * is not required.
60  * It only initializes the initial time. All configuration is done inside the
61  * configure() method.
62  */
64  logFile_(""),
65  latestMsgPrintedOnFile_(false),
66  latestMsgPrintedOnConsole_(false)
67 {
68  gettimeofday(&initialTime_, NULL);
69 }
70 
71 /**
72  * \brief Method to configure the logger.
73  *
74  * This method is called by the LOG_FILE() macro.
75  * @param outputFile Name of the file used for logging
76  */
77 void Logger::setFile (const std::string& outputFile)
78 {
79  Logger::lock();
82 
83  // Compute a new file name, if needed
84  if (outputFile != logFile_){
85 
86  if (logFile_ != "")
87  out_.close();
88  std::ostringstream oss;
89  time_t currTime;
90  time(&currTime);
91  struct tm *currTm = localtime(&currTime);
92  oss << outputFile << "_" <<
93  currTm->tm_mday << "_" <<
94  currTm->tm_mon << "_" <<
95  (1900 + currTm->tm_year) << "_" <<
96  currTm->tm_hour << "-" <<
97  currTm->tm_min << "-" <<
98  currTm->tm_sec << ".log";
99  logFile_ = oss.str().c_str();
100  }
101 
102  // Open a new stream:
103  out_.open(logFile_.c_str(), std::ios::app);
104 
105  Logger::unlock();
106 }
107 
108 
109 
110 /**
111  * \brief Destructor.
112  *
113  * It only closes the file, if open, and cleans memory.
114  */
115 
117 {
118  Logger::lock();
119  if (logFile_ != "")
120  out_.close();
121  delete m_;
122  Logger::unlock();
123 
124 }
125 
126 /**
127  * \brief Method to get a reference to the object (i.e., Singleton)
128  *
129  * This is a static method.
130  * @return Reference to the object.
131  */
133 {
134  if (m_ == 0){
135  Logger::lock();
136  if (m_ == 0)
137  m_ = new Logger;
138  Logger::unlock();
139  }
140  return *m_;
141 }
142 
143 /**
144  * @brief Method used to print messages on console.
145  *
146  * This method is called by the DEBUG(), WARNING() and ERROR() macros.
147  * @param severitylevel Severity of the debug message
148  * @param file Source file where the method has been called (set equal to __FILE__
149  * by the DEBUG macro)
150  * @param line Number of line in the source code where the method has been
151  * called (automatically set equal to __LINE__ by the DEBUG macro)
152  * @param message Message to be logged
153  */
154 void Logger::printOnConsole(const std::string& file,
155  const int line,
156  const std::string& message)
157 {
158 
159  struct timeval currentTime;
160  gettimeofday(&currentTime, NULL);
161 
162  Logger::lock();
163 
164  std::cout <<
165  (currentTime.tv_sec - initialTime_.tv_sec) <<
166  ":" << message << "\t\t[" << file << ":" << line << "]" <<
167  std::endl;
168 
170 
171  Logger::unlock();
172 }
173 
174 
175 /**
176  * @brief Method used to print messages on file.
177  *
178  * This method is called by the DEBUG(), WARNING() and ERROR() macros.
179  * @param severitylevel Severity of the debug message
180  * @param file Source file where the method has been called (set equal to __FILE__
181  * by the DEBUG macro)
182  * @param line Number of line in the source code where the method has been
183  * called (automatically set equal to __LINE__ by the DEBUG macro)
184  * @param message Message to be logged
185  */
186 void Logger::printOnFile(const std::string& file,
187  const int line,
188  const std::string& message)
189 {
190 
191  struct timeval currentTime;
192  gettimeofday(&currentTime, NULL);
193 
194  Logger::lock();
195 
196  latestMsgPrintedOnFile_ = false;
197 
198  if (logFile_ != "") {
199  out_ <<
200  (currentTime.tv_sec - initialTime_.tv_sec) <<
201  ":" << message << "\t\t[" << file << ":" << line << "]" <<
202  std::endl;
204  }
205 
206  Logger::unlock();
207 }
208 
209 } /* onposix */