jieba-rb 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.gitmodules +3 -0
- data/.travis.yml +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +85 -0
- data/Rakefile +15 -0
- data/ext/cppjieba/.gitignore +17 -0
- data/ext/cppjieba/.travis.yml +22 -0
- data/ext/cppjieba/CMakeLists.txt +28 -0
- data/ext/cppjieba/ChangeLog.md +236 -0
- data/ext/cppjieba/README.md +285 -0
- data/ext/cppjieba/README_EN.md +111 -0
- data/ext/cppjieba/appveyor.yml +32 -0
- data/ext/cppjieba/deps/CMakeLists.txt +1 -0
- data/ext/cppjieba/deps/gtest/CMakeLists.txt +5 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-death-test.h +283 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-message.h +230 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-param-test.h +1421 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-param-test.h.pump +487 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-printers.h +796 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-spi.h +232 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-test-part.h +176 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-typed-test.h +259 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest.h +2155 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest_pred_impl.h +358 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest_prod.h +58 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-death-test-internal.h +308 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-filepath.h +210 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-internal.h +1226 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-linked_ptr.h +233 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util-generated.h +4822 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util-generated.h.pump +301 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util.h +619 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-port.h +1788 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-string.h +350 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-tuple.h +968 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-tuple.h.pump +336 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-type-util.h +3330 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-type-util.h.pump +296 -0
- data/ext/cppjieba/deps/gtest/src/.deps/.dirstamp +0 -0
- data/ext/cppjieba/deps/gtest/src/.deps/gtest-all.Plo +681 -0
- data/ext/cppjieba/deps/gtest/src/.deps/gtest_main.Plo +509 -0
- data/ext/cppjieba/deps/gtest/src/.dirstamp +0 -0
- data/ext/cppjieba/deps/gtest/src/gtest-all.cc +48 -0
- data/ext/cppjieba/deps/gtest/src/gtest-death-test.cc +1234 -0
- data/ext/cppjieba/deps/gtest/src/gtest-filepath.cc +380 -0
- data/ext/cppjieba/deps/gtest/src/gtest-internal-inl.h +1038 -0
- data/ext/cppjieba/deps/gtest/src/gtest-port.cc +746 -0
- data/ext/cppjieba/deps/gtest/src/gtest-printers.cc +356 -0
- data/ext/cppjieba/deps/gtest/src/gtest-test-part.cc +110 -0
- data/ext/cppjieba/deps/gtest/src/gtest-typed-test.cc +110 -0
- data/ext/cppjieba/deps/gtest/src/gtest.cc +4898 -0
- data/ext/cppjieba/deps/gtest/src/gtest_main.cc +39 -0
- data/ext/cppjieba/deps/limonp/ArgvContext.hpp +70 -0
- data/ext/cppjieba/deps/limonp/BlockingQueue.hpp +49 -0
- data/ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp +67 -0
- data/ext/cppjieba/deps/limonp/BoundedQueue.hpp +65 -0
- data/ext/cppjieba/deps/limonp/Closure.hpp +206 -0
- data/ext/cppjieba/deps/limonp/Colors.hpp +31 -0
- data/ext/cppjieba/deps/limonp/Condition.hpp +38 -0
- data/ext/cppjieba/deps/limonp/Config.hpp +103 -0
- data/ext/cppjieba/deps/limonp/FileLock.hpp +74 -0
- data/ext/cppjieba/deps/limonp/ForcePublic.hpp +7 -0
- data/ext/cppjieba/deps/limonp/LocalVector.hpp +139 -0
- data/ext/cppjieba/deps/limonp/Logging.hpp +76 -0
- data/ext/cppjieba/deps/limonp/Md5.hpp +411 -0
- data/ext/cppjieba/deps/limonp/MutexLock.hpp +51 -0
- data/ext/cppjieba/deps/limonp/NonCopyable.hpp +21 -0
- data/ext/cppjieba/deps/limonp/StdExtension.hpp +159 -0
- data/ext/cppjieba/deps/limonp/StringUtil.hpp +365 -0
- data/ext/cppjieba/deps/limonp/Thread.hpp +44 -0
- data/ext/cppjieba/deps/limonp/ThreadPool.hpp +86 -0
- data/ext/cppjieba/dict/README.md +31 -0
- data/ext/cppjieba/dict/hmm_model.utf8 +34 -0
- data/ext/cppjieba/dict/idf.utf8 +258826 -0
- data/ext/cppjieba/dict/jieba.dict.utf8 +348982 -0
- data/ext/cppjieba/dict/pos_dict/char_state_tab.utf8 +6653 -0
- data/ext/cppjieba/dict/pos_dict/prob_emit.utf8 +166 -0
- data/ext/cppjieba/dict/pos_dict/prob_start.utf8 +259 -0
- data/ext/cppjieba/dict/pos_dict/prob_trans.utf8 +5222 -0
- data/ext/cppjieba/dict/stop_words.utf8 +1534 -0
- data/ext/cppjieba/dict/user.dict.utf8 +4 -0
- data/ext/cppjieba/include/cppjieba/DictTrie.hpp +227 -0
- data/ext/cppjieba/include/cppjieba/FullSegment.hpp +93 -0
- data/ext/cppjieba/include/cppjieba/HMMModel.hpp +129 -0
- data/ext/cppjieba/include/cppjieba/HMMSegment.hpp +190 -0
- data/ext/cppjieba/include/cppjieba/Jieba.hpp +108 -0
- data/ext/cppjieba/include/cppjieba/KeywordExtractor.hpp +153 -0
- data/ext/cppjieba/include/cppjieba/MPSegment.hpp +137 -0
- data/ext/cppjieba/include/cppjieba/MixSegment.hpp +109 -0
- data/ext/cppjieba/include/cppjieba/PosTagger.hpp +77 -0
- data/ext/cppjieba/include/cppjieba/PreFilter.hpp +54 -0
- data/ext/cppjieba/include/cppjieba/QuerySegment.hpp +90 -0
- data/ext/cppjieba/include/cppjieba/SegmentBase.hpp +46 -0
- data/ext/cppjieba/include/cppjieba/SegmentTagged.hpp +24 -0
- data/ext/cppjieba/include/cppjieba/TextRankExtractor.hpp +190 -0
- data/ext/cppjieba/include/cppjieba/Trie.hpp +174 -0
- data/ext/cppjieba/include/cppjieba/Unicode.hpp +215 -0
- data/ext/jieba/extconf.rb +28 -0
- data/ext/jieba/jieba.c +11 -0
- data/ext/jieba/jieba.h +11 -0
- data/ext/jieba/keyword.cc +92 -0
- data/ext/jieba/keyword.h +17 -0
- data/ext/jieba/segment.cc +107 -0
- data/ext/jieba/segment.h +17 -0
- data/ext/jieba/tagging.cc +76 -0
- data/ext/jieba/tagging.h +17 -0
- data/jieba_rb.gemspec +51 -0
- data/lib/jieba-rb.rb +66 -0
- data/lib/jieba_rb/version.rb +3 -0
- data/test/test_keyword.rb +17 -0
- data/test/test_segment.rb +32 -0
- data/test/test_tagging.rb +22 -0
- data/test/user.dict.utf8 +23 -0
- metadata +219 -0
@@ -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,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
|