winix/core/log.h

172 lines
3.5 KiB
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2008-2012, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfile_winix_core_log
#define headerfile_winix_core_log
#include <sstream>
#include <fstream>
#include <iostream>
#include <string>
#include "textstream.h"
#include "logmanipulators.h"
#include "textstream/textstream.h"
class TimeZones;
class Log
{
public:
Log();
~Log();
void SetTimeZones(TimeZones * ptime_zones);
void Init(int log_level_, bool save_each_line_, const std::string & 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<typename char_type, size_t stack_size, size_t heap_block_size>
Log & operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf);
template<class StringType>
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<std::wstring> 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<class StringType>
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<min_size ; ++i)
{
if( value[i] < 32 )
buffer << '.';
else
buffer << value[i];
}
}
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
Log & Log::operator<<(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & buf)
{
if( current_level > log_level )
return *this;
typename PT::TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator i = buf.begin();
// in the future we change buffer to PT::TextStreamBuffer
// so then there'll be << operator
for( ; i != buf.end() ; ++i)
buffer << *i;
return *this;
}
extern Log log;
extern Log nlog;
// for convenience, we have to use only #include "log.h" in the winix
#include "slog.h"
#endif