/**
* Copyright (c) 2018 Cornell University.
*
* Author: Ted Yin <tederminant@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <cstdarg>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <ctime>
#include <sys/time.h>
#include <cmath>
#include <event2/event.h>
#include "salticidae/util.h"
namespace salticidae {
void sec2tv(double t, struct timeval &tv) {
tv.tv_sec = trunc(t);
tv.tv_usec = trunc((t - tv.tv_sec) * 1e6);
}
void event_add_with_timeout(struct event *ev, double timeout) {
struct timeval tv;
tv.tv_sec = trunc(timeout);
tv.tv_usec = trunc((timeout - tv.tv_sec) * 1e6);
event_add(ev, &tv);
}
double gen_rand_timeout(double base_timeout, double alpha) {
return base_timeout + rand() / (double)RAND_MAX * alpha * base_timeout;
}
const std::string get_current_datetime() {
/* credit: http://stackoverflow.com/a/41381479/544806 */
char fmt[64], buf[64];
struct timeval tv;
gettimeofday(&tv, nullptr);
struct tm *tmp = localtime(&tv.tv_sec);
strftime(fmt, sizeof fmt, "%Y-%m-%d %H:%M:%S.%%06u", tmp);
snprintf(buf, sizeof buf, fmt, tv.tv_usec);
return std::string(buf);
}
SalticidaeError::SalticidaeError() : msg("unknown") {}
void Logger::write(const char *tag, const char *fmt, va_list ap) {
fprintf(output, "%s [%s %s] ", get_current_datetime().c_str(), prefix, tag);
vfprintf(output, fmt, ap);
fprintf(output, "\n");
}
void Logger::debug(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
write("debug", fmt, ap);
va_end(ap);
}
void Logger::info(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
write("info", fmt, ap);
va_end(ap);
}
void Logger::warning(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
write("warn", fmt, ap);
va_end(ap);
}
void Logger::error(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
write("error", fmt, ap);
va_end(ap);
}
Logger logger("salticidae");
void ElapsedTime::start() {
struct timezone tz;
gettimeofday(&t0, &tz);
cpu_t0 = clock();
}
void ElapsedTime::stop(bool show_info) {
struct timeval t1;
struct timezone tz;
gettimeofday(&t1, &tz);
cpu_elapsed_sec = (float)clock() / CLOCKS_PER_SEC -
(float)cpu_t0 / CLOCKS_PER_SEC;
elapsed_sec = (t1.tv_sec + t1.tv_usec * 1e-6) -
(t0.tv_sec + t0.tv_usec * 1e-6);
if (show_info)
SALTICIDAE_LOG_INFO("elapsed: %.3f (wall) %.3f (cpu)",
elapsed_sec, cpu_elapsed_sec);
}
Config::Opt::Opt(const std::string &optname, const std::string &optdoc,
const optval_t &optval, Action action,
char short_opt,
int idx):
optname(optname), optdoc(optdoc),
optval(optval), action(action),
short_opt(short_opt) {
opt.name = this->optname.c_str();
opt.has_arg = action == SWITCH_ON ? no_argument : required_argument;
opt.flag = nullptr;
opt.val = 0x100 + idx;
}
void Config::add_opt(const std::string &optname, const optval_t &optval, Action action,
char short_opt,
const std::string &optdoc) {
if (conf.count(optname))
throw SalticidaeError("option name already exists");
opts.push_back(new Opt(optname, optdoc,
optval, action, short_opt,