Panda3D
androidLogStream.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file androidLogStream.cxx
10  * @author rdb
11  * @date 2013-01-12
12  */
13 
14 #include "androidLogStream.h"
15 #include "configVariableString.h"
16 
17 #ifdef ANDROID
18 
19 #include <android/log.h>
20 
21 /**
22  *
23  */
24 AndroidLogStream::AndroidLogStreamBuf::
25 AndroidLogStreamBuf(int priority) :
26  _priority(priority) {
27 
28  static ConfigVariableString android_log_tag
29  ("android-log-tag", "Panda3D",
30  PRC_DESC("This defines the tag that Panda3D will use when writing to the "
31  "Android log. The default is \"Panda3D\"."));
32 
33  if (_tag.empty()) {
34  _tag = android_log_tag.get_value();
35  }
36 
37  // The AndroidLogStreamBuf doesn't actually need a buffer--it's happy
38  // writing characters one at a time, since they're just getting stuffed into
39  // a string. (Although the code is written portably enough to use a buffer
40  // correctly, if we had one.)
41  setg(0, 0, 0);
42  setp(0, 0);
43 }
44 
45 /**
46  *
47  */
48 AndroidLogStream::AndroidLogStreamBuf::
49 ~AndroidLogStreamBuf() {
50  sync();
51 }
52 
53 /**
54  * Called by the system ostream implementation when the buffer should be
55  * flushed to output (for instance, on destruction).
56  */
57 int AndroidLogStream::AndroidLogStreamBuf::
58 sync() {
59  std::streamsize n = pptr() - pbase();
60 
61  // Write the characters that remain in the buffer.
62  for (char *p = pbase(); p < pptr(); ++p) {
63  write_char(*p);
64  }
65 
66  pbump(-n); // Reset pptr().
67  return 0; // EOF to indicate write full.
68 }
69 
70 /**
71  * Called by the system ostream implementation when its internal buffer is
72  * filled, plus one character.
73  */
74 int AndroidLogStream::AndroidLogStreamBuf::
75 overflow(int ch) {
76  std::streamsize n = pptr() - pbase();
77 
78  if (n != 0 && sync() != 0) {
79  return EOF;
80  }
81 
82  if (ch != EOF) {
83  // Write one more character.
84  write_char(ch);
85  }
86 
87  return 0;
88 }
89 
90 /**
91  * Stores a single character.
92  */
93 void AndroidLogStream::AndroidLogStreamBuf::
94 write_char(char c) {
95  nout.put(c);
96  if (c == '\n') {
97  // Write a line to the log file.
98  __android_log_write(_priority, _tag.c_str(), _data.c_str());
99  _data.clear();
100  } else {
101  _data += c;
102  }
103 }
104 
105 /**
106  *
107  */
108 AndroidLogStream::
109 AndroidLogStream(int priority) :
110  std::ostream(new AndroidLogStreamBuf(priority)) {
111 }
112 
113 /**
114  *
115  */
116 AndroidLogStream::
117 ~AndroidLogStream() {
118  delete rdbuf();
119 }
120 
121 /**
122  * Returns an AndroidLogStream suitable for writing log messages with the
123  * indicated severity.
124  */
125 std::ostream &AndroidLogStream::
126 out(NotifySeverity severity) {
127  static AndroidLogStream* streams[NS_fatal + 1] = {nullptr};
128 
129  if (streams[severity] == nullptr) {
130  int priority = ANDROID_LOG_UNKNOWN;
131  if (severity != NS_unspecified) {
132  priority = ((int)severity) + 1;
133  }
134  streams[severity] = new AndroidLogStream(priority);
135  }
136 
137  return *streams[severity];
138 }
139 
140 #endif // ANDROID
This is a convenience class to specialize ConfigVariable as a string type.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.