From 02e8212d0b26834bc1d581c6d7ea752328aad0be Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 16 Dec 2022 18:58:34 +0800 Subject: [PATCH] 1 --- a8/jsonlog.cc | 131 -------------------------------------------------- a8/jsonlog.h | 32 ------------ 2 files changed, 163 deletions(-) delete mode 100644 a8/jsonlog.cc delete mode 100644 a8/jsonlog.h diff --git a/a8/jsonlog.cc b/a8/jsonlog.cc deleted file mode 100644 index 29f3689..0000000 --- a/a8/jsonlog.cc +++ /dev/null @@ -1,131 +0,0 @@ -#include -#include -#include - -#include -#include - -namespace a8 -{ - struct JsonLogMsgNode - { - a8::XObject* xobj = nullptr; - JsonLogMsgNode* next = nullptr; - }; - - struct JsonLogImpl - { - std::string log_filename; - - std::thread* save_thread = nullptr; - bool save_thread_shutdown = false; - - std::mutex msg_mutex; - JsonLogMsgNode* top_node = nullptr; - JsonLogMsgNode* bot_node = nullptr; - JsonLogMsgNode* work_node = nullptr; - std::mutex *save_cond_mutex = nullptr; - std::condition_variable *save_cond = nullptr; - }; - - JsonLog::JsonLog() - { - impl_ = new a8::JsonLogImpl(); - impl_->save_cond_mutex = new std::mutex(); - impl_->save_cond = new std::condition_variable(); - - impl_->top_node = nullptr; - impl_->bot_node = nullptr; - impl_->work_node = nullptr; - } - - JsonLog::~JsonLog() - { - delete impl_->save_cond_mutex; - impl_->save_cond_mutex = nullptr; - delete impl_->save_cond; - impl_->save_cond = nullptr; - delete impl_; - impl_ = nullptr; - } - - void JsonLog::Init() - { - impl_->save_thread_shutdown = false; - impl_->save_thread = new std::thread(&JsonLog::SaveToFileThreadProc, this); - } - - void JsonLog::UnInit() - { - impl_->save_thread_shutdown = true; - impl_->save_thread->join(); - delete impl_->save_thread; - impl_->save_thread = nullptr; - } - - void JsonLog::SetLogFileName(const std::string& filename) - { - impl_->log_filename = filename; - } - - void JsonLog::AddLog(a8::XObject* xobj) - { - JsonLogMsgNode *p = new JsonLogMsgNode(); - p->xobj = xobj; - 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 JsonLog::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) { - JsonLogMsgNode *nextnode = impl_->work_node->next; - std::string logmsg; - impl_->work_node->xobj->ToJsonStr(logmsg); - logmsg.push_back('\n'); - fwrite(logmsg.c_str(), 1, logmsg.size(), logfile); - delete impl_->work_node->xobj; - 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/jsonlog.h b/a8/jsonlog.h deleted file mode 100644 index 2fc79f7..0000000 --- a/a8/jsonlog.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include - -namespace a8 -{ - - struct JsonLogImpl; - class JsonLog : public a8::Singleton - { - private: - JsonLog(); - friend class a8::Singleton; - - public: - ~JsonLog(); - - void Init(); - void UnInit(); - - void SetLogFileName(const std::string& filename); - - void AddLog(a8::XObject* logobj); - - private: - void SaveToFileThreadProc(); - - private: - a8::JsonLogImpl* impl_ = nullptr; - }; - -}