winix/core/slog.h

176 lines
5.0 KiB
C++

/*
* This file is a part of Winix
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2011-2014, Tomasz Sowa
* 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.
*
* 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 HOLDER 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 headerfile_winix_core_slog
#define headerfile_winix_core_slog
#include "cur.h"
#include "logmanipulators.h"
#include "templates/locale.h"
#include "textstream/textstream.h"
namespace Winix
{
#define WINIX_SLOG_MAX_LOG_SIZE 10240
/*
session logger
sample:
#include "log.h" (or slog.h)
slog << logerror << "message" << "something" << logend;
slog << logwarning << T("message_to_translate") << x << logend;
if the latter example "message_to_translate" will be taken from locales
currently following manipulators are taken into account:
loginfo - the message in a normal info
logwarning - this is a warning
logerror - this is an error
logend - end of a line -- we have one kind of a message (info, warning, error) per line
loginfo, logwarning, logerror should be specified at the beginning of a line
(other manipulators are skipped)
*/
class SLog
{
public:
SLog();
void SetCur(Cur * pcur);
void SetLocale(Locale * plocale);
template<class RawType>
struct TranslateTextHelper
{
const RawType & par;
TranslateTextHelper(const TranslateTextHelper<RawType> & p) : par(p.par) {}
TranslateTextHelper(const RawType & p) : par(p) {}
};
SLog & operator<<(const void * s);
SLog & operator<<(const char * s);
SLog & operator<<(const std::string * s);
SLog & operator<<(const std::string & s);
SLog & operator<<(const wchar_t * s);
SLog & operator<<(const std::wstring * s);
SLog & operator<<(const std::wstring & s);
SLog & operator<<(int s);
SLog & operator<<(long s);
SLog & operator<<(char s);
SLog & operator<<(wchar_t s);
SLog & operator<<(size_t s);
SLog & operator<<(double s);
SLog & operator<<(LogManipulators m);
SLog & operator<<(const PT::Date & date);
template<typename char_type, size_t stack_size, size_t heap_block_size>
SLog & operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf);
SLog & TranslateText(const char * str);
SLog & TranslateText(const wchar_t * str);
template<size_t str_size>
SLog & operator<<(const TranslateTextHelper<const char [str_size]> & raw) { return TranslateText(raw.par); }
template<size_t str_size>
SLog & operator<<(const TranslateTextHelper<const wchar_t [str_size]> & raw){ return TranslateText(raw.par); }
template<size_t str_size>
SLog & operator<<(const TranslateTextHelper<char [str_size]> & raw) { return TranslateText(raw.par); }
template<size_t str_size>
SLog & operator<<(const TranslateTextHelper<wchar_t [str_size]> & raw){ return TranslateText(raw.par); }
SLog & operator<<(const TranslateTextHelper<const char*> & raw);
SLog & operator<<(const TranslateTextHelper<const wchar_t*> & raw);
SLog & operator<<(TranslateTextHelper<const std::string*> raw);
SLog & operator<<(TranslateTextHelper<const std::wstring*> raw);
SLog & operator<<(TranslateTextHelper<std::string> raw);
SLog & operator<<(TranslateTextHelper<std::wstring> raw);
private:
template<class LogParam>
SLog & PutLog(const LogParam & par);
Cur * cur;
Locale * locale;
std::wstring key_temp;
};
template<class RawType>
SLog::TranslateTextHelper<RawType> T(const RawType & par)
{
return SLog::TranslateTextHelper<RawType>(par);
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
SLog & SLog::operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf)
{
return PutLog(buf);
}
template<class LogParam>
SLog & SLog::PutLog(const LogParam & par)
{
if( cur && cur->session )
cur->session->log_buffer << par;
return *this;
}
extern SLog slog;
} // namespace Winix
#endif