Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
logging_android.cc
Go to the documentation of this file.
1 
18 #include "ion/port/logging.h"
19 
20 #include <android/log.h>
21 #include <cstdio>
22 #include <vector>
23 
24 namespace {
25 
27 std::vector<std::string> SplitStringOnLineBreaks(const std::string& str) {
28  std::vector<std::string> strings;
29 
30  size_t end_pos = 0;
31  const size_t length = str.length();
32  while (end_pos != std::string::npos && end_pos < length) {
33  const size_t start_pos = end_pos;
35  end_pos = str.find_first_of('\n', start_pos);
36 
37  strings.push_back(str.substr(start_pos, end_pos - start_pos));
40  if (end_pos != std::string::npos)
41  end_pos++;
42  }
43  return strings;
44 }
45 
46 class AndroidLogEntryWriter : public ion::port::LogEntryWriter {
47  public:
48  AndroidLogEntryWriter() {}
49  virtual ~AndroidLogEntryWriter() {}
50 
52  virtual void Write(ion::port::LogSeverity severity,
53  const std::string& message) {
54  android_LogPriority priority;
55  switch (severity) {
56  default:
57  case ion::port::INFO:
58  priority = ANDROID_LOG_INFO;
59  break;
60  case ion::port::WARNING:
61  priority = ANDROID_LOG_WARN;
62  break;
63  case ion::port::ERROR:
64  priority = ANDROID_LOG_ERROR;
65  break;
66  case ion::port::FATAL:
67  priority = ANDROID_LOG_FATAL;
68  break;
69  case ion::port::DFATAL:
70  priority = ANDROID_LOG_FATAL;
71  break;
72  }
73 
75  const std::vector<std::string> lines = SplitStringOnLineBreaks(message);
76  for (auto it = lines.begin(); it < lines.end(); ++it)
77  __android_log_write(priority, "Ion", it->c_str());
81  fprintf(stderr, "%s %s\n", GetSeverityName(severity), message.c_str());
82  }
83 };
84 
85 } // namespace
86 
88  return new AndroidLogEntryWriter();
89 }
Abstract class which can be overridden to integrate Ion logging with other logging systems...
Definition: logging.h:36
const std::string & str
LogSeverity
Definition: logging.h:26
uint32 length
virtual void Write(LogSeverity severity, const std::string &message)=0
const char * GetSeverityName(LogSeverity severity) const
Convenient way to map a severity-code to a printable represenation.
Definition: logging.cc:24
ION_API LogEntryWriter * CreateDefaultLogEntryWriter()
Instantiate a new LogEntryWriter of the default type for the current platform...