NCBI C++ ToolKit
logrotate.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: logrotate.cpp 33815 2007-05-04 17:18:18Z kazimird $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Aaron Ucko, NCBI
27 *
28 * File Description:
29 * File streams supporting log rotation
30 *
31 * ===========================================================================
32 */
33 
34 #include <ncbi_pch.hpp>
35 #include <util/logrotate.hpp>
36 #include <corelib/ncbifile.hpp>
37 
39 
41 public:
42  CRotatingLogStreamBuf(CRotatingLogStream* stream, const string& filename,
43  CNcbiStreamoff limit, IOS_BASE::openmode mode);
44  CNcbiStreamoff Rotate(void); // returns number of bytes in old log
45 
46 protected:
48  virtual int sync(void);
49 
50 private:
52  string m_FileName;
54  CNcbiStreamoff m_Limit; // in bytes
55  IOS_BASE::openmode m_Mode;
56 };
57 
58 
60  const string& filename,
61  CT_OFF_TYPE limit,
62  IOS_BASE::openmode mode)
63  : m_Stream(stream),
64  m_FileName(filename),
65  m_Size(0),
66  m_Limit(limit),
67  m_Mode(mode)
68 {
69  open(m_FileName.c_str(), m_Mode);
70  m_Size = seekoff(0, IOS_BASE::cur, IOS_BASE::out);
71 }
72 
73 
75 {
76  CNcbiStreampos old_size = m_Size;
77  close();
78  string old_name = m_FileName; // Copy in case x_BackupName mutates.
79  string new_name = m_Stream->x_BackupName(m_FileName);
80  if ( !new_name.empty() ) {
81  CFile(new_name).Remove();
82  CFile(old_name).Rename(new_name);
83  }
84  open(m_FileName.c_str(), m_Mode);
85  m_Size = seekoff(0, IOS_BASE::cur, IOS_BASE::out);
86  return m_Size - old_size;
87 }
88 
89 
90 // The use of new_size in overflow and sync is to avoid
91 // double-counting when one calls the other. (Which, if either, is
92 // actually lower-level seems to vary with compiler.)
93 
95 {
96  // The only operators CNcbiStreampos reliably seems to support
97  // are += and -=, so stick to those. :-/
98  CNcbiStreampos new_size = m_Size, old_size = m_Size;
99  new_size += pptr() - pbase();
100  if ( !CT_EQ_INT_TYPE(c, CT_EOF) ) {
101  new_size += 1;
102  }
103  // Perform output first, in case switching files discards data.
104  CT_INT_TYPE result = CNcbiFilebuf::overflow(c);
105  if (m_Size - old_size < 0) {
106  return result; // assume filebuf::overflow called Rotate via sync.
107  }
108  // Don't assume the buffer's actually empty; some implementations
109  // seem to handle the case of pptr() being null by setting the
110  // pointers and writing c to the buffer but not actually flushing
111  // it to disk. :-/
112  new_size -= pptr() - pbase();
113  m_Size = new_size;
114  // Hold off on rotating logs until actually producing new output
115  // (even if they were already overdue for rotation), to avoid a
116  // possible recursive double-rotation scenario.
117  if (m_Size - CNcbiStreampos(0) >= m_Limit && m_Size != old_size) {
118  Rotate();
119  }
120  return result;
121 }
122 
123 
125 {
126  // Perform output first, in case switching files discards data.
127  CNcbiStreampos new_size = m_Size, old_size = m_Size;
128  new_size += pptr() - pbase();
129  int result = CNcbiFilebuf::sync();
130  if (m_Size - old_size < 0) {
131  return result; // assume filebuf::sync called Rotate via overflow.
132  }
133  // pptr() ought to equal pbase() now, but just in case...
134  new_size -= pptr() - pbase();
135  m_Size = new_size;
136  // Hold off on rotating logs until actually producing new output.
137  if (m_Size - CNcbiStreampos(0) >= m_Limit && m_Size != old_size) {
138  Rotate();
139  }
140  return result;
141 }
142 
143 
144 
146  CNcbiStreamoff limit, openmode mode)
147  : CNcbiOstream(new CRotatingLogStreamBuf(this, filename, limit, mode))
148 {
149 }
150 
152 {
153  delete rdbuf();
154 }
155 
156 
158 {
159  flush();
160  return dynamic_cast<CRotatingLogStreamBuf*>(rdbuf())->Rotate();
161 }
162 
163 
165 {
166 #if defined(NCBI_OS_UNIX) && !defined(NCBI_OS_CYGWIN)
167  return name + CurrentTime().AsString(".Y-M-D-Z-h:m:s");
168 #else
169  // Colons are special; avoid them.
170  return name + CurrentTime().AsString(".Y-M-D-Z-h-m-s");
171 #endif
172 }
173 
174 
CFile –.
Definition: ncbifile.hpp:1604
CRotatingLogStreamBuf(CRotatingLogStream *stream, const string &filename, CNcbiStreamoff limit, IOS_BASE::openmode mode)
Definition: logrotate.cpp:59
CNcbiStreampos m_Size
Definition: logrotate.cpp:53
CNcbiStreamoff Rotate(void)
Definition: logrotate.cpp:74
IOS_BASE::openmode m_Mode
Definition: logrotate.cpp:55
virtual CT_INT_TYPE overflow(CT_INT_TYPE c=CT_EOF)
Definition: logrotate.cpp:94
CRotatingLogStream * m_Stream
Definition: logrotate.cpp:51
CNcbiStreamoff m_Limit
Definition: logrotate.cpp:54
virtual int sync(void)
Definition: logrotate.cpp:124
A file stream variant that automatically rotates its output when it grows beyond a specified size.
Definition: logrotate.hpp:55
int close(int fd)
Definition: connection.cpp:45
std::ofstream out("events_result.xml")
main entry point for tests
bool Rename(const string &new_path, TRenameFlags flags=fRF_Default)
Rename entry.
Definition: ncbifile.cpp:2456
virtual bool Remove(TRemoveFlags flags=eRecursive) const
Remove a directory entry.
Definition: ncbifile.cpp:2595
CRotatingLogStream(const string &filename, CNcbiStreamoff limit, openmode mode=app|ate|out)
Constructor.
Definition: logrotate.cpp:145
CNcbiStreamoff Rotate(void)
Actually rotate the log.
Definition: logrotate.cpp:157
virtual string x_BackupName(string &name)
Overridable implementation of naming policy.
Definition: logrotate.cpp:164
virtual ~CRotatingLogStream(void)
Destructor.
Definition: logrotate.cpp:151
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define CT_OFF_TYPE
Definition: ncbistre.hpp:731
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
#define CT_EOF
Definition: ncbistre.hpp:732
#define CT_INT_TYPE
Definition: ncbistre.hpp:728
#define CT_EQ_INT_TYPE
Definition: ncbistre.hpp:736
IO_PREFIX::streamoff CNcbiStreamoff
Portable alias for streamoff.
Definition: ncbistre.hpp:137
IO_PREFIX::streampos CNcbiStreampos
Portable alias for streampos.
Definition: ncbistre.hpp:134
IO_PREFIX::filebuf CNcbiFilebuf
Portable alias for filebuf.
Definition: ncbistre.hpp:373
string AsString(const CTimeFormat &format=kEmptyStr, TSeconds out_tz=eCurrentTimeZone) const
Transform time to string.
Definition: ncbitime.cpp:1511
CTime CurrentTime(CTime::ETimeZone tz=CTime::eLocal, CTime::ETimeZonePrecision tzp=CTime::eTZPrecisionDefault)
Definition: ncbitime.hpp:2185
This module supplies a file stream variant that supports customizable log rotation.
mdb_mode_t mode
Definition: lmdb++.h:38
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
else result
Definition: token2.c:20
Modified on Sat Dec 02 09:23:05 2023 by modify_doxy.py rev. 669887