Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Develop v1.3

See merge request Asteomount/Logger!3
  • Loading branch information
Gashmob committed Sep 30, 2021
2 parents 276d4e9 + 7f467e4 commit 1357941
Show file tree
Hide file tree
Showing 7 changed files with 784 additions and 166 deletions.
108 changes: 91 additions & 17 deletions C++/logger/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

std::ofstream Logger::file("");

int Logger::nbWrite = 0;
std::vector<std::ostream *> Logger::additionalStreams = std::vector<std::ostream *>();

int Logger::nbLog = 0;

pthread_mutex_t Logger::mutex;

LoggerOption Logger::verbose = FILE_AND_CONSOLE;

bool Logger::showTrace = true;

std::vector<LoggerType> Logger::showTypes = {INFO, SUCCESS, ERROR, WARNING, DEBUG};


Expand Down Expand Up @@ -93,10 +93,9 @@ std::string Logger::getTypeName(LoggerType type) {
}
}

void Logger::init(LoggerOption verboseP, bool showTraceP, const std::vector<LoggerType> &showTypesP) {
void Logger::init(LoggerOption verboseP, const std::vector<LoggerType> &showTypesP) {
if (!file.is_open()) {
verbose = verboseP;
showTrace = showTraceP;
showTypes = showTypesP;

bool dirCreated = false;
Expand All @@ -107,7 +106,7 @@ void Logger::init(LoggerOption verboseP, bool showTraceP, const std::vector<Logg
std::string fileName = (LOG_PATH "/" PROJECT_NAME "_log_") + getDate() + ".log";
file.open(fileName, std::ios::out);

nbWrite = 0;
nbLog = 0;

if (pthread_mutex_init(&mutex, nullptr) != 0)
ERROR_LOG(CONSOLE_ONLY, "Error mutex : %d\n", errno);
Expand All @@ -129,35 +128,110 @@ void Logger::exit() {
ERROR_LOG(CONSOLE_ONLY, "Please init before exit\n");
}

void Logger::addOutputStream(std::ostream *os) {
additionalStreams.push_back(os);
}

void Logger::genericLog(const std::string &function, const std::string &message, LoggerType type, LoggerOption option) {
std::string t = message;

if (showTrace) {
t = "[" + function + "]\t" + message;
}

if (t[t.length() - 1] != '\n') {
t += "\n";
}

if (option != FILE_ONLY && verbose != FILE_ONLY &&
std::find(showTypes.begin(), showTypes.end(), type) != showTypes.end())
std::cout << getTypeColor(type) << t << getColor(DEFAULT);
std::cout << getTypeColor(type) << constructMessage(t, function, getTypeName(type), CONSOLE_FORMAT)
<< getColor(DEFAULT);

if (option != CONSOLE_ONLY && verbose != CONSOLE_ONLY) {
pthread_mutex_lock(&mutex);
writeToFile(t, type);
writeToFile(constructMessage(t, function, getTypeName(type), FILE_FORMAT));
pthread_mutex_unlock(&mutex);
}

if (option != CONSOLE_ONLY && option != FILE_ONLY && verbose == FILE_AND_CONSOLE) {
for (const auto &os: additionalStreams) {
std::string m = constructMessage(t, function, getTypeName(type), ADDITIONAL_FORMAT);
os->write(m.c_str(), (long) m.length());
os->flush();
}
}

nbLog++;
}

std::string Logger::constructMessage(const std::string &message, const std::string &trace, const std::string &logType,
const std::string &format) {
std::string res;

int i = 0;
while (i < format.length()) {
char c = format[i];
if (c == '%') {
i++;
c = format[i];

struct timespec now{};
clock_gettime(CLOCK_REALTIME, &now);
struct tm *t = localtime(&now.tv_sec);
switch (c) {
case 'Y':
res += std::to_string(t->tm_year + 1900);
break;
case 'M':
res += std::to_string(t->tm_mon + 1);
break;
case 'D':
res += std::to_string(t->tm_mday);
break;
case 'H':
res += std::to_string(t->tm_hour);
break;
case 'm':
res += std::to_string(t->tm_min);
break;
case 'S':
res += std::to_string(t->tm_sec);
break;
case 'N':
res += std::to_string(now.tv_nsec);
break;
case 'd':
res += getDate();
break;
case 'h':
res += getHour();
break;
case 'T':
res += trace;
break;
case 'C':
res += message;
break;
case 'n':
res += std::to_string(nbLog);
break;
case 't':
res += logType;
break;
default:
break;
}
} else {
res += c;
}

i++;
}

return res;
}

void Logger::writeToFile(const std::string &message, LoggerType type) {
void Logger::writeToFile(const std::string &message) {
if (file.is_open()) {
std::string toPrint =
"[" + std::to_string(nbWrite) + "-" + getHour() + "-" + getTypeName(type) + "]\t" + message;
file << toPrint;
file << message;
file.flush();
nbWrite++;
} else
ERROR_LOG(CONSOLE_ONLY, "Please init logger\n");
}
Expand Down
95 changes: 81 additions & 14 deletions C++/logger/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
#include <vector>
#include <algorithm>

/**
/*
* Logger
* <p>
*
* Use Logger::init() to start the logger and Logger::exit() to close the logger
* <p>
*
* Simple usage :
* DEBUG_LOG(FILE_AND_CONSOLE, "Is it simple ? YES");
* Write 'Is it simple ? YES' (without the quote) in the console and the file
* <p>
*
* Complex usage :
* INFO_LOG(CONSOLE_ONLY, "Not too complex ? ", "Maybe");
* Write 'Not toot complex ? Maybe' (without the quote) only in the console
Expand All @@ -41,6 +41,40 @@
*/
#define PROJECT_NAME "project"

/*
* Different rules for the formats :
* %Y -> Year
* %M -> Month
* %D -> Day
* %H -> Hour
* %m -> Minute
* %S -> Second
* %N -> Nano second
* %d -> Date (%Y-%M-%D@%H-%m-%S)
* %h -> Hour (%H:%m:%S:%N)
* %T -> Trace
* %C -> Content message
* %n -> Log number
* %t -> Log type
*/

/**
* Format for console log
*/
#define CONSOLE_FORMAT "[%T]\t%C"

/**
* Format for log file
*/
#define FILE_FORMAT "[%n-%h-%t]\t[%T]\t%C"

/**
* Format for additional output stream
*/
#define ADDITIONAL_FORMAT "[%n-%t]\t[%T]\t%C"

// _.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.

/**
* Log colors
* Use DEFAULT for reset color
Expand Down Expand Up @@ -87,6 +121,8 @@ typedef enum LoggerOption {
FILE_AND_CONSOLE
} LoggerOption;

// _.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-.

class Logger {
private:
/**
Expand Down Expand Up @@ -115,14 +151,18 @@ class Logger {
* Initialisation
*/
static void init(LoggerOption verboseP = FILE_AND_CONSOLE,
bool showTraceP = true,
const std::vector<LoggerType> &showTypesP = {INFO, SUCCESS, ERROR, WARNING, DEBUG});

/**
* Quit the log and close the writer
*/
static void exit();

/**
* Add a new output for the logs
*/
static void addOutputStream(std::ostream *os);

public:
/**
* Info
Expand Down Expand Up @@ -192,14 +232,41 @@ class Logger {
* @param type LoggerType
* @param option LoggerOption
*/
static void genericLog(const std::string &function, const std::string &message, LoggerType type, LoggerOption option);
static void
genericLog(const std::string &function, const std::string &message, LoggerType type, LoggerOption option);

/**
* Construct the message from the format
* Different rules for the formats :
* %Y -> Year
* %M -> Month
* %D -> Day
* %H -> Hour
* %m -> Minute
* %S -> Second
* %N -> Nano second
* %d -> Date (%Y-%M-%D@%H-%m-%S)
* %h -> Hour (%H:%m:%S:%N)
* %T -> Trace
* %C -> Content message
* %n -> Log number
* %t -> Log type
*
* @param message
* @param trace
* @param logType
* @param format
* @return
*/
static std::string
constructMessage(const std::string &message, const std::string &trace, const std::string &logType,
const std::string &format);

/**
* Write the log into the file
* @param message std::string
* @param type LoggerType
*/
static void writeToFile(const std::string &message, LoggerType type);
static void writeToFile(const std::string &message);

/**
* The log's hour
Expand All @@ -211,7 +278,7 @@ class Logger {
/**
* The log's date
* yyyy-mm-dd@hh-mm-ss
* @return
* @return std::string
*/
static std::string getDate();

Expand All @@ -220,10 +287,14 @@ class Logger {
* The log file
*/
static std::ofstream file;
/**
* Additional output for the logs
*/
static std::vector<std::ostream *> additionalStreams;
/**
* The number of log
*/
static int nbWrite;
static int nbLog;
/**
* A mutex for writeToFile(), to be thread-safe
*/
Expand All @@ -232,10 +303,6 @@ class Logger {
* The type of verbose
*/
static LoggerOption verbose;
/**
* Show trace or not
*/
static bool showTrace;
/**
* The types of logs that be shown
*/
Expand Down
Loading

0 comments on commit 1357941

Please sign in to comment.