Skip to content

Commit 7dbb38e

Browse files
Yangqing Jiafacebook-github-bot
Yangqing Jia
authored andcommitted
Moving logging from caffe2 to c10. (pytorch#12881)
Summary: Pull Request resolved: pytorch#12881 TSIA. This should not change any functionality. Remaining work: - change the build script to deprecate use of CAFFE2_USE_MINIMAL_GOOGLE_GLOG and use a C10 macro instead. - Unify the exception name (EnforceNotMet -> Error) - Unify the logging and warning APIs (like AT_WARNING) Reviewed By: dzhulgakov Differential Revision: D10441597 fbshipit-source-id: 4784dc0cd5af83dacb10c4952a2d1d7236b3f14d
1 parent d120b9a commit 7dbb38e

9 files changed

+460
-407
lines changed

c10/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ if (${USE_GLOG})
3939
target_link_libraries(c10 PUBLIC glog::glog)
4040
endif()
4141

42+
if (ANDROID)
43+
target_link_libraries(c10 PRIVATE log)
44+
endif()
45+
4246
target_include_directories(
4347
c10 PUBLIC
4448
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../>

c10/macros/Macros.h

+7
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@
5252
// the c10 namespace but not any nontrivial files.
5353
namespace c10 {} // namespace c10
5454

55+
// C10_NORETURN
56+
#if defined(_MSC_VER)
57+
#define C10_NORETURN __declspec(noreturn)
58+
#else
59+
#define C10_NORETURN __attribute__((noreturn))
60+
#endif
61+
5562
#endif // C10_MACROS_MACROS_H_

caffe2/core/logging_test.cc renamed to c10/test/logging_test.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#include <algorithm>
22

3-
#include "caffe2/core/logging.h"
43
#include <gtest/gtest.h>
4+
#include "c10/util/Logging.h"
55

6-
namespace caffe2 {
6+
namespace c10_test {
7+
8+
using std::set;
9+
using std::string;
10+
using std::vector;
711

812
TEST(LoggingTest, TestEnforceTrue) {
913
// This should just work.
@@ -17,7 +21,7 @@ TEST(LoggingTest, TestEnforceFalse) {
1721
CAFFE_ENFORCE(false, "This throws.");
1822
// This should never be triggered.
1923
ADD_FAILURE();
20-
} catch (const EnforceNotMet&) {
24+
} catch (const ::c10::Error&) {
2125
}
2226
std::swap(FLAGS_caffe2_use_fatal_for_enforce, kFalse);
2327
}
@@ -29,7 +33,7 @@ TEST(LoggingTest, TestEnforceEquals) {
2933
CAFFE_ENFORCE_THAT(Equals(++x, ++y));
3034
// This should never be triggered.
3135
ADD_FAILURE();
32-
} catch (const EnforceNotMet& err) {
36+
} catch (const ::c10::Error& err) {
3337
EXPECT_NE(err.msg().find("5 vs 6"), string::npos);
3438
}
3539

@@ -45,11 +49,11 @@ TEST(LoggingTest, EnforceShowcase) {
4549
int one = 1;
4650
int two = 2;
4751
int three = 3;
48-
#define WRAP_AND_PRINT(exp) \
49-
try { \
50-
exp; \
51-
} catch (const EnforceNotMet&) { \
52-
/* EnforceNotMet already does LOG(ERROR) */ \
52+
#define WRAP_AND_PRINT(exp) \
53+
try { \
54+
exp; \
55+
} catch (const ::c10::Error&) { \
56+
/* ::c10::Error already does LOG(ERROR) */ \
5357
}
5458
WRAP_AND_PRINT(CAFFE_ENFORCE_EQ(one, two));
5559
WRAP_AND_PRINT(CAFFE_ENFORCE_NE(one * 2, two));
@@ -82,4 +86,4 @@ TEST(LoggingDeathTest, TestEnforceUsingFatal) {
8286
}
8387
#endif
8488

85-
} // namespace caffe2
89+
} // namespace c10_test

caffe2/core/logging.cc renamed to c10/util/Logging.cpp

+44-41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "caffe2/core/logging.h"
2-
#include "caffe2/core/flags.h"
1+
#include "c10/util/Logging.h"
2+
#include "c10/util/Flags.h"
33

44
#include <algorithm>
55
#include <cstring>
@@ -14,7 +14,7 @@ C10_DEFINE_bool(
1414
"If set true, when CAFFE_ENFORCE is not met, abort instead "
1515
"of throwing an exception.");
1616

17-
namespace caffe2 {
17+
namespace c10 {
1818
namespace enforce_detail {
1919
/* implicit */ EnforceFailMessage::EnforceFailMessage(std::string&& msg) {
2020
msg_ = new std::string(std::move(msg));
@@ -45,9 +45,9 @@ void ThrowEnforceNotMet(
4545
throw e;
4646
}
4747

48-
} // namespace caffe2
48+
} // namespace c10
4949

50-
#ifdef C10_USE_GFLAGS
50+
#if defined(C10_USE_GFLAGS) && defined(C10_USE_GLOG)
5151
// When GLOG depends on GFLAGS, these variables are being defined in GLOG
5252
// directly via the GFLAGS definition, so we will use DECLARE_* to declare
5353
// them, and use them in Caffe2.
@@ -57,25 +57,28 @@ DECLARE_int32(minloglevel);
5757
DECLARE_int32(v);
5858
// GLOG's logtostderr value
5959
DECLARE_bool(logtostderr);
60-
#elif !CAFFE2_MOBILE && !__APPLE__ && !defined(_WIN32)
61-
// Declare our own versions of the above flags so we don't error out
62-
// when they are passed into Caffe2.
63-
C10_DEFINE_int(minloglevel, 0, "Equivalent to glog minloglevel");
64-
C10_DEFINE_int(v, 0, "Equivalent to glog verbose");
60+
#endif // defined(C10_USE_GFLAGS) && defined(C10_USE_GLOG)
61+
62+
#if !defined(C10_USE_GLOG)
63+
// This backward compatibility flags are in order to deal with cases where
64+
// Caffe2 are not built with glog, but some init flags still pass in these
65+
// flags. They may go away in the future.
66+
C10_DEFINE_int32(minloglevel, 0, "Equivalent to glog minloglevel");
67+
C10_DEFINE_int32(v, 0, "Equivalent to glog verbose");
6568
C10_DEFINE_bool(logtostderr, false, "Equivalent to glog logtostderr");
66-
#endif // C10_USE_GFLAGS
69+
#endif // !defined(c10_USE_GLOG)
6770

68-
#ifdef CAFFE2_USE_GOOGLE_GLOG
71+
#ifdef C10_USE_GLOG
6972

7073
// Provide easy access to the above variables, regardless whether GLOG is
7174
// dependent on GFLAGS or not. Note that the namespace (fLI, fLB) is actually
7275
// consistent between GLOG and GFLAGS, so we can do the below declaration
7376
// consistently.
74-
namespace caffe2 {
77+
namespace c10 {
78+
using fLB::FLAGS_logtostderr;
7579
using fLI::FLAGS_minloglevel;
7680
using fLI::FLAGS_v;
77-
using fLB::FLAGS_logtostderr;
78-
} // namespace caffe2
81+
} // namespace c10
7982

8083
C10_DEFINE_int(
8184
caffe2_log_level,
@@ -89,21 +92,21 @@ C10_DEFINE_int(
8992
namespace google {
9093
namespace glog_internal_namespace_ {
9194
bool IsGoogleLoggingInitialized();
92-
} // namespace glog_internal_namespace_
93-
} // namespace google
95+
} // namespace glog_internal_namespace_
96+
} // namespace google
9497

95-
96-
namespace caffe2 {
98+
namespace c10 {
9799
bool InitCaffeLogging(int* argc, char** argv) {
98-
if (*argc == 0) return true;
100+
if (*argc == 0)
101+
return true;
99102
#if !defined(_MSC_VER)
100103
// This trick can only be used on UNIX platforms
101104
if (!::google::glog_internal_namespace_::IsGoogleLoggingInitialized())
102105
#endif
103106
{
104107
::google::InitGoogleLogging(argv[0]);
105108
#if !defined(_MSC_VER)
106-
// This is never defined on Windows
109+
// This is never defined on Windows
107110
::google::InstallFailureSignalHandler();
108111
#endif
109112
}
@@ -129,9 +132,9 @@ void ShowLogInfoToStderr() {
129132
FLAGS_logtostderr = 1;
130133
FLAGS_minloglevel = std::min(FLAGS_minloglevel, google::GLOG_INFO);
131134
}
132-
} // namespace caffe2
135+
} // namespace c10
133136

134-
#else // !CAFFE2_USE_GOOGLE_GLOG
137+
#else // !C10_USE_GLOG
135138

136139
#ifdef ANDROID
137140
#include <android/log.h>
@@ -142,11 +145,12 @@ C10_DEFINE_int(
142145
ERROR,
143146
"The minimum log level that caffe2 will output.");
144147

145-
namespace caffe2 {
148+
namespace c10 {
146149
bool InitCaffeLogging(int* argc, char** argv) {
147150
// When doing InitCaffeLogging, we will assume that caffe's flag paser has
148151
// already finished.
149-
if (*argc == 0) return true;
152+
if (*argc == 0)
153+
return true;
150154
if (!c10::CommandLineFlagsHasBeenParsed()) {
151155
std::cerr << "InitCaffeLogging() has to be called after "
152156
"c10::ParseCommandLineFlags. Modify your program to make sure "
@@ -162,24 +166,23 @@ bool InitCaffeLogging(int* argc, char** argv) {
162166
return true;
163167
}
164168

165-
void UpdateLoggingLevelsFromFlags() {
166-
}
169+
void UpdateLoggingLevelsFromFlags() {}
167170

168171
void ShowLogInfoToStderr() {
169172
FLAGS_caffe2_log_level = INFO;
170173
}
171174

172-
MessageLogger::MessageLogger(const char *file, int line, int severity)
173-
: severity_(severity) {
175+
MessageLogger::MessageLogger(const char* file, int line, int severity)
176+
: severity_(severity) {
174177
if (severity_ < FLAGS_caffe2_log_level) {
175178
// Nothing needs to be logged.
176179
return;
177180
}
178181
#ifdef ANDROID
179182
tag_ = "native";
180-
#else // !ANDROID
183+
#else // !ANDROID
181184
tag_ = "";
182-
#endif // ANDROID
185+
#endif // ANDROID
183186
/*
184187
time_t rawtime;
185188
struct tm * timeinfo;
@@ -210,12 +213,12 @@ MessageLogger::~MessageLogger() {
210213
stream_ << "\n";
211214
#ifdef ANDROID
212215
static const int android_log_levels[] = {
213-
ANDROID_LOG_FATAL, // LOG_FATAL
214-
ANDROID_LOG_ERROR, // LOG_ERROR
215-
ANDROID_LOG_WARN, // LOG_WARNING
216-
ANDROID_LOG_INFO, // LOG_INFO
217-
ANDROID_LOG_DEBUG, // VLOG(1)
218-
ANDROID_LOG_VERBOSE, // VLOG(2) .. VLOG(N)
216+
ANDROID_LOG_FATAL, // LOG_FATAL
217+
ANDROID_LOG_ERROR, // LOG_ERROR
218+
ANDROID_LOG_WARN, // LOG_WARNING
219+
ANDROID_LOG_INFO, // LOG_INFO
220+
ANDROID_LOG_DEBUG, // VLOG(1)
221+
ANDROID_LOG_VERBOSE, // VLOG(2) .. VLOG(N)
219222
};
220223
int android_level_index = FATAL - std::min(FATAL, severity_);
221224
int level = android_log_levels[std::min(android_level_index, 5)];
@@ -225,7 +228,7 @@ MessageLogger::~MessageLogger() {
225228
if (severity_ == FATAL) {
226229
__android_log_print(ANDROID_LOG_FATAL, tag_, "terminating.\n");
227230
}
228-
#else // !ANDROID
231+
#else // !ANDROID
229232
if (severity_ >= FLAGS_caffe2_log_level) {
230233
// If not building on Android, log all output to std::cerr.
231234
std::cerr << stream_.str();
@@ -236,12 +239,12 @@ MessageLogger::~MessageLogger() {
236239
std::cerr << std::flush;
237240
}
238241
}
239-
#endif // ANDROID
242+
#endif // ANDROID
240243
if (severity_ == FATAL) {
241244
DealWithFatal();
242245
}
243246
}
244247

245-
} // namespace caffe2
248+
} // namespace c10
246249

247-
#endif // !CAFFE2_USE_GOOGLE_GLOG
250+
#endif // !C10_USE_GLOG

0 commit comments

Comments
 (0)