General IRI utilities
log.cpp
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 #include <string.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <time.h>
23 #include <stdio.h>
24 #include <iostream>
25 #include <fstream>
26 #include <sstream>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include "log.h"
30 #include "logexceptions.h"
31 
32 const std::string path = "./log/";
33 const std::string ext = ".log";
34 
35 CLog::CLog(const std::string& filename)
36 {
37  struct stat st;
38  int error=0;
39 
40  if (filename.empty())
41  throw CLogException(_HERE_,"Empty filename for log","");
42 
43  error=stat(path.c_str(),&st);
44  if(error!=0)
45  {
46  if(mkdir(path.c_str(),S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)!=0)
47  throw CLogException(_HERE_,"Impossible to create log folder",path);
48  }
49  this->filename=path + filename + ext;
50  this->log_file.open(this->filename.c_str(),std::ios_base::out);
51 
52  if (! this->log_file)
53  throw CLogException(_HERE_,"Log file can not be open",this->filename);
54 
55  this->enabled = true;
56  this->set_time_format(ctf_datetime);
57 }
58 
59 void CLog::enable(void)
60 {
61  this->access.enter();
62  this->enabled=true;
63  this->access.exit();
64 }
65 
66 void CLog::disable(void)
67 {
68  this->access.enter();
69  this->enabled=false;
70  this->access.exit();
71 }
72 
73 bool CLog::is_enabled(void)
74 {
75  return this->enabled;
76 }
77 
78 std::string CLog::get_filename(void)
79 {
80  return this->filename;
81 }
82 
83 void CLog::set_time_format(ctimeformat format)
84 {
85  this->time_stamp.setFormat(format);
86 }
87 
88 ctimeformat CLog::get_time_format()
89 {
90  return this->time_stamp.getFormat();
91 }
92 
94 {
95  this->access.enter();
96 
97  this->log_file.close();
98  this->enabled=false;
99  this->access.exit();
100 }
~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
Log exception class.
Definition: logexceptions.h:40
void enable(void)
Function to enable the log This function enable the particular log associated with the object...
Definition: log.cpp:59
void set_time_format(ctimeformat format)
Function to set the time format.
Definition: log.cpp:83
ctimeformat getFormat()
Gets output string format.
Definition: ctime.cpp:356
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 setFormat(ctimeformat format)
Sets output string format.
Definition: ctime.cpp:351
bool is_enabled(void)
Function to check if the log is enabled.
Definition: log.cpp:73