/* * This file is a part of PikoTools * and is distributed under the (new) BSD licence. * Author: Tomasz Sowa */ /* * 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. */ #ifndef headerfile_picotools_logger_logger #define headerfile_picotools_logger_logger #include #include #include "textstream/textstream.h" namespace PT { class Logger { public: /* log1 - the first level log2 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 { log1, log2, log3, log4, l1, l2, l3, l4, logend, lend, logsave, lsave, // logendrequest, // logsave, // loginfo, // logerror, // logwarning }; Logger(); virtual ~Logger(); int get_log_level(); void set_log_level(int log_level); void init(int log_level, bool save_each_line, const std::wstring & log_file, bool log_stdout); Logger & operator<<(const void * s); 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); template Logger & operator<<(const PT::TextStreamBase & buf); template 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(); private: // buffer for the log (wide characters) PT::WTextStream buffer; // log lovel from init() int log_level; // current level set by a modifier (e.g. log << log3) int current_level; // file log std::string log_file; std::ofstream file; // 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 Logger & Logger::log_string(const StringType & value, size_t max_size) { size_t min_size = value.size() < max_size ? value.size() : max_size; if( current_level <= 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 Logger & Logger::operator<<(const PT::TextStreamBase & buf) { if( current_level <= log_level ) buffer << buf; return *this; } } // namespace #endif