1 #include "Errors.h"
2
3 #include <stdlib.h>
4
5 namespace android {
6 namespace stream_proto {
7
8 Errors ERRORS;
9
10 const string UNKNOWN_FILE;
11 const int UNKNOWN_LINE = 0;
12
Error()13 Error::Error()
14 {
15 }
16
Error(const Error & that)17 Error::Error(const Error& that)
18 :filename(that.filename),
19 lineno(that.lineno),
20 message(that.message)
21 {
22 }
23
Error(const string & f,int l,const char * m)24 Error::Error(const string& f, int l, const char* m)
25 :filename(f),
26 lineno(l),
27 message(m)
28 {
29 }
30
Errors()31 Errors::Errors()
32 :m_errors()
33 {
34 }
35
~Errors()36 Errors::~Errors()
37 {
38 }
39
40 void
Add(const string & filename,int lineno,const char * format,...)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
AddImpl(const string & filename,int lineno,const char * format,va_list args)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
Print() const66 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
HasErrors() const80 Errors::HasErrors() const
81 {
82 return m_errors.size() > 0;
83 }
84
85 } // namespace stream_proto
86 } // namespace android
87
88