rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-logger.cc
Go to the documentation of this file.
1 #include "config.h"
2 
3 #include <iostream>
4 #include <fstream>
5 #include <string>
6 #include <cassert>
7 
8 #ifdef HAVE_UNISTD_H
9 #include <unistd.h>
10 #endif
11 
12 #include <errno.h>
13 
14 #include "asserts.h"
15 #include "types.h"
16 #include "error.h"
17 #include "estring.h"
18 #include "fs.h"
19 #include "tstamp.h"
20 #include "rconfig.h"
21 #include "timer.h"
22 
23 #include "logger.h"
24 
25 // #define ERR_OUT(e) std::cerr << e
26 #define ERR_OUT(e)
27 
28 #define TEST(code,test) \
29  thrown = false; \
30  try { \
31  code; \
32  } \
33  catch(error e) { \
34  ERR_OUT(e); \
35  thrown = true; \
36  } \
37  catch(...) { \
38  assert(0); \
39  }
40 
41 
42 bool make_dir(const std::string& path)
43 {
44  bool thrown;
45 
46  thrown = false;
47  try {
48  mk_dir(path);
49  }
50  catch(...) {
51  thrown = true;
52  }
53 
54  return(thrown == false);
55 }
56 
57 void cleanup(void)
58 {
59  assert(system("rm -fr ./test-logger.dir") == 0);
60 }
61 
62 void setup(void)
63 {
64  std::ofstream out;
65 
66  assert(make_dir("./test-logger.dir"));
67  assert(make_dir("./test-logger.dir/log.dir"));
68  assert(make_dir("./test-logger.dir/vault.dir"));
69 
70  out.open("./test-logger.dir/rvm.conf");
71  assert(out.is_open());
72  out << "log-dir ./test-logger.dir/log.dir" << std::endl;
73  out << "vault ./test-logger.dir/vault.dir" << std::endl;
74  out << "<job>" << std::endl;
75  out << " archive-path pathname" << std::endl;
76  out << " rsync-connection-type local" << std::endl;
77  out << " path /var/spool" << std::endl;
78  out << "</job>" << std::endl;
79  out.close();
80 }
81 
82 const bool same(std::string s1, std::string s2)
83 {
84  s1[11] = 'x'; s2[11] = 'x';
85  s1[12] = 'x'; s2[12] = 'x';
86  s1[14] = 'x'; s2[14] = 'x';
87  s1[15] = 'x'; s2[15] = 'x';
88  s1[17] = 'x'; s2[17] = 'x';
89  s1[18] = 'x'; s2[18] = 'x';
90 
91  if (s1 == s2) {
92  return(true);
93  }
94  return(false);
95 }
96 
97 void test(void)
98 {
99  char const * argv[256] = { 0 };
100  int argc = 0;
101  bool thrown;
102 
103  thrown = false;
104  TEST(
105  logger.init(),
106  assert(e.num() == 0);
107  assert(!e.internal());
108  assert(e.size() == 1);
109  assert(e[0].what() == "Configuration manager is not initialized");
110  );
111  assert(thrown);
112 
113  argv[argc++] = "<program>";
114  argv[argc++] = "--archive";
115  config.default_file("./test-logger.dir/rvm.conf");
116  config.init(argc, argv);
117 
118  logger.clear();
119  TEST(logger.init(),);
120  assert(!thrown);
121 
122  logger.write("Testing... ");
123  logger.write("1... ");
124  logger.write("2... ");
125  logger.write("3... ");
126  logger.write("\n");
127  logger.write("Testing...\n");
128  logger.write("1...\n",1);
129  logger.write("2...\n",2);
130  logger.write("3...\n",3);
131  logger.write("Testing...\n1...\n2...\n3...\n",1);
132  logger.write("PID is ");
133  logger.write(estring(static_cast<unsigned long>(pid())));
134  logger.write("\n");
135  logger.clear();
136 
137  std::ifstream in;
138  std::string filename;
139  std::string str;
140  char line[1024];
141 
142  filename = "./test-logger.dir/log.dir/";
143  filename += config.timestamp().str();
144  filename += ".log";
145  in.open(filename.c_str());
146  assert(in.is_open());
147 
148  in.get(line,1024);
149  str = stamp(pid());
150  str += "Rsync Vault Manager - ";
151  str += estring(VERSION);
152  assert(same(str,line));
153 
154  in.ignore();
155 
156  in.get(line,1024);
157  str = stamp(pid());
158  str += "Testing... 1... 2... 3... ";
159  assert(same(str,line));
160 
161  in.ignore();
162 
163  in.get(line,1024);
164  str = stamp(pid());
165  str += "Testing...";
166  assert(same(str,line));
167 
168  in.ignore();
169 
170  in.get(line,1024);
171  str = stamp(pid());
172  str += " 1...";
173  assert(same(str,line));
174 
175  in.ignore();
176 
177  in.get(line,1024);
178  str = stamp(pid());
179  str += " 2...";
180  assert(same(str,line));
181 
182  in.ignore();
183 
184  in.get(line,1024);
185  str = stamp(pid());
186  str += " 3...";
187  assert(same(str,line));
188 
189  in.ignore();
190 
191  in.get(line,1024);
192  str = stamp(pid());
193  str += " Testing...";
194  assert(same(str,line));
195 
196  in.ignore();
197 
198  in.get(line,1024);
199  str = stamp(pid());
200  str += " 1...";
201  assert(same(str,line));
202 
203  in.ignore();
204 
205  in.get(line,1024);
206  str = stamp(pid());
207  str += " 2...";
208  assert(same(str,line));
209 
210  in.ignore();
211 
212  in.get(line,1024);
213  str = stamp(pid());
214  str += " 3...";
215  assert(same(str,line));
216 
217  in.ignore();
218 
219  in.get(line,1024);
220  str = stamp(pid());
221  str += "PID is ";
222  str += estring(static_cast<unsigned long>(pid()));
223  assert(same(str,line));
224 
225  in.close();
226 }
227 
228 int main(int argc, char const * argv[])
229 {
230  cleanup();
231  setup();
232  try {
233  test();
234  }
235  catch(error e) {
236  std::cerr << e;
237  assert(0);
238  }
239  catch(...) {
240  std::cerr << err_unknown;
241  assert(0);
242  }
243  cleanup();
244  return(0);
245 }
246 
void init(int argc, char const *argv[])
Initialize the configuration manager from rvm's command line options.
Definition: rconfig.cc:1218
const pid_t pid(void)
Return the PID of this process.
Definition: fs.cc:162
void mk_dir(const std::string &a_path)
Create a directory.
Definition: fs.cc:599
log_manager logger
The global log manager.
Definition: logger.cc:138
Basic types definitions and templates.
void cleanup(void)
Definition: test-logger.cc:57
An extended string class.
Definition: estring.h:52
void setup(void)
Definition: test-logger.cc:62
#define VERSION
Definition: config.h:214
void default_file(const std::string &a_path)
Set the default configuration filename.
Definition: rconfig.cc:1484
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 class timestamp & timestamp(void) const
Return the timestamp of this instance of rvm.
Definition: rconfig.cc:1505
std::string stamp(const pid_t a_pid, const int a_indention)
Generate a timstamp string.
Definition: timer.cc:552
#define err_unknown
Definition: error.h:114
void test(void)
Definition: test-logger.cc:97
An error class.
Definition: error.h:72
void clear(void)
Clear the log manager.
Definition: logger.cc:32
void init(void)
Initialize the log manager.
Definition: logger.cc:42
configuration_manager config
The global configuration manager instance.
Definition: rconfig.cc:3364
const bool same(std::string s1, std::string s2)
Definition: test-logger.cc:82
#define TEST(code, test)
Definition: test-logger.cc:28
int main(int argc, char const *argv[])
Definition: test-logger.cc:228
bool make_dir(const std::string &path)
Definition: test-logger.cc:42
const std::string str(void) const
Generate a string.
Definition: tstamp.cc:387