changed in logger:
renamed Logger -> Log this class has an api (<< operators) for taking what should be put to the log file as the buffer is used WTextStream which should be allocated elsewhere added: FileLog - this class saves to a file log git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1149 e52654a7-88a9-db11-a3e9-0013d4bc506emaster
parent
6984a34fcd
commit
e971e1ef9b
10
Makefile
10
Makefile
|
@ -25,7 +25,7 @@ export LDFLAGS
|
|||
export AR
|
||||
|
||||
|
||||
all: space mainparser mainspaceparser utf8 date convert logger
|
||||
all: space mainparser mainspaceparser utf8 date convert log
|
||||
|
||||
|
||||
|
||||
|
@ -48,8 +48,8 @@ date: FORCE
|
|||
convert: FORCE
|
||||
@cd convert ; $(MAKE) -e
|
||||
|
||||
logger: FORCE
|
||||
@cd logger ; $(MAKE) -e
|
||||
log: FORCE
|
||||
@cd log ; $(MAKE) -e
|
||||
|
||||
|
||||
|
||||
|
@ -64,7 +64,7 @@ clean:
|
|||
@cd utf8 ; $(MAKE) -e clean
|
||||
@cd date ; $(MAKE) -e clean
|
||||
@cd convert ; $(MAKE) -e clean
|
||||
@cd logger ; $(MAKE) -e clean
|
||||
@cd log ; $(MAKE) -e clean
|
||||
|
||||
depend:
|
||||
@cd space ; $(MAKE) -e depend
|
||||
|
@ -73,4 +73,4 @@ depend:
|
|||
@cd utf8 ; $(MAKE) -e depend
|
||||
@cd date ; $(MAKE) -e depend
|
||||
@cd convert ; $(MAKE) -e depend
|
||||
@cd logger ; $(MAKE) -e depend
|
||||
@cd log ; $(MAKE) -e depend
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
include Makefile.o.dep
|
||||
|
||||
libname=logger.a
|
||||
libname=log.a
|
||||
|
||||
all: $(libname)
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
# DO NOT DELETE
|
||||
|
||||
filelog.o: filelog.h ../textstream/textstream.h ../space/space.h
|
||||
filelog.o: ../textstream/types.h ../date/date.h ../convert/convert.h
|
||||
filelog.o: ../convert/inttostr.h ../convert/strtoint.h ../convert/text.h
|
||||
filelog.o: ../convert/misc.h ../membuffer/membuffer.h ../textstream/types.h
|
||||
filelog.o: ../utf8/utf8.h
|
||||
log.o: log.h ../textstream/textstream.h ../space/space.h
|
||||
log.o: ../textstream/types.h ../date/date.h ../convert/convert.h
|
||||
log.o: ../convert/inttostr.h ../convert/strtoint.h ../convert/text.h
|
||||
log.o: ../convert/misc.h ../membuffer/membuffer.h ../textstream/types.h
|
||||
log.o: filelog.h ../utf8/utf8.h
|
|
@ -0,0 +1 @@
|
|||
o = filelog.o log.o
|
|
@ -0,0 +1,174 @@
|
|||
/*
|
||||
* 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) 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:
|
||||
*
|
||||
* 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 "filelog.h"
|
||||
#include <ctime>
|
||||
#include <string.h>
|
||||
#include "utf8/utf8.h"
|
||||
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
|
||||
FileLog::FileLog()
|
||||
{
|
||||
log_level = 4;
|
||||
log_stdout = false;
|
||||
log_file_open = false;
|
||||
save_each_line = false;
|
||||
}
|
||||
|
||||
|
||||
FileLog::~FileLog()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool FileLog::synchro_lock()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void FileLog::synchro_unlock()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void FileLog::init(const std::wstring & log_file, bool log_stdout, int log_level, bool save_each_line)
|
||||
{
|
||||
this->log_stdout = log_stdout;
|
||||
this->log_level = log_level;
|
||||
this->save_each_line = save_each_line;
|
||||
PT::WideToUTF8(log_file, this->log_file);
|
||||
}
|
||||
|
||||
|
||||
int FileLog::get_log_level()
|
||||
{
|
||||
return log_level;
|
||||
}
|
||||
|
||||
|
||||
bool FileLog::should_save_each_line()
|
||||
{
|
||||
return save_each_line;
|
||||
}
|
||||
|
||||
|
||||
void FileLog::open_file()
|
||||
{
|
||||
if( !log_file.empty() )
|
||||
{
|
||||
file.open( log_file.c_str(), std::ios_base::out | std::ios_base::app );
|
||||
log_file_open = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FileLog::PutBuffer(PT::WTextStream * buffer, std::ostream & out_stream)
|
||||
{
|
||||
char utf8_buffer[256];
|
||||
std::size_t index = 0;
|
||||
|
||||
PT::WTextStream::const_iterator i = buffer->begin();
|
||||
|
||||
while( i != buffer->end() )
|
||||
{
|
||||
utf8_buffer[index++] = *i;
|
||||
|
||||
if( index + 10 > sizeof(utf8_buffer) / sizeof(char) )
|
||||
{
|
||||
out_stream.write(utf8_buffer, index);
|
||||
index = 0;
|
||||
}
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
if( index > 0 )
|
||||
{
|
||||
out_stream.write(utf8_buffer, index);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void FileLog::save_log(PT::WTextStream * buffer)
|
||||
{
|
||||
if( buffer->empty() )
|
||||
return;
|
||||
|
||||
synchro_lock();
|
||||
|
||||
try
|
||||
{
|
||||
if( log_stdout )
|
||||
{
|
||||
PutBuffer(buffer, std::cout);
|
||||
}
|
||||
|
||||
if( !log_file.empty() )
|
||||
{
|
||||
if( !log_file_open || !file )
|
||||
{
|
||||
file.close();
|
||||
file.clear();
|
||||
|
||||
open_file();
|
||||
}
|
||||
|
||||
if( file )
|
||||
{
|
||||
PutBuffer(buffer, file);
|
||||
file.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
}
|
||||
|
||||
synchro_unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* 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) 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:
|
||||
*
|
||||
* 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_picotools_log_filelog
|
||||
#define headerfile_picotools_log_filelog
|
||||
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "textstream/textstream.h"
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
|
||||
class FileLog
|
||||
{
|
||||
public:
|
||||
|
||||
FileLog();
|
||||
virtual ~FileLog();
|
||||
|
||||
virtual void init(const std::wstring & log_file, bool log_stdout, int log_level, bool save_each_line);
|
||||
virtual void save_log(PT::WTextStream * buffer);
|
||||
|
||||
virtual int get_log_level();
|
||||
virtual bool should_save_each_line();
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
// file log
|
||||
std::string log_file;
|
||||
std::ofstream file;
|
||||
|
||||
// logging to stdout
|
||||
bool log_stdout;
|
||||
|
||||
// is the config file already open
|
||||
bool log_file_open;
|
||||
|
||||
// log lovel
|
||||
int log_level;
|
||||
|
||||
// whether to save each line (for debug)
|
||||
bool save_each_line;
|
||||
|
||||
|
||||
virtual bool synchro_lock();
|
||||
virtual void synchro_unlock();
|
||||
virtual void PutBuffer(PT::WTextStream * buffer, std::ostream & out_stream);
|
||||
virtual void open_file();
|
||||
};
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,451 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <ctime>
|
||||
#include <string.h>
|
||||
#include "log.h"
|
||||
#include "date/date.h"
|
||||
#include "utf8/utf8.h"
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
|
||||
Log::Log()
|
||||
{
|
||||
buffer = nullptr;
|
||||
file_log = nullptr;
|
||||
|
||||
current_level = 4;
|
||||
max_buffer_length = 2 * 1024 * 1024; // 2MB
|
||||
}
|
||||
|
||||
|
||||
Log::~Log()
|
||||
{
|
||||
save_log_and_clear();
|
||||
}
|
||||
|
||||
|
||||
void Log::SetLogBuffer(PT::WTextStream * buffer)
|
||||
{
|
||||
this->buffer = buffer;
|
||||
}
|
||||
|
||||
|
||||
PT::WTextStream * Log::GetLogBuffer()
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
void Log::SetMaxBufferLength(size_t max_buffer_length)
|
||||
{
|
||||
this->max_buffer_length = max_buffer_length;
|
||||
}
|
||||
|
||||
|
||||
size_t Log::GetMaxBufferLength()
|
||||
{
|
||||
return max_buffer_length;
|
||||
}
|
||||
|
||||
|
||||
void Log::SetFileLog(FileLog * file_log)
|
||||
{
|
||||
this->file_log = file_log;
|
||||
}
|
||||
|
||||
|
||||
FileLog * Log::GetFileLog()
|
||||
{
|
||||
return file_log;
|
||||
}
|
||||
|
||||
|
||||
Log & Log::operator<<(const void * s)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const char * s)
|
||||
{
|
||||
if( buffer && file_log && s && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::string & s)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::string * s)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << *s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const wchar_t * s)
|
||||
{
|
||||
if( buffer && file_log && s && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::wstring & s)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::wstring * s)
|
||||
{
|
||||
if( buffer && file_log && s && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << *s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(int s)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(long s)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(char s)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Log & Log::operator<<(wchar_t s)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Log & Log::operator<<(size_t s)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(double s)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const PT::Space & s)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const PT::Date & date)
|
||||
{
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << date;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(Manipulators m)
|
||||
{
|
||||
switch(m)
|
||||
{
|
||||
case lend:
|
||||
case logend:
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << '\n';
|
||||
|
||||
if( file_log->should_save_each_line() || buffer->size() > max_buffer_length )
|
||||
save_log_and_clear();
|
||||
}
|
||||
break;
|
||||
|
||||
case lsave:
|
||||
case logsave:
|
||||
save_log_and_clear();
|
||||
break;
|
||||
|
||||
case l1:
|
||||
case log1:
|
||||
current_level = 1;
|
||||
break;
|
||||
|
||||
case l2:
|
||||
case log2:
|
||||
current_level = 2;
|
||||
break;
|
||||
|
||||
case l3:
|
||||
case log3:
|
||||
current_level = 3;
|
||||
break;
|
||||
|
||||
case l4:
|
||||
case log4:
|
||||
current_level = 4;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Log & Log::LogString(const std::string & value, size_t max_size)
|
||||
{
|
||||
return log_string_generic(value, max_size);
|
||||
}
|
||||
|
||||
|
||||
Log & Log::LogString(const std::wstring & value, size_t max_size)
|
||||
{
|
||||
return log_string_generic(value, max_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
char Log::get_hex_digit(unsigned char c)
|
||||
{
|
||||
if( c < 10 )
|
||||
return c + '0';
|
||||
|
||||
return c - 10 + 'A';
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Log::to_hex(char * buf, unsigned char c)
|
||||
{
|
||||
buf[0] = get_hex_digit(c >> 4);
|
||||
buf[1] = get_hex_digit(c & 0xf);
|
||||
buf[2] = 0;
|
||||
}
|
||||
|
||||
|
||||
Log & Log::LogBinary(const char * blob, size_t blob_len)
|
||||
{
|
||||
size_t i=0;
|
||||
char buf[3];
|
||||
|
||||
if( buffer && file_log && blob && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
while( i < blob_len )
|
||||
{
|
||||
size_t oldi = i;
|
||||
|
||||
for(size_t a=0 ; a<16 ; ++a)
|
||||
{
|
||||
if( i < blob_len )
|
||||
{
|
||||
to_hex(buf, blob[i]);
|
||||
(*buffer) << buf << ' ';
|
||||
++i;
|
||||
}
|
||||
else
|
||||
{
|
||||
(*buffer) << " ";
|
||||
}
|
||||
|
||||
if( a == 7 )
|
||||
{
|
||||
if( i < blob_len )
|
||||
(*buffer) << "- ";
|
||||
else
|
||||
(*buffer) << " ";
|
||||
}
|
||||
}
|
||||
|
||||
i = oldi;
|
||||
(*buffer) << ' ';
|
||||
|
||||
for(size_t a=0 ; a<16 && i<blob_len ; ++a, ++i)
|
||||
{
|
||||
if( blob[i] > 31 && blob[i] < 127 )
|
||||
(*buffer) << blob[i];
|
||||
else
|
||||
(*buffer) << '.';
|
||||
}
|
||||
|
||||
(*this) << logend;
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Log & Log::LogBinary(const std::string & blob)
|
||||
{
|
||||
return LogBinary(blob.c_str(), blob.size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Log::save_log_and_clear()
|
||||
{
|
||||
save_log();
|
||||
|
||||
if( buffer )
|
||||
{
|
||||
buffer->clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Log::save_log()
|
||||
{
|
||||
if( file_log && buffer )
|
||||
{
|
||||
file_log->save_log(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -35,12 +35,13 @@
|
|||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef headerfile_picotools_logger_logger
|
||||
#define headerfile_picotools_logger_logger
|
||||
#ifndef headerfile_picotools_log_log
|
||||
#define headerfile_picotools_log_log
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include "textstream/textstream.h"
|
||||
#include "filelog.h"
|
||||
|
||||
|
||||
|
||||
|
@ -48,7 +49,7 @@ namespace PT
|
|||
{
|
||||
|
||||
|
||||
class Logger
|
||||
class Log
|
||||
{
|
||||
public:
|
||||
|
||||
|
@ -60,16 +61,7 @@ public:
|
|||
log3
|
||||
log4 - the last level (debug level)
|
||||
logend - the end of a line
|
||||
logendrequest - end of a current request
|
||||
logsave - current log buffer is saved and cleared
|
||||
|
||||
manipulators used by the session logger (SLog)
|
||||
loginfo - normal info to a user
|
||||
logerror - we are reporting an error
|
||||
logwarning - we are reporting a warning
|
||||
|
||||
make sure that loginfo, logerror and logwarning have values less than 32 (space)
|
||||
their are used as control codes in a string
|
||||
*/
|
||||
enum Manipulators
|
||||
{
|
||||
|
@ -86,127 +78,109 @@ public:
|
|||
logend,
|
||||
lend,
|
||||
|
||||
|
||||
logsave,
|
||||
lsave,
|
||||
|
||||
|
||||
// logendrequest,
|
||||
// logsave,
|
||||
|
||||
// loginfo,
|
||||
// logerror,
|
||||
// logwarning
|
||||
};
|
||||
|
||||
|
||||
|
||||
Logger();
|
||||
virtual ~Logger();
|
||||
Log();
|
||||
virtual ~Log();
|
||||
|
||||
virtual void SetLogBuffer(PT::WTextStream * buffer);
|
||||
virtual PT::WTextStream * GetLogBuffer();
|
||||
|
||||
void SetFileLog(FileLog * file_log);
|
||||
FileLog * GetFileLog();
|
||||
|
||||
void SetMaxBufferLength(size_t max_buffer_length);
|
||||
size_t GetMaxBufferLength();
|
||||
|
||||
virtual Log & operator<<(const void * s);
|
||||
|
||||
virtual Log & operator<<(const char * s);
|
||||
virtual Log & operator<<(const std::string * s);
|
||||
virtual Log & operator<<(const std::string & s);
|
||||
|
||||
virtual Log & operator<<(const wchar_t * s);
|
||||
virtual Log & operator<<(const std::wstring * s);
|
||||
virtual Log & operator<<(const std::wstring & s);
|
||||
|
||||
virtual Log & operator<<(char s);
|
||||
virtual Log & operator<<(wchar_t s);
|
||||
|
||||
virtual Log & operator<<(int s);
|
||||
virtual Log & operator<<(long s);
|
||||
//virtual Log & operator<<(long long s); // added
|
||||
|
||||
|
||||
int get_log_level();
|
||||
void set_log_level(int log_level);
|
||||
// add unsigned long, unsigned int
|
||||
virtual Log & operator<<(size_t s);
|
||||
|
||||
//virtual Log & operator<<(float s); // added
|
||||
virtual Log & operator<<(double s);
|
||||
|
||||
void init(int log_level, bool save_each_line, const std::wstring & log_file, bool log_stdout);
|
||||
virtual Log & operator<<(const PT::Space & space);
|
||||
virtual Log & operator<<(const PT::Date & date);
|
||||
|
||||
Logger & operator<<(const void * s);
|
||||
virtual Log & operator<<(Manipulators m);
|
||||
|
||||
Logger & operator<<(const char * s);
|
||||
Logger & operator<<(const std::string * s);
|
||||
Logger & operator<<(const std::string & s);
|
||||
|
||||
Logger & operator<<(const wchar_t * s);
|
||||
Logger & operator<<(const std::wstring * s);
|
||||
Logger & operator<<(const std::wstring & s);
|
||||
|
||||
Logger & operator<<(char s);
|
||||
Logger & operator<<(wchar_t s);
|
||||
|
||||
Logger & operator<<(int s);
|
||||
Logger & operator<<(long s);
|
||||
//Logger & operator<<(long long s); // added
|
||||
|
||||
|
||||
Logger & operator<<(size_t s);
|
||||
|
||||
//Logger & operator<<(float s); // added
|
||||
Logger & operator<<(double s);
|
||||
|
||||
Logger & operator<<(const PT::Space & space);
|
||||
Logger & operator<<(Manipulators m);
|
||||
Logger & operator<<(const PT::Date & date);
|
||||
virtual Log & LogString(const std::string & value, size_t max_size);
|
||||
virtual Log & LogString(const std::wstring & value, size_t max_size);
|
||||
|
||||
|
||||
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||
Logger & operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf);
|
||||
|
||||
|
||||
template<class StringType>
|
||||
Logger & log_string(const StringType & value, size_t max_size);
|
||||
|
||||
Logger & log_binary(const char * blob, size_t blob_len);
|
||||
Logger & log_binary(const std::string & blob);
|
||||
|
||||
|
||||
void save_log();
|
||||
void save_log_and_clear();
|
||||
Log & operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf);
|
||||
|
||||
|
||||
|
||||
private:
|
||||
virtual Log & LogBinary(const char * blob, size_t blob_len);
|
||||
virtual Log & LogBinary(const std::string & blob);
|
||||
|
||||
// buffer for the log (wide characters)
|
||||
PT::WTextStream buffer;
|
||||
|
||||
// log lovel from init()
|
||||
int log_level;
|
||||
|
||||
protected:
|
||||
|
||||
// buffer for the log
|
||||
PT::WTextStream * buffer;
|
||||
|
||||
// file logger
|
||||
FileLog * file_log;
|
||||
|
||||
// current level set by a modifier (e.g. log << log3)
|
||||
int current_level;
|
||||
|
||||
// file log
|
||||
std::string log_file;
|
||||
std::ofstream file;
|
||||
// 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;
|
||||
|
||||
// logging to stdout
|
||||
bool log_stdout;
|
||||
|
||||
// how many lines there are in the buffer
|
||||
int lines;
|
||||
|
||||
// is the config file already open
|
||||
bool log_file_open;
|
||||
|
||||
// how many lines can be in the config buffer
|
||||
// default: 500
|
||||
int max_lines;
|
||||
|
||||
// whether to save each line (for debug purpopes)
|
||||
bool save_each_line;
|
||||
|
||||
void open_file();
|
||||
char get_hex_digit(unsigned char c);
|
||||
void to_hex(char * buf, unsigned char c);
|
||||
|
||||
template<class StringType>
|
||||
Log & log_string_generic(const StringType & value, size_t max_size);
|
||||
|
||||
virtual void save_log();
|
||||
virtual void save_log_and_clear();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<class StringType>
|
||||
Logger & Logger::log_string(const StringType & value, size_t max_size)
|
||||
Log & Log::log_string_generic(const StringType & value, size_t max_size)
|
||||
{
|
||||
size_t min_size = value.size() < max_size ? value.size() : max_size;
|
||||
std::size_t min_size = value.size() < max_size ? value.size() : max_size;
|
||||
|
||||
if( current_level <= log_level )
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
for(size_t i=0 ; i < min_size ; ++i)
|
||||
{
|
||||
if( value[i] < 32 )
|
||||
buffer << '.'; // unprintable characters
|
||||
(*buffer) << '.'; // unprintable characters
|
||||
else
|
||||
buffer << value[i];
|
||||
(*buffer) << value[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -216,10 +190,12 @@ Logger & Logger::log_string(const StringType & value, size_t max_size)
|
|||
|
||||
|
||||
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
||||
Logger & Logger::operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf)
|
||||
Log & Log::operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
buffer << buf;
|
||||
if( buffer && file_log && current_level <= file_log->get_log_level() )
|
||||
{
|
||||
(*buffer) << buf;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
# DO NOT DELETE
|
||||
|
||||
logger.o: logger.h ../textstream/textstream.h ../space/space.h
|
||||
logger.o: ../textstream/types.h ../date/date.h ../convert/convert.h
|
||||
logger.o: ../convert/inttostr.h ../convert/strtoint.h ../convert/text.h
|
||||
logger.o: ../convert/misc.h ../membuffer/membuffer.h ../textstream/types.h
|
||||
logger.o: ../utf8/utf8.h
|
|
@ -1 +0,0 @@
|
|||
o = logger.o
|
|
@ -1,472 +0,0 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <ctime>
|
||||
#include <string.h>
|
||||
#include "logger.h"
|
||||
#include "date/date.h"
|
||||
#include "utf8/utf8.h"
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
Logger::Logger()
|
||||
{
|
||||
log_level = 1;
|
||||
current_level = 100; // nothing to log (call Init() first)
|
||||
lines = 0;
|
||||
max_lines = 500;
|
||||
log_file_open = false;
|
||||
}
|
||||
|
||||
|
||||
Logger::~Logger()
|
||||
{
|
||||
save_log_and_clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int Logger::get_log_level()
|
||||
{
|
||||
return log_level;
|
||||
}
|
||||
|
||||
|
||||
void Logger::set_log_level(int log_level)
|
||||
{
|
||||
this->log_level = log_level;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Logger::init(int log_level, bool save_each_line, const std::wstring & log_file, bool log_stdout)
|
||||
{
|
||||
this->log_level = log_level;
|
||||
this->save_each_line = save_each_line;
|
||||
this->log_stdout = log_stdout;
|
||||
|
||||
PT::WideToUTF8(log_file, this->log_file);
|
||||
// don't open the file here
|
||||
// because it would be created with the root as an owner if you created something like a www server
|
||||
// which starts as root and then drops privileges
|
||||
}
|
||||
|
||||
|
||||
void Logger::open_file()
|
||||
{
|
||||
if( !log_file.empty() )
|
||||
{
|
||||
file.open(log_file.c_str(), std::ios_base::out | std::ios_base::app);
|
||||
log_file_open = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Logger & Logger::operator<<(const void * s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
buffer << s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(const char * s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
if( !s )
|
||||
return *this;
|
||||
|
||||
buffer << s;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(const std::string & s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
buffer << s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(const std::string * s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
buffer << *s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(const wchar_t * s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
if( s )
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(const std::wstring & s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(const std::wstring * s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << *s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(int s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(long s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(char s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Logger & Logger::operator<<(wchar_t s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Logger & Logger::operator<<(size_t s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(double s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(const PT::Space & s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(const PT::Date & date)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << date;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Logger & Logger::operator<<(Manipulators m)
|
||||
{
|
||||
switch(m)
|
||||
{
|
||||
case lend:
|
||||
case logend:
|
||||
// IMPROVE ME use max_lines here
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << '\n';
|
||||
lines += 1;
|
||||
|
||||
if( save_each_line )
|
||||
save_log_and_clear();
|
||||
}
|
||||
break;
|
||||
|
||||
case lsave:
|
||||
case logsave:
|
||||
save_log_and_clear();
|
||||
break;
|
||||
|
||||
case l1:
|
||||
case log1:
|
||||
current_level = 1;
|
||||
break;
|
||||
|
||||
case l2:
|
||||
case log2:
|
||||
current_level = 2;
|
||||
break;
|
||||
|
||||
case l3:
|
||||
case log3:
|
||||
current_level = 3;
|
||||
break;
|
||||
|
||||
case l4:
|
||||
case log4:
|
||||
current_level = 4;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
char Logger::get_hex_digit(unsigned char c)
|
||||
{
|
||||
if( c < 10 )
|
||||
return c + '0';
|
||||
|
||||
return c - 10 + 'A';
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Logger::to_hex(char * buf, unsigned char c)
|
||||
{
|
||||
buf[0] = get_hex_digit(c >> 4);
|
||||
buf[1] = get_hex_digit(c & 0xf);
|
||||
buf[2] = 0;
|
||||
}
|
||||
|
||||
|
||||
Logger & Logger::log_binary(const char * blob, size_t blob_len)
|
||||
{
|
||||
size_t i=0;
|
||||
char buf[3];
|
||||
|
||||
|
||||
while( i < blob_len )
|
||||
{
|
||||
size_t oldi = i;
|
||||
|
||||
for(size_t a=0 ; a<16 ; ++a)
|
||||
{
|
||||
if( i < blob_len )
|
||||
{
|
||||
to_hex(buf, blob[i]);
|
||||
buffer << buf << ' ';
|
||||
++i;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer << " ";
|
||||
}
|
||||
|
||||
if( a == 7 )
|
||||
{
|
||||
if( i < blob_len )
|
||||
buffer << "- ";
|
||||
else
|
||||
buffer << " ";
|
||||
}
|
||||
}
|
||||
|
||||
i = oldi;
|
||||
buffer << ' ';
|
||||
|
||||
for(size_t a=0 ; a<16 && i<blob_len ; ++a, ++i)
|
||||
{
|
||||
if( blob[i] > 31 && blob[i] < 127 )
|
||||
buffer << blob[i];
|
||||
else
|
||||
buffer << '.';
|
||||
}
|
||||
|
||||
(*this) << logend;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Logger & Logger::log_binary(const std::string & blob)
|
||||
{
|
||||
return log_binary(blob.c_str(), blob.size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Logger::save_log_and_clear()
|
||||
{
|
||||
save_log();
|
||||
|
||||
buffer.clear();
|
||||
lines = 0;
|
||||
}
|
||||
|
||||
|
||||
void Logger::save_log()
|
||||
{
|
||||
std::wstring log_wide;// IMPROVE ME do something better
|
||||
std::string log_ascii;// IMPROVE ME
|
||||
|
||||
if( buffer.empty() )
|
||||
return;
|
||||
|
||||
// IMPROVE ME
|
||||
// add special version of TextStream.to_string()
|
||||
// which can convert from wide to utf8 (by using if constexpr)
|
||||
|
||||
// may we need a different buffer type? utf8 buffer?
|
||||
|
||||
buffer.to_string(log_wide);
|
||||
PT::WideToUTF8(log_wide, log_ascii);
|
||||
|
||||
if( log_stdout )
|
||||
std::cout << log_ascii;
|
||||
|
||||
if( log_file.empty() )
|
||||
return;
|
||||
|
||||
if( !log_file_open || !file )
|
||||
{
|
||||
file.close();
|
||||
file.clear();
|
||||
|
||||
open_file();
|
||||
|
||||
if( !file )
|
||||
return;
|
||||
}
|
||||
|
||||
file << log_ascii;
|
||||
file.flush();
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue