25 vsnprintf(buf,
sizeof(buf),fmt,ap);
27 return std::string(buf);
35 std::vector<int16_t>
tokens;
37 ss << std::setfill(
'0');
45 while(input >= 1000) {
46 tokens.push_back(input % 1000);
52 for(std::vector<int16_t>::const_reverse_iterator it =
tokens.rbegin();
53 it !=
tokens.rend(); it++) {
54 ss <<
"," << std::setw(3) << *it;
64 snprintf(buf,
sizeof(buf),
"%02x:%02x:%02x:%02x:%02x:%02x",
65 addr[0],addr[1],addr[2],addr[3],addr[4],addr[5]);
66 return std::string(buf);
77 size_t debug_prefix_size = strlen(pfx) + 16;
78 debug_prefix = (
char *)calloc(
sizeof(
char), debug_prefix_size);
81 snprintf(
debug_prefix, debug_prefix_size,
"%s[%d]", pfx, (
int) getpid());
91 static std::vector<std::string> &
split(
const std::string &s,
char delim, std::vector<std::string> &elems)
93 std::stringstream ss(s);
95 while(std::getline(ss, item, delim)) {
96 elems.push_back(item);
101 static std::vector<std::string>
split(
const std::string &s,
char delim)
103 std::vector<std::string> elems;
104 return split(s, delim, elems);
114 static std::set<std::string> made_dirs;
120 path = path.substr(1);
123 std::vector<std::string> parts =
split(path,
'/');
128 for(std::vector<std::string>::const_iterator it=parts.begin();it!=parts.end();it++){
129 if(made_dirs.find(mpath)==made_dirs.end()){
131 int r =
MKDIR(mpath.c_str(),0777);
134 if(access(mpath.c_str(),X_OK)<0){
135 perror(mpath.c_str());
139 made_dirs.insert(mpath);
142 if(mpath.size()>0) mpath +=
"/";
156 vfprintf(stderr, fmt, ap);
159 fprintf(stderr,
"\n");
160 (void) fflush(stderr);
175 [[noreturn]]
void die(
const char *fmt, ...)
197 #if defined(HAVE_SIGACTION)
198 struct sigaction act, oact;
200 memset(&act, 0,
sizeof(act));
201 memset(&oact, 0,
sizeof(oact));
202 act.sa_handler = func;
203 sigemptyset(&act.sa_mask);
205 if (sigaction(signo, &act, &oact) < 0)
return (SIG_ERR);
206 return (oact.sa_handler);
207 #elif defined(HAVE_SIGSET)
208 return sigset(signo, func);
210 return signal(signo, func);
219 #ifdef HAVE_SYS_MMAN_H
220 #include <sys/mman.h>
226 #if !defined(HAVE_MMAP)
230 void *
mmap(
void *addr,
size_t length,
int prot,
int flags,
int fd, off_t offset)
232 void *buf = (
void *)malloc(length);
static const char tokens[256]
void(*)(int) portable_signal(int signo, void(*func)(int))
void init_debug(const char *pfx, int include_pid)
void die(const char *fmt,...)
std::string macaddr(const uint8_t *addr)
std::string comma_number_string(int64_t input)
void print_debug_message(const char *fmt, va_list ap)
void mkdirs_for_path(std::string path)
static char * debug_prefix
std::string ssprintf(const char *fmt,...)
void debug_real(const char *fmt,...)
void * mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
void munmap(void *buf, size_t size)
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)