blob: 6890d99b104ca8e3b2b5d7ed261538ffe367b883 [file] [log] [blame]
Joe Onorato6c9547d2016-09-07 18:43:49 -07001#include "Errors.h"
2
Colin Cross7cdbd682021-09-13 16:30:12 -07003#include <stdarg.h>
Joe Onorato6c9547d2016-09-07 18:43:49 -07004#include <stdlib.h>
5
6namespace android {
Yi Jin0473f88b2017-10-09 11:21:40 -07007namespace stream_proto {
Joe Onorato6c9547d2016-09-07 18:43:49 -07008
9Errors ERRORS;
10
11const string UNKNOWN_FILE;
12const int UNKNOWN_LINE = 0;
13
14Error::Error()
15{
16}
17
18Error::Error(const Error& that)
19 :filename(that.filename),
20 lineno(that.lineno),
21 message(that.message)
22{
23}
24
25Error::Error(const string& f, int l, const char* m)
26 :filename(f),
27 lineno(l),
28 message(m)
29{
30}
31
32Errors::Errors()
33 :m_errors()
34{
35}
36
37Errors::~Errors()
38{
39}
40
41void
42Errors::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
50void
51Errors::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
66void
67Errors::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
80bool
81Errors::HasErrors() const
82{
83 return m_errors.size() > 0;
84}
85
Yi Jin0473f88b2017-10-09 11:21:40 -070086} // namespace stream_proto
Joe Onorato6c9547d2016-09-07 18:43:49 -070087} // namespace android
88