Files

467 lines
8.4 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/Decimal.h>
#include <OnixS/CME/MDH/Time.h>
#include <OnixS/CME/MDH/Logger.h>
ONIXS_CMEMDH_NAMESPACE_BEGIN
/// By default, logging machinery provides access
/// to the entry with ability to store arbitrary text.
/// Given class adds basic information about time of
/// entry record, category and source of log entry.
/// It also enhances API with ability to put data into
/// log entries in simple and elegant way by using
/// streaming operators.
ONIXS_CMEMDH_LTWT_CLASS LogEvent
{
LogEntry& entry_;
std::string& message_;
// Object copying is not assumed.
LogEvent(const LogEvent&);
LogEvent& operator =(const LogEvent&);
public:
/// Initializes event with LogEntry instance.
LogEvent(
LogEntry& entry,
LogCategory::Enum category,
const StrRef& source)
: entry_(entry)
, message_(entry.message())
{
entry_.category(category);
entry_.source(source);
}
/// Commits entry to the log.
~LogEvent()
{
entry_.commit();
}
/// Informational message to be added to a log.
std::string& message()
{
return message_;
}
/// Informational message to be added to a log.
const std::string& message() const
{
return message_;
}
};
// A few descendants for a particular purposes.
/// Specializes LogEvent to log errors.
ONIXS_CMEMDH_LTWT_STRUCT LogError : LogEvent
{
/// Initializes event as error.
LogError(
LogEntry& entry,
const Char* source)
: LogEvent(
entry,
LogCategory::Error,
toStrRef(source))
{
}
/// Initializes event as error.
LogError(
LogEntry& entry,
const StrRef& source)
: LogEvent(
entry,
LogCategory::Error,
source)
{
}
/// Initializes event as error.
LogError(
LogEntry& entry,
const std::string& source)
: LogEvent(
entry,
LogCategory::Error,
toStrRef(source))
{
}
};
/// Specializes LogEvent to log warnings.
ONIXS_CMEMDH_LTWT_STRUCT LogWarning : LogEvent
{
/// Initializes event as warning.
LogWarning(
LogEntry& entry,
const Char* source)
: LogEvent(
entry,
LogCategory::Warning,
toStrRef(source))
{
}
/// Initializes event as warning.
LogWarning(
LogEntry& entry,
const StrRef& source)
: LogEvent(
entry,
LogCategory::Warning,
source)
{
}
/// Initializes event as warning.
LogWarning(
LogEntry& entry,
const std::string& source)
: LogEvent(
entry,
LogCategory::Warning,
toStrRef(source))
{
}
};
/// Specializes LogEvent to log informations.
ONIXS_CMEMDH_LTWT_STRUCT LogInfo : LogEvent
{
/// Initializes event as information.
LogInfo(
LogEntry& entry,
const Char* source)
: LogEvent(
entry,
LogCategory::Info,
toStrRef(source))
{
}
/// Initializes event as information.
LogInfo(
LogEntry& entry,
const StrRef& source)
: LogEvent(
entry,
LogCategory::Info,
source)
{
}
/// Initializes event as information.
LogInfo(
LogEntry& entry,
const std::string& source)
: LogEvent(
entry,
LogCategory::Info,
toStrRef(source))
{
}
};
/// Specializes LogEvent to log debug information.
ONIXS_CMEMDH_LTWT_STRUCT LogDebug : LogEvent
{
/// Initializes event as debug information.
LogDebug(
LogEntry& entry,
const Char* source)
: LogEvent(
entry,
LogCategory::Debug,
toStrRef(source))
{
}
/// Initializes event as debug information.
LogDebug(
LogEntry& entry,
const StrRef& source)
: LogEvent(
entry,
LogCategory::Debug,
source)
{
}
/// Initializes event as debug information.
LogDebug(
LogEntry& entry,
const std::string& source)
: LogEvent(
entry,
LogCategory::Debug,
toStrRef(source))
{
}
};
// Sugar-coat for adding basic type data to log entry.
/// Prevents data of unrecognized type to be
/// logged without properly defined serialization.
template
<
typename Undefined
>
LogEvent&
operator <<(
LogEvent& event,
Undefined undefined)
{
struct
DefineOperatorForGivenType
{}
var = undefined;
return event;
}
/// Logs zero-terminated string.
inline
LogEvent&
operator <<(
LogEvent& event,
const Char* cStr)
{
event.message() += cStr;
return event;
}
/// Logs standard string.
inline
LogEvent&
operator <<(
LogEvent& event,
const std::string& str)
{
event.message() += str;
return event;
}
/// Logs string by its reference.
inline
LogEvent&
operator <<(
LogEvent& event,
const StrRef& strRef)
{
toStr(
event.message(),
strRef);
return event;
}
/// Logs single character.
inline
LogEvent&
operator <<(
LogEvent& event,
Char character)
{
event.message() += character;
return event;
}
/// Logs an integer.
inline
LogEvent&
operator <<(
LogEvent& event,
Int8 number)
{
toStr(
event.message(),
number);
return event;
}
/// Logs an integer.
inline
LogEvent&
operator <<(
LogEvent& event,
UInt8 number)
{
toStr(
event.message(),
number);
return event;
}
/// Logs an integer.
inline
LogEvent&
operator <<(
LogEvent& event,
Int16 number)
{
toStr(
event.message(),
number);
return event;
}
/// Logs an integer.
inline
LogEvent&
operator <<(
LogEvent& event,
UInt16 number)
{
toStr(
event.message(),
number);
return event;
}
/// Logs an integer.
inline
LogEvent&
operator <<(
LogEvent& event,
Int32 number)
{
toStr(
event.message(),
number);
return event;
}
/// Logs an integer.
inline
LogEvent&
operator <<(
LogEvent& event,
UInt32 number)
{
toStr(
event.message(),
number);
return event;
}
/// Logs an integer.
inline
LogEvent&
operator <<(
LogEvent& event,
Int64 number)
{
toStr(
event.message(),
number);
return event;
}
/// Logs an integer.
inline
LogEvent&
operator <<(
LogEvent& event,
UInt64 number)
{
toStr(
event.message(),
number);
return event;
}
/// Logs decimal number.
template
<
class Mantissa,
class Exponent
>
inline
LogEvent&
operator <<(
LogEvent& event,
const
FixedPointDecimal
<
Mantissa,
Exponent
>& number)
{
toStr(
event.message(),
number);
return event;
}
// Logs timestamp.
inline
LogEvent&
operator <<(
LogEvent& event,
const Timestamp& timestamp)
{
toStr(
event.message(),
timestamp);
return event;
}
// Logs time span.
inline
LogEvent&
operator <<(
LogEvent& event,
const TimeSpan& timeSpan)
{
toStr(
event.message(),
timeSpan);
return event;
}
ONIXS_CMEMDH_NAMESPACE_END