自己写的一个小LOGGER
awtools.h
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#ifndef AWTOOLS_H_
#define AWTOOLS_H_
#include <sstream>
namespace AWTookit
{
template<class Target, class Source> Target lexical_cast(Source arg)
{
std::stringstream interpreter;
Target result;
if(!(interpreter<<arg)||!(interpreter>>result)||!(interpreter>>std::ws).eof())
throw std::bad_cast();
return result;
}
extern int get_process_id();
extern std::string replace_space(const std::string& str);
}
#endif
#define AWTOOLS_H_
#include <sstream>
namespace AWTookit
{
template<class Target, class Source> Target lexical_cast(Source arg)
{
std::stringstream interpreter;
Target result;
if(!(interpreter<<arg)||!(interpreter>>result)||!(interpreter>>std::ws).eof())
throw std::bad_cast();
return result;
}
extern int get_process_id();
extern std::string replace_space(const std::string& str);
}
#endif
awtools.cpp
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include "awtools.h"
#ifdef _WIN32
#include <windows.h>
#endif
namespace AWTookit
{
int get_process_id()
{
#ifdef _WIN32
return (int)GetCurrentProcessId();
#else
return (int)getpid();
#endif
}
std::string replace_space(const std::string& str)
{
std::string ret(str);
int n = str.length();
for (int i = 0; i < n; ++i)
if (ret[i] == ' ')ret[i] = '_';
return ret;
}
}
#ifdef _WIN32
#include <windows.h>
#endif
namespace AWTookit
{
int get_process_id()
{
#ifdef _WIN32
return (int)GetCurrentProcessId();
#else
return (int)getpid();
#endif
}
std::string replace_space(const std::string& str)
{
std::string ret(str);
int n = str.length();
for (int i = 0; i < n; ++i)
if (ret[i] == ' ')ret[i] = '_';
return ret;
}
}
awlogger.h
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
//
// Copyright (c) 2009 - 2010
//
// Author: Weiyu(Adams) Wang
//
// Lightweight Logger
//
#ifndef AWLOGGER_H_
#define AWLOGGER_H_
#include <fstream>
#include <string>
#include <iostream>
#include "awtools.h"
#ifdef _WIN32
#include <windows.h>
#endif
namespace AWTookit
{
#define DISABLE_LOG 0x00000000
#define ENABLE_LOG 0x00000001
#define ENABLE_ERROR_LOG 0x00000001
#define ENABLE_WARNING_LOG 0x00000002
#define ENABLE_INFO_LOG 0x00000004
class AWLogger
{
friend AWLogger& logger();
private:
AWLogger();
~AWLogger();
private:
int flag;
std::ofstream log_file;
std::string m_log;
private:
AWLogger( const AWLogger& );
AWLogger& operator=(const AWLogger& );
public:
const std::string& get_log_name();
const int get_flag()const;
void set_flag(const int);
static const std::string get_time();
void open_log_file(const std::string& module);
void close_log_file();
CRITICAL_SECTION _criticalSection;
std::ofstream lfile;
};
extern AWLogger& logger();
}
#define AWLOG_INIT(str) AWTookit::logger().open_log_file(str);
#define AWLOG_CLOSE() AWTookit::logger().close_log_file();
#define AWLOG_ERROR(str) \
if (AWTookit::logger().get_flag() & ENABLE_ERROR_LOG ){\
EnterCriticalSection(&(AWTookit::logger()._criticalSection));\
AWTookit::logger().lfile << "[ERROR: ][" <<AWLogger::get_time().substr(4,15)<<"]: "\
<<__FILE__ << "(" << __LINE__ << ")," << str << std::endl;\
LeaveCriticalSection(&(AWTookit::logger()._criticalSection));}
#define AWLOG_WARNING(str) \
if (AWTookit::logger().get_flag() & ENABLE_WARNING_LOG ){\
EnterCriticalSection(&(AWTookit::logger()._criticalSection));\
AWTookit::logger().lfile<< "[WARNING:][" <<AWLogger::get_time().substr(4,15)<<"]: "\
<<__FILE__ << "(" << __LINE__ << ")," << str << std::endl;\
LeaveCriticalSection(&(AWTookit::logger()._criticalSection));}
#define AWLOG_INFO(str) \
if ( AWTookit::logger().get_flag() & ENABLE_INFO_LOG ){\
EnterCriticalSection(&(AWTookit::logger()._criticalSection));\
AWTookit::logger().lfile << "[INFO: ][" <<AWLogger::get_time().substr(4,15)<<"]: "\
<< str << std::endl;\
LeaveCriticalSection(&(AWTookit::logger()._criticalSection));}
#define AWLOG_COUT(str) \
std::cout<< "["<<get_process_id()<<"][" << AWLogger::get_time().substr(4,15)<<"] "<< str << std::endl;
#define AWLOG_ON() AWTookit::logger().set_flag( ENABLE_LOG );
#define AWLOG_OFF() AWTookit::logger().set_flag( DISABLE_LOG );
#define AWLOG_SWITCH_MODE(flg)AWTookit::logger().set_flag( flg );
#endif
// Copyright (c) 2009 - 2010
//
// Author: Weiyu(Adams) Wang
//
// Lightweight Logger
//
#ifndef AWLOGGER_H_
#define AWLOGGER_H_
#include <fstream>
#include <string>
#include <iostream>
#include "awtools.h"
#ifdef _WIN32
#include <windows.h>
#endif
namespace AWTookit
{
#define DISABLE_LOG 0x00000000
#define ENABLE_LOG 0x00000001
#define ENABLE_ERROR_LOG 0x00000001
#define ENABLE_WARNING_LOG 0x00000002
#define ENABLE_INFO_LOG 0x00000004
class AWLogger
{
friend AWLogger& logger();
private:
AWLogger();
~AWLogger();
private:
int flag;
std::ofstream log_file;
std::string m_log;
private:
AWLogger( const AWLogger& );
AWLogger& operator=(const AWLogger& );
public:
const std::string& get_log_name();
const int get_flag()const;
void set_flag(const int);
static const std::string get_time();
void open_log_file(const std::string& module);
void close_log_file();
CRITICAL_SECTION _criticalSection;
std::ofstream lfile;
};
extern AWLogger& logger();
}
#define AWLOG_INIT(str) AWTookit::logger().open_log_file(str);
#define AWLOG_CLOSE() AWTookit::logger().close_log_file();
#define AWLOG_ERROR(str) \
if (AWTookit::logger().get_flag() & ENABLE_ERROR_LOG ){\
EnterCriticalSection(&(AWTookit::logger()._criticalSection));\
AWTookit::logger().lfile << "[ERROR: ][" <<AWLogger::get_time().substr(4,15)<<"]: "\
<<__FILE__ << "(" << __LINE__ << ")," << str << std::endl;\
LeaveCriticalSection(&(AWTookit::logger()._criticalSection));}
#define AWLOG_WARNING(str) \
if (AWTookit::logger().get_flag() & ENABLE_WARNING_LOG ){\
EnterCriticalSection(&(AWTookit::logger()._criticalSection));\
AWTookit::logger().lfile<< "[WARNING:][" <<AWLogger::get_time().substr(4,15)<<"]: "\
<<__FILE__ << "(" << __LINE__ << ")," << str << std::endl;\
LeaveCriticalSection(&(AWTookit::logger()._criticalSection));}
#define AWLOG_INFO(str) \
if ( AWTookit::logger().get_flag() & ENABLE_INFO_LOG ){\
EnterCriticalSection(&(AWTookit::logger()._criticalSection));\
AWTookit::logger().lfile << "[INFO: ][" <<AWLogger::get_time().substr(4,15)<<"]: "\
<< str << std::endl;\
LeaveCriticalSection(&(AWTookit::logger()._criticalSection));}
#define AWLOG_COUT(str) \
std::cout<< "["<<get_process_id()<<"][" << AWLogger::get_time().substr(4,15)<<"] "<< str << std::endl;
#define AWLOG_ON() AWTookit::logger().set_flag( ENABLE_LOG );
#define AWLOG_OFF() AWTookit::logger().set_flag( DISABLE_LOG );
#define AWLOG_SWITCH_MODE(flg)AWTookit::logger().set_flag( flg );
#endif
awlogger.cpp
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include <fstream>
#include <ctime>
#include "awlogger.h"
namespace AWTookit
{
AWLogger::AWLogger()
{
flag = ENABLE_ERROR_LOG|ENABLE_WARNING_LOG|ENABLE_INFO_LOG;
InitializeCriticalSection(&_criticalSection);
}
AWLogger::~AWLogger()
{
log_file.close();
}
const std::string& AWLogger::get_log_name()
{
return m_log;
}
const int AWLogger::get_flag()const
{
return flag;
}
void AWLogger::set_flag(const int flg)
{
flag = flg;
}
const std::string AWLogger::get_time()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
return asctime (timeinfo);
}
void AWLogger::open_log_file(const std::string& module)
{
std::string time_stamp = AWLogger::get_time().substr(0,24);
for (int i = 0; i < 24; ++i)
if (time_stamp[i] == ' ' || time_stamp[i] == ':')
{
time_stamp[i] = '_';
};
m_log = module + "." + time_stamp + ".log";
lfile.open(m_log.c_str(), std::ios_base::app);
lfile << "--------------------------------------------------------------------------------\n";
lfile << " Copyright Weiyu Wang\n";
lfile << " Order Server Built at : "<<__DATE__ << " " << __TIME__ << "\n";
lfile << " Order Server Start at : "<<get_time();
lfile << "--------------------------------------------------------------------------------"<<std::endl;
}
void AWLogger::close_log_file()
{
lfile.close();
}
AWLogger& logger()
{
static AWLogger logger;
return logger;
}
}
#include <ctime>
#include "awlogger.h"
namespace AWTookit
{
AWLogger::AWLogger()
{
flag = ENABLE_ERROR_LOG|ENABLE_WARNING_LOG|ENABLE_INFO_LOG;
InitializeCriticalSection(&_criticalSection);
}
AWLogger::~AWLogger()
{
log_file.close();
}
const std::string& AWLogger::get_log_name()
{
return m_log;
}
const int AWLogger::get_flag()const
{
return flag;
}
void AWLogger::set_flag(const int flg)
{
flag = flg;
}
const std::string AWLogger::get_time()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
return asctime (timeinfo);
}
void AWLogger::open_log_file(const std::string& module)
{
std::string time_stamp = AWLogger::get_time().substr(0,24);
for (int i = 0; i < 24; ++i)
if (time_stamp[i] == ' ' || time_stamp[i] == ':')
{
time_stamp[i] = '_';
};
m_log = module + "." + time_stamp + ".log";
lfile.open(m_log.c_str(), std::ios_base::app);
lfile << "--------------------------------------------------------------------------------\n";
lfile << " Copyright Weiyu Wang\n";
lfile << " Order Server Built at : "<<__DATE__ << " " << __TIME__ << "\n";
lfile << " Order Server Start at : "<<get_time();
lfile << "--------------------------------------------------------------------------------"<<std::endl;
}
void AWLogger::close_log_file()
{
lfile.close();
}
AWLogger& logger()
{
static AWLogger logger;
return logger;
}
}
注意一点, 最后用了CriticalSection, 不过稍微改一下就可以跨平台了
用这个类
mutex.h
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#ifndef MUTEX_H
#define MUTEX_H
#include "utility.h"
namespace FIX
{
/// Portable implementation of a mutex.
class Mutex
{
private:
#ifdef _MSC_VER
CRITICAL_SECTION m_mutex;
#else
pthread_mutex_t m_mutex;
pthread_t m_threadID;
int m_count;
#endif
public:
Mutex()
{
#ifdef _MSC_VER
InitializeCriticalSection( &m_mutex );
#else
m_count = 0;
m_threadID = 0;
pthread_mutex_init( &m_mutex, 0 );
#endif
}
~Mutex()
{
#ifdef _MSC_VER
DeleteCriticalSection( &m_mutex );
#else
pthread_mutex_destroy( &m_mutex );
#endif
}
void lock()
{
#ifdef _MSC_VER
EnterCriticalSection( &m_mutex );
#else
if ( m_count && m_threadID == pthread_self() )
{ ++m_count; return ; }
pthread_mutex_lock( &m_mutex );
++m_count;
m_threadID = pthread_self();
#endif
}
void unlock()
{
#ifdef _MSC_VER
LeaveCriticalSection( &m_mutex );
#else
if ( m_count > 1 )
{ m_count--; return ; }
--m_count;
m_threadID = 0;
pthread_mutex_unlock( &m_mutex );
#endif
}
};
}
#endif //MUTEX_H
#define MUTEX_H
#include "utility.h"
namespace FIX
{
/// Portable implementation of a mutex.
class Mutex
{
private:
#ifdef _MSC_VER
CRITICAL_SECTION m_mutex;
#else
pthread_mutex_t m_mutex;
pthread_t m_threadID;
int m_count;
#endif
public:
Mutex()
{
#ifdef _MSC_VER
InitializeCriticalSection( &m_mutex );
#else
m_count = 0;
m_threadID = 0;
pthread_mutex_init( &m_mutex, 0 );
#endif
}
~Mutex()
{
#ifdef _MSC_VER
DeleteCriticalSection( &m_mutex );
#else
pthread_mutex_destroy( &m_mutex );
#endif
}
void lock()
{
#ifdef _MSC_VER
EnterCriticalSection( &m_mutex );
#else
if ( m_count && m_threadID == pthread_self() )
{ ++m_count; return ; }
pthread_mutex_lock( &m_mutex );
++m_count;
m_threadID = pthread_self();
#endif
}
void unlock()
{
#ifdef _MSC_VER
LeaveCriticalSection( &m_mutex );
#else
if ( m_count > 1 )
{ m_count--; return ; }
--m_count;
m_threadID = 0;
pthread_mutex_unlock( &m_mutex );
#endif
}
};
}
#endif //MUTEX_H