General IRI utilities
log.h
1 // Copyright (C) 2009-2010 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
2 // Author Sergi Hernandez (shernand@iri.upc.edu)
3 // All rights reserved.
4 //
5 // This file is part of iriutils
6 // iriutils is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // at your option) any later version.
10 //
11 // This program 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
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef _LOG_H
20 #define _LOG_H
21 
22 #include <iostream>
23 #include <fstream>
24 #include <stdio.h>
25 #include <string>
26 #include <vector>
27 #include "mutex.h"
28 #include "ctime.h"
29 
50 class CLog
51 {
52  private:
62  std::fstream log_file;
63 
72  std::string filename;
80  bool enabled;
81 
90  CMutex access;
91 
100  CTime time_stamp;
101  public:
119  CLog(const std::string& filename);
120 
129  void enable(void);
130 
139  void disable(void);
140 
153  bool is_enabled(void);
154 
166  std::string get_filename(void);
167 
178  void set_time_format(ctimeformat format);
179 
190  ctimeformat get_time_format();
191 
207  template<class T>
208  void log(const T& value)
209  {
210  this->access.enter();
211 
212  this->time_stamp.set();
213  if (this->is_enabled())
214  this->log_file << "[" << this->time_stamp.getString() << "] - " << value << std::endl;
215 
216  this->access.exit();
217  }
218 
233  template<class T>
234  void log_vector(const std::vector<T>& values)
235  {
236  unsigned int i=0;
237 
238  this->access.enter();
239  this->time_stamp.set();
240  if (this->is_enabled())
241  {
242  this->log_file << "[" << this->time_stamp.getString() << "] - ";
243  for(i=0;i<values.size();i++)
244  {
245  this->log_file << values[i];
246  if(i<values.size()-1)
247  this->log_file << ",";
248  }
249  this->log_file << std::endl;
250  }
251  this->access.exit();
252  }
253 
254 
264  ~CLog();
265 };
266 
267 #endif
void log(const T &value)
Function to write a variable to the log.
Definition: log.h:208
~CLog()
Destructor.
Definition: log.cpp:93
std::string get_filename(void)
Function to get the full filename.
Definition: log.cpp:78
ctimeformat get_time_format()
Function to get the time format.
Definition: log.cpp:88
Implementation of a mutual exclusion mechanism.
Definition: mutex.h:46
void enable(void)
Function to enable the log This function enable the particular log associated with the object...
Definition: log.cpp:59
Implementation of a log file.
Definition: log.h:50
void log_vector(const std::vector< T > &values)
Function to write a vector of variable to the log.
Definition: log.h:234
std::string getString(void)
Gives a formatted string of this time.
Definition: ctime.cpp:293
void set_time_format(ctimeformat format)
Function to set the time format.
Definition: log.cpp:83
Implementation of a time class and operations.
Definition: ctime.h:75
void disable(void)
Function to disable the log This function disables the particular log associated with the object...
Definition: log.cpp:66
void exit(void)
function to release the critical section
Definition: mutex.cpp:64
void enter(void)
function to request access to the critical section
Definition: mutex.cpp:35
CLog(const std::string &filename)
Constructor.
Definition: log.cpp:35
void set(double milliseconds=-1.0)
Sets the internal time.
Definition: ctime.cpp:110
bool is_enabled(void)
Function to check if the log is enabled.
Definition: log.cpp:73