OSDN Git Service

logging処理の追加
[gikomona/libcore.git] / src / GikoMona.cpp
index 74d450c..e9e65f8 100644 (file)
-#include <wx/xrc/xmlres.h>
 #include <exception>
+#include <memory>
+#include <sstream>
 
 #include <boost/filesystem.hpp>
+#include <boost/log/trivial.hpp>
 
+#include <wx/xrc/xmlres.h>
 #include <wx/msgdlg.h>
 
 #include "GikoMona.hpp"
 #include "database.hpp"
 #include "model.hpp"
+#include "communication/post-office.hpp"
 
 #include "extension.hpp"
 
+namespace monazilla { namespace GikoMona {
+
+const char* severity_names[] = {
+    "debug_trace",
+    "debug_message",
+    "debug_warning",
+    "debug_error",
+    
+    "trace",
+    "message",
+    "warning",
+    "error",
+};
+    
+boost::log::sources::severity_logger<severity_level>* severity_logger;
+
+} }
+
+std::ostream& operator<<(std::ostream& strm, const monazilla::GikoMona::severity_level level) {
+    strm << monazilla::GikoMona::severity_names[static_cast<int>(level)];
+    return strm;
+}
+
 namespace monazilla { namespace GikoMona { namespace core {
 
 namespace {
 
-model *app_model;
-extension *app_extension;
+std::shared_ptr<model> app_model;
+std::shared_ptr<extension> app_extension;
+//std::shared_ptr<communication::post_office> app_postoffice;
 
 }
 
-bool init_core() {
+bool init_core(const mona_string& app_name) {
+    severity_logger = new boost::log::sources::severity_logger<severity_level>;
+    boost::log::register_simple_formatter_factory<severity_level, char>( "Severity" );
+    boost::log::add_file_log(
+        boost::log::keywords::file_name = "logs/%Y%m%d.log",
+        boost::log::keywords::format = "%Severity% - [GikoMona. %TimeStamp%]:%Message%",
+        boost::log::keywords::open_mode = (std::ios::out | std::ios::app)
+    );
+    boost::log::core::get()->add_global_attribute("TimeStamp", boost::log::attributes::local_clock());
+    
     wxXmlResource::Get()->InitAllHandlers();
     wxXmlResource::Get()->LoadAllFiles(wxT("resouce"));
     
     boost::system::error_code reason;
-    if(!boost::filesystem::create_directory(".tmp", reason) || reason) {
-        // log
+    std::stringstream path_builder;
+    path_builder << ".tmp/" << app_name;
+    
+    // 一時解凍ファイルなどを溜め込む .tmp フォルダを作成
+    if(!boost::filesystem::exists(path_builder.str(), reason) &&
+       !boost::filesystem::create_directories(path_builder.str(), reason)) {
+        ERROR(".tmpフォルダがすでに存在するか、.tmpフォルダの作成に失敗しました。");
+        MESSAGE(".tmpフォルダが存在するか確認し、存在すれば.tmpフォルダを削除してください。");
         std::terminate();
     }
     
-    app_model = new model;
-    app_extension = new extension;
+    app_model = std::make_shared<model>();
+    app_extension = std::make_shared<extension>();
+    //app_postoffice = std::make_shared<communication::post_office>(app_name);
     
     return true;
 }
 
-void done_core() noexcept {
-    delete app_extension;
-    delete app_model;
-
+void done_core(const mona_string& app_name) noexcept {
+    boost::system::error_code reason;
+    boost::filesystem::remove_all(".tmp", reason);
+    
     optimize_database("./history.db");
 }
 
 void optimize_database(const boost::filesystem::path& db_path) {
-    database db(db_path);
-    /*
-    auto value = db.compile(u8"SELECT * FROM file-information;");
-    value.execute();
-    value.get_result<int>();
-    */
-    if(db.select<int>("count-of-deleting-values", "file-infomation") >= 1000) {
-        db.optimize();
+    boost::system::error_code ec;
+    
+    database db(db_path, ec);
+    sqlite::connection con = db.get_connection();
+
+    mona_string sql(u8"SELECT counts-of-deleting-value FROM file-information;");
+    auto count = sqlite::execute_statement<int>(db, sql, ec);
+
+    if(!count) { WARNING("libsqlitexx '" << ec.message() << "'"); }
+    
+    if(count->at<0>(0) >= 1000) {
+        db.optimize(ec);
     }
 }