NetCpp  v0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
logger.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Evidence Srl - www.evidence.eu.com
3  *
4  * Boost Software License - Version 1.0 - August 17th, 2003
5  *
6  * Permission is hereby granted, free of charge, to any person or organization
7  * obtaining a copy of the software and accompanying documentation covered by
8  * this license (the "Software") to use, reproduce, display, distribute,
9  * execute, and transmit the Software, and to prepare derivative works of the
10  * Software, and to permit third-parties to whom the Software is furnished to
11  * do so, all subject to the following:
12  *
13  * The copyright notices in the Software and this entire statement, including
14  * the above license grant, this restriction and the following disclaimer,
15  * must be included in all copies of the Software, in whole or in part, and
16  * all derivative works of the Software, unless such copies or derivative
17  * works are solely in the form of machine-executable object code generated by
18  * a source language processor.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  */
28 
29 #include <iostream>
30 #include <new>
31 #include <cstdlib>
32 
33 #include "logger.hpp"
34 
35 namespace log {
36 
37 // Definition (and initialization) of static attributes
38 Logger* Logger::m_ = 0;
39 
40 #ifdef LOGGER_MULTITHREAD
41  std::mutex Logger::lock_ ;
42 
43  inline void Logger::lock()
44  {
45  lock_.lock();
46  }
47 
48  inline void Logger::unlock()
49  {
50  lock_.unlock();
51  }
52 #else
53  inline void Logger::lock(){}
54  inline void Logger::unlock(){}
55 #endif
56 
57 
58 /**
59  * @brief Constructor.
60  *
61  * It is a private constructor, called only by getInstance() and only the
62  * first time. It is called inside a lock, so lock inside this method
63  * is not required.
64  * It only initializes the initial time. All configuration is done inside the
65  * configure() method.
66  */
68  logFile_("")
69 {
70  initialTime_ = std::chrono::system_clock::now();
71 }
72 
73 /**
74  * @brief Method to configure the logger.
75  *
76  * This method is called by the LOG_FILE() macro.
77  * @param outputFile Name of the file used for logging
78  */
79 void Logger::setFile (const std::string& outputFile)
80 {
81  Logger::lock();
82 
83  // Compute the whole file name:
84  std::ostringstream oss;
85  auto now = std::chrono::system_clock::now();
86  std::time_t currTime = std::chrono::system_clock::to_time_t(now);
87  struct tm *currTm = std::localtime(&currTime);
88  oss << outputFile << "_" <<
89  (1900 + currTm->tm_year) << "-" <<
90  currTm->tm_mon << "-" <<
91  currTm->tm_mday << "_" <<
92  currTm->tm_hour << "-" <<
93  currTm->tm_min << "-" <<
94  currTm->tm_sec << ".log";
95  logFile_ = oss.str().c_str();
96 
97  // Open a new stream:
98  out_.open(logFile_.c_str(), std::ios::app);
99 
100  Logger::unlock();
101 }
102 
103 /**
104  * @brief Destructor.
105  *
106  * It only closes the file, if open, and cleans memory.
107  */
108 
110 {
111  Logger::lock();
112  if (logFile_ != "")
113  out_.close();
114  delete m_;
115  Logger::unlock();
116 
117 }
118 
119 /**
120  * @brief Method to get a reference to the object (i.e., Singleton)
121  *
122  * This is a static method.
123  * @return Reference to the object.
124  */
126 {
127  if (m_ == 0){
128  Logger::lock();
129  if (m_ == 0)
130  m_ = new Logger;
131  Logger::unlock();
132  }
133  return *m_;
134 }
135 
136 
137 /**
138  * @brief Method used to print messages.
139  *
140  * This method is called by the DEBUG(), WARNING() and ERROR() macros.
141  * @param severitylevel Severity of the debug message
142  * @param file Source file where the method has been called (set equal to __FILE__
143  * by the DEBUG macro)
144  * @param line Number of line in the source code where the method has been
145  * called (automatically set equal to __LINE__ by the DEBUG macro)
146  * @param message Message to be logged
147  */
148 void Logger::print(const std::string& file,
149  const int line,
150  const std::string& message)
151 {
152  std::chrono::time_point<std::chrono::system_clock> currentTime =
153  std::chrono::system_clock::now();
154 
155  int elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>
156  (currentTime - initialTime_).count();
157 
158  Logger::lock();
159 
160  if (logFile_ != "") {
161  out_ << "[ " << elapsed_seconds << " ] " << message <<
162  "\t[ " << file << ":" << line << "]" << std::endl;
163  }
164 
165  std::cerr << "[ " << elapsed_seconds << " ] " << message <<
166  "\t[ " << file << ":" << line << "]" << std::endl;
167 
168  Logger::unlock();
169 }
170 
171 } // log