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::GLOG_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 !defined(_MSC_VER) 139 if (!::google::glog_internal_namespace_::IsGoogleLoggingInitialized())
142 ::google::InitGoogleLogging(argv[0]);
143 #if !defined(_MSC_VER) 145 ::google::InstallFailureSignalHandler();
150 FLAGS_minloglevel = std::min(FLAGS_caffe2_log_level, FLAGS_minloglevel);
152 if (FLAGS_caffe2_log_level < google::GLOG_ERROR) {
153 FLAGS_logtostderr = 1;
156 if (FLAGS_caffe2_log_level < 0) {
157 FLAGS_v = std::min(FLAGS_v, -FLAGS_caffe2_log_level);
163 FLAGS_logtostderr = 1;
164 FLAGS_minloglevel = std::min(FLAGS_minloglevel, google::GLOG_INFO);
168 #else // !CAFFE2_USE_GOOGLE_GLOG 171 #include <android/log.h> 174 CAFFE2_DEFINE_int(caffe2_log_level, ERROR,
175 "The minimum log level that caffe2 will output.");
178 bool InitCaffeLogging(
int* argc,
char** argv) {
181 if (*argc == 0)
return true;
183 std::cerr <<
"InitCaffeLogging() has to be called after " 184 "ParseCaffeCommandLineFlags. Modify your program to make sure " 185 "of this." << std::endl;
188 if (FLAGS_caffe2_log_level > FATAL) {
189 std::cerr <<
"The log level of Caffe2 has to be no larger than FATAL(" 190 << FATAL <<
"). Capping it to FATAL." << std::endl;
191 FLAGS_caffe2_log_level = FATAL;
197 FLAGS_caffe2_log_level = INFO;
200 MessageLogger::MessageLogger(
const char *file,
int line,
int severity)
201 : severity_(severity) {
202 if (severity_ < FLAGS_caffe2_log_level) {
220 stream_ <<
"[" << CAFFE2_SEVERITY_PREFIX[std::min(4, FATAL - severity_)]
227 <<
" " << StripBasename(std::string(file)) <<
":" << line <<
"] ";
231 MessageLogger::~MessageLogger() {
232 if (severity_ < FLAGS_caffe2_log_level) {
238 static const int android_log_levels[] = {
246 int android_level_index = FATAL - std::min(FATAL, severity_);
247 int level = android_log_levels[std::min(android_level_index, 5)];
249 __android_log_print(level, tag_,
"%s", stream_.str().c_str());
251 if (severity_ == FATAL) {
252 __android_log_print(ANDROID_LOG_FATAL, tag_,
"terminating.\n");
255 if (severity_ >= FLAGS_caffe2_log_level) {
257 std::cerr << stream_.str();
260 if (severity_ == FATAL) {
267 #endif // !CAFFE2_USE_GOOGLE_GLOG A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
void ShowLogInfoToStderr()
A utility to allow one to show log info to stderr after the program starts.
Commandline flags support for Caffe2.
bool CommandLineFlagsHasBeenParsed()
Checks if the commandline flags has already been passed.