AtilaCalculatorSoftware  2.3.4
C++ Interface for Atila fem resources files
logger.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Project: AtilaCalculatorSoftware
4  File: logger.h
5 
6  Copyright (c) 2020 - All rights reserved
7  Distributed under the MIT License (https://opensource.org/licenses/MIT)
8 
9 =========================================================================*/
10 #pragma once
11 #ifndef ATILACALCULATORSOFTWARE_LOGGER_H
12 #define ATILACALCULATORSOFTWARE_LOGGER_H
13 
14 #include "common.h"
15 #include "core/utilities.h"
16 #include "log_entries.h"
17 
18 #include <ctime>
19 #include <fstream>
20 #include <iomanip>
21 #include <map>
22 #include <regex>
23 #include <set>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 
32 class LogMetaData {
33  public:
38  explicit LogMetaData(LogLevel level, time_t timestamp = time(nullptr));
39 
44 
48  time_t getTimestamp() const;
49 
50  private:
56 
60  time_t timestamp;
61 };
62 
68 class Logger {
69  public:
76  Logger(const Logger& logger);
77 
78 
82  ~Logger();
83 
90  Logger& operator=(const Logger& logger);
91 
99  size_t
100  log(const std::string& message, LogLevel level = Debug, time_t timestamp = time(nullptr));
101 
107  static std::string getEncodedLogLevel(LogLevel level);
108 
115  static std::string formatInt(const int& value, int size);
116 
123  static std::string formatEncodedLogLevel(const std::string& str, LogLevel level);
124 
131  static std::string formatTime(std::string str, time_t timestamp, tm* t);
132 
140  static std::string format(LogMetaData metaData,
141  const std::string& message,
142  const std::string& format = Logger::getLoggingFormat());
143 
148  static Logger* getInstance();
149 
154  static LogEntries* getLogs();
155 
160  static std::string getLoggingFormat();
161 
166  static std::string getDefaultLoggingFormat();
167 
171  static void setVerbosity(bool verbosity);
172 
177  static void setVerboseLevel(LogLevel level);
178 
184  static void setVerboseLevelRange(LogLevel lowest, LogLevel highest);
185 
191  static void setVerboseLevels(std::set<LogLevel> levels);
192 
198  static void logToFile(const std::string& filename, bool legacy = true);
199 
203  static void setFileLogging(bool value);
204 
210  static void setLoggingFormat(std::string format);
211 
217  static size_t trace_s(const std::string& message);
218 
225  template<typename... TArgs> static size_t trace(TArgs const&... args) {
226  return Logger::trace_s(concat(args...));
227  }
228 
234  static size_t debug_s(const std::string& message);
235 
242  template<typename... TArgs> static size_t debug(TArgs const&... args) {
243  return Logger::debug_s(concat(args...));
244  }
245 
251  static size_t info_s(const std::string& message);
252 
259  template<typename... TArgs> static size_t info(TArgs const&... args) {
260  return Logger::info_s(concat(args...));
261  }
262 
268  static size_t warn_s(const std::string& message);
269 
276  template<typename... TArgs> static size_t warn(TArgs const&... args) {
277  return Logger::warn_s(concat(args...));
278  }
279 
285  static size_t error_s(const std::string& message);
286 
293  template<typename... TArgs> static size_t error(TArgs const&... args) {
294  return Logger::error_s(concat(args...));
295  }
296 
302  static size_t fatal_s(const std::string& message);
303 
310  template<typename... TArgs> static size_t fatal(TArgs const&... args) {
311  return Logger::fatal_s(concat(args...));
312  }
313 
319  static size_t addEntryListener(entry_listener listener);
320 
321  private:
325  Logger();
326 
333 
337  bool verbose;
338 
342  std::set<LogLevel> verbosityLevels;
343 
348 
352  std::ofstream loggingFile;
353 
357  std::vector<entry_listener> listeners;
358 
364 
388  static std::string logFormat;
389 
394  static std::string defaultLogFormat;
395 
399  static Logger* instance;
400 };
401 
402 #endif // ATILACALCULATORSOFTWARE_LOGGER_H
Logging interface that stores logs, allow logfile writing, also contains other output methods...
Definition: logger.h:68
static size_t fatal(TArgs const &... args)
Concatenate and log the given elements as an FATAL entry.
Definition: logger.h:310
static size_t debug(TArgs const &... args)
Concatenate and log the given elements as an DEBUG entry.
Definition: logger.h:242
static size_t trace(TArgs const &... args)
Concatenate and log the given elements as an TRACE entry.
Definition: logger.h:225
static size_t info_s(const std::string &message)
Log an INFO entry from the given string into the logger.
Definition: logger.cpp:228
LogMetaData(LogLevel level, time_t timestamp=time(nullptr))
Definition: logger.cpp:16
static size_t error(TArgs const &... args)
Concatenate and log the given elements as an ERROR entry.
Definition: logger.h:293
time_t timestamp
Timestamp of the log entry.
Definition: logger.h:60
static LogTimeMode timeMode
Time mode of the log entries (Local or UTC - absolute) Default value is TimeLocal.
Definition: logger.h:363
static Logger * instance
Logger instance.
Definition: logger.h:399
std::vector< entry_listener > listeners
List of listeners that will trigger on new entry.
Definition: logger.h:357
static std::string getLoggingFormat()
Definition: logger.cpp:92
std::ofstream loggingFile
Output file stream for file writing log messages.
Definition: logger.h:352
static std::string logFormat
Current log formatting string, if empty will use defaultLogFormat.
Definition: logger.h:388
static size_t error_s(const std::string &message)
Log an ERROR entry from the given string into the logger.
Definition: logger.cpp:236
time_t getTimestamp() const
Definition: logger.cpp:24
Contains the entries of the logger.
Definition: log_entries.h:25
static std::string defaultLogFormat
Default log formatting string.
Definition: logger.h:394
std::string concat(TArgs const &... args)
Cast and concatenate parameter pack elements as string.
Definition: utilities.h:99
static size_t warn_s(const std::string &message)
Log a WARN entry from the given string into the logger.
Definition: logger.cpp:232
static size_t trace_s(const std::string &message)
Log a TRACE entry from the given string into the logger.
Definition: logger.cpp:220
static size_t warn(TArgs const &... args)
Concatenate and log the given elements as an WARN entry.
Definition: logger.h:276
static size_t fatal_s(const std::string &message)
Log a FATAL entry from the given string into the logger.
Definition: logger.cpp:240
LogEntries * entries
Nonformatted raw log entries.
Definition: logger.h:332
static size_t debug_s(const std::string &message)
Log a DEBUG entry from the given string into the logger.
Definition: logger.cpp:224
Store information about the log entry (LogLevel, Timestamp, ...)
Definition: logger.h:32
LogTimeMode
Logging time mode, TimeUTC means use abo.
Definition: common.h:32
bool verbose
If set on true, will show log messages in console.
Definition: logger.h:337
Definition: common.h:38
LogLevel getLogLevel()
Definition: logger.cpp:22
bool fileLogging
If set on true, will write log messages into file.
Definition: logger.h:347
LogLevel level
Logging level of the log entry.
Definition: logger.h:55
LogLevel
Logging level of a log entry, used to filter log entries, each filtering level will hide lower level ...
Definition: common.h:38
void(* entry_listener)(size_t index, const std::pair< LogMetaData, std::string > &entry, const std::string &formatted, const LogEntries &entries)
Entry listener for Logger, will be called as callback for each new entry in the logger with the given...
Definition: common.h:52
static size_t info(TArgs const &... args)
Concatenate and log the given elements as an INFO entry.
Definition: logger.h:259
std::set< LogLevel > verbosityLevels
Allowed logging levels on verbose (in console)
Definition: logger.h:342