Skip to content
This repository has been archived by the owner on Jul 22, 2023. It is now read-only.

Commit

Permalink
Feature/i90 add boost logging (#147)
Browse files Browse the repository at this point in the history
* added log and log_setup to CMakeLists.txt
and basic logging example in CodeGenerator.cpp

* Example with line number and file name

* example with global logger

* test

* Reworked logger

* Added info logging

* Changed log location

* Added logging to StateMachine.cpp

* Added more logging

* Fixed doxygen folders

* Added logging to perror

* Added logging to perror

* Added Error logging in CodeGenerator.cpp and Option.cpp

* Added editors to logger

* added justify class source url

Co-authored-by: swolf2211 <[email protected]>
  • Loading branch information
TobiasGoetz and R4nq3r authored Jul 24, 2022
1 parent 1065308 commit ef212f6
Show file tree
Hide file tree
Showing 15 changed files with 503 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ add_executable(



find_package (Boost REQUIRED)
find_package (Boost COMPONENTS log log_setup REQUIRED)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(CodeGenerator ${Boost_LIBRARIES})
Expand Down
2 changes: 1 addition & 1 deletion config-file
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ WARN_LOGFILE =
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
# Note: If this tag is empty the current directory is searched.

INPUT = ./src
INPUT = ./src, ./include

# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
Expand Down
1 change: 1 addition & 0 deletions include/Justify.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using namespace boost;

/**
* @brief Class to justify a string
* Custom Justify class according to https://www.geeksforgeeks.org/justify-the-given-text-based-on-the-given-width-of-each-line/
*/
class Justify
{
Expand Down
66 changes: 66 additions & 0 deletions include/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Editors: Tobias Goetz, Sebastian Wolf
*/

#ifndef LOG_LOGGER_H
#define LOG_LOGGER_H
#pragma once

#include <boost/log/trivial.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/attributes/mutable_constant.hpp>
#include <boost/log/utility/manipulators/add_value.hpp>
#include <string>

BOOST_LOG_GLOBAL_LOGGER(sysLogger,
boost::log::sources::severity_channel_logger_mt<boost::log::trivial::severity_level>);
BOOST_LOG_GLOBAL_LOGGER(dataLogger,
boost::log::sources::severity_channel_logger_mt<boost::log::trivial::severity_level>);

/**
* @brief Custom logger class.
* Custom logger according to https://objectcomputing.com/resources/publications/sett/may-2016-boostlog-library
*/
class Logger {
public:
/// Init with default trivial logging
static void init();

/// @param configFileName config ini file that contains boost logging properties.
/// If configFileName.empty() then default initialization.
static void initFromConfig(const std::string& configFileName);

/// Disable logging
static void disable();

/// Add a file sink for LOG_DATA_* for >= INFO.
/// This file sink will be used along with any configured via Config in init().
static void addDataFileLog(const std::string& logFileName);
};

#define LOG_LOG_LOCATION(LOGGER, LEVEL, ARG) \
BOOST_LOG_SEV(LOGGER, boost::log::trivial::LEVEL) \
<< boost::log::add_value("Line", __LINE__) \
<< boost::log::add_value("File", __FILE__) \
<< boost::log::add_value("Function", __FUNCTION__) << ARG;

/// System Log macros.
/// TRACE < DEBUG < INFO < WARN < ERROR < FATAL
#define LOG_TRACE(ARG) LOG_LOG_LOCATION(sysLogger::get(), trace, ARG);
#define LOG_DEBUG(ARG) LOG_LOG_LOCATION(sysLogger::get(), debug, ARG);
#define LOG_INFO(ARG) LOG_LOG_LOCATION(sysLogger::get(), info, ARG);
#define LOG_WARN(ARG) LOG_LOG_LOCATION(sysLogger::get(), warning, ARG);
#define LOG_ERROR(ARG) LOG_LOG_LOCATION(sysLogger::get(), error, ARG);
#define LOG_FATAL(ARG) LOG_LOG_LOCATION(sysLogger::get(), fatal, ARG);

/// Data Log macros. Does not include LINE, FILE, FUNCTION.
/// TRACE < DEBUG < INFO < WARN < ERROR < FATAL
#define LOG_DATA_TRACE(ARG) BOOST_LOG_SEV(dataLogger::get(), boost::log::trivial::trace) << ARG
#define LOG_DATA_DEBUG(ARG) BOOST_LOG_SEV(dataLogger::get(), boost::log::trivial::debug) << ARG
#define LOG_DATA_INFO(ARG) BOOST_LOG_SEV(dataLogger::get(), boost::log::trivial::info) << ARG
#define LOG_DATA_WARN(ARG) BOOST_LOG_SEV(dataLogger::get(), boost::log::trivial::warning) << ARG
#define LOG_DATA_ERROR(ARG) BOOST_LOG_SEV(dataLogger::get(), boost::log::trivial::error) << ARG
#define LOG_DATA_FATAL(ARG) BOOST_LOG_SEV(dataLogger::get(), boost::log::trivial::fatal) << ARG

#endif /* LOG_LOGGER_H */
81 changes: 81 additions & 0 deletions logconfig.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#
# See http://www.boost.org/doc/libs/1_60_0/libs/log/doc/html/log/detailed/utilities.html#log.detailed.utilities.setup.filter_formatter
#
# Many of the property values have to be in quotes, best to just use quotes for all of them.
#
# SYSLF is the System Log File for logging standard 'debug' type info.
# DATALF is the Data log File for logging modification to business data.
#
[Core]
# Set DisableLogging to true to disable all logging.
DisableLogging="false"

# SYSLF - system log
[Sinks.SYSLF]
Destination="TextFile"
# If Asynchronous true then thread dedicated to writing to log, otherwise blocks main thread to write.
Asynchronous="true"
# If AutoFlush is true then non-buffered output
AutoFlush="true"
# Line Formats available: TimeStamp, Uptime, Severity, LineID (counter), ProcessID, ThreadID, Line, File, Function
# TimeStamp and Uptime support boost date time format:
# http://www.boost.org/doc/libs/1_60_0/doc/html/date_time/date_time_io.html#date_time.format_flags
Format="[%TimeStamp(format=\"%Y-%m-%d %H:%M:%S.%f\")%][%Uptime(format=\"%O:%M:%S.%f\")%][%Severity%] %File%(%Line%) %Function%: %Message%"
# Target directory in which rotated files will be stored.
Target="./logs"
# FileName pattern to use. %N is a counter for files.
FileName="app_syslog_%N.log"
# RotationSize in bytes, File size, in bytes, upon which file rotation will be performed.
# Time based rotation also available via RotationInterval and RotationTimePoint see boost log docs.
RotationSize="10485760"
# Matching used so that only files matching FileName pattern are deleted.
ScanForFiles="Matching"
# MaxSize - Total size of files in the target directory in bytes upon which the oldest file will be deleted.
#MaxSize=100485760
# MinFreeSpace - Minimum free space in the Target directory in bytes. Above this value oldest file is deleted.
#MinFreeSpace=1485760
# Specify level of log, options are: trace, debug, info, warning, error, fatal
# Since Channel not part of filter all log output will be included.
# If only SYSLF logging desired, change to: Filter="%Severity% >= trace & %Channel% matches \"SYSLF\""
Filter="%Severity% >= trace"

# DATALF - data log
[Sinks.DATALF]
Destination="TextFile"
# If Asynchronous true then thread dedicated to writing to log, otherwise blocks main thread to write.
Asynchronous="false"
# If AutoFlush is true then non-buffered output
AutoFlush="true"
# Line Formats available: TimeStamp, Uptime, Severity, LineID (counter), ProcessID, ThreadID
# TimeStamp and Uptime support boost date time format:
# http://www.boost.org/doc/libs/1_60_0/doc/html/date_time/date_time_io.html#date_time.format_flags
Format="[%TimeStamp(format=\"%Y-%m-%d %H:%M:%S.%f\")%][%Uptime(format=\"%O:%M:%S.%f\")%][%Severity%] %Message%"
# Target directory in which rotated files will be stored.
Target="./logs"
# FileName pattern to use. %N is a counter for files.
FileName="app_datalog_%N.log"
# RotationSize in bytes, File size, in bytes, upon which file rotation will be performed.
# Time based rotation also available via RotationInterval and RotationTimePoint see boost log docs.
RotationSize="10485760"
# Matching used so that only files matching FileName pattern are deleted.
ScanForFiles="Matching"
# MaxSize - Total size of files in the target directory in bytes upon which the oldest file will be deleted.
#MaxSize=100485760
# MinFreeSpace - Minimum free space in the Target directory in bytes. Above this value oldest file is deleted.
#MinFreeSpace=1485760
# Specify level of log, options are: trace, debug, info, warning, error, fatal
# Specify Channel otherwise all log output will be included.
Filter="%Severity% >= trace & %Channel% matches \"DATALF\""

# Console log, logs both DATALF and SYSLF
[Sinks.Console]
# Remove the following lines to disable console logging
Destination="Console"
# If AutoFlush is true then non-buffered output
AutoFlush="true"
# Line Formats available: TimeStamp, Uptime, Severity, LineID (counter), ProcessID, ThreadID
# TimeStamp and Uptime support boost date time format:
# http://www.boost.org/doc/libs/1_60_0/doc/html/date_time/date_time_io.html#date_time.format_flags
Format="[%TimeStamp(format=\"%Y-%m-%d %H:%M:%S.%f\")%][%Uptime(format=\"%O:%M:%S.%f\")%][%Severity%] - %Message%"
# Specify level of log, options are: trace, debug, info, warning, error, fatal
Filter="%Severity% >= info"
17 changes: 14 additions & 3 deletions src/CodeGenerator.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* Editors: Tobias Goetz, Noel Kempter
*/

#include "CodeGenerator.h"
#include "XMLParser.h"
#include "SourceCodeWriter.h"
#include "Logger.h"
#include <getopt.h>

std::string CodeGenerator::getFilePath() {
Expand All @@ -24,23 +24,31 @@ void CodeGenerator::setOutputDir(const std::string &dir) {
}

void CodeGenerator::run() {
printf("Starting Codegenerator!\n");
LOG_INFO("Starting CodeGenerator");
if(getFilePath().empty()){
perror("The path to the XML-File must be set.");
LOG_ERROR("The path to the XML-File must be set.");
exit(EXIT_FAILURE);
}

LOG_INFO("Starting XMLParser");
XMLParser parser(getFilePath());
parser.parse();
LOG_INFO("Finished XMLParser");

LOG_INFO("Starting SourceCodeWriter");
SourceCodeWriter writer = SourceCodeWriter(parser.getGetOptSetup());
writer.setOutputDir(getOutputDir());
writer.writeFile();
printf("Codegenerator finished!\n");
LOG_INFO("Finished SourceCodeWriter");

LOG_INFO("Codegenerator finished!");
}


int main(int argc, char **argv) {
Logger::initFromConfig("logconfig.ini");

CodeGenerator generator;
int c;
int option_index;
Expand All @@ -55,20 +63,23 @@ int main(int argc, char **argv) {
case 'p':
if (optarg == nullptr){
perror("The path to the XML-File to be parsed was not set.");
LOG_ERROR("The path to the XML-File to be parsed was not set.");
exit(EXIT_FAILURE);
}
generator.setFilePath(optarg);
break;
case 'o':
if(optarg == nullptr){
perror("When using \"-o/--output\" the path to the target directory must be set.");
LOG_ERROR("When using \"-o/--output\" the path to the target directory must be set.");
exit(EXIT_FAILURE);
}
generator.setOutputDir(optarg);
break;
case '?':
default:
perror("GetOpt encountered an unknown option.");
LOG_ERROR("GetOpt encountered an unknown option.");
exit(EXIT_FAILURE);
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/HelpText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

#include "HelpText.h"
#include "Logger.h"

HelpText::HelpText(GetOptSetup *getOptSetup)
{
Expand Down Expand Up @@ -132,25 +133,35 @@ void HelpText::parseAuthor()

string HelpText::parseHelpMessage()
{
LOG_TRACE("Starting to parse help message");
// add function beginning to string
printHelpText.append("void ");
printHelpText.append(getOptSetup->getClassName());
printHelpText.append("::printHelp(){puts(\"");

// parse the description and add text to string
LOG_TRACE("Starting to parse description");
parseDescription();
LOG_TRACE("Finished parsing description");

// parse the option and add text to string
LOG_TRACE("Starting to parse option");
parseOption();
LOG_TRACE("Finished parsing option");

// parse the usage and add text to string
LOG_TRACE("Starting to parse usage");
parseUsage();
LOG_TRACE("Finished parsing usage");

// parse the author and add text to string
LOG_TRACE("Starting to parse author");
parseAuthor();
LOG_TRACE("Finished parsing author");

// add function ending to string
printHelpText.append(R"(");})");
LOG_TRACE("Finished parsing help message");

return printHelpText;
}
9 changes: 8 additions & 1 deletion src/Justify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

#include "Justify.h"
#include "Logger.h"

string Justify::JoinALineWithSpace(
vector<string>& words,
Expand Down Expand Up @@ -211,19 +212,25 @@ string Justify::returnJustifiedText(vector<string>& result)

string Justify::justifyTheText(const string& str, int L, bool isOption, int optionShift)
{
LOG_TRACE("Starting Justify::justifyTheText");

vector<string> words;

// Inserting words from
// given string
LOG_TRACE("Inserting words from given string");
words = this->splitWords(str);
LOG_TRACE("Inserting words from given string done");

// Function call to
// justify the text
LOG_TRACE("Function call to justify the text");
vector<string> result
= this->JustifyText(words, L, isOption, optionShift);

LOG_TRACE("Function call to justify the text done");
// Return the justified
// text

LOG_TRACE("Returning the justified text");
return returnJustifiedText(result);
}
Loading

0 comments on commit ef212f6

Please sign in to comment.