File: testRollingFileAppender.cpp

package info (click to toggle)
log4cpp 1.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,444 kB
  • sloc: cpp: 6,715; sh: 3,670; ansic: 1,049; makefile: 295
file content (93 lines) | stat: -rw-r--r-- 1,968 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <log4cpp/Category.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <iostream>

using namespace log4cpp;
using namespace std;
static const char* const test_message = "message";

bool remove_impl(const char* filename)
{
   int res = remove(filename);
   
   if (res != 0 && errno != ENOENT)
      cout << "Can't remove file '" << filename << "'.\n";

   return res == 0 || (res != 0 && errno == ENOENT);
}

bool remove_files()
{
   if (!remove_impl("rolling_file.log"))
      return false;
   if (!remove_impl("rolling_file.log.1"))
      return false;
   if (!remove_impl("rolling_file.log.2"))
      return false;

   return true;
}

bool setup()
{
   if (!remove_files())
      return false;

   Category& root = Category::getRoot();
   root.addAppender(new RollingFileAppender("rolling-appender", "rolling_file.log", 40, 3));
   root.setPriority(Priority::DEBUG);

   return true;
}

void make_log_files()
{
   Category::getRoot().debugStream() << test_message << 1;
   Category::getRoot().debugStream() << test_message << 2;
   Category::getRoot().debugStream() << test_message << 3;
   Category::getRoot().debugStream() << test_message << 4;
   Category::getRoot().debugStream() << test_message << 5;
}

bool exists(const char* filename)
{
   FILE* f = fopen(filename, "r");
   if (f == NULL)
   {
      cout << "File '" << filename << "' doesn't exists.\n"; 
      return false;
   }

   fclose(f);

   return true;
}

bool check_log_files()
{
   bool result = exists("rolling_file.log") &&
                 exists("rolling_file.log.1") &&
                 exists("rolling_file.log.2");

   Category::shutdown();
   return result && remove_files();
}

int main()
{
   if (!setup())
   {
      cout << "Setup has failed. Check for permissions on files 'rolling_file.log*'.\n";
      return -1;
   }

   make_log_files();

   if (check_log_files())
      return 0;
   else 
      return -1;
}