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