28 this->logger = logger;
36 if (err_len > 0 && err_data != NULL) free(err_data);
66 logger->
log(FATAL,
"Serializable::setError: error already exists.\n");
70 if (data_len == 0)
return;
72 err_data = (uint8_t *) malloc(err_len);
73 memcpy(err_data, data, data_len);
76 uint8_t Serializable::getErrorCode()
const {
80 uint8_t Serializable::getErrorSubCode()
const {
84 const uint8_t* Serializable::getError()
const {
88 size_t Serializable::getErrorLength()
const {
98 setError(other.getErrorCode(), other.getErrorSubCode(), other.getError(), other.getErrorLength());
111 return doPrint(0, &to, &buf_sz);
126 return doPrint(indent, &to, &buf_sz);
145 if (*buf_left <= indent * 4)
return 0;
146 for (
size_t i = 0; i < indent; i++) {
147 sprintf((
char *) *to,
" ");
152 va_start(args, format);
153 ssize_t sz = vsnprintf((
char *) *to, *buf_left, format, args);
156 if (sz < 0)
return sz;
157 if ((
size_t) sz > *buf_left) {
158 size_t written = *buf_left;
160 return written + indent * 4;
166 return sz + indent * 4;
177 logger->
log(WARN,
"Serializable::length: using default implementation of Serializable::length() (write to dummy).\n");
178 uint8_t buffer[4096];
179 return write(buffer, 4096);
188 this->logger = logger;
virtual ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const =0
Print implementation.
virtual ssize_t length() const
Get size in bytes required to serialize the object.
static ssize_t _print(size_t indent, uint8_t **to, size_t *buf_left, const char *format,...)
Print helper.
bool hasError() const
Check if error information available.
~Serializable()
Destroy the Serializable:: Serializable object.
The serializable base class.
ssize_t print(uint8_t *to, size_t buf_sz) const
Print the Serializable object as human readable string.
void log(LogLevel level, const char *format_str,...)
Log a message. Consider using LIBBGP_LOG if logging the message needs a lot of computing power...
virtual ssize_t write(uint8_t *to, size_t buf_sz) const =0
Serialize the object and write to buffer.
void setLogger(BgpLogHandler *logger)
Replace logger for this object.
void setError(uint8_t err, uint8_t suberr, const uint8_t *data, size_t data_len)
Set the error information.
Serializable(BgpLogHandler *logger)
Construct a new Serializable:: Serializable object.
void forwardParseError(const Serializable &other)
Forward error information from other Serializable object.