1 #include "caffe2/core/logging.h" 10 CAFFE2_DEFINE_bool(caffe2_use_fatal_for_enforce,
false,
11 "If set true, when CAFFE_ENFORCE is not met, abort instead " 12 "of throwing an exception.");
15 std::string StripBasename(
const std::string &full_path) {
16 const char kSeparator =
'/';
17 size_t pos = full_path.rfind(kSeparator);
18 if (pos != std::string::npos) {
19 return full_path.substr(pos + 1, std::string::npos);
25 size_t ReplaceAll(
string& s,
const char* from,
const char* to) {
26 CAFFE_ENFORCE(from && *from);
29 size_t numReplaced = 0;
30 string::size_type lenFrom = std::strlen(from);
31 string::size_type lenTo = std::strlen(to);
32 for (string::size_type pos = s.find(from); pos != string::npos;
33 pos = s.find(from, pos + lenTo)) {
34 s.replace(pos, lenFrom, to);
40 static std::function<string(void)> FetchStackTrace = []() {
return ""; };
42 void SetStackTraceFetcher(std::function<
string(
void)> fetcher) {
43 FetchStackTrace = fetcher;
46 static std::function<void(const OperatorDef&)> OperatorLogger =
47 [](
const OperatorDef&) {
return; };
49 void SetOperatorLogger(std::function<
void(
const OperatorDef&)> tracer) {
50 OperatorLogger = tracer;
53 std::function<void(const OperatorDef&)> GetOperatorLogger() {
54 return OperatorLogger;
57 EnforceNotMet::EnforceNotMet(
60 const char* condition,
63 : msg_stack_{MakeString(
65 StripBasename(std::string(file)),
73 stack_trace_(FetchStackTrace()) {
74 if (FLAGS_caffe2_use_fatal_for_enforce) {
75 LOG(FATAL) << msg_stack_[0];
78 full_msg_ = this->msg();
81 void EnforceNotMet::AppendMessage(
const string& msg) {
82 msg_stack_.push_back(msg);
83 full_msg_ = this->msg();
86 string EnforceNotMet::msg()
const {
87 return std::accumulate(msg_stack_.begin(), msg_stack_.end(), string(
"")) +
91 const char* EnforceNotMet::what() const noexcept {
92 return full_msg_.c_str();
95 const void* EnforceNotMet::caller() const noexcept {
102 #ifdef CAFFE2_USE_GOOGLE_GLOG 104 #ifdef CAFFE2_USE_GFLAGS 106 CAFFE2_DECLARE_int(minloglevel);
108 CAFFE2_DECLARE_int(v);
110 CAFFE2_DECLARE_bool(logtostderr);
114 using fLI::FLAGS_minloglevel;
116 using fLB::FLAGS_logtostderr;
118 #endif // CAFFE2_USE_GFLAGS 120 CAFFE2_DEFINE_int(caffe2_log_level, google::ERROR,
121 "The minimum log level that caffe2 will output.");
128 namespace glog_internal_namespace_ {
129 bool IsGoogleLoggingInitialized();
135 bool InitCaffeLogging(
int* argc,
char** argv) {
136 if (*argc == 0)
return true;
137 if (!::google::glog_internal_namespace_::IsGoogleLoggingInitialized()) {
138 ::google::InitGoogleLogging(argv[0]);
139 ::google::InstallFailureSignalHandler();
143 FLAGS_minloglevel = std::min(FLAGS_caffe2_log_level, FLAGS_minloglevel);
145 if (FLAGS_caffe2_log_level < google::ERROR) {
146 FLAGS_logtostderr = 1;
149 if (FLAGS_caffe2_log_level < 0) {
150 FLAGS_v = std::min(FLAGS_v, -FLAGS_caffe2_log_level);
156 #else // !CAFFE2_USE_GOOGLE_GLOG 159 #include <android/log.h> 162 CAFFE2_DEFINE_int(caffe2_log_level, ERROR,
163 "The minimum log level that caffe2 will output.");
166 bool InitCaffeLogging(
int* argc,
char** argv) {
169 if (*argc == 0)
return true;
171 std::cerr <<
"InitCaffeLogging() has to be called after " 172 "ParseCaffeCommandLineFlags. Modify your program to make sure " 173 "of this." << std::endl;
176 if (FLAGS_caffe2_log_level > FATAL) {
177 std::cerr <<
"The log level of Caffe2 has to be no larger than FATAL(" 178 << FATAL <<
"). Capping it to FATAL." << std::endl;
179 FLAGS_caffe2_log_level = FATAL;
184 MessageLogger::MessageLogger(
const char *file,
int line,
int severity)
185 : severity_(severity) {
186 if (severity_ < FLAGS_caffe2_log_level) {
204 stream_ <<
"[" << CAFFE2_SEVERITY_PREFIX[std::min(4, FATAL - severity_)]
211 <<
" " << StripBasename(std::string(file)) <<
":" << line <<
"] ";
215 MessageLogger::~MessageLogger() {
216 if (severity_ < FLAGS_caffe2_log_level) {
222 static const int android_log_levels[] = {
230 int android_level_index = FATAL - std::min(FATAL, severity_);
231 int level = android_log_levels[std::min(android_level_index, 5)];
233 __android_log_print(level, tag_,
"%s", stream_.str().c_str());
235 if (severity_ == FATAL) {
236 __android_log_print(ANDROID_LOG_FATAL, tag_,
"terminating.\n");
239 if (severity_ >= FLAGS_caffe2_log_level) {
241 std::cerr << stream_.str();
244 if (severity_ == FATAL) {
251 #endif // !CAFFE2_USE_GOOGLE_GLOG Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
Commandline flags support for Caffe2.
bool CommandLineFlagsHasBeenParsed()
Checks if the commandline flags has already been passed.