101 explicit Logging(
const std::string & logfile,
LOGLEVEL max_level,
bool to_stderr,
bool to_syslog);
154 static bool init_logging(
const std::string & logfile,
LOGLEVEL max_level,
bool to_stderr,
bool to_syslog);
162 template <
typename T,
typename... Args>
163 static void trace(
const T filename,
const std::string &format_string, Args &&...args)
181 template <
typename T,
typename... Args>
182 static void debug(
const T filename,
const std::string &format_string, Args &&...args)
200 template <
typename T,
typename... Args>
201 static void info(
const T filename,
const std::string &format_string, Args &&...args)
219 template <
typename T,
typename... Args>
220 static void warning(
const T filename,
const std::string &format_string, Args &&...args)
238 template <
typename T,
typename... Args>
239 static void error(
const T filename,
const std::string &format_string, Args &&...args)
330 template<class T, typename std::enable_if<!std::is_same<T, std::string>::value,
bool>::type =
true>
353 const std::string &string_to_update,
354 const size_t __attribute__((unused)) index_to_replace);
367 template <
typename T,
typename... Args>
369 const std::string &string_to_search,
370 const size_t index_to_replace,
376 std::regex exp(
"%(<([^>]+)>)*" + std::to_string(index_to_replace) +
"(?=[^0-9]|$)");
378 std::string string_to_update(string_to_search);
379 std::string::const_iterator searchStart(string_to_search.cbegin());
382 while (std::regex_search(searchStart, string_to_search.cend(), res, exp))
384 std::ostringstream ostr;
389 std::vector<char> fmt;
390 std::string ftmspec = res[2].str();
392 if (ftmspec.front() !=
'%')
398 fmt.insert(fmt.end(), ftmspec.begin(), ftmspec.end());
401 size_t size =
static_cast<size_t>(std::snprintf(
nullptr, 0, fmt.data(),
fix_std_string(val))) + 1;
402 std::vector<char> buffer;
404 std::snprintf(buffer.data(), size, fmt.data(),
fix_std_string(val));
405 ostr << buffer.data();
413 string_to_update.replace(
static_cast<size_t>(res.position()) + offset,
static_cast<size_t>(res[0].length()), ostr.str());
415 offset +=
static_cast<size_t>(res.position()) + ostr.str().length();
417 searchStart = res.suffix().first;
422 index_to_replace + 1,
423 std::forward<Args>(args)...);
433 template <
typename... Args>
434 static std::string
format(
const std::string &format_string, Args &&...args)
436 return format_helper(format_string, 1, std::forward<Args>(args)...);
Logger()
Construct Logger object.
const LOGLEVEL m_loglevel
Log level required to write log entry.
const std::string m_filename
Name of file for which this log entry was written. May be empty.
Logger(LOGLEVEL loglevel, const std::string &filename)
Construct Logger object.
virtual ~Logger()
Destroy Logger object.
static const std::map< LOGLEVEL, int > m_syslog_level_map
Map our log levels to syslog levels.
static const std::map< LOGLEVEL, std::string > m_level_name_map
Map log level enums to strings.
static const std::map< LOGLEVEL, std::string > m_level_colour_map
Map log level enums to colours (logging to stderr only)
static std::string convert(char *val)
Convert char* to std::string.
bool GetFail() const
Check whether either failbit or badbit is set.
static void log_with_level(LOGLEVEL loglevel, const char *filename, const std::string &message)
Write log entry.
static std::string format(const std::string &format_string, Args &&...args)
format string with single token
static void warning(const T filename, const std::string &format_string, Args &&...args)
Write warning level log entry.
friend Logger
Make logger class our friend.
static bool init_logging(const std::string &logfile, LOGLEVEL max_level, bool to_stderr, bool to_syslog)
Initialise the logging facility.
static void debug(const T filename, const std::string &format_string, Args &&...args)
Write debug level log entry.
static std::unique_ptr< Logging > m_logging
Reference to self, Logging is a singleton.
static std::string format_helper(const std::string &string_to_update, const size_t __attribute__((unused)) index_to_replace)
Standard format_helper without parameters.
static std::string format_helper(const std::string &string_to_search, const size_t index_to_replace, T &&val, Args &&...args)
format_helper with variadic parameters.
static T & convert(T &val)
Dummy: return orignal value unchanged.
static std::string convert(const char *val)
Convert const char* to std::string.
Logging(const std::string &logfile, LOGLEVEL max_level, bool to_stderr, bool to_syslog)
static T & fix_std_string(T &val)
Dummy: return orignal value unchanged if not type std::string.
static void trace(const T filename, const std::string &format_string, Args &&...args)
Write trace level log entry.
friend Logger Log(LOGLEVEL loglevel, const std::string &filename)
Make logger class our friend for our constructor.
std::ofstream m_logfile
Log file object for writing to disk.
const LOGLEVEL m_max_level
The maximum level of log output to write.
static std::recursive_mutex m_mutex
Access mutex.
static void info(const T filename, const std::string &format_string, Args &&...args)
Write info level log entry.
static bool show(LOGLEVEL loglevel)
Check if log entry should be displayed at the current log level.
static void error(const T filename, const std::string &format_string, Args &&...args)
Write error level log entry.
const bool m_to_stderr
Whether to write log output to stderr.
static const char * fix_std_string(const std::string &val)
Convert std::string to const char*.
const bool m_to_syslog
Whether to write log output to syslog.
LOGLEVEL
Logging level types enum.
constexpr Logging::LOGLEVEL LOGDEBUG
Shorthand for log level DEBUG.
constexpr Logging::LOGLEVEL LOGTRACE
Shorthand for log level TRACE.
constexpr Logging::LOGLEVEL LOGWARN
Shorthand for log level WARNING.
constexpr Logging::LOGLEVEL LOGERROR
Shorthand for log level ERROR.
constexpr Logging::LOGLEVEL LOGINFO
Shorthand for log level INFO.