Add mutltiple Log Targets #4
parent
626ae32e0d
commit
92ec8658fa
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* 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 2 of the License, or
|
||||
* (at your option) 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, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "LogConsoleTarget.h"
|
||||
|
||||
CLogConsoleTarget::CLogConsoleTarget(LOG_SEVERITY logLevel) :
|
||||
CLogTarget(logLevel)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CLogConsoleTarget::printLogInt(const std::string& msg)
|
||||
{
|
||||
std::cout << msg;
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* 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 2 of the License, or
|
||||
* (at your option) 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, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "LogTarget.h"
|
||||
|
||||
class CLogConsoleTarget : public CLogTarget
|
||||
{
|
||||
public:
|
||||
CLogConsoleTarget(LOG_SEVERITY logLevel);
|
||||
|
||||
protected:
|
||||
virtual void printLogInt(const std::string& msg);
|
||||
};
|
||||
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* 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 2 of the License, or
|
||||
* (at your option) 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, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
|
||||
#include "LogFileTarget.h"
|
||||
|
||||
CLogFileTarget::CLogFileTarget(LOG_SEVERITY logLevel, const std::string & dir, bool rotate) :
|
||||
CLogTarget(logLevel),
|
||||
m_dir(dir),
|
||||
m_rotate(rotate)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CLogFileTarget::printLogInt(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);
|
||||
char buf[64];
|
||||
std::strftime(buf, 42, "-%Y-%m-%d", now_tm);
|
||||
fileName.append(std::string(buf));
|
||||
}
|
||||
fileName.append(".log");
|
||||
|
||||
std::ofstream file;
|
||||
file.open(fileName, std::ios::app);
|
||||
if(file.is_open()) {
|
||||
file << msg;
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* 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 2 of the License, or
|
||||
* (at your option) 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, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "LogTarget.h"
|
||||
|
||||
class CLogFileTarget : public CLogTarget
|
||||
{
|
||||
public:
|
||||
CLogFileTarget(LOG_SEVERITY logLevel, const std::string& directory, bool rotate);
|
||||
|
||||
protected:
|
||||
virtual void printLogInt(const std::string& msg);
|
||||
|
||||
private:
|
||||
std::string m_dir;
|
||||
bool m_rotate;
|
||||
};
|
||||
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* 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 2 of the License, or
|
||||
* (at your option) 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, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include "LogTarget.h"
|
||||
|
||||
CLogTarget::CLogTarget(LOG_SEVERITY logLevel) :
|
||||
m_logLevel(logLevel)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CLogTarget::~CLogTarget()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CLogTarget::printLog(const std::string& msg)
|
||||
{
|
||||
printLogInt(msg);
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA
|
||||
*
|
||||
* 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 2 of the License, or
|
||||
* (at your option) 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, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
enum LOG_SEVERITY {
|
||||
LS_TRACE = 1,
|
||||
LS_DEBUG,
|
||||
LS_INFO,
|
||||
LS_WARNING,
|
||||
LS_ERROR,
|
||||
LS_FATAL
|
||||
};
|
||||
|
||||
class CLogTarget
|
||||
{
|
||||
public:
|
||||
CLogTarget(LOG_SEVERITY logLevel);
|
||||
virtual ~CLogTarget();
|
||||
void printLog(const std::string& msg);
|
||||
LOG_SEVERITY getLevel() { return m_logLevel; }
|
||||
|
||||
protected:
|
||||
virtual void printLogInt(const std::string& msg) = 0;
|
||||
|
||||
private:
|
||||
LOG_SEVERITY m_logLevel;
|
||||
};
|
||||
Loading…
Reference in new issue