aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2018-04-22 21:53:59 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2018-04-22 21:53:59 +0200
commitdb2b590f1a8cdc5081706bdebea8ff6295e1e5ec (patch)
tree2a9c64475dd6d2e3f1cb4d748b715e5f2aa65ce2
parent2e23c3c051d0727596858d34b7e2e3eb31e4e534 (diff)
downloaddabmux-db2b590f1a8cdc5081706bdebea8ff6295e1e5ec.tar.gz
dabmux-db2b590f1a8cdc5081706bdebea8ff6295e1e5ec.tar.bz2
dabmux-db2b590f1a8cdc5081706bdebea8ff6295e1e5ec.zip
Take Log improvements from ODR-DabMod
-rw-r--r--src/Log.cpp56
-rw-r--r--src/Log.h82
-rw-r--r--src/dabOutput/dabOutputRaw.cpp7
-rw-r--r--src/zmq2edi/EDISender.cpp7
4 files changed, 87 insertions, 65 deletions
diff --git a/src/Log.cpp b/src/Log.cpp
index 9a58805..6b78fe0 100644
--- a/src/Log.cpp
+++ b/src/Log.cpp
@@ -3,7 +3,7 @@
Her Majesty the Queen in Right of Canada (Communications Research
Center Canada)
- Copyright (C) 2017
+ Copyright (C) 2018
Matthias P. Braendli, matthias.braendli@mpb.li
http://www.opendigitalradio.org
@@ -25,8 +25,16 @@
along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <list>
+#include <cstdarg>
+#include <chrono>
+
#include "Log.h"
+using namespace std;
+
+/* etiLog is a singleton used in all parts of ODR-DabMod to output log messages.
+ */
Logger etiLog;
void Logger::register_backend(std::shared_ptr<LogBackend> backend)
@@ -59,10 +67,10 @@ void Logger::log(log_level_t level, const char* fmt, ...)
size *= 2;
}
- logstr(level, str);
+ logstr(level, move(str));
}
-void Logger::logstr(log_level_t level, std::string message)
+void Logger::logstr(log_level_t level, std::string&& message)
{
if (level == discard) {
return;
@@ -91,3 +99,45 @@ LogLine Logger::level(log_level_t level)
return LogLine(this, level);
}
+LogToFile::LogToFile(const std::string& filename) : name("FILE")
+{
+ FILE* fd = fopen(filename.c_str(), "a");
+ if (fd == nullptr) {
+ fprintf(stderr, "Cannot open log file !");
+ throw std::runtime_error("Cannot open log file !");
+ }
+
+ log_file.reset(fd);
+}
+
+void LogToFile::log(log_level_t level, const std::string& message)
+{
+ if (level != log_level_t::discard) {
+ const char* log_level_text[] = {
+ "DEBUG", "INFO", "WARN", "ERROR", "ALERT", "EMERG"};
+
+ // fprintf is thread-safe
+ fprintf(log_file.get(), SYSLOG_IDENT ": %s: %s\n",
+ log_level_text[(size_t)level], message.c_str());
+ fflush(log_file.get());
+ }
+}
+
+void LogToSyslog::log(log_level_t level, const std::string& message)
+{
+ if (level != log_level_t::discard) {
+ int syslog_level = LOG_EMERG;
+ switch (level) {
+ case debug: syslog_level = LOG_DEBUG; break;
+ case info: syslog_level = LOG_INFO; break;
+ /* we don't have the notice level */
+ case warn: syslog_level = LOG_WARNING; break;
+ case error: syslog_level = LOG_ERR; break;
+ default: syslog_level = LOG_CRIT; break;
+ case alert: syslog_level = LOG_ALERT; break;
+ case emerg: syslog_level = LOG_EMERG; break;
+ }
+
+ syslog(syslog_level, SYSLOG_IDENT " %s", message.c_str());
+ }
+}
diff --git a/src/Log.h b/src/Log.h
index 91c9dc3..18f8c99 100644
--- a/src/Log.h
+++ b/src/Log.h
@@ -3,7 +3,7 @@
Her Majesty the Queen in Right of Canada (Communications Research
Center Canada)
- Copyright (C) 2017
+ Copyright (C) 2018
Matthias P. Braendli, matthias.braendli@mpb.li
http://www.opendigitalradio.org
@@ -31,16 +31,15 @@
# include "config.h"
#endif
-#include <stdarg.h>
-#include <stdio.h>
#include <syslog.h>
+#include <cstdarg>
+#include <cstdio>
#include <fstream>
#include <sstream>
#include <iostream>
#include <list>
#include <stdexcept>
#include <string>
-#include <map>
#include <mutex>
#include <memory>
@@ -55,79 +54,47 @@ static const std::string levels_as_str[] =
/** Abstract class all backends must inherit from */
class LogBackend {
public:
- virtual void log(log_level_t level, std::string message) = 0;
- virtual std::string get_name() = 0;
+ virtual ~LogBackend() {};
+ virtual void log(log_level_t level, const std::string& message) = 0;
+ virtual std::string get_name() const = 0;
};
/** A Logging backend for Syslog */
class LogToSyslog : public LogBackend {
public:
- LogToSyslog() {
- name = "SYSLOG";
+ LogToSyslog() : name("SYSLOG") {
openlog(SYSLOG_IDENT, LOG_PID, SYSLOG_FACILITY);
}
- ~LogToSyslog() {
+ virtual ~LogToSyslog() {
closelog();
}
- void log(log_level_t level, std::string message) {
-
- int syslog_level = LOG_EMERG;
- switch (level) {
- case debug: syslog_level = LOG_DEBUG; break;
- case info: syslog_level = LOG_INFO; break;
- /* we don't have the notice level */
- case warn: syslog_level = LOG_WARNING; break;
- case error: syslog_level = LOG_ERR; break;
- default: syslog_level = LOG_CRIT; break;
- case alert: syslog_level = LOG_ALERT; break;
- case emerg: syslog_level = LOG_EMERG; break;
- }
-
- syslog(syslog_level, SYSLOG_IDENT " %s", message.c_str());
- }
+ void log(log_level_t level, const std::string& message);
- std::string get_name() { return name; }
+ std::string get_name() const { return name; }
private:
- std::string name;
+ const std::string name;
+
+ LogToSyslog(const LogToSyslog& other) = delete;
+ const LogToSyslog& operator=(const LogToSyslog& other) = delete;
};
class LogToFile : public LogBackend {
public:
- LogToFile(std::string filename) {
- name = "FILE";
+ LogToFile(const std::string& filename);
+ void log(log_level_t level, const std::string& message);
+ std::string get_name() const { return name; }
- log_file = fopen(filename.c_str(), "a");
- if (log_file == NULL) {
- fprintf(stderr, "Cannot open log file !");
- throw std::runtime_error("Cannot open log file !");
- }
- }
-
- ~LogToFile() {
- if (log_file != NULL) {
- fclose(log_file);
- }
- }
-
- void log(log_level_t level, std::string message) {
-
- const char* log_level_text[] =
- {"DEBUG", "INFO", "WARN", "ERROR", "ALERT", "EMERG"};
-
- // fprintf is thread-safe
- fprintf(log_file, SYSLOG_IDENT ": %s: %s\n",
- log_level_text[(size_t)level], message.c_str());
- fflush(log_file);
- }
+ private:
+ const std::string name;
- std::string get_name() { return name; }
+ struct FILEDeleter{ void operator()(FILE* fd){ if(fd) fclose(fd);}};
+ std::unique_ptr<FILE, FILEDeleter> log_file;
- private:
- std::string name;
- FILE* log_file;
+ LogToFile(const LogToFile& other) = delete;
+ const LogToFile& operator=(const LogToFile& other) = delete;
};
class LogLine;
@@ -139,7 +106,7 @@ class Logger {
/* Log the message to all backends */
void log(log_level_t level, const char* fmt, ...);
- void logstr(log_level_t level, std::string message);
+ void logstr(log_level_t level, std::string&& message);
/* Return a LogLine for the given level
* so that you can write etiLog.level(info) << "stuff = " << 21 */
@@ -158,6 +125,7 @@ extern Logger etiLog;
class LogLine {
public:
LogLine(const LogLine& logline);
+ const LogLine& operator=(const LogLine& other) = delete;
LogLine(Logger* logger, log_level_t level) :
logger_(logger)
{
diff --git a/src/dabOutput/dabOutputRaw.cpp b/src/dabOutput/dabOutputRaw.cpp
index 201a7a6..42248c5 100644
--- a/src/dabOutput/dabOutputRaw.cpp
+++ b/src/dabOutput/dabOutputRaw.cpp
@@ -31,8 +31,6 @@
#if defined(HAVE_OUTPUT_RAW)
-#include <cstdio>
-#include <cstring>
#include "dabOutput.h"
#ifdef _WIN32
# include <fscfg.h>
@@ -49,6 +47,11 @@
# include "farsync.h"
#endif
+#include <cstdio>
+#include <cstring>
+#include <map>
+
+
const unsigned char revTable[] = {
0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
diff --git a/src/zmq2edi/EDISender.cpp b/src/zmq2edi/EDISender.cpp
index 11e3a6b..2921b3c 100644
--- a/src/zmq2edi/EDISender.cpp
+++ b/src/zmq2edi/EDISender.cpp
@@ -25,11 +25,12 @@
along with ODR-DabMux. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <numeric>
-#include <algorithm>
-#include <cmath>
#include "EDISender.h"
#include "Log.h"
+#include <cmath>
+#include <numeric>
+#include <map>
+#include <algorithm>
using namespace std;