OSDN Git Service

46ab8dfcd4aaf53b76973709abe2acaec579d239
[gikomona/libcore.git] / src / GikoMona.cpp
1 #include <exception>
2 #include <memory>
3 #include <sstream>
4
5 #include <boost/filesystem.hpp>
6 #include <boost/log/trivial.hpp>
7
8 #include <wx/xrc/xmlres.h>
9 #include <wx/msgdlg.h>
10
11 #include "GikoMona.hpp"
12 #include "database.hpp"
13 #include "model.hpp"
14 #include "communication/post-office.hpp"
15
16 #include "extension.hpp"
17
18 namespace monazilla { namespace GikoMona {
19
20 const char* severity_names[] = {
21     "debug_trace",
22     "debug_message",
23     "debug_warning",
24     "debug_error",
25     
26     "trace",
27     "message",
28     "warning",
29     "error",
30 };
31     
32 boost::log::sources::severity_logger<severity_level>* severity_logger;
33
34 } }
35
36 std::ostream& operator<<(std::ostream& strm, const monazilla::GikoMona::severity_level level) {
37     strm << monazilla::GikoMona::severity_names[static_cast<int>(level)];
38     return strm;
39 }
40
41 namespace monazilla { namespace GikoMona { namespace core {
42
43 namespace {
44
45 std::shared_ptr<model> app_model;
46 std::shared_ptr<extension> app_extension;
47 //std::shared_ptr<communication::post_office> app_postoffice;
48
49 }
50
51 bool init_core(const mona_string& app_name) {
52     severity_logger = new boost::log::sources::severity_logger<severity_level>;
53     boost::log::register_simple_formatter_factory<severity_level, char>( "Severity" );
54     boost::log::add_file_log(
55         boost::log::keywords::file_name = "logs/%Y%m%d.log",
56         boost::log::keywords::format = "%Severity% - [GikoMona. %TimeStamp%]:%Message%",
57         boost::log::keywords::open_mode = (std::ios::out | std::ios::app)
58     );
59     boost::log::core::get()->add_global_attribute("TimeStamp", boost::log::attributes::local_clock());
60     
61     wxXmlResource::Get()->InitAllHandlers();
62     wxXmlResource::Get()->LoadAllFiles(wxT("resouce"));
63     
64     boost::system::error_code reason;
65     std::stringstream path_builder;
66     path_builder << ".tmp/" << app_name;
67     
68     // 一時解凍ファイルなどを溜め込む .tmp フォルダを作成
69     if(!boost::filesystem::exists(path_builder.str(), reason) &&
70        !boost::filesystem::create_directories(path_builder.str(), reason)) {
71         ERROR(".tmpフォルダがすでに存在するか、.tmpフォルダの作成に失敗しました。");
72         MESSAGE(".tmpフォルダが存在するか確認し、存在すれば.tmpフォルダを削除してください。");
73         std::terminate();
74     }
75     
76     app_model = std::make_shared<model>();
77     app_extension = std::make_shared<extension>();
78     //app_postoffice = std::make_shared<communication::post_office>(app_name);
79     
80     return true;
81 }
82
83 void done_core(const mona_string& app_name) noexcept {
84     boost::system::error_code reason;
85     boost::filesystem::remove_all(".tmp", reason);
86     
87     optimize_database("./history.db");
88 }
89
90 void optimize_database(const boost::filesystem::path& db_path) {
91     boost::system::error_code ec;
92     
93     database db(db_path, ec);
94     sqlite::connection con = db.get_connection();
95
96     mona_string sql(u8"SELECT counts-of-deleting-value FROM file-information;");
97     auto count = sqlite::execute_statement<int>(db, sql, ec);
98
99     if(!count) { /* log */ }
100     
101     if(count->at<0>(0) >= 1000) {
102         db.optimize(ec);
103     }
104 }
105
106 } } }