2018-04-23 16:28:22 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of PikoTools
|
|
|
|
* and is distributed under the (new) BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2018, 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:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * 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.
|
|
|
|
*
|
|
|
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
|
|
|
* project may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
#ifndef headerfile_picotools_log_log
|
|
|
|
#define headerfile_picotools_log_log
|
2018-04-23 16:28:22 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include "textstream/textstream.h"
|
2018-11-23 18:07:52 +01:00
|
|
|
#include "filelog.h"
|
2018-04-23 16:28:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace PT
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
class Log
|
2018-04-23 16:28:22 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
log1 - the first level
|
|
|
|
log2
|
|
|
|
log3
|
|
|
|
log4 - the last level (debug level)
|
|
|
|
logend - the end of a line
|
|
|
|
logsave - current log buffer is saved and cleared
|
|
|
|
*/
|
|
|
|
enum Manipulators
|
|
|
|
{
|
|
|
|
log1,
|
|
|
|
log2,
|
|
|
|
log3,
|
|
|
|
log4,
|
|
|
|
|
|
|
|
l1,
|
|
|
|
l2,
|
|
|
|
l3,
|
|
|
|
l4,
|
|
|
|
|
|
|
|
logend,
|
|
|
|
lend,
|
|
|
|
|
|
|
|
logsave,
|
|
|
|
lsave,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
Log();
|
|
|
|
virtual ~Log();
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
virtual void SetLogBuffer(PT::WTextStream * buffer);
|
|
|
|
virtual PT::WTextStream * GetLogBuffer();
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
void SetFileLog(FileLog * file_log);
|
|
|
|
FileLog * GetFileLog();
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
void SetMaxBufferLength(size_t max_buffer_length);
|
|
|
|
size_t GetMaxBufferLength();
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2019-01-28 19:06:18 +01:00
|
|
|
virtual Log & IntMinWidth(size_t min_width);
|
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
virtual Log & operator<<(const void * s);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
virtual Log & operator<<(const char * s);
|
|
|
|
virtual Log & operator<<(const std::string * s);
|
|
|
|
virtual Log & operator<<(const std::string & s);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
virtual Log & operator<<(const wchar_t * s);
|
|
|
|
virtual Log & operator<<(const std::wstring * s);
|
|
|
|
virtual Log & operator<<(const std::wstring & s);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
virtual Log & operator<<(char s);
|
|
|
|
virtual Log & operator<<(wchar_t s);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
virtual Log & operator<<(int s);
|
|
|
|
virtual Log & operator<<(long s);
|
2019-01-28 19:06:18 +01:00
|
|
|
virtual Log & operator<<(long long s);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
// add unsigned long, unsigned int
|
|
|
|
virtual Log & operator<<(size_t s);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
//virtual Log & operator<<(float s); // added
|
|
|
|
virtual Log & operator<<(double s);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
virtual Log & operator<<(const PT::Space & space);
|
|
|
|
virtual Log & operator<<(const PT::Date & date);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
virtual Log & operator<<(Manipulators m);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
virtual Log & LogString(const std::string & value, size_t max_size);
|
|
|
|
virtual Log & LogString(const std::wstring & value, size_t max_size);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
Log & operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
virtual Log & LogBinary(const char * blob, size_t blob_len);
|
|
|
|
virtual Log & LogBinary(const std::string & blob);
|
2018-04-23 16:28:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
protected:
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
// buffer for the log
|
|
|
|
PT::WTextStream * buffer;
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
// file logger
|
|
|
|
FileLog * file_log;
|
2018-04-23 16:28:22 +02:00
|
|
|
|
|
|
|
// current level set by a modifier (e.g. log << log3)
|
|
|
|
int current_level;
|
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
// if there is logend modifier used and the buffer exceeds max_buffer_length then
|
|
|
|
// the buffer is passed to file_log
|
|
|
|
size_t max_buffer_length;
|
2018-04-23 16:28:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
char get_hex_digit(unsigned char c);
|
|
|
|
void to_hex(char * buf, unsigned char c);
|
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
template<class StringType>
|
|
|
|
Log & log_string_generic(const StringType & value, size_t max_size);
|
|
|
|
|
|
|
|
virtual void save_log();
|
|
|
|
virtual void save_log_and_clear();
|
|
|
|
|
2018-04-23 16:28:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
2018-11-23 18:07:52 +01:00
|
|
|
Log & Log::log_string_generic(const StringType & value, size_t max_size)
|
2018-04-23 16:28:22 +02:00
|
|
|
{
|
2018-11-23 18:07:52 +01:00
|
|
|
std::size_t min_size = value.size() < max_size ? value.size() : max_size;
|
2018-04-23 16:28:22 +02:00
|
|
|
|
2018-11-23 18:07:52 +01:00
|
|
|
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
2018-04-23 16:28:22 +02:00
|
|
|
{
|
|
|
|
for(size_t i=0 ; i < min_size ; ++i)
|
|
|
|
{
|
|
|
|
if( value[i] < 32 )
|
2018-11-23 18:07:52 +01:00
|
|
|
(*buffer) << '.'; // unprintable characters
|
2018-04-23 16:28:22 +02:00
|
|
|
else
|
2018-11-23 18:07:52 +01:00
|
|
|
(*buffer) << value[i];
|
2018-04-23 16:28:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
2018-11-23 18:07:52 +01:00
|
|
|
Log & Log::operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf)
|
2018-04-23 16:28:22 +02:00
|
|
|
{
|
2018-11-23 18:07:52 +01:00
|
|
|
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
|
|
|
{
|
|
|
|
(*buffer) << buf;
|
|
|
|
}
|
2018-04-23 16:28:22 +02:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|