moved winix directories to winixd subdirectory
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@1027 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
487
winixd/core/log.cpp
Normal file
487
winixd/core/log.cpp
Normal file
@@ -0,0 +1,487 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is distributed under the 2-Clause BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "log.h"
|
||||
#include <ctime>
|
||||
#include <string.h>
|
||||
#include "utf8/utf8.h"
|
||||
#include "timezones.h"
|
||||
|
||||
|
||||
namespace Winix
|
||||
{
|
||||
|
||||
|
||||
|
||||
Log::Log()
|
||||
{
|
||||
log_level = 1;
|
||||
current_level = 100; // nothing to log (call Init() first)
|
||||
request = 0;
|
||||
max_requests = 1;
|
||||
lines = 0;
|
||||
max_lines = 5000;
|
||||
log_file_open = false;
|
||||
time_zones = 0;
|
||||
}
|
||||
|
||||
|
||||
Log::~Log()
|
||||
{
|
||||
SaveLogAndClear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Log::SetTimeZones(TimeZones * ptime_zones)
|
||||
{
|
||||
time_zones = ptime_zones;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int Log::LogLevel()
|
||||
{
|
||||
return log_level;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Log::Init(int log_level_, bool save_each_line_, const std::wstring & log_file_, bool log_std, int log_max_requests)
|
||||
{
|
||||
log_level = log_level_;
|
||||
log_stdout = log_std;
|
||||
max_requests = log_max_requests;
|
||||
save_each_line = save_each_line_;
|
||||
|
||||
PT::WideToUTF8(log_file_, log_file);
|
||||
// don't open the file here
|
||||
// because it would be created with the root as an owner
|
||||
}
|
||||
|
||||
|
||||
void Log::OpenFile()
|
||||
{
|
||||
if( !log_file.empty() )
|
||||
{
|
||||
file.open( log_file.c_str(), std::ios_base::out | std::ios_base::app );
|
||||
log_file_open = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Log::PrintDate(const PT::Date & date, size_t time_zone_id)
|
||||
{
|
||||
if( time_zones )
|
||||
{
|
||||
TimeZone * tz = time_zones->GetZone(time_zone_id);
|
||||
|
||||
if( tz )
|
||||
{
|
||||
PT::Date local_date = tz->ToLocal(date);
|
||||
log << local_date;
|
||||
}
|
||||
else
|
||||
{
|
||||
(*this) << date << " UTC"; // unknown time zone identifier
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
(*this) << date << " UTC"; // time_zones object was not set
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const void * s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
buffer << s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const char * s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
if( !s )
|
||||
return *this;
|
||||
|
||||
buffer << s;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::string & s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
buffer << s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::string * s)
|
||||
{
|
||||
if( current_level > log_level )
|
||||
return *this;
|
||||
|
||||
buffer << *s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const wchar_t * s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
if( s )
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::wstring & s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const std::wstring * s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << *s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(int s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(long s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(char s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Log & Log::operator<<(wchar_t s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Log & Log::operator<<(size_t s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(double s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const PT::Space & s)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << s;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(const PT::Date & date)
|
||||
{
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << date;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log & Log::operator<<(LogManipulators m)
|
||||
{
|
||||
switch(m)
|
||||
{
|
||||
case logend:
|
||||
if( current_level <= log_level )
|
||||
{
|
||||
buffer << '\n';
|
||||
lines += 1;
|
||||
|
||||
if( save_each_line )
|
||||
SaveLogAndClear();
|
||||
}
|
||||
break;
|
||||
|
||||
case logsave:
|
||||
SaveLogAndClear();
|
||||
break;
|
||||
|
||||
case logendrequest:
|
||||
if( ++request >= max_requests || lines > max_lines )
|
||||
SaveLogAndClear();
|
||||
break;
|
||||
|
||||
case log1:
|
||||
current_level = 1;
|
||||
break;
|
||||
|
||||
case log2:
|
||||
current_level = 2;
|
||||
break;
|
||||
|
||||
case log3:
|
||||
current_level = 3;
|
||||
break;
|
||||
|
||||
case log4:
|
||||
current_level = 4;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
char Log::GetHEXdigit(unsigned char c)
|
||||
{
|
||||
if( c < 10 )
|
||||
return c + '0';
|
||||
|
||||
return c - 10 + 'A';
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Log::ToHEX(char * buf, unsigned char c)
|
||||
{
|
||||
buf[0] = GetHEXdigit(c >> 4);
|
||||
buf[1] = GetHEXdigit(c & 0xf);
|
||||
buf[2] = 0;
|
||||
}
|
||||
|
||||
|
||||
void Log::LogBinary(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 )
|
||||
{
|
||||
ToHEX(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<blob_len ; ++a, ++i)
|
||||
{
|
||||
if( blob[i] > 31 && blob[i] < 127 )
|
||||
buffer << blob[i];
|
||||
else
|
||||
buffer << '.';
|
||||
}
|
||||
|
||||
(*this) << logend;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Log::LogBinary(const std::string & blob)
|
||||
{
|
||||
LogBinary(blob.c_str(), blob.size());
|
||||
}
|
||||
|
||||
|
||||
void Log::SystemErr(int err)
|
||||
{
|
||||
(*this) << "errno: " << err;
|
||||
|
||||
const char * err_msg = strerror(err);
|
||||
|
||||
if( err_msg )
|
||||
(*this) << " (" << err_msg << ")";
|
||||
}
|
||||
|
||||
|
||||
void Log::SaveLogAndClear()
|
||||
{
|
||||
SaveLog();
|
||||
|
||||
buffer.Clear();
|
||||
request = 0;
|
||||
lines = 0;
|
||||
}
|
||||
|
||||
|
||||
void Log::SaveLog()
|
||||
{
|
||||
if( buffer.Str().empty() )
|
||||
return;
|
||||
|
||||
if( log_stdout )
|
||||
PT::WideToUTF8(buffer.Str(), std::cout);
|
||||
|
||||
if( log_file.empty() )
|
||||
return;
|
||||
|
||||
if( !log_file_open || !file )
|
||||
{
|
||||
file.close();
|
||||
file.clear();
|
||||
|
||||
OpenFile();
|
||||
|
||||
if( !file )
|
||||
return;
|
||||
}
|
||||
|
||||
PT::WideToUTF8(buffer.Str(), file);
|
||||
file.flush();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
||||
Reference in New Issue
Block a user