/* * This file is a part of Winix * and is distributed under the 2-Clause BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2008-2014, 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" namespace Winix { class TimeZones; class Log { public: Log(); ~Log(); void SetTimeZones(TimeZones * ptime_zones); void Init(int log_level_, bool save_each_line_, const std::wstring & log_file_, bool log_std, int log_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, size_t time_zone_id); 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(); int LogLevel(); private: // time zones for printing the time in the log file TimeZones * time_zones; // buffer for the log TextStream buffer; // log lovel from the config file int log_level; // 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; // 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: 5000 int max_lines; // whether to save each line (for debug) bool save_each_line; void OpenFile(); 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 <= log_level ) { for(size_t i=0 ; i Log & Log::operator<<(const PT::TextStreamBase & buf) { if( current_level <= log_level ) buffer << buf; return *this; } extern Log log; extern Log nlog; } // namespace Winix // for convenience, we have to use only #include "log.h" in the winix #include "slog.h" #endif