UTILLib 2.0
Common C++ library with utilities.
Loading...
Searching...
No Matches
logconfig.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Ingemar Hedvall
3 * SPDX-License-Identifier: MIT
4 */
5
9#pragma once
10#include <atomic>
11#include <map>
12#include <memory>
13#include <mutex>
14#include <string>
15#include <vector>
16
17#include "ilogger.h"
18#include "logmessage.h"
19#include "util/stringutil.h"
20
21namespace util::log {
22
27std::string ProgramDataPath();
28
55class LogConfig final {
56 public:
57 LogConfig(const LogConfig &) = delete;
58 LogConfig(LogConfig &&) = delete;
59 LogConfig &operator=(const LogConfig &) = delete;
60 LogConfig &operator=(LogConfig &&) = delete;
61
67 static LogConfig &Instance();
68
75
80
89 void AddLogger(const std::string &logger_name,
90 std::unique_ptr<ILogger> logger);
91
101 void AddLogger(const std::string &logger_name, LogType type,
102 const std::vector<std::string> &arg_list);
103
108 void DeleteLogger(const std::string &logger_name);
109
116 const std::string &logger_name) const;
117
124 std::string GetLogFile(const std::string &logger_name =
125 "Default") const;
126
132 void AddLogMessage(const LogMessage &message) const;
133
134 void Type(LogType log_type);
135
136 [[nodiscard]] LogType Type() const;
137
145 void BaseName(const std::string &
146 base_name);
147 [[nodiscard]] const std::string &BaseName() const;
148
149 void Enabled(bool enabled);
150
151 [[nodiscard]] bool Enabled()
152 const;
153
154 void RootDir(const std::string &root_dir);
155 [[nodiscard]] const std::string &RootDir()
156 const;
157
158 void SubDir(const std::string &sub_dir);
159 [[nodiscard]] const std::string &SubDir()
160 const;
161
169 void ApplicationName(const std::string &app_name) {
170 application_name_ = app_name;
171 }
172
179 [[nodiscard]] const std::string &ApplicationName() {
180 return application_name_;
181 }
182
183 private:
184 std::atomic<bool> enabled_ = true;
185 std::atomic<LogType> log_type_ = LogType::LogNothing;
186 std::string base_name_;
187 std::string sub_dir_;
188 std::string root_dir_;
189 std::string application_name_;
190
191 mutable std::mutex locker_;
192 std::map<std::string, std::unique_ptr<ILogger>, util::string::IgnoreCase>
193 log_chain_;
194
195 LogConfig() = default;
196 ~LogConfig();
197};
198} // namespace util::log
Interface against a generic logger.
Definition ilogger.h:35
Singleton class that is used for configure the default logger in an application.
Definition logconfig.h:55
void Enabled(bool enabled)
Turns off or on the loggers.
void BaseName(const std::string &base_name)
Sets the base name (stem) of the log file.
const std::string & BaseName() const
Returns the base name.
bool CreateDefaultLogger()
Create a logger named 'Default'.
ILogger * GetLogger(const std::string &logger_name) const
Returns a logger by name.
static LogConfig & Instance()
Returns this class (singleton).
void AddLogMessage(const LogMessage &message) const
Adds a log message.
void AddLogger(const std::string &logger_name, std::unique_ptr< ILogger > logger)
Adds a logger with a unique name.
void SubDir(const std::string &sub_dir)
Sets the sub directory.
void RootDir(const std::string &root_dir)
Sets the root directory.
void DeleteLogChain()
Clear the list of loggers.
void DeleteLogger(const std::string &logger_name)
Deletes a logger.
bool Enabled() const
Returns true if the loggers are enabled.
std::string GetLogFile(const std::string &logger_name="Default") const
Returns a log file.
void AddLogger(const std::string &logger_name, LogType type, const std::vector< std::string > &arg_list)
Adds a predefined logger with a unique name.
LogType Type() const
Returns the type of default logger.
void Type(LogType log_type)
Sets the type of default logger.
const std::string & ApplicationName()
Returns the default application name.
Definition logconfig.h:179
const std::string & SubDir() const
Returns the sub directory.
const std::string & RootDir() const
Return the root directory.
void ApplicationName(const std::string &app_name)
Sets the default application name.
Definition logconfig.h:169
Definition stringutil.h:33
Defines an interface against a generic logger.
The log namespace is used for log related classes and functions.
Definition idirectory.h:18
std::string ProgramDataPath()
LogType
Definition ilogger.h:21
@ LogNothing
No logger.
Definition ilogger.h:22
Definition logmessage.h:26