testRollingFileAppender.cpp
Go to the documentation of this file.
1 #include <log4cpp/Category.hh>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <iostream>
7 
8 using namespace log4cpp;
9 using namespace std;
10 static const char* const test_message = "message";
11 
12 bool remove_impl(const char* filename)
13 {
14  int res = remove(filename);
15 
16  if (res != 0 && errno != ENOENT)
17  cout << "Can't remove file '" << filename << "'.\n";
18 
19  return res == 0 || (res != 0 && errno == ENOENT);
20 }
21 
23 {
24  if (!remove_impl("rolling_file.log"))
25  return false;
26  if (!remove_impl("rolling_file.log.1"))
27  return false;
28  if (!remove_impl("rolling_file.log.2"))
29  return false;
30 
31  return true;
32 }
33 
34 bool setup()
35 {
36  if (!remove_files())
37  return false;
38 
39  Category& root = Category::getRoot();
40  root.addAppender(new RollingFileAppender("rolling-appender", "rolling_file.log", 40, 3));
42 
43  return true;
44 }
45 
47 {
53 }
54 
55 bool exists(const char* filename)
56 {
57  FILE* f = fopen(filename, "r");
58  if (f == NULL)
59  {
60  cout << "File '" << filename << "' doesn't exists.\n";
61  return false;
62  }
63 
64  fclose(f);
65 
66  return true;
67 }
68 
70 {
71  bool result = exists("rolling_file.log") &&
72  exists("rolling_file.log.1") &&
73  exists("rolling_file.log.2");
74 
76  return result && remove_files();
77 }
78 
79 int main()
80 {
81  if (!setup())
82  {
83  cout << "Setup has failed. Check for permissions on files 'rolling_file.log*'.\n";
84  return -1;
85  }
86 
88 
89  if (check_log_files())
90  return 0;
91  else
92  return -1;
93 }
bool remove_files()
CategoryStream debugStream()
Definition: Category.hh:330
static void shutdown()
Definition: Category.cpp:48
void make_log_files()
bool exists(const char *filename)
static Category & getRoot()
Definition: Category.cpp:23
static const char *const test_message
virtual void setPriority(Priority::Value priority)
Definition: Category.cpp:71
bool remove_impl(const char *filename)
bool check_log_files()
virtual void addAppender(Appender *appender)
Definition: Category.cpp:94
bool setup()


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:10:00