Skip to content

Commit 7d25ca7

Browse files
committed
Uses absl::log for logging. deperecates --minloglevel flag, but introduced --quiet.
1 parent 0d8dfe2 commit 7d25ca7

22 files changed

+426
-266
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ set_property(CACHE SPM_PROTOBUF_PROVIDER PROPERTY STRINGS "internal" "package")
3636
set(SPM_ABSL_PROVIDER "internal" CACHE STRING "Provider of absl library")
3737
set_property(CACHE SPM_ABSL_PROVIDER PROPERTY STRINGS "internal" "module" "package")
3838

39+
if (SPM_ENABLE_SHARED)
40+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
41+
endif()
42+
3943
# Includes processor name to avoid conflicts.
4044
set(CPACK_PACKAGE_FILE_NAME
4145
"sentencepiece-${SPM_VERSION}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}")

src/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ if (SPM_ABSL_PROVIDER STREQUAL "module" OR SPM_ABSL_PROVIDER STREQUAL "package")
2222
list(APPEND SPM_LIBS absl::check)
2323
add_compile_definitions(_USE_EXTERNAL_ABSL)
2424
elseif (SPM_ABSL_PROVIDER STREQUAL "internal")
25-
set(ABSL_FLAGS_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/absl/flags/flag.cc)
25+
set(ABSL_FLAGS_SRCS
26+
${CMAKE_CURRENT_SOURCE_DIR}/../third_party/absl/flags/flag.cc
27+
${CMAKE_CURRENT_SOURCE_DIR}/../third_party/absl/log/log.cc)
2628
endif()
2729

