/**
* 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 <cmath>
#include <sys/time.h>
#include "salticidae/util.h"
namespace salticidae {
const char *TTY_COLOR_RED = "\x1b[31m";
const char *TTY_COLOR_GREEN = "\x1b[32m";
const char *TTY_COLOR_YELLOW = "\x1b[33m";
const char *TTY_COLOR_BLUE = "\x1b[34m";
const char *TTY_COLOR_MAGENTA = "\x1b[35m";
const char *TTY_COLOR_CYAN = "\x1b[36m";
const char *TTY_COLOR_RESET = "\x1b[0m";
void sec2tv(double t, struct timeval &tv) {
tv.tv_sec = trunc(t);
tv.tv_usec = trunc((t - tv.tv_sec) * 1e6);
}
double gen_rand_timeout(double base_timeout, double alpha) {
return base_timeout + rand() / (double)RAND_MAX * alpha * base_timeout;
}
std::string vstringprintf(const char *fmt, va_list ap) {
int guessed_size = 1024;
std::string buff;
buff.resize(guessed_size);
int nwrote = vsnprintf(&buff[0], guessed_size, fmt, ap);
if (nwrote < 0) buff = "";
else
{
buff.resize(nwrote);
if (nwrote > guessed_size)
{
if (vsnprintf(&buff[0], nwrote, fmt, ap) != nwrote)
buff = "";
}
}
return std::move(buff);
}
std::string stringprintf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
auto ret = vstringprintf(fmt, ap);
va_end(ap);
return std::move(ret);
}
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::set_color() {
if (is_tty())
{
color_info = TTY_COLOR_GREEN;
color_debug = TTY_COLOR_BLUE;
color_warning = TTY_COLOR_YELLOW;
color_error = TTY_COLOR_RED;
}
else
{
color_info = color_debug = color_warning = color_error = nullptr;
}
}
void Logger::write(