aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/platform/default/logging.cc
diff options
context:
space:
mode:
Diffstat (limited to 'tensorflow/core/platform/default/logging.cc')
-rw-r--r--tensorflow/core/platform/default/logging.cc125
1 files changed, 125 insertions, 0 deletions
diff --git a/tensorflow/core/platform/default/logging.cc b/tensorflow/core/platform/default/logging.cc
new file mode 100644
index 0000000000..8a16a537b0
--- /dev/null
+++ b/tensorflow/core/platform/default/logging.cc
@@ -0,0 +1,125 @@
+#include "tensorflow/core/platform/default/logging.h"
+
+#if defined(PLATFORM_POSIX_ANDROID)
+#include <android/log.h>
+#include <sstream>
+#endif
+
+#include <stdlib.h>
+
+namespace tensorflow {
+namespace internal {
+
+LogMessage::LogMessage(const char* fname, int line, int severity)
+ : fname_(fname), line_(line), severity_(severity) {}
+
+#if defined(PLATFORM_POSIX_ANDROID)
+void LogMessage::GenerateLogMessage() {
+ int android_log_level;
+ switch (severity_) {
+ case INFO:
+ android_log_level = ANDROID_LOG_INFO;
+ break;
+ case WARNING:
+ android_log_level = ANDROID_LOG_WARN;
+ break;
+ case ERROR:
+ android_log_level = ANDROID_LOG_ERROR;
+ break;
+ case FATAL:
+ android_log_level = ANDROID_LOG_FATAL;
+ break;
+ default:
+ if (severity_ < INFO) {
+ android_log_level = ANDROID_LOG_VERBOSE;
+ } else {
+ android_log_level = ANDROID_LOG_ERROR;
+ }
+ break;
+ }
+
+ std::stringstream ss;
+ ss << fname_ << ":" << line_ << " " << str();
+ __android_log_write(android_log_level, "native", ss.str().c_str());
+
+ // Android logging at level FATAL does not terminate execution, so abort()
+ // is still required to stop the program.
+ if (severity_ == FATAL) {
+ abort();
+ }
+}
+
+#else
+
+void LogMessage::GenerateLogMessage() {
+ // TODO(jeff,sanjay): For open source version, replace this with something
+ // that logs through the env or something and fill in appropriate time info.
+ fprintf(stderr, "%c %s:%d] %s\n", "IWEF"[severity_], fname_, line_,
+ str().c_str());
+}
+#endif
+
+LogMessage::~LogMessage() { GenerateLogMessage(); }
+
+LogMessageFatal::LogMessageFatal(const char* file, int line)
+ : LogMessage(file, line, FATAL) {}
+LogMessageFatal::~LogMessageFatal() {
+ // abort() ensures we don't return (we promised we would not via
+ // ATTRIBUTE_NORETURN).
+ GenerateLogMessage();
+ abort();
+}
+
+template <>
+void MakeCheckOpValueString(std::ostream* os, const char& v) {
+ if (v >= 32 && v <= 126) {
+ (*os) << "'" << v << "'";
+ } else {
+ (*os) << "char value " << (short)v;
+ }
+}
+
+template <>
+void MakeCheckOpValueString(std::ostream* os, const signed char& v) {
+ if (v >= 32 && v <= 126) {
+ (*os) << "'" << v << "'";
+ } else {
+ (*os) << "signed char value " << (short)v;
+ }
+}
+
+template <>
+void MakeCheckOpValueString(std::ostream* os, const unsigned char& v) {
+ if (v >= 32 && v <= 126) {
+ (*os) << "'" << v << "'";
+ } else {
+ (*os) << "unsigned char value " << (unsigned short)v;
+ }
+}
+
+#if LANG_CXX11
+template <>
+void MakeCheckOpValueString(std::ostream* os, const std::nullptr_t& p) {
+ (*os) << "nullptr";
+}
+#endif
+
+CheckOpMessageBuilder::CheckOpMessageBuilder(const char* exprtext)
+ : stream_(new std::ostringstream) {
+ *stream_ << "Check failed: " << exprtext << " (";
+}
+
+CheckOpMessageBuilder::~CheckOpMessageBuilder() { delete stream_; }
+
+std::ostream* CheckOpMessageBuilder::ForVar2() {
+ *stream_ << " vs. ";
+ return stream_;
+}
+
+string* CheckOpMessageBuilder::NewString() {
+ *stream_ << ")";
+ return new string(stream_->str());
+}
+
+} // namespace internal
+} // namespace tensorflow