Files

187 lines
5.2 KiB
C++

// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
//
// This software owned by Onix Solutions Limited [OnixS] and is
// protected by copyright law and international copyright treaties.
//
// Access to and use of the software is governed by the terms of the applicable
// OnixS Software Services Agreement (the Agreement) and Customer end user license
// agreements granting a non-assignable, non-transferable and non-exclusive license
// to use the software for it's own data processing purposes under the terms defined
// in the Agreement.
//
// Except as otherwise granted within the terms of the Agreement, copying or
// reproduction of any part of this source code or associated reference material
// to any other location for further reproduction or redistribution, and any
// amendments to this copyright notice, are expressly prohibited.
//
// Any reproduction or redistribution for sale or hiring of the Software not in
// accordance with the terms of the Agreement is a violation of copyright law.
//
#pragma once
#include <OnixS/CME/MDH/String.h>
#include <OnixS/CME/MDH/Integral.h>
#include <OnixS/CME/MDH/Logger.h>
#include <OnixS/CME/MDH/Time.h>
ONIXS_CMEMDH_NAMESPACE_BEGIN
/// Base parameters affecting synchronous logging services.
ONIXS_CMEMDH_LTWT_CLASS FileLoggerSettings
{
std::string filename_;
UInt64 fileSizeLimit_;
LogSeverity::Enum severity_;
WatchService* watch_;
public:
/// Initializes parameters with default values.
FileLoggerSettings()
: filename_()
, fileSizeLimit_(2145386496)
, severity_(LogSeverity::Regular)
, watch_(&UtcWatch::service())
{
}
/// Cleans everything up.
~FileLoggerSettings()
{
}
/// Log file name.
///
/// Ignored if logging is disabled.
const std::string& filename() const
{
return filename_;
}
/// Updates log file name.
///
/// Ignored if logging is disabled.
void filename(const std::string& value)
{
filename_ = value;
}
/// Log file size limit (in bytes).
///
/// Handler detaches logged data into separate
/// file upon reaching given size limit.
///
/// @note Default value is '2145386496'.
UInt64 fileSizeLimit() const
{
return fileSizeLimit_;
}
/// Sets logFileSizeLimit. @see logFileSizeLimit.
void fileSizeLimit(UInt64 value)
{
fileSizeLimit_ = value;
}
/// Specifies whether the logger should output events
/// and which of events should be put into the log.
///
/// @note The default value is LogEntrySeverity::Regular.
LogSeverity::Enum severityLevel() const
{
return severity_;
}
/// Specifies whether logger should output its events
/// and which of events should be put into the log.
void severityLevel(LogSeverity::Enum value)
{
severity_ = value;
}
/// Watch service used by logger while
/// assigning timestamps to log entries.
WatchService& watch() const
{
return *watch_;
}
/// Watch service used by logger while
/// assigning timestamps to log entries.
void watch(WatchService& watch)
{
watch_ = &watch;
}
};
/// Makes filename for log file for the given channel.
ONIXS_CMEMDH_EXPORTED
std::string
makeLogFilename(UInt32);
ONIXS_CMEMDH_EXPORTED_CLASS_DECL(FileLogger);
/// Collection of events raised by FileLogger during its life time.
ONIXS_CMEMDH_EXPORTED_STRUCT FileLoggerListener
{
/// Invoked by FileLogger in case of issue.
///
/// FileLogger may experience issues like inability to
/// add a new entry due to insufficient disk space. Once
/// occurred, issue is reported through given callback.
virtual
void
onFileLoggerIssue(
const FileLogger&,
const Char*)
{
}
};
/// Implements logging services to put logged data into a
/// regular file. Provides additional facilities like splitting
/// logged data into chunks not exceeding specified size limit.
ONIXS_CMEMDH_EXPORTED_CLASS FileLogger : public Logger
{
// File logger machinery implementation.
ONIXS_CMEMDH_LTWT_CLASS_DECL(Workhorse);
Workhorse* workhorse_;
public:
/// Initializes logger according to given settings.
FileLogger(
const FileLoggerSettings&,
FileLoggerListener* = NULL);
/// Disposes logging machinery.
~FileLogger();
/// Configuration of given logger instance.
const
FileLoggerSettings&
settings() const;
/// Adds a new entry into the log.
///
/// Returns instance for further fulfill or NULL if
/// given severity is lower than defined by settings
/// at the moment of instance construction.
LogEntry*
addEntry(
LogSeverity::Enum);
/// Rolls over current log and starts recording a new chunk.
///
/// Detached data is stored into a file whose name is
/// suffixed with a timestamp taken at the moment of detach.
/// Timestamp is taken using watch service associated with
/// the logger using its settings.
///
/// @return Name of file containing detached data.
std::string rollover();
};
ONIXS_CMEMDH_NAMESPACE_END