source: XMLIO_V2/external/include/Poco/ArchiveStrategy.h @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1//
2// ArchiveStrategy.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/ArchiveStrategy.h#5 $
5//
6// Library: Foundation
7// Package: Logging
8// Module:  FileChannel
9//
10// Definition of the ArchiveStrategy class and subclasses.
11//
12// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
13// and Contributors.
14//
15// Permission is hereby granted, free of charge, to any person or organization
16// obtaining a copy of the software and accompanying documentation covered by
17// this license (the "Software") to use, reproduce, display, distribute,
18// execute, and transmit the Software, and to prepare derivative works of the
19// Software, and to permit third-parties to whom the Software is furnished to
20// do so, all subject to the following:
21//
22// The copyright notices in the Software and this entire statement, including
23// the above license grant, this restriction and the following disclaimer,
24// must be included in all copies of the Software, in whole or in part, and
25// all derivative works of the Software, unless such copies or derivative
26// works are solely in the form of machine-executable object code generated by
27// a source language processor.
28//
29// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35// DEALINGS IN THE SOFTWARE.
36//
37
38
39#ifndef Foundation_ArchiveStrategy_INCLUDED
40#define Foundation_ArchiveStrategy_INCLUDED
41
42
43#include "Poco/Foundation.h"
44#include "Poco/LogFile.h"
45#include "Poco/File.h"
46#include "Poco/DateTimeFormatter.h"
47#include "Poco/NumberFormatter.h"
48
49
50namespace Poco {
51
52
53class ArchiveCompressor;
54
55
56class Foundation_API ArchiveStrategy
57        /// The ArchiveStrategy is used by FileChannel
58        /// to rename a rotated log file for archiving.
59        ///
60        /// Archived files can be automatically compressed,
61        /// using the gzip file format.
62{
63public:
64        ArchiveStrategy();
65        virtual ~ArchiveStrategy();
66
67        virtual LogFile* archive(LogFile* pFile) = 0;
68                /// Renames the given log file for archiving
69                /// and creates and returns a new log file.
70                /// The given LogFile object is deleted.
71
72        void compress(bool flag = true);
73                /// Enables or disables compression of archived files. 
74
75protected:
76        void moveFile(const std::string& oldName, const std::string& newName);
77        bool exists(const std::string& name);
78       
79private:
80        ArchiveStrategy(const ArchiveStrategy&);
81        ArchiveStrategy& operator = (const ArchiveStrategy&);
82       
83        bool _compress;
84        ArchiveCompressor* _pCompressor;
85};
86
87
88class Foundation_API ArchiveByNumberStrategy: public ArchiveStrategy
89        /// A monotonic increasing number is appended to the
90        /// log file name. The most recent archived file
91        /// always has the number zero.
92{
93public:
94        ArchiveByNumberStrategy();
95        ~ArchiveByNumberStrategy();
96        LogFile* archive(LogFile* pFile);
97};
98
99
100template <class DT>
101class ArchiveByTimestampStrategy: public ArchiveStrategy
102        /// A timestamp (YYYYMMDDhhmmssiii) is appended to archived
103        /// log files.
104{
105public:
106        ArchiveByTimestampStrategy()
107        {
108        }
109       
110        ~ArchiveByTimestampStrategy()
111        {
112        }
113       
114        LogFile* archive(LogFile* pFile)
115                /// Archives the file by appending the current timestamp to the
116                /// file name. If the new file name exists, additionally a monotonic
117                /// increasing number is appended to the log file name.
118        {
119                std::string path = pFile->path();
120                delete pFile;
121                std::string archPath = path;
122                archPath.append(".");
123                DateTimeFormatter::append(archPath, DT().timestamp(), "%Y%m%d%H%M%S%i");
124               
125                if (exists(archPath)) archiveByNumber(archPath);
126                else moveFile(path, archPath);
127
128                return new LogFile(path);
129        }
130
131private:
132        void archiveByNumber(const std::string& basePath)
133                /// A monotonic increasing number is appended to the
134                /// log file name. The most recent archived file
135                /// always has the number zero.
136        {
137                int n = -1;
138                std::string path;
139                do
140                {
141                        path = basePath;
142                        path.append(".");
143                        NumberFormatter::append(path, ++n);
144                }
145                while (exists(path));
146               
147                while (n >= 0)
148                {
149                        std::string oldPath = basePath;
150                        if (n > 0)
151                        {
152                                oldPath.append(".");
153                                NumberFormatter::append(oldPath, n - 1);
154                        }
155                        std::string newPath = basePath;
156                        newPath.append(".");
157                        NumberFormatter::append(newPath, n);
158                        moveFile(oldPath, newPath);
159                        --n;
160                }
161        }
162};
163
164
165} // namespace Poco
166
167
168#endif // Foundation_ArchiveStrategy_INCLUDED
Note: See TracBrowser for help on using the repository browser.