Joe Onorato | 6c9547d | 2016-09-07 18:43:49 -0700 | [diff] [blame] | 1 | #include "Errors.h" |
| 2 | |
| 3 | #include <stdlib.h> |
| 4 | |
| 5 | namespace android { |
| 6 | namespace javastream_proto { |
| 7 | |
| 8 | Errors ERRORS; |
| 9 | |
| 10 | const string UNKNOWN_FILE; |
| 11 | const int UNKNOWN_LINE = 0; |
| 12 | |
| 13 | Error::Error() |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | Error::Error(const Error& that) |
| 18 | :filename(that.filename), |
| 19 | lineno(that.lineno), |
| 20 | message(that.message) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | Error::Error(const string& f, int l, const char* m) |
| 25 | :filename(f), |
| 26 | lineno(l), |
| 27 | message(m) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | Errors::Errors() |
| 32 | :m_errors() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | Errors::~Errors() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | Errors::Add(const string& filename, int lineno, const char* format, ...) |
| 42 | { |
| 43 | va_list args; |
| 44 | va_start(args, format); |
| 45 | AddImpl(filename, lineno, format, args); |
| 46 | va_end(args); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | Errors::AddImpl(const string& filename, int lineno, const char* format, va_list args) |
| 51 | { |
| 52 | va_list args2; |
| 53 | va_copy(args2, args); |
| 54 | int message_size = vsnprintf((char*)NULL, 0, format, args2); |
| 55 | va_end(args2); |
| 56 | |
| 57 | char* buffer = new char[message_size+1]; |
| 58 | vsnprintf(buffer, message_size, format, args); |
| 59 | Error error(filename, lineno, buffer); |
| 60 | delete[] buffer; |
| 61 | |
| 62 | m_errors.push_back(error); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | Errors::Print() const |
| 67 | { |
| 68 | for (vector<Error>::const_iterator it = m_errors.begin(); it != m_errors.end(); it++) { |
| 69 | if (it->filename == UNKNOWN_FILE) { |
| 70 | fprintf(stderr, "%s", it->message.c_str()); |
| 71 | } else if (it->lineno == UNKNOWN_LINE) { |
| 72 | fprintf(stderr, "%s:%s", it->filename.c_str(), it->message.c_str()); |
| 73 | } else { |
| 74 | fprintf(stderr, "%s:%d:%s", it->filename.c_str(), it->lineno, it->message.c_str()); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | bool |
| 80 | Errors::HasErrors() const |
| 81 | { |
| 82 | return m_errors.size() > 0; |
| 83 | } |
| 84 | |
| 85 | } // namespace javastream_proto |
| 86 | } // namespace android |
| 87 | |