/* * This file is a part of Winix * and is distributed under the 2-Clause BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2008-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_winix_core_log #define headerfile_winix_core_log #include #include #include #include #include "textstream.h" #include "logmanipulators.h" #include "textstream/textstream.h" #include "filelog.h" namespace Winix { class Log { public: Log(); ~Log(); void SetLogBuffer(TextStream * buffer); void SetFileLog(FileLog * file_log); FileLog * GetFileLog(); void SetDependency(Log * log); void Init(int max_requests); Log & operator<<(const void * s); Log & operator<<(const char * s); Log & operator<<(const std::string * s); Log & operator<<(const std::string & s); Log & operator<<(const wchar_t * s); Log & operator<<(const std::wstring * s); Log & operator<<(const std::wstring & s); Log & operator<<(int s); Log & operator<<(long s); Log & operator<<(char s); Log & operator<<(wchar_t s); Log & operator<<(size_t s); Log & operator<<(double s); Log & operator<<(const PT::Space & space); Log & operator<<(LogManipulators m); Log & operator<<(const PT::Date & date); void PrintDate(const PT::Date & date); template Log & operator<<(const PT::TextStreamBase & buf); template void LogString(const StringType & value, size_t max_size); void LogBinary(const char * blob, size_t blob_len); void LogBinary(const std::string & blob); void SystemErr(int err); void SaveLog(); void SaveLogAndClear(); private: // buffer for the log TextStream * buffer; // IMPROVE ME this buffer should be a common buffer for all objects for a one thread // current level set by a modifier (e.g. log << log3) int current_level; // current request for logging // starts from zero and incremented after logendrequest modifier int request; // how many request to save at once int max_requests; // IMPROVE ME now we don't know how many lines there are in the buffer as the buffer can be used by other Logs // how many lines there are in the buffer int lines; // how many lines can be in the config buffer // default: 5000 int max_lines; FileLog * file_log; char GetHEXdigit(unsigned char c); void ToHEX(char * buf, unsigned char c); }; template void Log::LogString(const StringType & value, size_t max_size) { size_t min_size = value.size() < max_size ? value.size() : max_size; if( current_level <= file_log->get_log_level() && buffer ) { for(size_t i=0 ; i Log & Log::operator<<(const PT::TextStreamBase & buf) { if( current_level <= file_log->get_log_level() && buffer ) (*buffer) << buf; return *this; } } // namespace Winix #endif