From 3612467cc01921d5382f289b70c450ebef450d9d Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 16 Dec 2022 18:45:45 +0800 Subject: [PATCH] 1 --- a8/a8.h | 1 - a8/ioloop.cc | 7 -- a8/udplog.cc | 221 --------------------------------------------------- a8/udplog.h | 39 --------- 4 files changed, 268 deletions(-) delete mode 100644 a8/udplog.cc delete mode 100644 a8/udplog.h diff --git a/a8/a8.h b/a8/a8.h index 5aaa0cc..01cb2d2 100644 --- a/a8/a8.h +++ b/a8/a8.h @@ -22,7 +22,6 @@ #include #include -#define NEW_NET #define A8_SAFE_DELETE(p) { if(p){delete(p); (p)=nullptr;} } #define A8_DEFINE_RANGE_BIT(type, begin, end) ((((type)1) << (end + 1)) - 1) & (~((((type)1) << (begin + 0)) - 1)) diff --git a/a8/ioloop.cc b/a8/ioloop.cc index 8ceefca..744d5ab 100644 --- a/a8/ioloop.cc +++ b/a8/ioloop.cc @@ -16,7 +16,6 @@ #include #include #include -#include #include enum IoLoopIMMsg_e @@ -209,9 +208,6 @@ namespace a8 void IoLoop::OnEvent(void* context, unsigned long long data) { - #ifdef DEBUG - a8::UdpLog::Instance()->Info("OnEvent %d", {data}); - #endif ++event_times; } @@ -220,9 +216,6 @@ namespace a8 IoLoopThreadContext* thread_context = (IoLoopThreadContext*)context; ++thread_context->tick; ++timer_times; - #ifdef DEBUG - a8::UdpLog::Instance()->Info("OnTimer %d", {data}); - #endif } void IoLoop::PostAsyncConnect(AsyncTcpClient* client, int timeout_ms) diff --git a/a8/udplog.cc b/a8/udplog.cc deleted file mode 100644 index 001f8d8..0000000 --- a/a8/udplog.cc +++ /dev/null @@ -1,221 +0,0 @@ -#include -#include -#include - -#include -#include - -namespace a8 -{ - struct UdpLogMsgNode - { - std::string logmsg; - UdpLogMsgNode* next = nullptr; - }; - - struct UdpLogImpl - { - unsigned short log_level = 0; - bool debuging = false; - std::string log_filename; - - std::thread* save_thread = nullptr; - bool save_thread_shutdown = false; - - std::mutex msg_mutex; - UdpLogMsgNode* top_node = nullptr; - UdpLogMsgNode* bot_node = nullptr; - UdpLogMsgNode* work_node = nullptr; - std::mutex *save_cond_mutex = nullptr; - std::condition_variable *save_cond = nullptr; - - }; - - UdpLog::UdpLog() - { - impl_ = new a8::UdpLogImpl(); - impl_->save_cond_mutex = new std::mutex(); - impl_->save_cond = new std::condition_variable(); - - impl_->log_level = 0; - #ifdef DEBUG - impl_->debuging = true; - #endif - impl_->top_node = nullptr; - impl_->bot_node = nullptr; - impl_->work_node = nullptr; - } - - UdpLog::~UdpLog() - { - delete impl_->save_cond_mutex; - impl_->save_cond_mutex = nullptr; - delete impl_->save_cond; - impl_->save_cond = nullptr; - delete impl_; - impl_ = nullptr; - } - - void UdpLog::Init() - { - impl_->save_thread_shutdown = false; - impl_->save_thread = new std::thread(&UdpLog::SaveToFileThreadProc, this); - } - - void UdpLog::UnInit() - { - impl_->save_thread_shutdown = true; - impl_->save_thread->join(); - delete impl_->save_thread; - impl_->save_thread = nullptr; - impl_->msg_mutex.lock(); - if (impl_->work_node) { - impl_->work_node = impl_->top_node; - impl_->top_node = nullptr; - impl_->bot_node = nullptr; - } - while (impl_->work_node) { - UdpLogMsgNode* pdelnode = impl_->work_node; - impl_->work_node = impl_->work_node->next; - if (!impl_->work_node) { - impl_->work_node = impl_->top_node; - impl_->top_node = nullptr; - impl_->bot_node = nullptr; - } - delete pdelnode; - } - impl_->msg_mutex.unlock(); - } - - void UdpLog::SetLogFileName(const std::string& filename) - { - impl_->log_filename = filename; - } - - void UdpLog::Emergency(const char *format, std::initializer_list args) - { - if (impl_->log_level > 6) { - return; - } - SendLog("[EMERGENCY]", format, args); - } - - void UdpLog::Alert(const char *format, std::initializer_list args) - { - if (impl_->log_level > 5) { - return; - } - SendLog("[ALERT]", format, args); - } - - void UdpLog::Error(const char *format, std::initializer_list args) - { - if (impl_->log_level > 4) { - return; - } - SendLog("[ERROR]", format, args); - } - - void UdpLog::Warning(const char *format, std::initializer_list args) - { - if (impl_->log_level > 3) { - return; - } - SendLog("[WARNING]", format, args); - } - - void UdpLog::Notice(const char *format, std::initializer_list args) - { - if (impl_->log_level > 2) { - return; - } - SendLog("[NOTICE]", format, args); - } - - void UdpLog::Info(const char *format, std::initializer_list args) - { - if (impl_->log_level > 1) { - return; - } - SendLog("[INFO]", format, args); - } - - void UdpLog::Debug(const char *format, std::initializer_list args) - { - if (impl_->log_level > 0) { - return; - } - SendLog("[DEBUG]", format, args); - } - - void UdpLog::SendLog(const char *category, const char *format, std::initializer_list& args) - { - time_t nowtime = time(nullptr); - struct tm tm_nowtime = {0}; - struct tm *ptr = localtime_r(&nowtime, &tm_nowtime); - char strtime[80]; - strftime(strtime, 80, "%F %T", ptr); - - UdpLogMsgNode *p = new UdpLogMsgNode(); - p->logmsg.reserve(128); - p->logmsg.append((char*)strtime); - p->logmsg.append(category); - p->logmsg.append(" "); - p->logmsg.append(a8::FormatEx(format, args)); - p->logmsg.append("\n"); - if (impl_->debuging) { - printf("%s", p->logmsg.c_str()); - } - impl_->msg_mutex.lock(); - if (impl_->bot_node) { - impl_->bot_node->next = p; - impl_->bot_node = p; - } else { - impl_->top_node = p; - impl_->bot_node = p; - } - impl_->msg_mutex.unlock(); - { - std::unique_lock lk(*impl_->save_cond_mutex); - impl_->save_cond->notify_all(); - } - } - - void UdpLog::SaveToFileThreadProc() - { - while (!impl_->save_thread_shutdown) { - if (!impl_->work_node && impl_->top_node) { - impl_->msg_mutex.lock(); - impl_->work_node = impl_->top_node; - impl_->top_node = nullptr; - impl_->bot_node = nullptr; - impl_->msg_mutex.unlock(); - } - - if (impl_->work_node) { - time_t nowtime = time(nullptr); - struct tm tm_nowtime = {0}; - localtime_r(&nowtime, &tm_nowtime); - - char szfilename[256]; - strftime(szfilename, a8::ArraySize(szfilename), impl_->log_filename.c_str(), &tm_nowtime); - std::string filename((char*)szfilename); - FILE *logfile = fopen(filename.c_str(), "a+"); - if (logfile) { - while (impl_->work_node) { - UdpLogMsgNode *nextnode = impl_->work_node->next; - fwrite(impl_->work_node->logmsg.c_str(), 1, impl_->work_node->logmsg.length(), logfile); - delete impl_->work_node; - impl_->work_node = nextnode; - } - fclose(logfile); - } - } - { - std::unique_lock lk(*impl_->save_cond_mutex); - impl_->save_cond->wait_for(lk, std::chrono::seconds(10)); - } - } - } - -} diff --git a/a8/udplog.h b/a8/udplog.h deleted file mode 100644 index 2c71f43..0000000 --- a/a8/udplog.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include - -namespace a8 -{ - - struct UdpLogImpl; - class UdpLog : public a8::Singleton - { - private: - UdpLog(); - friend class a8::Singleton; - - public: - ~UdpLog(); - - void Init(); - void UnInit(); - - void SetLogFileName(const std::string& filename); - - void Emergency(const char *format, std::initializer_list args); - void Alert(const char *format, std::initializer_list args); - void Error(const char *format, std::initializer_list args); - void Warning(const char *format, std::initializer_list args); - void Notice(const char *format, std::initializer_list args); - void Info(const char *format, std::initializer_list args); - void Debug(const char *format, std::initializer_list args); - - private: - void SendLog(const char *category, const char *format, std::initializer_list& args); - void SaveToFileThreadProc(); - - private: - a8::UdpLogImpl* impl_ = nullptr; - }; - -}