/* * This file is a part of PikoTools * and is distributed under the (new) BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2018-2021, 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. */ #ifndef headerfile_picotools_log_log #define headerfile_picotools_log_log #include #include #include "textstream/textstream.h" #include "filelog.h" namespace morm { class Model; } namespace pt { class Log { 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, }; Log(); virtual ~Log(); virtual void SetLogBuffer(WTextStream * buffer); virtual WTextStream * GetLogBuffer(); void SetFileLog(FileLog * file_log); FileLog * GetFileLog(); void SetMaxBufferLength(size_t max_buffer_length); size_t GetMaxBufferLength(); virtual Log & IntMinWidth(size_t min_width); 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); // add unsigned long, unsigned int virtual Log & operator<<(size_t s); //virtual Log & operator<<(float s); // added virtual Log & operator<<(double s); virtual Log & operator<<(const Space & space); virtual Log & operator<<(const Date & date); #ifdef PT_HAS_MORM virtual Log & operator<<(morm::Model & model); #endif virtual Log & operator<<(Manipulators m); virtual Log & LogString(const std::string & value, size_t max_size); virtual Log & LogString(const std::wstring & value, size_t max_size); template Log & operator<<(const TextStreamBase & buf); virtual Log & LogBinary(const char * blob, size_t blob_len); virtual Log & LogBinary(const std::string & blob); protected: // buffer for the log WTextStream * buffer; // file logger FileLog * file_log; // current level set by a modifier (e.g. log << log3) int current_level; // 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; char get_hex_digit(unsigned char c); void to_hex(char * buf, unsigned char c); template Log & log_string_generic(const StringType & value, size_t max_size); virtual void save_log(); virtual void save_log_and_clear(); }; template Log & Log::log_string_generic(const StringType & value, size_t max_size) { std::size_t min_size = value.size() < max_size ? value.size() : max_size; 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 else (*buffer) << value[i]; } } return *this; } template Log & Log::operator<<(const TextStreamBase & buf) { if( buffer && file_log && current_level <= file_log->get_log_level() ) { (*buffer) << buf; } return *this; } } // namespace #endif