Commit 5d456e76 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

drop legacy logger

parent fdf5e637
/** libcurl based advanced IO aka ADVIO.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#pragma once
#include <cstdio>
#include <curl/curl.h>
#include <villas/utils.hpp>
#include <villas/exceptions.hpp>
struct advio {
CURL *curl;
FILE *file;
long uploaded; /**< Upload progress. How much has already been uploaded to the remote file. */
long downloaded; /**< Download progress. How much has already been downloaded from the remote file. */
int completed:1; /**< Was the upload completd */
unsigned char hash[SHA_DIGEST_LENGTH];
char mode[3];
char *uri;
};
typedef struct advio AFILE;
/* The remaining functions from stdio are just replaced macros */
#define afeof(af) feof((af)->file)
#define afgets(ln, sz, af) fgets(ln, sz, (af)->file)
#define aftell(af) ftell((af)->file)
#define afileno(af) fileno((af)->file)
#define afread(ptr, sz, nitems, af) fread(ptr, sz, nitems, (af)->file)
#define afwrite(ptr, sz, nitems, af) fwrite(ptr, sz, nitems, (af)->file)
#define afputs(ptr, af) fputs(ptr, (af)->file)
#define afprintf(af, fmt, ...) fprintf((af)->file, fmt, __VA_ARGS__)
#define afscanf(af, fmt, ...) fprintf((af)->file, fmt, __VA_ARGS__)
#define agetline(linep, linecapp, af) getline(linep, linecapp, (af)->file)
/* Extensions */
#define auri(af) ((af)->uri)
#define ahash(af) ((af)->hash)
/** Check if a URI is pointing to a local file. */
int aislocal(const char *uri);
AFILE *afopen(const char *url, const char *mode);
int afclose(AFILE *file);
int afflush(AFILE *file);
int afseek(AFILE *file, long offset, int origin);
void arewind(AFILE *file);
/** Download contens from remote file
*
* @param resume Do a partial download and append to the local file
*/
int adownload(AFILE *af, int resume);
int aupload(AFILE *af, int resume);
......@@ -27,6 +27,8 @@
#include <jansson.h>
#include <villas/log.hpp>
#define HEIGHT (LOG_WIDTH - 55)
#define SEQ 17
......@@ -58,10 +60,10 @@ public:
double getStddev() const;
/** Print all statistical properties of distribution including a graphilcal plot of the histogram. */
void print(bool details) const;
void print(Logger logger, bool details) const;
/** Print ASCII style plot of histogram */
void plot() const;
void plot(Logger logger) const;
/** Dump histogram data in Matlab format.
*
......
......@@ -123,8 +123,6 @@ void vlist_extend(struct vlist *l, size_t len, void *val);
/** Remove all elements for which the callback returns a non-zero return code. */
void vlist_filter(struct vlist *l, dtor_cb_t cb);
#include <villas/log.h>
/** Lookup an element from the list based on an UUID */
template<typename T>
T * vlist_lookup_uuid(struct vlist *l, uuid_t uuid)
......@@ -158,4 +156,4 @@ ssize_t vlist_lookup_index(struct vlist *l, const std::string &name)
return f ? vlist_index(l, f) : -1;
}
int vlist_init_and_push(struct vlist *l, void *p);
\ No newline at end of file
int vlist_init_and_push(struct vlist *l, void *p);
/** Logging and debugging routines
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#pragma once
#include <cstdarg>
#include <jansson.h>
/* The log level which is passed as first argument to print() */
enum log_level {
LOG_LVL_DEBUG,
LOG_LVL_INFO,
LOG_LVL_WARN,
LOG_LVL_ERROR
};
/** Debug facilities.
*
* To be or-ed with the debug level
*/
enum log_facilities {
LOG_POOL = (1L << 8),
LOG_QUEUE = (1L << 9),
LOG_CONFIG = (1L << 10),
LOG_HOOK = (1L << 11),
LOG_PATH = (1L << 12),
LOG_NODE = (1L << 13),
LOG_MEM = (1L << 14),
LOG_WEB = (1L << 15),
LOG_API = (1L << 16),
LOG_LOG = (1L << 17),
LOG_VFIO = (1L << 18),
LOG_PCI = (1L << 19),
LOG_XIL = (1L << 20),
LOG_TC = (1L << 21),
LOG_IF = (1L << 22),
LOG_ADVIO = (1L << 23),
LOG_IO = (1L << 24),
/* Node-types */
LOG_SOCKET = (1L << 25),
LOG_FILE = (1L << 26),
LOG_FPGA = (1L << 27),
LOG_NGSI = (1L << 28),
LOG_WEBSOCKET = (1L << 29),
LOG_OPAL = (1L << 30),
LOG_COMEDI = (1L << 31),
LOG_IB = (1LL << 32),
/* Classes */
LOG_NODES = LOG_NODE | LOG_SOCKET | LOG_FILE | LOG_FPGA | LOG_NGSI | LOG_WEBSOCKET | LOG_OPAL | LOG_COMEDI | LOG_IB,
LOG_KERNEL = LOG_VFIO | LOG_PCI | LOG_TC | LOG_IF,
LOG_ALL = ~0xFF
};
int log_get_width();
/** Printf alike debug message with level. */
void debug(long long lvl, const char *fmt, ...)
__attribute__ ((format(printf, 2, 3)));
/** Printf alike info message. */
void info(const char *fmt, ...)
__attribute__ ((format(printf, 1, 2)));
/** Printf alike warning message. */
void warning(const char *fmt, ...)
__attribute__ ((format(printf, 1, 2)));
/** Print error and exit. */
void error(const char *fmt, ...)
__attribute__ ((format(printf, 1, 2)));
/** Print error and strerror(errno). */
void serror(const char *fmt, ...)
__attribute__ ((format(printf, 1, 2)));
/** Print configuration error and exit. */
void jerror(json_error_t *err, const char *fmt, ...)
__attribute__ ((format(printf, 2, 3)));
......@@ -65,7 +65,7 @@ public:
/**< Get the real usable log output width which fits into one line. */
int getWidth();
void parse(json_t *cfg);
void parse(json_t *json);
Logger get(const std::string &name);
......
......@@ -110,7 +110,7 @@ public:
int load(const std::string &path);
int unload();
virtual int parse(json_t *cfg);
virtual int parse(json_t *json);
};
class Plugin {
......
......@@ -30,6 +30,10 @@
#include <vector>
#include <string>
#include <villas/log.hpp>
namespace villas {
class Table;
class TableColumn {
......@@ -78,10 +82,13 @@ protected:
std::vector<TableColumn> columns;
Logger logger;
public:
Table(const std::vector<TableColumn> &cols) :
Table(Logger log, const std::vector<TableColumn> &cols) :
width(-1),
columns(cols)
columns(cols),
logger(log)
{ }
/** Print a table header consisting of \p n columns. */
......@@ -96,4 +103,6 @@ public:
}
};
} /* namespace villas */
/** @} */
......@@ -40,7 +40,6 @@
#include <uuid/uuid.h>
#include <villas/config.h>
#include <villas/log.h>
#ifdef __GNUC__
#define LIKELY(x) __builtin_expect((x),1)
......
......@@ -21,30 +21,28 @@
##############################################################################
add_library(villas-common SHARED
advio.cpp
base64.cpp
buffer.cpp
common.cpp
compat.cpp
hist.cpp
cpuset.cpp
dsp/pid.cpp
hist.cpp
kernel/kernel.cpp
kernel/rt.cpp
list.cpp
log.cpp
log_legacy.cpp
memory.cpp
memory_manager.cpp
memory.cpp
plugin.cpp
popen.cpp
table.cpp
task.cpp
terminal.cpp
timing.cpp
tool.cpp
utils.cpp
cpuset.cpp
terminal.cpp
version.cpp
common.cpp
tool.cpp
base64.cpp
popen.cpp
)
if(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
......
/** libcurl based advanced IO aka ADVIO.
*
* This example requires libcurl 7.9.7 or later.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#if __GNUC__ <= 7 && !defined(__clang__)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cerrno>
#include <unistd.h>
#include <curl/curl.h>
#include <villas/utils.hpp>
#include <villas/config.h>
#include <villas/advio.hpp>
using namespace villas;
using namespace villas::utils;
#define BAR_WIDTH 60 /**< How wide you want the progress meter to be. */
static int advio_trace(CURL * /* handle */, curl_infotype type, char *data, size_t size, void * /* userp */)
{
const char *text;
switch (type) {
case CURLINFO_TEXT:
text = "info";
break;
case CURLINFO_HEADER_OUT:
text = "send header";
break;
case CURLINFO_DATA_OUT:
text = "send data";
break;
case CURLINFO_HEADER_IN:
text = "recv header";
break;
case CURLINFO_DATA_IN:
text = "recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "recv SSL data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "send SSL data";
break;
default: /* in case a new one is introduced to shock us */
return 0;
}
debug(LOG_ADVIO | 5, "CURL: %s: %.*s", text, (int) size-1, data);
return 0;
}
static char * advio_human_time(double t, char *buf, size_t len)
{
unsigned i = 0;
const char *units[] = { "secs", "mins", "hrs", "days", "weeks", "months", "years" };
int divs[] = { 60, 60, 24, 7, 4, 12 };
while (i < ARRAY_LEN(divs) && t > divs[i]) {
t /= divs[i];
i++;
}
snprintf(buf, len, "%.2f %s", t, units[i]);
return buf;
}
static char * advio_human_size(double s, char *buf, size_t len)
{
unsigned i = 0;
const char *units[] = { "B", "kiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" };
while (s > 1024 && i < ARRAY_LEN(units)) {
s /= 1024;
i++;
}
snprintf(buf, len, "%.*f %s", i ? 2 : 0, s, units[i]);
return buf;
}
#if LIBCURL_VERSION_NUM >= 0x072000
static int advio_xferinfo(void *p, curl_off_t dl_total_bytes, curl_off_t dl_bytes, curl_off_t ul_total_bytes, curl_off_t ul_bytes)
{
struct advio *af = (struct advio *) p;
double cur_time, eta_time, estimated_time, frac;
curl_easy_getinfo(af->curl, CURLINFO_TOTAL_TIME, &cur_time);
/* Is this transaction an upload? */
int upload = ul_total_bytes > 0;
curl_off_t total_bytes = upload ? ul_total_bytes : dl_total_bytes;
curl_off_t bytes = upload ? ul_bytes : dl_bytes;
/* Are we finished? */
if (bytes == 0)
af->completed = 0;
if (af->completed)
return 0;
/* Ensure that the file to be downloaded is not empty
* because that would cause a division by zero error later on */
if (total_bytes <= 0)
return 0;
frac = (double) bytes / total_bytes;
estimated_time = cur_time * (1.0 / frac);
eta_time = estimated_time - cur_time;
/* Print file sizes in human readable format */
char buf[4][32];
char *bytes_human = advio_human_size(bytes, buf[0], sizeof(buf[0]));
char *total_bytes_human = advio_human_size(total_bytes, buf[1], sizeof(buf[1]));
char *eta_time_human = advio_human_time(eta_time, buf[2], sizeof(buf[2]));
/* Part of the progressmeter that's already "full" */
int dotz = round(frac * BAR_WIDTH);
/* Progress bar */
fprintf(stderr, "\r[");
for (int i = 0 ; i < BAR_WIDTH; i++) {
if (upload)
fputc(BAR_WIDTH - i > dotz ? ' ' : '<', stderr);
else
fputc(i > dotz ? ' ' : '>', stderr);
}
fprintf(stderr, "] ");
/* Details */
fprintf(stderr, "eta %-12s %12s of %-12s", eta_time_human, bytes_human, total_bytes_human);
fflush(stderr);
if (bytes == total_bytes) {
af->completed = 1;
fprintf(stderr, "\33[2K\r");
}
return 0;
}
#endif /* LIBCURL_VERSION_NUM >= 0x072000 */
int aislocal(const char *uri)
{
const char *sep;
const char *supported_schemas[] = { "file", "http", "https", "tftp", "ftp", "scp", "sftp", "smb", "smbs" };
sep = strstr(uri, "://");
if (!sep)
return 1; /* no schema, we assume its a local file */
for (unsigned i = 0; i < ARRAY_LEN(supported_schemas); i++) {
if (!strncmp(supported_schemas[i], uri, sep - uri))
return 0;
}
return -1; /* none of the supported schemas match. this is an invalid uri */
}
AFILE * afopen(const char *uri, const char *mode)
{
int ret;
const char *sep;
AFILE *af = new AFILE;
if (!af)
throw MemoryAllocationError();
memset(af, 0, sizeof(AFILE));
snprintf(af->mode, sizeof(af->mode), "%s", mode);
sep = strstr(uri, "://");
if (sep) {
af->uri = strdup(uri);
if (!af->uri)
goto out2;
}
else { /* Open local file by prepending file:// schema. */
if (strlen(uri) <= 1)
return nullptr;
/* Handle relative paths */
if (uri[0] != '/')
af->uri = strf("file://%s/%s", fs::current_path().c_str(), uri);
else
af->uri = strf("file://%s", uri);
}
af->file = tmpfile();
if (!af->file)
goto out2;
af->curl = curl_easy_init();
if (!af->curl)
goto out1;
/* Setup libcurl handle */
curl_easy_setopt(af->curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(af->curl, CURLOPT_UPLOAD, 0L);
curl_easy_setopt(af->curl, CURLOPT_USERAGENT, HTTP_USER_AGENT);
curl_easy_setopt(af->curl, CURLOPT_URL, af->uri);
curl_easy_setopt(af->curl, CURLOPT_WRITEDATA, af->file);
curl_easy_setopt(af->curl, CURLOPT_READDATA, af->file);
curl_easy_setopt(af->curl, CURLOPT_DEBUGFUNCTION, advio_trace);
curl_easy_setopt(af->curl, CURLOPT_VERBOSE, 1);
/* CURLOPT_XFERINFOFUNCTION is only supported on libcurl >= 7.32.0 */
#if LIBCURL_VERSION_NUM >= 0x072000
curl_easy_setopt(af->curl, CURLOPT_XFERINFOFUNCTION, advio_xferinfo);
curl_easy_setopt(af->curl, CURLOPT_XFERINFODATA, af);
#endif /* LIBCURL_VERSION_NUM >= 0x072000 */
ret = adownload(af, 0);
if (ret)
goto out0;
af->uploaded = 0;
af->downloaded = 0;
return af;
out0: curl_easy_cleanup(af->curl);
out1: fclose(af->file);
out2: free(af->uri);
delete af;
return nullptr;
}
int afclose(AFILE *af)
{
int ret;
ret = afflush(af);
if (ret)
return ret;
curl_easy_cleanup(af->curl);
ret = fclose(af->file);
if (ret)
return ret;
free(af->uri);
delete af;
return 0;
}
int afseek(AFILE *af, long offset, int origin)
{
long new_seek, cur_seek = aftell(af);
switch (origin) {