winix/winixd/templates/slog.cpp

163 lines
3.3 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.
*
*/
#include "core/slog.h"
#include "templates.h"
namespace Winix
{
namespace TemplatesFunctions
{
static size_t slog_index = 0;
static LogManipulators slog_last_type;
bool slog_is()
{
if( !cur->session )
return false;
return slog_index < cur->session->log_buffer.Size();
}
void slog_skipline()
{
if( !cur->session )
return;
TextStream<std::wstring> & buf = cur->session->log_buffer;
while( slog_index < buf.Size() && buf[slog_index]!=10 )
slog_index += 1;
if( slog_index < buf.Size() )
slog_index += 1; // skipping the '\n'
}
void slog_settype()
{
if( !slog_is() )
return;
TextStream<std::wstring> & buf = cur->session->log_buffer;
slog_last_type = static_cast<LogManipulators>(buf[slog_index]);
switch( slog_last_type )
{
case loginfo:
case logwarning:
case logerror:
slog_index += 1;
break;
default:
slog_last_type = loginfo;
}
}
void slog_tab(Info & i)
{
if( i.iter == 0 )
slog_index = 0;
else
slog_skipline();
// the type is written as the first character (after '\n' or at the beginning of the string)
slog_settype();
i.res = slog_is();
if( cur->session && !i.res )
cur->session->log_buffer.Clear(); // the session log has been printed
}
void slog_tab_is_info(Info & i)
{
if( !slog_is() )
return;
i.res = (slog_last_type == loginfo);
}
void slog_tab_is_warning(Info & i)
{
if( !slog_is() )
return;
i.res = (slog_last_type == logwarning);
}
void slog_tab_is_error(Info & i)
{
if( !slog_is() )
return;
i.res = (slog_last_type == logerror);
}
void slog_tab_print(Info & i)
{
if( !slog_is() )
return;
TextStream<std::wstring> & buf = cur->session->log_buffer;
while( slog_index < buf.Size() && buf[slog_index]!=10 )
{
i.out << buf[slog_index];
slog_index += 1;
}
// don't skip the last '\n' here
}
} // namespace TemplatesFunctions
} // namespace Winix