523 lines
9.7 KiB
C++
523 lines
9.7 KiB
C++
#pragma once
|
|
|
|
#include <windows.h>
|
|
#include <stdio.h>
|
|
#include <wtypes.h>
|
|
#include <list>
|
|
#include <WINDEF.H>
|
|
#include <wtypes.h>
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#pragma warning(disable:4996)
|
|
#include <memory>>
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <ctime>
|
|
#include <mutex>
|
|
|
|
using namespace std;
|
|
#define MEGABYTE_SIZE (1024*1024)
|
|
#define LOGFILE_SIZE (1024*1024*20) //°¢ ¸ðµâ¿¡¼ ¾Ë¾Æ¼ define ÇÑ´Ù.
|
|
#define MAX_BSONE_DEBUG_LEN 2048
|
|
|
|
#include "_log4cpp/log4cpp/Category.hh"
|
|
#include "_log4cpp/log4cpp/Appender.hh"
|
|
#include "_log4cpp/log4cpp/FileAppender.hh"
|
|
#include "_log4cpp/log4cpp/OstreamAppender.hh"
|
|
#include "_log4cpp/log4cpp/Layout.hh"
|
|
#include "_log4cpp/log4cpp/BasicLayout.hh"
|
|
#include "_log4cpp/log4cpp/Priority.hh"
|
|
#include "_log4cpp/log4cpp/PatternLayout.hh"
|
|
#include "_log4cpp/log4cpp/RollingFileAppender.hh"
|
|
|
|
#if defined(WIN32)
|
|
#include "_log4cpp/log4cpp/Win32DebugAppender.hh"
|
|
|
|
#if defined(_WIN64)
|
|
|
|
#if defined(_MT) & defined(_DLL)
|
|
#if defined(_DEBUG)
|
|
#pragma comment(lib, "_log4cpp/log4cpp/static/log4cppLIB_mdd_x64.lib")
|
|
#else
|
|
#pragma comment(lib, "_log4cpp/log4cpp/static/log4cppLIB_md_x64.lib")
|
|
#endif
|
|
#else
|
|
#if defined(_DEBUG)
|
|
#pragma comment(lib, "_log4cpp/log4cpp/static/log4cppLIB_mtd_x64.lib")
|
|
#else
|
|
#pragma comment(lib, "_log4cpp/log4cpp/static/log4cppLIB_mt_x64.lib")
|
|
#endif
|
|
#endif
|
|
#else
|
|
#if defined(_MT) & defined(_DLL)
|
|
#if defined(_DEBUG)
|
|
#pragma comment(lib, "_log4cpp/log4cpp/static/log4cppLIB_mdd.lib")
|
|
#else
|
|
#pragma comment(lib, "_log4cpp/log4cpp/static/log4cppLIB_md.lib")
|
|
#endif
|
|
#else
|
|
#if defined(_DEBUG)
|
|
#pragma comment(lib, "_log4cpp/log4cpp/static/log4cppLIB_mtd.lib")
|
|
#else
|
|
#pragma comment(lib, "_log4cpp/log4cpp/static/log4cppLIB_mt.lib")
|
|
#endif
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#endif
|
|
|
|
typedef log4cpp::Category CATEGORY;
|
|
|
|
class Log4cppLogger
|
|
{
|
|
public:
|
|
Log4cppLogger(const char* instance_name) :
|
|
out(CATEGORY::getInstance(instance_name)),
|
|
layout_(nullptr),
|
|
appender_(nullptr)
|
|
{
|
|
|
|
debug_level_ = 700;
|
|
show_debug_ = true;
|
|
debug_type_ = 0;
|
|
logFileSize_ = 1024 * 1024 * 20; //20MB
|
|
classification_ = 0;
|
|
}
|
|
|
|
~Log4cppLogger()
|
|
{
|
|
|
|
try {
|
|
log4cpp::Category::shutdownForced();
|
|
out.removeAllAppenders();
|
|
appender_.release();
|
|
layout_.release();
|
|
}
|
|
catch (...) {
|
|
// prevent exceptions during destruction
|
|
}
|
|
}
|
|
|
|
void make_dirA(char* p_dirpath)
|
|
{
|
|
char dirName[MAX_PATH * 2] = { 0, };
|
|
char* p = p_dirpath;
|
|
char* q = dirName;
|
|
|
|
while (*p)
|
|
{
|
|
if (('\\' == *p))
|
|
{
|
|
if (':' != *(p - 1))
|
|
{
|
|
|
|
CreateDirectoryA(dirName, NULL);
|
|
}
|
|
}
|
|
|
|
*q++ = *p++;
|
|
*q = '\0';
|
|
}
|
|
|
|
CreateDirectoryA(dirName, NULL);
|
|
}
|
|
void set_layout()
|
|
{
|
|
layout_.reset(new log4cpp::PatternLayout());
|
|
static_cast<log4cpp::PatternLayout*>(layout_.get())->setConversionPattern("%d %c::%p %m\n");
|
|
}
|
|
|
|
void set_Priority(int debug_level)
|
|
{
|
|
debug_level_ = debug_level;
|
|
out.setPriority(debug_level);
|
|
}
|
|
//static Log4cppLogger* getInstance()
|
|
//{
|
|
// if (inst == nullptr)
|
|
// inst = new Singleton();
|
|
|
|
// return inst;
|
|
//}
|
|
void show_debug(bool show_debug)
|
|
{
|
|
show_debug_ = show_debug;
|
|
}
|
|
|
|
bool isdebug() const
|
|
{
|
|
return show_debug_;
|
|
}
|
|
|
|
|
|
//
|
|
// convert_utf8_to_unicode_string
|
|
//
|
|
|
|
DWORD convert_utf8_to_unicode_string(
|
|
__out std::wstring& unicode,
|
|
__in const char* utf8,
|
|
__in const size_t utf8_size
|
|
) {
|
|
|
|
unicode = L"";
|
|
DWORD error = 0;
|
|
|
|
do {
|
|
|
|
if ((NULL == utf8) || (0 == utf8_size)) {
|
|
error = ERROR_INVALID_PARAMETER;
|
|
break;
|
|
}
|
|
|
|
unicode.clear();
|
|
|
|
//
|
|
// getting required cch.
|
|
//
|
|
|
|
int required_cch = ::MultiByteToWideChar(
|
|
CP_UTF8,
|
|
MB_ERR_INVALID_CHARS,
|
|
utf8, static_cast<int>(utf8_size),
|
|
NULL, 0
|
|
);
|
|
if (0 == required_cch) {
|
|
error = ::GetLastError();
|
|
break;
|
|
}
|
|
|
|
//
|
|
// allocate.
|
|
//
|
|
|
|
unicode.resize(required_cch);
|
|
|
|
//
|
|
// convert.
|
|
//
|
|
|
|
if (0 == ::MultiByteToWideChar(
|
|
CP_UTF8,
|
|
MB_ERR_INVALID_CHARS,
|
|
utf8, static_cast<int>(utf8_size),
|
|
const_cast<wchar_t*>(unicode.c_str()), static_cast<int>(unicode.size())
|
|
)) {
|
|
error = ::GetLastError();
|
|
break;
|
|
}
|
|
|
|
} while (false);
|
|
|
|
return error;
|
|
}
|
|
|
|
|
|
|
|
|
|
DWORD convert_unicode_to_ansi_string(
|
|
__out std::string& ansi,
|
|
__in const wchar_t* unicode,
|
|
__in const size_t unicode_size
|
|
) {
|
|
|
|
ansi = "";
|
|
DWORD error = 0;
|
|
|
|
do {
|
|
|
|
if ((NULL == unicode) || (0 == unicode_size)) {
|
|
error = ERROR_INVALID_PARAMETER;
|
|
break;
|
|
}
|
|
|
|
ansi.clear();
|
|
|
|
//
|
|
// getting required cch.
|
|
//
|
|
|
|
int required_cch = ::WideCharToMultiByte(
|
|
CP_ACP,
|
|
0,
|
|
unicode, static_cast<int>(unicode_size),
|
|
NULL, 0,
|
|
NULL, NULL
|
|
);
|
|
|
|
if (0 == required_cch) {
|
|
error = ::GetLastError();
|
|
break;
|
|
}
|
|
|
|
//
|
|
// allocate.
|
|
//
|
|
|
|
ansi.resize(required_cch);
|
|
|
|
//
|
|
// convert.
|
|
//
|
|
|
|
if (0 == ::WideCharToMultiByte(
|
|
CP_ACP,
|
|
0,
|
|
unicode, static_cast<int>(unicode_size),
|
|
const_cast<char*>(ansi.c_str()), static_cast<int>(ansi.size()),
|
|
NULL, NULL
|
|
)) {
|
|
error = ::GetLastError();
|
|
break;
|
|
}
|
|
|
|
} while (false);
|
|
|
|
return error;
|
|
}
|
|
|
|
DWORD convert_utf8_to_ansi_pointer(string& utf8_to_ansi)
|
|
{
|
|
DWORD error;
|
|
wstring uni;
|
|
string ansi;
|
|
|
|
error = convert_utf8_to_unicode_string(uni, utf8_to_ansi.c_str(), utf8_to_ansi.length());
|
|
if (error)
|
|
return error;
|
|
//BSONE_DEBUGW(L"Java_CODITSCAN_PiCheck_GetPrivateInfoString UNI (%s)\n", uni.c_str());
|
|
utf8_to_ansi = "";
|
|
|
|
error = convert_unicode_to_ansi_string(ansi, uni.c_str(), uni.length());
|
|
if (error)
|
|
return error;
|
|
|
|
utf8_to_ansi = ansi;
|
|
|
|
return error;
|
|
}
|
|
|
|
void set_classification(int classification)
|
|
{
|
|
classification_ = classification;
|
|
return;
|
|
}
|
|
|
|
void is_classification()
|
|
{
|
|
if (classification_ == 1 && debug_type_ == 2)
|
|
{
|
|
string timestamp;
|
|
get_current_timestemp(timestamp);
|
|
if (currentTime_.compare(timestamp) == 0)
|
|
return;
|
|
|
|
convert_logFilePath(log_name_);
|
|
|
|
out.removeAllAppenders();
|
|
appender_.reset(new log4cpp::RollingFileAppender("default", log_name_, logFileSize_, 10, true));
|
|
set_layout();
|
|
//log4cpp::Priority
|
|
appender_->setLayout(layout_.get());
|
|
out.addAppender(appender_.get());
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
void get_current_timestemp(string& currentTime)
|
|
{
|
|
time_t current_time = 0;
|
|
struct tm* struct_time = NULL;
|
|
char szTime[1024] = { 0, };
|
|
|
|
time(¤t_time);
|
|
struct_time = localtime(¤t_time);
|
|
|
|
int year = (struct_time->tm_year + 1900);
|
|
int mon = struct_time->tm_mon + 1;
|
|
int mday = struct_time->tm_mday;
|
|
|
|
sprintf(szTime, "%04d-%d-%d", year, mon, mday);
|
|
currentTime = szTime;
|
|
return;
|
|
}
|
|
|
|
void convert_logFilePath(string& log_path)
|
|
{
|
|
if (classification_ == 1)
|
|
{
|
|
get_current_timestemp(currentTime_);
|
|
|
|
log_path = logFilePath_;
|
|
log_path += currentTime_;
|
|
|
|
make_dirA((char*)log_path.c_str());
|
|
log_path += "\\";
|
|
log_path += logFileName_;
|
|
|
|
}
|
|
else
|
|
{
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
void init_log4cpp(const char* log_name, int debug_type = 1, int logfilesize = LOGFILE_SIZE, bool show_debug = true)
|
|
{
|
|
std::lock_guard<std::mutex> lock(logMutex);
|
|
debug_type_ = debug_type;
|
|
log_name_ = log_name;
|
|
show_debug_ = show_debug;
|
|
logFileSize_ = logfilesize;
|
|
|
|
std:cout << "log_name_" << log_name_.c_str() << "debug_type" << debug_type << "show_debug" << show_debug << "logfilesize" << logfilesize << "\n";
|
|
|
|
if (appender_.get() != nullptr)
|
|
return;
|
|
|
|
if (debug_type_ == 2)
|
|
{
|
|
int find = (int)log_name_.rfind("\\") + 1;
|
|
|
|
logFilePath_ = log_name_.substr(0, find);
|
|
logFileName_ = log_name_.substr(find, log_name_.length() - find);
|
|
|
|
convert_logFilePath(log_name_);
|
|
//printf("log_name_(%s), ", log_name_.c_str());
|
|
appender_.reset(new log4cpp::RollingFileAppender("default", log_name_, logFileSize_, 5, true));
|
|
|
|
}
|
|
else if (debug_type_ == 1)
|
|
{
|
|
#if defined(WIN32)
|
|
appender_.reset(new log4cpp::Win32DebugAppender("default"));
|
|
#else
|
|
appender_.reset(new log4cpp::OstreamAppender("default", &std::cout));
|
|
#endif
|
|
}
|
|
else if (debug_type_ == 4)
|
|
{
|
|
appender_.reset(new log4cpp::OstreamAppender("default", &std::cout));
|
|
}
|
|
else
|
|
{
|
|
show_debug_ = false;
|
|
return;
|
|
}
|
|
|
|
set_layout();
|
|
|
|
if (layout_.get() != nullptr) {
|
|
appender_->setLayout(layout_.get());
|
|
}
|
|
|
|
set_Priority(debug_level_);
|
|
out.addAppender(appender_.get());
|
|
}
|
|
|
|
void BSONE_DEBUG_TEST(int type, const char* fmt, ...)
|
|
{
|
|
if (!isdebug())
|
|
return;
|
|
|
|
is_classification();
|
|
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
out.logva(type, fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
|
|
|
|
void BSONE_DEBUG_LOG4(int type, const char* fmt, ...)
|
|
{
|
|
if (!isdebug())
|
|
return;
|
|
|
|
is_classification();
|
|
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
out.logva(type, fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
|
|
|
|
void BSONE_DEBUG_UTF8A(int type, const char* fmt, ...)
|
|
{
|
|
if (!isdebug())
|
|
return;
|
|
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
|
|
is_classification();
|
|
|
|
#if defined(WIN32)
|
|
|
|
char buff[1024 + 1] = { 0, };
|
|
string s;
|
|
|
|
_vsnprintf(buff, sizeof(buff) - 1, fmt, ap);
|
|
s = buff;
|
|
convert_utf8_to_ansi_pointer(s);
|
|
out.log(type, s);
|
|
#else
|
|
|
|
out.logva(type, fmt, ap);
|
|
va_end(ap);
|
|
#endif
|
|
}
|
|
|
|
#if defined(WIN32)
|
|
|
|
|
|
void BSONE_DEBUG_LOG4W(int type, const wchar_t* fmt, ...)
|
|
{
|
|
wchar_t buff[1024 + 1] = { 0, };
|
|
va_list ap;
|
|
string s = "";
|
|
|
|
if (!isdebug())
|
|
return;
|
|
|
|
is_classification();
|
|
|
|
va_start(ap, fmt);
|
|
int n = _vsnwprintf(buff, ARRAYSIZE(buff) - 1, fmt, ap);
|
|
if (n < 0) buff[0] = L'\0'; // ½ÇÆÐ ½Ã ºó ¹®ÀÚ¿·Î
|
|
|
|
buff[ARRAYSIZE(buff) - 1] = L'\0'; // ¸í½ÃÀûÀ¸·Î ³Î Á¾·á
|
|
convert_unicode_to_ansi_string(s, buff, wcslen(buff));
|
|
out.log(type, s.c_str());
|
|
va_end(ap);
|
|
}
|
|
|
|
|
|
#endif
|
|
private:
|
|
CATEGORY& out;
|
|
string instance_name_;
|
|
string log_name_;
|
|
#if defined(CENTOS6)
|
|
log4cpp::Appender* appender_;
|
|
log4cpp::PatternLayout* layout_;
|
|
#else
|
|
std::auto_ptr<log4cpp::Layout> layout_;
|
|
std::auto_ptr<log4cpp::Appender> appender_;
|
|
#endif
|
|
int debug_type_;
|
|
bool show_debug_;
|
|
int debug_level_;
|
|
string logFilePath_;
|
|
string logFileName_;
|
|
int logFileSize_;
|
|
int classification_;
|
|
string currentTime_;
|
|
|
|
std::mutex logMutex;
|
|
|
|
}; |