cppjieba_rb 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/ext/cppjieba/.gitignore +17 -0
  3. data/ext/cppjieba/.travis.yml +21 -0
  4. data/ext/cppjieba/CMakeLists.txt +28 -0
  5. data/ext/cppjieba/ChangeLog.md +249 -0
  6. data/ext/cppjieba/README.md +289 -0
  7. data/ext/cppjieba/README_EN.md +115 -0
  8. data/ext/cppjieba/appveyor.yml +32 -0
  9. data/ext/cppjieba/deps/CMakeLists.txt +1 -0
  10. data/ext/cppjieba/deps/gtest/CMakeLists.txt +5 -0
  11. data/ext/cppjieba/deps/gtest/include/gtest/gtest-death-test.h +283 -0
  12. data/ext/cppjieba/deps/gtest/include/gtest/gtest-message.h +230 -0
  13. data/ext/cppjieba/deps/gtest/include/gtest/gtest-param-test.h +1421 -0
  14. data/ext/cppjieba/deps/gtest/include/gtest/gtest-param-test.h.pump +487 -0
  15. data/ext/cppjieba/deps/gtest/include/gtest/gtest-printers.h +796 -0
  16. data/ext/cppjieba/deps/gtest/include/gtest/gtest-spi.h +232 -0
  17. data/ext/cppjieba/deps/gtest/include/gtest/gtest-test-part.h +176 -0
  18. data/ext/cppjieba/deps/gtest/include/gtest/gtest-typed-test.h +259 -0
  19. data/ext/cppjieba/deps/gtest/include/gtest/gtest.h +2155 -0
  20. data/ext/cppjieba/deps/gtest/include/gtest/gtest_pred_impl.h +358 -0
  21. data/ext/cppjieba/deps/gtest/include/gtest/gtest_prod.h +58 -0
  22. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-death-test-internal.h +308 -0
  23. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-filepath.h +210 -0
  24. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-internal.h +1226 -0
  25. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-linked_ptr.h +233 -0
  26. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util-generated.h +4822 -0
  27. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util-generated.h.pump +301 -0
  28. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util.h +619 -0
  29. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-port.h +1788 -0
  30. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-string.h +350 -0
  31. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-tuple.h +968 -0
  32. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-tuple.h.pump +336 -0
  33. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-type-util.h +3330 -0
  34. data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-type-util.h.pump +296 -0
  35. data/ext/cppjieba/deps/gtest/src/.deps/.dirstamp +0 -0
  36. data/ext/cppjieba/deps/gtest/src/.deps/gtest-all.Plo +681 -0
  37. data/ext/cppjieba/deps/gtest/src/.deps/gtest_main.Plo +509 -0
  38. data/ext/cppjieba/deps/gtest/src/.dirstamp +0 -0
  39. data/ext/cppjieba/deps/gtest/src/gtest-all.cc +48 -0
  40. data/ext/cppjieba/deps/gtest/src/gtest-death-test.cc +1234 -0
  41. data/ext/cppjieba/deps/gtest/src/gtest-filepath.cc +380 -0
  42. data/ext/cppjieba/deps/gtest/src/gtest-internal-inl.h +1038 -0
  43. data/ext/cppjieba/deps/gtest/src/gtest-port.cc +746 -0
  44. data/ext/cppjieba/deps/gtest/src/gtest-printers.cc +356 -0
  45. data/ext/cppjieba/deps/gtest/src/gtest-test-part.cc +110 -0
  46. data/ext/cppjieba/deps/gtest/src/gtest-typed-test.cc +110 -0
  47. data/ext/cppjieba/deps/gtest/src/gtest.cc +4898 -0
  48. data/ext/cppjieba/deps/gtest/src/gtest_main.cc +39 -0
  49. data/ext/cppjieba/deps/limonp/ArgvContext.hpp +70 -0
  50. data/ext/cppjieba/deps/limonp/BlockingQueue.hpp +49 -0
  51. data/ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp +67 -0
  52. data/ext/cppjieba/deps/limonp/BoundedQueue.hpp +65 -0
  53. data/ext/cppjieba/deps/limonp/Closure.hpp +206 -0
  54. data/ext/cppjieba/deps/limonp/Colors.hpp +31 -0
  55. data/ext/cppjieba/deps/limonp/Condition.hpp +38 -0
  56. data/ext/cppjieba/deps/limonp/Config.hpp +103 -0
  57. data/ext/cppjieba/deps/limonp/FileLock.hpp +74 -0
  58. data/ext/cppjieba/deps/limonp/ForcePublic.hpp +7 -0
  59. data/ext/cppjieba/deps/limonp/LocalVector.hpp +139 -0
  60. data/ext/cppjieba/deps/limonp/Logging.hpp +76 -0
  61. data/ext/cppjieba/deps/limonp/Md5.hpp +411 -0
  62. data/ext/cppjieba/deps/limonp/MutexLock.hpp +51 -0
  63. data/ext/cppjieba/deps/limonp/NonCopyable.hpp +21 -0
  64. data/ext/cppjieba/deps/limonp/StdExtension.hpp +157 -0
  65. data/ext/cppjieba/deps/limonp/StringUtil.hpp +365 -0
  66. data/ext/cppjieba/deps/limonp/Thread.hpp +44 -0
  67. data/ext/cppjieba/deps/limonp/ThreadPool.hpp +86 -0
  68. data/ext/cppjieba/dict/README.md +31 -0
  69. data/ext/cppjieba/dict/hmm_model.utf8 +34 -0
  70. data/ext/cppjieba/dict/idf.utf8 +258826 -0
  71. data/ext/cppjieba/dict/jieba.dict.utf8 +348982 -0
  72. data/ext/cppjieba/dict/pos_dict/char_state_tab.utf8 +6653 -0
  73. data/ext/cppjieba/dict/pos_dict/prob_emit.utf8 +166 -0
  74. data/ext/cppjieba/dict/pos_dict/prob_start.utf8 +259 -0
  75. data/ext/cppjieba/dict/pos_dict/prob_trans.utf8 +5222 -0
  76. data/ext/cppjieba/dict/stop_words.utf8 +1534 -0
  77. data/ext/cppjieba/dict/user.dict.utf8 +4 -0
  78. data/ext/cppjieba/include/cppjieba/DictTrie.hpp +277 -0
  79. data/ext/cppjieba/include/cppjieba/FullSegment.hpp +93 -0
  80. data/ext/cppjieba/include/cppjieba/HMMModel.hpp +129 -0
  81. data/ext/cppjieba/include/cppjieba/HMMSegment.hpp +190 -0
  82. data/ext/cppjieba/include/cppjieba/Jieba.hpp +130 -0
  83. data/ext/cppjieba/include/cppjieba/KeywordExtractor.hpp +153 -0
  84. data/ext/cppjieba/include/cppjieba/MPSegment.hpp +137 -0
  85. data/ext/cppjieba/include/cppjieba/MixSegment.hpp +109 -0
  86. data/ext/cppjieba/include/cppjieba/PosTagger.hpp +77 -0
  87. data/ext/cppjieba/include/cppjieba/PreFilter.hpp +54 -0
  88. data/ext/cppjieba/include/cppjieba/QuerySegment.hpp +89 -0
  89. data/ext/cppjieba/include/cppjieba/SegmentBase.hpp +46 -0
  90. data/ext/cppjieba/include/cppjieba/SegmentTagged.hpp +23 -0
  91. data/ext/cppjieba/include/cppjieba/TextRankExtractor.hpp +190 -0
  92. data/ext/cppjieba/include/cppjieba/Trie.hpp +174 -0
  93. data/ext/cppjieba/include/cppjieba/Unicode.hpp +227 -0
  94. data/ext/cppjieba/test/CMakeLists.txt +5 -0
  95. data/ext/cppjieba/test/demo.cpp +80 -0
  96. data/ext/cppjieba/test/load_test.cpp +54 -0
  97. data/ext/cppjieba/test/testdata/curl.res +1 -0
  98. data/ext/cppjieba/test/testdata/extra_dict/jieba.dict.small.utf8 +109750 -0
  99. data/ext/cppjieba/test/testdata/gbk_dict/hmm_model.gbk +34 -0
  100. data/ext/cppjieba/test/testdata/gbk_dict/jieba.dict.gbk +348982 -0
  101. data/ext/cppjieba/test/testdata/jieba.dict.0.1.utf8 +93 -0
  102. data/ext/cppjieba/test/testdata/jieba.dict.0.utf8 +93 -0
  103. data/ext/cppjieba/test/testdata/jieba.dict.1.utf8 +67 -0
  104. data/ext/cppjieba/test/testdata/jieba.dict.2.utf8 +64 -0
  105. data/ext/cppjieba/test/testdata/load_test.urls +2 -0
  106. data/ext/cppjieba/test/testdata/review.100 +100 -0
  107. data/ext/cppjieba/test/testdata/review.100.res +200 -0
  108. data/ext/cppjieba/test/testdata/server.conf +19 -0
  109. data/ext/cppjieba/test/testdata/testlines.gbk +9 -0
  110. data/ext/cppjieba/test/testdata/testlines.utf8 +8 -0
  111. data/ext/cppjieba/test/testdata/userdict.2.utf8 +1 -0
  112. data/ext/cppjieba/test/testdata/userdict.english +2 -0
  113. data/ext/cppjieba/test/testdata/userdict.utf8 +8 -0
  114. data/ext/cppjieba/test/testdata/weicheng.utf8 +247 -0
  115. data/ext/cppjieba/test/unittest/CMakeLists.txt +24 -0
  116. data/ext/cppjieba/test/unittest/gtest_main.cpp +39 -0
  117. data/ext/cppjieba/test/unittest/jieba_test.cpp +133 -0
  118. data/ext/cppjieba/test/unittest/keyword_extractor_test.cpp +79 -0
  119. data/ext/cppjieba/test/unittest/pos_tagger_test.cpp +41 -0
  120. data/ext/cppjieba/test/unittest/pre_filter_test.cpp +43 -0
  121. data/ext/cppjieba/test/unittest/segments_test.cpp +256 -0
  122. data/ext/cppjieba/test/unittest/textrank_test.cpp +86 -0
  123. data/ext/cppjieba/test/unittest/trie_test.cpp +177 -0
  124. data/ext/cppjieba/test/unittest/unicode_test.cpp +43 -0
  125. data/lib/cppjieba_rb/version.rb +1 -1
  126. metadata +125 -2
