📄 filechannel.h
字号:
//
// FileChannel.h
//
// $Id: //poco/Main/Foundation/include/Foundation/FileChannel.h#5 $
//
// Definition of the FileChannel class.
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#ifndef Foundation_FileChannel_INCLUDED
#define Foundation_FileChannel_INCLUDED
#ifndef Foundation_Foundation_INCLUDED
#include "Foundation/Foundation.h"
#endif
#ifndef Foundation_Channel_INCLUDED
#include "Foundation/Channel.h"
#endif
#ifndef Foundation_Timestamp_INCLUDED
#include "Foundation/Timestamp.h"
#endif
#ifndef Foundation_Mutex_INCLUDED
#include "Foundation/Mutex.h"
#endif
Foundation_BEGIN
class LogFile;
class RotateStrategy;
class ArchiveStrategy;
class Foundation_API FileChannel: public Channel
/// A Channel that writes to a file.
///
/// Only the message's text is written, followed
/// by a newline.
///
/// Chain this channel to a FormattingChannel with an
/// appropriate Formatter to control what is in the text.
///
/// The FileChannel support log file rotation based
/// on log file size or time intervals.
///
/// The rotation strategy can be specified with the
/// "rotation" property, which can take one of the
/// follwing values:
///
/// * never: no log rotation
/// * daily: the file is rotated daily
/// * weekly: the file is rotated every seven days
/// * monthly: the file is rotated every 30 days
/// * <n> hours: the file is rotated every <n> hours, where
/// <n> is an integer greater than zero.
/// * <n> days: the file is rotated every <n> days, where
/// <n> is an integer greater than zero.
/// * <n> weeks: the file is rotated every <n> weeks, where
/// <n> is an integer greater than zero.
/// * <n> months: the file is rotated every <n> months, where
/// <n> is an integer greater than zero and
/// a month has 30 days.
/// * <n>: the file is rotated when its size exceeds
/// <n> bytes.
/// * <n> K: the file is rotated when its size exceeds
/// <n> Kilobytes.
/// * <n> M: the file is rotated when its size exceeds
/// <n> Megabytes.
///
/// Using the "archive" property it is possible to specify
/// how archived log files are named. The following values
/// for the "archive" property are supported:
///
/// * number: A number, starting with 0, is appended to
/// the name of archived log files. The newest
/// archived log file always has the number 0.
/// For example, if the log file is named
/// "access.log", and it fulfils the criteria
/// for rotation, the file is renamed to
/// "access.log.0". If a file named "access.log.0"
/// already exists, it is renamed to "access.log.1",
/// and so on.
/// * timestamp: A timestamp is appended to the log file name.
/// For example, if the log file is named
/// "access.log", and it fulfils the criteria
/// for rotation, the file is renamed to
/// "access.log.20050802110300".
{
public:
FileChannel();
/// Creates the FileChannel.
FileChannel(const std::string& path);
/// Creates the FileChannel for a file with the given path.
void open();
/// Opens the FileChannel and creates the log file if necessary.
void close();
/// Closes the FileChannel.
void log(const Message& msg);
/// Logs the given message to the file.
void setProperty(const std::string& name, const std::string& value);
/// Sets the property with the given name.
///
/// The following properties are supported:
/// * path: The log file's path.
/// * rotation: The log file's rotation mode. See the
/// FileChannel class for details.
/// * archive: The log file's archive mode. See the
/// FileChannel class for details.
std::string getProperty(const std::string& name) const;
/// Returns the value of the property with the given name.
/// See setProperty() for a description of the supported
/// properties.
Timestamp creationDate() const;
/// Returns the log file's creation date.
UInt64 size() const;
/// Returns the log file's current size in bytes.
const std::string& path() const;
/// Returns the log file's path.
static const std::string PROP_PATH;
static const std::string PROP_ROTATION;
static const std::string PROP_ARCHIVE;
protected:
~FileChannel();
void setRotation(const std::string& rotation);
void setArchive(const std::string& archive);
private:
std::string _path;
std::string _rotation;
std::string _archive;
LogFile* _pFile;
RotateStrategy* _pRotateStrategy;
ArchiveStrategy* _pArchiveStrategy;
FastMutex _mutex;
};
Foundation_END
#endif // Foundation_FileChannel_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -