libbgp  0.6
A C++ BGP Library.
bgp-log-handler.cc
Go to the documentation of this file.
1 
11 #include "bgp-log-handler.h"
12 #include <stdarg.h>
13 #include <stdio.h>
14 
15 namespace libbgp {
16 
17 const char* bgp_log_level_str[] = {
18  "FATAL",
19  "ERROR",
20  "WARN ",
21  "INFO ",
22  "DEBUG"
23 };
24 
25 BgpLogHandler::BgpLogHandler() {
26  level = INFO;
27 }
28 
35  this->level = level;
36 }
37 
44  return level;
45 }
46 
56 void BgpLogHandler::log(LogLevel level, const char* format_str, ...) {
57  if (level > this->level) return;
58 
59  buf_mtx.lock();
60  int pre_sz = snprintf(out_buffer, 4096, "[%s] ", bgp_log_level_str[level]);
61 
62  va_list args;
63  va_start(args, format_str);
64  vsnprintf(out_buffer + pre_sz, 4096 - pre_sz, format_str, args);
65  va_end(args);
66  logImpl(out_buffer);
67  buf_mtx.unlock();
68 }
69 
78 void BgpLogHandler::log(LogLevel level, const Serializable &serializable) {
79  buf_mtx.lock();
80  int pre_sz = snprintf(out_buffer, 4096, "[%s] ", bgp_log_level_str[level]);
81  serializable.print((uint8_t *) (out_buffer + pre_sz), 4096 - pre_sz);
82  logImpl(out_buffer);
83  buf_mtx.unlock();
84 }
85 
86 void BgpLogHandler::logImpl(const char* str) {
87  fprintf(::stderr, "%s", str);
88 }
89 
90 }
LogLevel
Log levels for logger (BgpLogHandler)
The serializable base class.
Definition: serializable.h:26
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...
Definition: bgp-afi.h:14
BGP log handler.
virtual void logImpl(const char *str)
Log implementation. By default, it writes to stderr. You may implement your own BgpLogHandler to writ...
void setLogLevel(LogLevel level)
Set the log level.
LogLevel getLogLevel() const
Get the log level.