@@ -0,0 +1,31 @@
1
+ #ifndef LIMONP_COLOR_PRINT_HPP
2
+ #define LIMONP_COLOR_PRINT_HPP
3
+
4
+ #include <string>
5
+ #include <stdarg.h>
6
+
7
+ namespace limonp {
8
+
9
+ using std::string;
10
+
11
+ enum Color {
12
+ BLACK = 30,
13
+ RED,
14
+ GREEN,
15
+ YELLOW,
16
+ BLUE,
17
+ PURPLE
18
+ }; // enum Color
19
+
20
+ static void ColorPrintln(enum Color color, const char * fmt, ...) {
21
+ va_list ap;
22
+ printf("\033[0;%dm", color);
23
+ va_start(ap, fmt);
24
+ vprintf(fmt, ap);
25
+ va_end(ap);
26
+ printf("\033[0m\n"); // if not \n , in some situation , the next lines will be set the same color unexpectedly
27
+ }
28
+
29
+ } // namespace limonp
30
+
31
+ #endif // LIMONP_COLOR_PRINT_HPP
@@ -0,0 +1,38 @@
1
+ #ifndef LIMONP_CONDITION_HPP
2
+ #define LIMONP_CONDITION_HPP
3
+
4
+ #include "MutexLock.hpp"
5
+
6
+ namespace limonp {
7
+
8
+ class Condition : NonCopyable {
9
+ public:
10
+ explicit Condition(MutexLock& mutex)
11
+ : mutex_(mutex) {
12
+ XCHECK(!pthread_cond_init(&pcond_, NULL));
13
+ }
14
+
15
+ ~Condition() {
16
+ XCHECK(!pthread_cond_destroy(&pcond_));
17
+ }
18
+
19
+ void Wait() {
20
+ XCHECK(!pthread_cond_wait(&pcond_, mutex_.GetPthreadMutex()));
21
+ }
22
+
23
+ void Notify() {
24
+ XCHECK(!pthread_cond_signal(&pcond_));
25
+ }
26
+
27
+ void NotifyAll() {
28
+ XCHECK(!pthread_cond_broadcast(&pcond_));
29
+ }
30
+
31
+ private:
32
+ MutexLock& mutex_;
33
+ pthread_cond_t pcond_;
34
+ }; // class Condition
35
+
36
+ } // namespace limonp
37
+
38
+ #endif // LIMONP_CONDITION_HPP
@@ -0,0 +1,103 @@
1
+ /************************************
2
+ * file enc : utf8
3
+ * author : wuyanyi09@gmail.com
4
+ ************************************/
5
+ #ifndef LIMONP_CONFIG_H
6
+ #define LIMONP_CONFIG_H
7
+
8
+ #include <map>
9
+ #include <fstream>
10
+ #include <iostream>
11
+ #include <assert.h>
12
+ #include "StringUtil.hpp"
13
+
14
+ namespace limonp {
15
+
16
+ using namespace std;
17
+
18
+ class Config {
19
+ public:
20
+ explicit Config(const string& filePath) {
21
+ LoadFile(filePath);
22
+ }
23
+
24
+ operator bool () {
25
+ return !map_.empty();
26
+ }
27
+
28
+ string Get(const string& key, const string& defaultvalue) const {
29
+ map<string, string>::const_iterator it = map_.find(key);
30
+ if(map_.end() != it) {
31
+ return it->second;
32
+ }
33
+ return defaultvalue;
34
+ }
35
+ int Get(const string& key, int defaultvalue) const {
36
+ string str = Get(key, "");
37
+ if("" == str) {
38
+ return defaultvalue;
39
+ }
40
+ return atoi(str.c_str());
41
+ }
42
+ const char* operator [] (const char* key) const {
43
+ if(NULL == key) {
44
+ return NULL;
45
+ }
46
+ map<string, string>::const_iterator it = map_.find(key);
47
+ if(map_.end() != it) {
48
+ return it->second.c_str();
49
+ }
50
+ return NULL;
51
+ }
52
+
53
+ string GetConfigInfo() const {
54
+ string res;
55
+ res << *this;
56
+ return res;
57
+ }
58
+
59
+ private:
60
+ void LoadFile(const string& filePath) {
61
+ ifstream ifs(filePath.c_str());
62
+ assert(ifs);
63
+ string line;
64
+ vector<string> vecBuf;
65
+ size_t lineno = 0;
66
+ while(getline(ifs, line)) {
67
+ lineno ++;
68
+ Trim(line);
69
+ if(line.empty() || StartsWith(line, "#")) {
70
+ continue;
71
+ }
72
+ vecBuf.clear();
73
+ Split(line, vecBuf, "=");
74
+ if(2 != vecBuf.size()) {
75
+ fprintf(stderr, "line[%s] illegal.\n", line.c_str());
76
+ assert(false);
77
+ continue;
78
+ }
79
+ string& key = vecBuf[0];
80
+ string& value = vecBuf[1];
81
+ Trim(key);
82
+ Trim(value);
83
+ if(!map_.insert(make_pair(key, value)).second) {
84
+ fprintf(stderr, "key[%s] already exits.\n", key.c_str());
85
+ assert(false);
86
+ continue;
87
+ }
88
+ }
89
+ ifs.close();
90
+ }
91
+
92
+ friend ostream& operator << (ostream& os, const Config& config);
93
+
94
+ map<string, string> map_;
95
+ }; // class Config
96
+
97
+ inline ostream& operator << (ostream& os, const Config& config) {
98
+ return os << config.map_;
99
+ }
100
+
101
+ } // namespace limonp
102
+
103
+ #endif // LIMONP_CONFIG_H
@@ -0,0 +1,74 @@
1
+ #ifndef LIMONP_FILELOCK_HPP
2
+ #define LIMONP_FILELOCK_HPP
3
+
4
+ #include <unistd.h>
5
+ #include <stdlib.h>
6
+ #include <stdio.h>
7
+ #include <fcntl.h>
8
+ #include <errno.h>
9
+ #include <string>
10
+ #include <string.h>
11
+ #include <assert.h>
12
+
13
+ namespace limonp {
14
+
15
+ using std::string;
16
+
17
+ class FileLock {
18
+ public:
19
+ FileLock() : fd_(-1), ok_(true) {
20
+ }
21
+ ~FileLock() {
22
+ if(fd_ > 0) {
23
+ Close();
24
+ }
25
+ }
26
+ void Open(const string& fname) {
27
+ assert(fd_ == -1);
28
+ fd_ = open(fname.c_str(), O_RDWR | O_CREAT, 0644);
29
+ if(fd_ < 0) {
30
+ ok_ = false;
31
+ err_ = strerror(errno);
32
+ }
33
+ }
34
+ void Close() {
35
+ ::close(fd_);
36
+ }
37
+ void Lock() {
38
+ if(LockOrUnlock(fd_, true) < 0) {
39
+ ok_ = false;
40
+ err_ = strerror(errno);
41
+ }
42
+ }
43
+ void UnLock() {
44
+ if(LockOrUnlock(fd_, false) < 0) {
45
+ ok_ = false;
46
+ err_ = strerror(errno);
47
+ }
48
+ }
49
+ bool Ok() const {
50
+ return ok_;
51
+ }
52
+ string Error() const {
53
+ return err_;
54
+ }
55
+ private:
56
+ static int LockOrUnlock(int fd, bool lock) {
57
+ errno = 0;
58
+ struct flock f;
59
+ memset(&f, 0, sizeof(f));
60
+ f.l_type = (lock ? F_WRLCK : F_UNLCK);
61
+ f.l_whence = SEEK_SET;
62
+ f.l_start = 0;
63
+ f.l_len = 0; // Lock/unlock entire file
64
+ return fcntl(fd, F_SETLK, &f);
65
+ }
66
+
67
+ int fd_;
68
+ bool ok_;
69
+ string err_;
70
+ }; // class FileLock
71
+
72
+ }// namespace limonp
73
+
74
+ #endif // LIMONP_FILELOCK_HPP
@@ -0,0 +1,7 @@
1
+ #ifndef LIMONP_FORCE_PUBLIC_H
2
+ #define LIMONP_FORCE_PUBLIC_H
3
+
4
+ #define private public
5
+ #define protected public
6
+
7
+ #endif // LIMONP_FORCE_PUBLIC_H
@@ -0,0 +1,139 @@
1
+ #ifndef LIMONP_LOCAL_VECTOR_HPP
2
+ #define LIMONP_LOCAL_VECTOR_HPP
3
+
4
+ #include <iostream>
5
+ #include <stdlib.h>
6
+ #include <assert.h>
7
+ #include <string.h>
8
+
9
+ namespace limonp {
10
+ using namespace std;
11
+ /*
12
+ * LocalVector<T> : T must be primitive type (char , int, size_t), if T is struct or class, LocalVector<T> may be dangerous..
13
+ * LocalVector<T> is simple and not well-tested.
14
+ */
15
+ const size_t LOCAL_VECTOR_BUFFER_SIZE = 16;
16
+ template <class T>
17
+ class LocalVector {
18
+ public:
19
+ typedef const T* const_iterator ;
20
+ typedef T value_type;
21
+ typedef size_t size_type;
22
+ private:
23
+ T buffer_[LOCAL_VECTOR_BUFFER_SIZE];
24
+ T * ptr_;
25
+ size_t size_;
26
+ size_t capacity_;
27
+ public:
28
+ LocalVector() {
29
+ init_();
30
+ };
31
+ LocalVector(const LocalVector<T>& vec) {
32
+ init_();
33
+ *this = vec;
34
+ }
35
+ LocalVector(const_iterator begin, const_iterator end) { // TODO: make it faster
36
+ init_();
37
+ while(begin != end) {
38
+ push_back(*begin++);
39
+ }
40
+ }
41
+ LocalVector(size_t size, const T& t) { // TODO: make it faster
42
+ init_();
43
+ while(size--) {
44
+ push_back(t);
45
+ }
46
+ }
47
+ ~LocalVector() {
48
+ if(ptr_ != buffer_) {
49
+ free(ptr_);
50
+ }
51
+ };
52
+ public:
53
+ LocalVector<T>& operator = (const LocalVector<T>& vec) {
54
+ clear();
55
+ size_ = vec.size();
56
+ capacity_ = vec.capacity();
57
+ if(vec.buffer_ == vec.ptr_) {
58
+ memcpy(buffer_, vec.buffer_, sizeof(T) * size_);
59
+ ptr_ = buffer_;
60
+ } else {
61
+ ptr_ = (T*) malloc(vec.capacity() * sizeof(T));
62
+ assert(ptr_);
63
+ memcpy(ptr_, vec.ptr_, vec.size() * sizeof(T));
64
+ }
65
+ return *this;
66
+ }
67
+ private:
68
+ void init_() {
69
+ ptr_ = buffer_;
70
+ size_ = 0;
71
+ capacity_ = LOCAL_VECTOR_BUFFER_SIZE;
72
+ }
73
+ public:
74
+ T& operator [] (size_t i) {
75
+ return ptr_[i];
76
+ }
77
+ const T& operator [] (size_t i) const {
78
+ return ptr_[i];
79
+ }
80
+ void push_back(const T& t) {
81
+ if(size_ == capacity_) {
82
+ assert(capacity_);
83
+ reserve(capacity_ * 2);
84
+ }
85
+ ptr_[size_ ++ ] = t;
86
+ }
87
+ void reserve(size_t size) {
88
+ if(size <= capacity_) {
89
+ return;
90
+ }
91
+ T * next = (T*)malloc(sizeof(T) * size);
92
+ assert(next);
93
+ T * old = ptr_;
94
+ ptr_ = next;
95
+ memcpy(ptr_, old, sizeof(T) * capacity_);
96
+ capacity_ = size;
97
+ if(old != buffer_) {
98
+ free(old);
99
+ }
100
+ }
101
+ bool empty() const {
102
+ return 0 == size();
103
+ }
104
+ size_t size() const {
105
+ return size_;
106
+ }
107
+ size_t capacity() const {
108
+ return capacity_;
109
+ }
110
+ const_iterator begin() const {
111
+ return ptr_;
112
+ }
113
+ const_iterator end() const {
114
+ return ptr_ + size_;
115
+ }
116
+ void clear() {
117
+ if(ptr_ != buffer_) {
118
+ free(ptr_);
119
+ }
120
+ init_();
121
+ }
122
+ };
123
+
124
+ template <class T>
125
+ ostream & operator << (ostream& os, const LocalVector<T>& vec) {
126
+ if(vec.empty()) {
127
+ return os << "[]";
128
+ }
129
+ os<<"[\""<<vec[0];
130
+ for(size_t i = 1; i < vec.size(); i++) {
131
+ os<<"\", \""<<vec[i];
132
+ }
133
+ os<<"\"]";
134
+ return os;
135
+ }
136
+
137
+ }
138
+
139
+ #endif
@@ -0,0 +1,76 @@
1
+ #ifndef LIMONP_LOGGING_HPP
2
+ #define LIMONP_LOGGING_HPP
3
+
4
+ #include <sstream>
5
+ #include <iostream>
6
+ #include <cassert>
7
+ #include <cstdlib>
8
+ #include <ctime>
9
+
10
+ #ifdef XLOG
11
+ #error "XLOG has been defined already"
12
+ #endif // XLOG
13
+ #ifdef XCHECK
14
+ #error "XCHECK has been defined already"
15
+ #endif // XCHECK
16
+
17
+ #define XLOG(level) limonp::Logger(limonp::LL_##level, __FILE__, __LINE__).Stream()
18
+ #define XCHECK(exp) if(!(exp)) XLOG(FATAL) << "exp: ["#exp << "] false. "
19
+
20
+ namespace limonp {
21
+
22
+ enum {
23
+ LL_DEBUG = 0,
24
+ LL_INFO = 1,
25
+ LL_WARNING = 2,
26
+ LL_ERROR = 3,
27
+ LL_FATAL = 4,
28
+ }; // enum
29
+
30
+ static const char * LOG_LEVEL_ARRAY[] = {"DEBUG","INFO","WARN","ERROR","FATAL"};
31
+ static const char * LOG_TIME_FORMAT = "%Y-%m-%d %H:%M:%S";
32
+
33
+ class Logger {
34
+ public:
35
+ Logger(size_t level, const char* filename, int lineno)
36
+ : level_(level) {
37
+ #ifdef LOGGING_LEVEL
38
+ if (level_ < LOGGING_LEVEL) {
39
+ return;
40
+ }
41
+ #endif
42
+ assert(level_ <= sizeof(LOG_LEVEL_ARRAY)/sizeof(*LOG_LEVEL_ARRAY));
43
+ char buf[32];
44
+ time_t now;
45
+ time(&now);
46
+ strftime(buf, sizeof(buf), LOG_TIME_FORMAT, localtime(&now));
47
+ stream_ << buf
48
+ << " " << filename
49
+ << ":" << lineno
50
+ << " " << LOG_LEVEL_ARRAY[level_]
51
+ << " ";
52
+ }
53
+ ~Logger() {
54
+ #ifdef LOGGING_LEVEL
55
+ if (level_ < LOGGING_LEVEL) {
56
+ return;
57
+ }
58
+ #endif
59
+ std::cerr << stream_.str() << std::endl;
60
+ if (level_ == LL_FATAL) {
61
+ abort();
62
+ }
63
+ }
64
+
65
+ std::ostream& Stream() {
66
+ return stream_;
67
+ }
68
+
69
+ private:
70
+ std::ostringstream stream_;
71
+ size_t level_;
72
+ }; // class Logger
73
+
74
+ } // namespace limonp
75
+
76
+ #endif // LIMONP_LOGGING_HPP