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