Merge branch 'develop' into feature/NatTraversal

pull/32/head
Geoffrey Merck 4 years ago
commit acfcaaf7c9

35
Log.h

@ -19,11 +19,11 @@
#pragma once
#include <ctime>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <mutex>
#include <sstream>
#include <cassert>
#include "StringUtils.h"
@ -36,45 +36,46 @@ private:
static bool m_addedTargets;
static std::recursive_mutex m_targetsMutex;
static void getTimeStamp(std::string & s);
static void getTimeStamp(std::string& s);
template<typename... Args> static void formatLogMessage(std::string& output, LOG_SEVERITY severity, const std::string & f, Args... args)
template<typename... Args>
static void formatLogMessage(std::string& output, LOG_SEVERITY severity, const std::string & f, Args... args)
{
assert(severity != LOG_NONE);
std::string severityStr;
std::string severityStr(" ");
switch (severity)
{
case LOG_DEBUG:
severityStr = "DEBUG ";
severityStr.assign("DEBUG ");
break;
case LOG_ERROR:
severityStr = "ERROR ";
severityStr.assign("ERROR ");
break;
case LOG_FATAL:
severityStr = "FATAL ";
severityStr.assign("FATAL ");
break;
case LOG_INFO :
severityStr = "INFO ";
severityStr.assign("INFO ");
break;
case LOG_WARNING:
severityStr = "WARNING";
severityStr.assign("WARNING");
break;
case LOG_TRACE:
severityStr = "TRACE ";
severityStr.assign("TRACE ");
break;
default:
break;
}
std::string message = CStringUtils::string_format(f, args...);
boost::trim(message);
std::string timeUtc;
getTimeStamp(timeUtc);
std::stringstream s;
s << "[" << timeUtc << "] [" << severityStr << "] " << message << std::endl;
std::string timestamp;
getTimeStamp(timestamp);
output = s.str();
std::string f2("[%s] [%s] ");
f2.append(f);
CStringUtils::string_format_in_place(output, f2, timestamp.c_str(), severityStr.c_str(), args...);
boost::trim_if(output, [](char c){ return c == '\n' || c == '\r' || c == ' ' || c == '\t'; });
output.push_back('\n');
}
public:

@ -24,34 +24,79 @@
#include "LogFileTarget.h"
#define LOG_FILE_ROOT "dstargateway"
CLogFileTarget::CLogFileTarget(LOG_SEVERITY logLevel, const std::string & dir, bool rotate) :
CLogTarget(logLevel),
m_dir(dir),
m_rotate(rotate)
m_rotate(rotate),
m_file(),
m_day(0)
{
}
void CLogFileTarget::printLogInt(const std::string& msg)
CLogFileTarget::~CLogFileTarget()
{
if(m_file.is_open()) {
m_file.close();
}
}
void CLogFileTarget::printLogIntFixed(const std::string& msg)
{
if(m_file.is_open()) {
m_file << msg;
m_file.flush();
return;
}
std::string filename(m_dir);
if(filename[filename.length() - 1U] != '/') filename.push_back('/');
filename.append(LOG_FILE_ROOT).append(".log");
m_file.open(filename, std::ios::app);
if(m_file.is_open()) {
printLogIntFixed(msg);
}
else {
std::cerr << "FAILED TO OPEN LOG FILE :" << filename;
}
}
void CLogFileTarget::printLogIntRotate(const std::string& msg)
{
// construct filename
std::string fileName(m_dir);
if(fileName[fileName.length() - 1U] != '/') fileName.push_back('/');
fileName.append("dstargateway");
if(m_rotate) {
std::time_t now = std::time(0);
std::tm* now_tm = std::gmtime(&now);
std::time_t now = std::time(0);
std::tm* now_tm = std::gmtime(&now);
if(now_tm->tm_yday != m_day) {
m_day = now_tm->tm_yday;
if(m_file.is_open()) {
m_file.close();
}
}
if(!m_file.is_open()) {
std::string filename(m_dir);
if(filename[filename.length() - 1U] != '/') filename.push_back('/');
char buf[64];
std::strftime(buf, 42, "-%Y-%m-%d", now_tm);
fileName.append(std::string(buf));
filename.append(LOG_FILE_ROOT).append(buf).append(".log");
m_file.open(filename, std::ios::app);
if(!m_file.is_open()) {
std::cerr << "FAILED TO OPEN LOG FILE :" << filename;
}
}
fileName.append(".log");
std::ofstream file;
file.open(fileName, std::ios::app);
if(file.is_open()) {
file << msg;
file.close();
if(m_file.is_open()) {
m_file << msg;
m_file.flush();
return;
}
}
}
void CLogFileTarget::printLogInt(const std::string& msg)
{
if(m_rotate)
printLogIntRotate(msg);
else
printLogIntFixed(msg);
}

@ -19,6 +19,7 @@
#pragma once
#include <string>
#include <fstream>
#include "LogTarget.h"
@ -26,11 +27,17 @@ class CLogFileTarget : public CLogTarget
{
public:
CLogFileTarget(LOG_SEVERITY logLevel, const std::string& directory, bool rotate);
~CLogFileTarget();
protected:
virtual void printLogInt(const std::string& msg);
private:
void printLogIntRotate(const std::string& msg);
void printLogIntFixed(const std::string& msg);
std::string buildFileName();
std::string m_dir;
bool m_rotate;
std::fstream m_file;
int m_day;
};

@ -112,6 +112,7 @@ I Use [Git flow](https://danielkummer.github.io/git-flow-cheatsheet/) as my work
- Code formating rules are observed (these are very lousy though)
# 5. Version History
## 5.1. Version 0.5
- [Bugfix] Trying to connect to ghost ircDDB when no ircDDB is configured
## 5.2. Version 0.4
- [Improvement] Add APRS status link feature ([#8](https://github.com/F4FXL/DStarGateway/issues/8))
- [Bugfix] Posotions received over radio were not sent to APRS-IS when GPDS connection failed. ([#7](https://github.com/F4FXL/DStarGateway/issues/7))

@ -30,12 +30,23 @@ class CStringUtils {
public:
template<typename ... Args>
static std::string string_format( const std::string& format, Args ... args )
{
std::string ret;
string_format_in_place(ret, format, args...);
return ret;
}
template<typename ... Args>
static void string_format_in_place(std::string& output, const std::string& format, Args ... args )
{
int size_s = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
if( size_s <= 0 ){ throw std::runtime_error( "Error during formatting." ); }
auto size = static_cast<size_t>( size_s );
auto buf = std::make_unique<char[]>( size );
std::snprintf( buf.get(), size, format.c_str(), args ... );
return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
output.reserve(size);
output.assign(buf.get(), size - 1); // -1 because we do not need trailing '\0'
}
};

Loading…
Cancel
Save

Powered by TurnKey Linux.