2830
if (SPM_PROTOBUF_PROVIDER STREQUAL "internal")
@@ -102,6 +104,7 @@ set(SPM_SRCS
102104
char_model.cc
103105
error.cc
104106
filesystem.cc
107+
init.cc
105108
model_factory.cc
106109
model_interface.cc
107110
normalizer.cc

src/common.h

Lines changed: 19 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@
1515
#ifndef COMMON_H_
1616
#define COMMON_H_
1717

18-
#include <stdint.h>
19-
#include <stdlib.h>
20-
#include <string.h>
21-
18+
#include <cstdint>
2219
#include <iostream>
23-
#include <memory>
24-
#include <string>
25-
#include <utility>
26-
#include <vector>
2720

28-
#include "config.h"
21+
#include "third_party/absl/log/check.h"
22+
#include "third_party/absl/log/globals.h"
23+
#include "third_party/absl/log/log.h"
2924
#include "third_party/absl/strings/string_view.h"
3025

3126
#if defined(_WIN32) && !defined(__CYGWIN__)
@@ -41,134 +36,33 @@
4136
#include <windows.h>
4237
#endif
4338

44-
typedef uint32_t char32;
39+
using char32 = uint32_t;
4540

4641
static constexpr uint32_t kUnicodeError = 0xFFFD;
4742

48-
#if defined(_FREEBSD)
49-
#include <sys/endian.h>
50-
#endif
51-
#if !defined(__APPLE__) && !defined(_WIN32) && !defined(_FREEBSD) && \
52-
!defined(_AIX)
53-
#include <endian.h>
54-
#if BYTE_ORDER == __BIG_ENDIAN
55-
#define IS_BIG_ENDIAN
56-
#endif
57-
#endif
58-
59-
#if defined(_AIX) && BYTE_ORDER == BIG_ENDIAN
60-
#define IS_BIG_ENDIAN
61-
#endif
62-
63-
namespace sentencepiece {
64-
namespace util {
65-
66-
inline uint32_t Swap32(uint32_t x) {
67-
#ifdef OS_WIN
68-
return _byteswap_ulong(x);
69-
#else // OS_WIN
70-
return __builtin_bswap32(x);
71-
#endif // OS_WIN
72-
}
73-
74-
} // namespace util
75-
76-
constexpr bool is_bigendian() {
77-
#ifdef IS_BIG_ENDIAN
78-
return true;
79-
#else // IS_BIG_ENDIAN
80-
return false;
81-
#endif // IS_BIG_ENDIAN
82-
}
83-
84-
namespace error {
85-
86-
void Abort();
87-
void Exit(int code);
88-
89-
class Die {
90-
public:
91-
explicit Die(bool die) : die_(die) {}
92-
~Die() {
93-
std::cerr << std::endl;
94-
if (die_) {
95-
Abort();
96-
}
97-
}
98-
int operator&(std::ostream &) { return 0; }
99-
100-
private:
101-
bool die_;
102-
};
103-
} // namespace error
104-
105-
namespace logging {
106-
enum LogSeverity {
107-
LOG_INFO = 0,
108-
LOG_WARNING = 1,
109-
LOG_ERROR = 2,
110-
LOG_FATAL = 3,
111-
LOG_SEVERITY_SIZE = 4,
112-
};
113-
114-
int GetMinLogLevel();
115-
void SetMinLogLevel(int v);
116-
117-
inline absl::string_view BaseName(absl::string_view path) {
118-
#ifdef OS_WIN
119-
const size_t pos = path.find_last_of('\\');
120-
#else
121-
const size_t pos = path.find_last_of('/');
122-
#endif
123-
return pos == absl::string_view::npos ? path : path.substr(pos + 1);
124-
}
125-
126-
} // namespace logging
127-
} // namespace sentencepiece
128-
129-
#define LOG(severity) \
130-
(::sentencepiece::logging::GetMinLogLevel() > \
131-
::sentencepiece::logging::LOG_##severity) \
132-
? 0 \
133-
: ::sentencepiece::error::Die( \
134-
::sentencepiece::logging::LOG_##severity >= \
135-
::sentencepiece::logging::LOG_FATAL) & \
136-
std::cerr << ::sentencepiece::logging::BaseName(__FILE__) << "(" \
137-
<< __LINE__ << ") " \
138-
<< "LOG(" << #severity << ") "
43+
#define FRIEND_TEST(a, b) friend class a##_Test_##b;
13944

140-
#define CHECK(condition) \
141-
(condition) ? 0 \
142-
: ::sentencepiece::error::Die(true) & \
143-
std::cerr << ::sentencepiece::logging::BaseName(__FILE__) \
144-
<< "(" << __LINE__ << ") [" << #condition \
145-
<< "] "
45+
#define RETURN_IF_ERROR(expr) \
46+
do { \
47+
const auto _status = expr; \
48+
if (!_status.ok()) return _status; \
49+
} while (0)
14650

147-
#define CHECK_EQ(a, b) CHECK((a) == (b))
148-
#define CHECK_NE(a, b) CHECK((a) != (b))
149-
#define CHECK_GE(a, b) CHECK((a) >= (b))
150-
#define CHECK_LE(a, b) CHECK((a) <= (b))
151-
#define CHECK_GT(a, b) CHECK((a) > (b))
152-
#define CHECK_LT(a, b) CHECK((a) < (b))
51+
// CHECK_OK must work on util::Status, not absl::Status.
52+
#if defined CHECK_OK
53+
#undef CHECK_OK
54+
#endif // CHECK_OK
15355

154-
#define FRIEND_TEST(a, b) friend class a##_Test_##b;
56+
#if defined QCHECK_OK
57+
#undef QCHECK_OK
58+
#endif // QCHECK_OK
15559

15660
#define CHECK_OK(expr) \
15761
do { \
15862
const auto _status = expr; \
15963
CHECK(_status.ok()) << _status.ToString(); \
16064
} while (0)
16165

162-
#define CHECK_NOT_OK(expr) \
163-
do { \
164-
const auto _status = expr; \
165-
CHECK(!_status.ok()) << _status.ToString(); \
166-
} while (0)
167-
168-
#define RETURN_IF_ERROR(expr) \
169-
do { \
170-
const auto _status = expr; \
171-
if (!_status.ok()) return _status; \
172-
} while (0)
66+
#define QCHECK_OK CHECK_OK
17367

17468
#endif // COMMON_H_

src/error.cc

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,9 @@
1515
#include <cstring>
1616

1717
#include "common.h"
18-
#include "init.h"
1918
#include "sentencepiece_processor.h"
2019

21-
#ifdef _USE_EXTERNAL_ABSL
22-
// Naive workaround to define minloglevel on external absl package.
23-
// We want to define them in other cc file.
24-
#include "third_party/absl/flags/flag.h"
25-
#include "third_party/absl/flags/parse.h"
26-
ABSL_FLAG(int32_t, minloglevel, 0,
27-
"Messages logged at a lower level than this don't actually.");
28-
#endif
29-
3020
namespace sentencepiece {
31-
namespace error {
32-
33-
void Abort() {
34-
std::cerr << "Program terminated with an unrecoverable error." << std::endl;
35-
ShutdownLibrary();
36-
std::exit(-1);
37-
}
38-
39-
void Exit(int code) {
40-
ShutdownLibrary();
41-
std::exit(code);
42-
}
43-
44-
} // namespace error
45-
4621
namespace util {
4722

4823
Status::Status() {}

src/init.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2016 Google Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.!
14+
15+
#include "init.h"
16+
17+
#include "common.h"
18+
#include "config.h"
19+
#include "third_party/absl/flags/flag.h"
20+
#include "third_party/absl/flags/parse.h"
21+
#include "third_party/absl/flags/usage.h"
22+
#include "third_party/absl/flags/usage_config.h"
23+
#include "third_party/absl/strings/str_cat.h"
24+
#include "util.h"
25+
26+
#ifdef _USE_EXTERNAL_PROTOBUF
27+
#include "google/protobuf/message_lite.h"
28+
#else
29+
#include "third_party/protobuf-lite/google/protobuf/message_lite.h"
30+
#endif
31+
32+
ABSL_FLAG(bool, quiet, false, "Suppress logging message.");
33+
34+
namespace sentencepiece {
35+
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv,
36+
bool remove_arg) {
37+
absl::SetProgramUsageMessage(absl::StrCat(PACKAGE_STRING, " ", VERSION,
38+
"\n\n", "Usage: ", *argv[0],
39+
" [options] files"));
40+
absl::SetFlagsUsageConfig({.version_string = [&]() {
41+
return absl::StrCat(PACKAGE_STRING, " ", VERSION, "\n");
42+
}});
43+
44+
const auto unused_args = absl::ParseCommandLine(*argc, *argv);
45+
46+
if (remove_arg) {
47+
char **argv_val = *argv;
48+
*argv = argv_val = argv_val + *argc - unused_args.size();
49+
std::copy(unused_args.begin(), unused_args.end(), argv_val);
50+
*argc = static_cast<int>(unused_args.size());
51+
}
52+
53+
if (absl::GetFlag(FLAGS_quiet)) {
54+
absl::SetMinLogLevel(static_cast<absl::LogSeverityAtLeast>(100));
55+
}
56+
}
57+
58+
void ShutdownLibrary() { google::protobuf::ShutdownProtobufLibrary(); }
59+
60+
} // namespace sentencepiece

src/init.h

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,13 @@
1616
#define INIT_H_
1717

1818
#include "common.h"
19-
#include "third_party/absl/flags/flag.h"
20-
#include "third_party/absl/flags/parse.h"
21-
#include "third_party/absl/flags/usage.h"
2219

23-
#ifdef _USE_EXTERNAL_PROTOBUF
24-
#include "google/protobuf/message_lite.h"
25-
#else
26-
#include "third_party/protobuf-lite/google/protobuf/message_lite.h"
27-
#endif
20+
namespace sentencepiece {
2821

29-
ABSL_DECLARE_FLAG(int32_t, minloglevel);
22+
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv,
23+
bool remove_arg = true);
3024

31-
namespace sentencepiece {
32-
inline void ParseCommandLineFlags(const char *usage, int *argc, char ***argv,
33-
bool remove_arg = true) {
34-
absl::SetProgramUsageMessage(*argv[0]);
35-
const auto unused_args = absl::ParseCommandLine(*argc, *argv);
36-
37-
if (remove_arg) {
38-
char **argv_val = *argv;
39-
*argv = argv_val = argv_val + *argc - unused_args.size();
40-
std::copy(unused_args.begin(), unused_args.end(), argv_val);
41-
*argc = static_cast<int>(unused_args.size());
42-
}
43-
44-
logging::SetMinLogLevel(absl::GetFlag(FLAGS_minloglevel));
45-
}
46-
47-
inline void ShutdownLibrary() {
48-
google::protobuf::ShutdownProtobufLibrary();
49-
#ifdef HAS_ABSL_CLEANUP_FLAGS
50-
absl::CleanupFlags();
51-
#endif
52-
}
25+
void ShutdownLibrary();
5326

5427
class ScopedResourceDestructor {
5528
public:

src/normalizer.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ std::string Normalizer::EncodePrecompiledCharsMap(
258258
blob.append(string_util::EncodePOD<uint32_t>(trie_blob.size()));
259259
blob.append(trie_blob.data(), trie_blob.size());
260260

261-
if constexpr (is_bigendian()) {
261+
if constexpr (util::is_bigendian()) {
262262
uint32_t *data = reinterpret_cast<uint32_t *>(blob.data());
263263
for (int i = 0; i < blob.size() / 4; ++i) data[i] = util::Swap32(data[i]);
264264
}
@@ -280,7 +280,7 @@ util::Status Normalizer::DecodePrecompiledCharsMap(
280280
return util::InternalError("Blob for normalization rule is broken.");
281281
}
282282

283-
if constexpr (is_bigendian()) {
283+
if constexpr (util::is_bigendian()) {
284284
trie_blob_size = util::Swap32(trie_blob_size);
285285
}
286286

@@ -295,7 +295,7 @@ util::Status Normalizer::DecodePrecompiledCharsMap(
295295

296296
blob.remove_prefix(sizeof(trie_blob_size));
297297

298-
if constexpr (is_bigendian()) {
298+
if constexpr (util::is_bigendian()) {
299299
CHECK_OR_RETURN(buffer);
300300
buffer->assign(blob.data(), trie_blob_size);
301301
uint32_t *data =

src/sentencepiece_trainer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ util::Status SentencePieceTrainer::MergeSpecsFromArgs(
150150
} else if (key == "minloglevel") {
151151
int v = 0;
152152
CHECK_OR_RETURN(absl::SimpleAtoi(value, &v));
153-
logging::SetMinLogLevel(v);
153+
SetMinLogLevel(v);
154154
continue;
155155
}
156156

0 commit comments

Comments
 (0)