/* * 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. */ #include #include #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 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