LogHelper.cpp
Go to the documentation of this file.
1 
18 #include "LogHelper.hpp"
19 #include <iostream>
20 
21 #include "boost/algorithm/string.hpp"
22 #include "boost/date_time/posix_time/posix_time.hpp"
23 #include "boost/date_time/posix_time/posix_time_io.hpp"
24 #include <boost/filesystem.hpp>
25 
26 namespace ISM {
27 
29 
30  const char* LogHelper::LOG_COLOR_DEFAULT = "\033[0m";
31  const char* LogHelper::LOG_COLOR_BLUE = "\033[34;1m";
32  const char* LogHelper::LOG_COLOR_RED = "\033[31m";
33  const char* LogHelper::LOG_COLOR_GREEN = "\033[32m";
34  const char* LogHelper::LOG_COLOR_YELLOW = "\033[33m";
35  const char* LogHelper::LOG_COLOR_MAGENTA = "\033[35m";
36 
37  unsigned int LogHelper::mBarWidth = 70;
38 
39 
40  LogHelper::LogHelper(path logFilePath, LogLevel level)
41  {
42  mLogLevel = level;
43  if (!boost::filesystem::exists(logFilePath.parent_path()))
44  {
45  boost::filesystem::create_directories(logFilePath.parent_path());
46  }
47 
48  std::ios_base::iostate exceptionMask = mLogFile.exceptions() | std::ios::failbit | std::ios::badbit;
49  mLogFile.exceptions(exceptionMask);
50  try
51  {
52  mLogFile.open(logFilePath.string());
53  }
54  catch (std::ios_base::failure& e)
55  {
56  std::cerr << e.what() << "\n";
57  }
58  }
59 
60  void LogHelper::log(const std::string & message, LogLevel logLevel, const char *logColor)
61  {
62  if (logLevel <= mLogLevel)
63  {
64  std::string coloredMessage = logColor + message + LOG_COLOR_DEFAULT;
65  std::cout << coloredMessage << std::endl;
66 
67  std::vector<std::string> lines;
68  boost::split(lines, message, boost::is_any_of("\n"));
69 
70  if (lines.size() != 0)
71  {
72  std::string infoString = "[ " + getLogLevelString(logLevel) + "][" + getTimeString() + "]: ";
73  std::string indent(infoString.size(), ' ');
74 
75  bool logedInfo = false;
76  for (unsigned int i = 0; i < lines.size(); ++i)
77  {
78  if (!lines[i].empty())
79  {
80  if (logedInfo)
81  {
82  mLogFile << indent << lines[i] << std::endl;
83  }
84  else
85  {
86  mLogFile << infoString << lines[i] << std::endl;
87  logedInfo = true;
88  }
89  }
90  }
91  mLogFile.flush();
92  }
93  }
94  }
95 
96  void LogHelper::logMessage(const std::string & message, LogLevel logLevel, const char *logColor)
97  {
98  LogHelper::getInstance()->log(message, logLevel, logColor);
99  }
100 
102  {
103  LogHelper::getInstance()->log("\n==========================\n", logLevel);
104  }
105 
106  void LogHelper::init(path logFilePath, LogLevel level)
107  {
108  if (mInstance)
109  {
111  }
112 
113  mInstance = LogHelperPtr(new LogHelper(logFilePath, level));
114  }
115 
117  {
118  LogHelper::getInstance()->mLogFile.close();
119  mInstance.reset();
120  }
121 
123  {
124  if (mInstance) {
125  return mInstance;
126  }
127  else
128  {
129  throw std::runtime_error("LogHelper was not initialiesed!");
130  }
131  }
132 
134  {
135  std::string levels[] = {"FATAL", "ERROR", "WARNING", "INFO", "DEBUG"};
136  return levels[level];
137  }
138 
140  {
141  const boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
142  std::stringstream nowStream;
143  nowStream << now;
144  return nowStream.str();
145  }
146 
147  void LogHelper::displayProgress(double progress)
148  {
149  unsigned int position = mBarWidth * progress;
150  std::cout << "[";
151  for (unsigned int i = 0; i < mBarWidth; ++i) {
152  if (i <= position)
153  {
154  std::cout << "#";
155  }
156  else
157  {
158  std::cout << " ";
159  }
160  }
161  std::cout << "] " << int(progress * 100) << " %\r";
162  std::cout.flush();
163  }
164 
165 }
static unsigned int mBarWidth
Definition: LogHelper.hpp:64
static const char * LOG_COLOR_GREEN
Definition: LogHelper.hpp:46
void log(const std::string &message, LogLevel logLevel=LOG_INFO, const char *logColor=LOG_COLOR_DEFAULT)
Definition: LogHelper.cpp:60
SOCI_EMPTY_DECL empty_backend_factory const empty
static void logLine(LogLevel logLevel=LOG_INFO)
Definition: LogHelper.cpp:101
static void displayProgress(double progress)
Definition: LogHelper.cpp:147
LogHelper(path logFilePath, LogLevel level)
Definition: LogHelper.cpp:40
std::string getTimeString()
Definition: LogHelper.cpp:139
LogLevel mLogLevel
Definition: LogHelper.hpp:62
boost::shared_ptr< LogHelper > LogHelperPtr
Definition: typedef.hpp:49
static LogHelperPtr mInstance
Definition: LogHelper.hpp:59
static const char * LOG_COLOR_DEFAULT
Definition: LogHelper.hpp:43
static void logMessage(const std::string &message, LogLevel logLevel=LOG_INFO, const char *logColor=LOG_COLOR_DEFAULT)
Definition: LogHelper.cpp:96
std::string getLogLevelString(LogLevel level)
Definition: LogHelper.cpp:133
static const char * LOG_COLOR_YELLOW
Definition: LogHelper.hpp:47
LogLevel
Definition: LogHelper.hpp:35
static LogHelperPtr getInstance()
Definition: LogHelper.cpp:122
static void init(path logFilePath, LogLevel level)
Definition: LogHelper.cpp:106
static const char * LOG_COLOR_BLUE
Definition: LogHelper.hpp:44
static const char * LOG_COLOR_MAGENTA
Definition: LogHelper.hpp:48
static const char * LOG_COLOR_RED
Definition: LogHelper.hpp:45
this namespace contains all generally usable classes.
std::ofstream mLogFile
Definition: LogHelper.hpp:61
static void close()
Definition: LogHelper.cpp:116


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40