testDailyRollingFileAppender.cpp
Go to the documentation of this file.
1 #include <log4cpp/Category.hh>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <iostream>
8 
9 #ifdef WIN32
10 #pragma comment(lib, "Ws2_32.lib")
11 #endif
12 
13 using namespace log4cpp;
14 using namespace std;
15 static const char* const test_message = "message";
16 static const char* const daily_file_prefix = "dailyrolling_file.log";
17 static const char* const nestedDir = "nesteddir";
18 #ifndef WIN32
19 #define PATHDELIMITER "/"
20 #else
21 #define PATHDELIMITER "\\"
22 #endif
23 const char* const nesteddirname = "nesteddir"PATHDELIMITER;
24 
25 
28 public:
29  bool remove_impl(const char* filename)
30  {
31  int res = remove(filename);
32 
33  if (res != 0 && errno != ENOENT)
34  cout << "Can't remove file '" << filename << "'.\n";
35 
36  return res == 0 || (res != 0 && errno == ENOENT);
37  }
38 
39  bool remove_files()
40  {
41 // if (!remove_impl(daily_file_prefix))
42 // return false;
43 
44  return true;
45  }
46 
47  bool setup()
48  {
49  if (!remove_files())
50  return false;
51 
52  Category& root = Category::getRoot();
53  dailyApp = new DailyRollingFileAppender("daily-rolling-appender", daily_file_prefix, 1);
54  nestedDirDailyApp = new DailyRollingFileAppender("nesteddir-daily-rolling-appender", std::string(nesteddirname).append(daily_file_prefix), 1);
55  root.addAppender(dailyApp);
56  root.addAppender(nestedDirDailyApp);
58 
59  return true;
60  }
61 
63  {
67  Category::getRoot().debugStream() << "The message before rolling over attempt";
68  dailyApp->rollOver();
69  nestedDirDailyApp->rollOver();
70  Category::getRoot().debugStream() << "The message after rolling over attempt";
73  }
74 
75  bool exists(const char* filename)
76  {
77  FILE* f = fopen(filename, "r");
78  if (f == NULL)
79  {
80  cout << "File '" << filename << "' doesn't exists.\n";
81  return false;
82  }
83 
84  fclose(f);
85 
86  return true;
87  }
88 
90  {
91  bool result = exists(daily_file_prefix);
92 
94  return result && remove_files();
95  }
96 };
97 
99  DailyRollingTest dailyTest;
100  if (!dailyTest.setup())
101  {
102  cout << "Setup has failed. Check for permissions on files " << daily_file_prefix << "*'.\n";
103  return -1;
104  }
105 
106  dailyTest.make_log_files();
107 
108  if (dailyTest.check_log_files())
109  return 0;
110  else
111  return -1;
112 }
113 
115 {
116  /* looking for the init file in $srcdir is a requirement of
117  automake's distcheck target.
118  */
119  const char* srcdir = getenv("srcdir");
120  std::string initFileName;
121  try {
122 #if defined(WIN32)
123  initFileName = "./testConfig.log4cpp.dailyroll.nt.properties";
124 #else
125  initFileName = "./testConfig.log4cpp.dailyroll.properties";
126 #endif
127  if (srcdir != NULL) {
128  initFileName = std::string(srcdir) + PATHDELIMITER + initFileName;
129  }
130 
132  } catch(log4cpp::ConfigureFailure& f) {
133  std::cout << "Configure Problem " << f.what() << "($srcdir=" << ((srcdir != NULL)?srcdir:"NULL") << ")" << std::endl;
134  return -1;
135  }
136 
138 
139  log4cpp::Category& sub1 =
140  log4cpp::Category::getInstance(std::string("sub1"));
141 
142  root.error("root error");
143  root.warn("root warn");
144  sub1.error("sub1 error");
145  sub1.warn("sub1 warn");
146 
148  return 0;
149 }
150 
151 int main()
152 {
154  if (!res)
156 
157  return res;
158 }
bool remove_files()
int testConfigDailyRollingFileAppender()
CategoryStream debugStream()
Definition: Category.hh:330
DailyRollingFileAppender * nestedDirDailyApp
void warn(const char *stringFormat,...)
Definition: Category.cpp:333
bool exists(const char *filename)
static Category & getInstance(const std::string &name)
Definition: Category.cpp:35
static void shutdown()
Definition: Category.cpp:48
int testOnlyDailyRollingFileAppender()
#define PATHDELIMITER
bool exists(const char *filename)
static Category & getRoot()
Definition: Category.cpp:23
static const char *const daily_file_prefix
static const char *const nestedDir
const char *const nesteddirname
bool remove_impl(const char *filename)
virtual void setPriority(Priority::Value priority)
Definition: Category.cpp:71
virtual void addAppender(Appender *appender)
Definition: Category.cpp:94
void error(const char *stringFormat,...)
Definition: Category.cpp:347
static const char *const test_message
static void configure(const std::string &initFileName)


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