summaryrefslogtreecommitdiff
path: root/tools/streaming_proto/Errors.cpp
diff options
context:
space:
mode:
authorJoe Onorato <joeo@google.com>2016-09-07 18:43:49 -0700
committerJoe Onorato <joeo@google.com>2016-10-12 16:37:18 -0700
commit6c9547d8e1c35d7afa9bc9be11d5ff86ec60db14 (patch)
treee541af3eda20820eaa81c1e306f704046ac3ea54 /tools/streaming_proto/Errors.cpp
parent87ff355f0a91785a44d50f8e15392781c2065f1d (diff)
Add android.util.proto package as an @TestApi.
The classes there add a way for the platform to write out protocol buffers that doesn't require lots of small objects, generate code, and extra copying. Includes the plugin for protoc to generate the constants. Test: proto cts tests Change-Id: I6385c198cecda9ac6fa533151609e3ace341af01
Diffstat (limited to 'tools/streaming_proto/Errors.cpp')
-rw-r--r--tools/streaming_proto/Errors.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/tools/streaming_proto/Errors.cpp b/tools/streaming_proto/Errors.cpp
new file mode 100644
index 000000000000..91c6b9245de0
--- /dev/null
+++ b/tools/streaming_proto/Errors.cpp
@@ -0,0 +1,87 @@
+#include "Errors.h"
+
+#include <stdlib.h>
+
+namespace android {
+namespace javastream_proto {
+
+Errors ERRORS;
+
+const string UNKNOWN_FILE;
+const int UNKNOWN_LINE = 0;
+
+Error::Error()
+{
+}
+
+Error::Error(const Error& that)
+ :filename(that.filename),
+ lineno(that.lineno),
+ message(that.message)
+{
+}
+
+Error::Error(const string& f, int l, const char* m)
+ :filename(f),
+ lineno(l),
+ message(m)
+{
+}
+
+Errors::Errors()
+ :m_errors()
+{
+}
+
+Errors::~Errors()
+{
+}
+
+void
+Errors::Add(const string& filename, int lineno, const char* format, ...)
+{
+ va_list args;
+ va_start(args, format);
+ AddImpl(filename, lineno, format, args);
+ va_end(args);
+}
+
+void
+Errors::AddImpl(const string& filename, int lineno, const char* format, va_list args)
+{
+ va_list args2;
+ va_copy(args2, args);
+ int message_size = vsnprintf((char*)NULL, 0, format, args2);
+ va_end(args2);
+
+ char* buffer = new char[message_size+1];
+ vsnprintf(buffer, message_size, format, args);
+ Error error(filename, lineno, buffer);
+ delete[] buffer;
+
+ m_errors.push_back(error);
+}
+
+void
+Errors::Print() const
+{
+ for (vector<Error>::const_iterator it = m_errors.begin(); it != m_errors.end(); it++) {
+ if (it->filename == UNKNOWN_FILE) {
+ fprintf(stderr, "%s", it->message.c_str());
+ } else if (it->lineno == UNKNOWN_LINE) {
+ fprintf(stderr, "%s:%s", it->filename.c_str(), it->message.c_str());
+ } else {
+ fprintf(stderr, "%s:%d:%s", it->filename.c_str(), it->lineno, it->message.c_str());
+ }
+ }
+}
+
+bool
+Errors::HasErrors() const
+{
+ return m_errors.size() > 0;
+}
+
+} // namespace javastream_proto
+} // namespace android
+