rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
logger.cc
Go to the documentation of this file.
1 #include "config.h"
2 
3 #ifdef HAVE_UNISTD_H
4 #include <unistd.h>
5 #endif
6 
7 #include <iostream>
8 #include <string>
9 #include <fstream>
10 
11 #include "asserts.h"
12 #include "error.h"
13 #include "timer.h"
14 #include "rconfig.h"
15 #include "estring.h"
16 
17 #include "logger.h"
18 
19 //-----------------------------------------------------------------------------
20 
21 /** C'tor */
23 {
24  if (this != &logger)
25  throw(
26  INTERNAL_ERROR(0,"Attempt to allocate multiple log managers")
27  );
28  clear();
29 }
30 
31 /** Clear the log manager */
33 {
34  m_new_line = true;
35  if (m_out.is_open())
36  m_out.close();
38  m_initialized = false;
39 }
40 
41 /** Initialize the log manager */
43 {
44  std::string es;
45  std::string filename;
46  bool done = false;
47  int count = 0;
48  estring str;
49 
50 
51  done = false;
52  while (!done) {
53  TRY_nomem(filename = config.log_dir());
54  TRY_nomem(filename += "/");
55  TRY_nomem(filename += config.timestamp().str());
57  TRY_nomem(filename += ".log");
58  }
60  TRY_nomem(filename += ".relink");
61  }
62  if (count != 0) {
63  TRY_nomem(filename += ".");
64  TRY_nomem(filename += estring(count));
65  }
66  if (exists(filename))
67  ++count;
68  else
69  done = true;
70  }
71  m_out.open(filename.c_str());
72  if (!m_out.is_open()) {
73  TRY_nomem(es = "Could not open log file: \"");
74  TRY_nomem(es += filename);
75  TRY_nomem(es += "\"");
76  throw(ERROR(errno,es));
77  }
78 
80 
81  m_initialized = true;
82 
83  str = "Rsync Vault Manager - ";
84  str += VERSION;
85  str += '\n';
86  logger.write(str);
87 }
88 
89 /** The initialized state of the log manager */
90 const bool log_manager::initialized(void) const
91 {
92  return(m_initialized);
93 }
94 
95 /** Write a string to the log file */
97  const std::string& a_str,
98  const uint16 a_indention,
99  const configuration_manager::logging_type a_logging_level,
100  const pid_t a_pid
101  )
102 {
103  std::string::const_iterator csi;
104 
105  if (!initialized())
106  throw(INTERNAL_ERROR(0,"Log manager is not initialized"));
107 
109  if (a_logging_level > config.error_logging_level())
110  return;
111  }
112  else {
113  if (a_logging_level > config.logging_level())
114  return;
115  }
116 
117  for (csi = a_str.begin(); csi != a_str.end(); ++csi) {
118  if (m_new_line) {
119  m_out << stamp(a_pid, a_indention);
120  m_new_line = false;
121  }
122  m_out << *csi;
123  if (*csi == '\n')
124  m_new_line = true;
125  }
126  m_out.flush();
127 }
128 
129 /** Use error-logging-level instead of logging-level */
131 {
133 }
134 
135 //-----------------------------------------------------------------------------
136 
137 /** The global log manager */
139 
140 //-----------------------------------------------------------------------------
141 
log_manager logger
The global log manager.
Definition: logger.cc:138
bool m_use_error_logging_level
Definition: logger.h:39
std::ofstream m_out
Definition: logger.h:37
An extended string class.
Definition: estring.h:52
void set_error_logging(bool a_b)
Use error-logging-level instead of logging-level.
Definition: logger.cc:130
#define VERSION
Definition: config.h:214
void write(const std::string &a_str, const uint16 a_indention=0, const configuration_manager::logging_type a_logging_level=configuration_manager::logging_manager, const pid_t a_pid=pid())
Write a string to the log file.
Definition: logger.cc:96
const bool initialized(void) const
The initialized state of the log manager.
Definition: logger.cc:90
const class timestamp & timestamp(void) const
Return the timestamp of this instance of rvm.
Definition: rconfig.cc:1505
const std::string & log_dir(void) const
Return the log-dir path.
Definition: rconfig.cc:1523
const action_type action(void) const
Return the action rvm is to take.
Definition: rconfig.cc:1466
std::string stamp(const pid_t a_pid, const int a_indention)
Generate a timstamp string.
Definition: timer.cc:552
Write information to a log file.
Definition: logger.h:17
bool exists(const std::string &a_path)
Return true if the file or directory exists.
Definition: fs.cc:385
#define TRY_nomem(code)
Definition: error.h:144
void clear(void)
Clear the log manager.
Definition: logger.cc:32
void init(void)
Initialize the log manager.
Definition: logger.cc:42
#define INTERNAL_ERROR(e, s)
Definition: error.h:123
bool m_new_line
Definition: logger.h:36
configuration_manager config
The global configuration manager instance.
Definition: rconfig.cc:3364
bool m_initialized
Definition: logger.h:38
const std::string str(void) const
Generate a string.
Definition: tstamp.cc:387
#define ERROR(e, s)
Definition: error.h:120
log_manager()
C'tor.
Definition: logger.cc:22
const logging_type & error_logging_level(void) const
Return the error-logging-level.
Definition: rconfig.cc:1685
const logging_type & logging_level(void) const
Return the logging-level.
Definition: rconfig.cc:1676