changed to singelton

This commit is contained in:
Hendrik Schutter 2020-09-07 15:31:09 +02:00
parent 48c76b5efd
commit f14919ed5d
6 changed files with 82 additions and 33 deletions

View File

@ -19,20 +19,11 @@ int main(void)
{
cout << "Hello World!\n";
struct sID id;
id.description = "Software-Name - Copyright Company 2020"; //use your values here
id.deviceID = "Device-Name"; //use your values here
id.softwareID = "0.1.1.8"; //use your values here
id.hardwareID = "7.77.9"; //use your values here
Logger* logging = new Logger("./test.txt", id); //use your path here
logging->info("Alle Systeme laufen!"); //demo
Logger::logThis()->info("Alle Systeme laufen!"); //demo
usleep(1465);
logging->warning("Alle Systeme laufen!"); //demo
Logger::logThis()->warning("Alle Systeme laufen!"); //demo
usleep(51654);
logging->error("Alle Systeme laufen!"); //demo
Logger::logThis()->error("Alle Systeme laufen!"); //demo
cout << "bye!\n";

View File

@ -2,4 +2,4 @@
## build example
clear && g++ -Wall logger.cpp MainExample.cpp -o myTest && ./myTest
rm -f test.txt && clear && g++ -Wall logger.cpp MainExample.cpp -o myTest && ./myTest

View File

@ -9,7 +9,10 @@
using namespace std;
string version = "0.2"; //logger version
string version = "0.2.1"; //logger version
bool Logger::instanceFlag = false;
Logger* Logger::single = NULL;
/**
* \brief create new logger instance
@ -17,21 +20,20 @@ string version = "0.2"; //logger version
* \param struct with data
* \return instance of Logger
*/
Logger::Logger(string pLogPath,struct sID id)
Logger::Logger()
{
this->logPath =pLogPath;
this->logPath = LOG_PATH;
writeLog(menuLine('+', MENU_LINE_SIZE));
writeLog(padStringMenu('+', " ", MENU_LINE_SIZE));
writeLog(padStringMenu('+', ("Device: " + id.deviceID + " -- " + id.description), MENU_LINE_SIZE));
writeLog(padStringMenu('+', ("Device: " + string(DEVICE_ID) + " -- " + string(DESCRIPTION)), MENU_LINE_SIZE));
writeLog(padStringMenu('+', " ", MENU_LINE_SIZE));
writeLog(padStringMenu('+', ("Software ID: " + id.softwareID + " -- Build time: " + __DATE__ + " " + __TIME__), MENU_LINE_SIZE));
writeLog(padStringMenu('+', ("Software ID: " + string(SOFTWARE_VERSION) + " -- Build time: " + __DATE__ + " " + __TIME__), MENU_LINE_SIZE));
writeLog(padStringMenu('+', " ", MENU_LINE_SIZE));
writeLog(padStringMenu('+', ("Hardware ID: " + id.hardwareID + " -- MAC: " + getMacAddress()), MENU_LINE_SIZE));
writeLog(padStringMenu('+', ("Hardware ID: " + string(HARDWARE_VERSION) + " -- MAC: " + getMacAddress()), MENU_LINE_SIZE));
writeLog(padStringMenu('+', " ", MENU_LINE_SIZE));
writeLog(padStringMenu('+', ("cppSimpleLogger -- available from https://git.mosad.xyz/localhorst/cppSimpleLogger -- Version: " + version), MENU_LINE_SIZE));
@ -44,10 +46,12 @@ Logger::Logger(string pLogPath,struct sID id)
}
/**
* \brief deconctructor
* \brief deconstructor
* \return void
*/
Logger::~Logger()
{
instanceFlag = false;
}
/**
@ -208,7 +212,22 @@ string Logger::menuLine(char cBorder, uint8_t u8LineLenght)
return result;
}
/**
* \brief return a instance of the logger
* \return logger obj
*/
Logger* Logger::logThis()
{
if (!instanceFlag)
{
single = new Logger(); //create new obj
instanceFlag = true;
return single;
}
else
{
return single; //return existing obj
}
}

View File

@ -5,6 +5,9 @@
* @date 04.09.2020
*/
#ifndef LOGGER_H_
#define LOGGER_H_
#include <time.h>
#include <string.h>
#include <iostream>
@ -27,32 +30,51 @@ using namespace std;
#define MENU_LINE_SIZE 110 //Size of menu lines
struct sID
{
string description;
string deviceID;
string softwareID;
string hardwareID;
};
#ifndef LOG_PATH
#define LOG_PATH "./test.txt"
#endif
#ifndef DESCRIPTION
#define DESCRIPTION "Software-Name - Copyright Company 2020" //use your values here
#endif
#ifndef DEVICE_ID
#define DEVICE_ID "Device-Name" //use your values here
#endif
#ifndef SOFTWARE_VERSION
#define SOFTWARE_VERSION "0.1.1.8" //use your values here
#endif
#ifndef HARDWARE_VERSION
#define HARDWARE_VERSION "7.77.9" //use your values here
#endif
class Logger
{
private:
string logPath;
mutex mtxLog;
static bool instanceFlag;
static Logger *single;
string getTimestamp();
void writeLog(string s);
string getMacAddress();
string padStringMenu(char cBorder, string text, uint8_t u8LineLenght);
string menuLine(char cBorder, uint8_t u8LineLenght);
Logger();
~Logger();
public:
Logger(string pLogPath, struct sID id);
~Logger();
void info(string s);
void warning(string s);
void error(string s);
void newLine();
static Logger* logThis();
};
#endif // LOGGER_H_

BIN
myTest Executable file

Binary file not shown.

17
test.txt Normal file
View File

@ -0,0 +1,17 @@
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
+ Device: Device-Name -- Software-Name - Copyright Company 2020 +
+ +
+ Software ID: 0.1.1.8 -- Build time: Sep 7 2020 15:29:48 +
+ +
+ Hardware ID: 7.77.9 -- MAC: 9E:81:AE:7F:00:00 +
+ +
+ cppSimpleLogger -- available from https://git.mosad.xyz/localhorst/cppSimpleLogger -- Version: 0.2.1 +
+ +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
07/09/2020 15:29:49.239 [INFO] Created new log file
07/09/2020 15:29:49.239 [INFO] Alle Systeme laufen!
07/09/2020 15:29:49.240 [WARNING] Alle Systeme laufen!
07/09/2020 15:29:49.292 [ERROR] Alle Systeme laufen!