added logger
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
#ifndef QUERY_LOGGER_HPP
|
||||
#define QUERY_LOGGER_HPP
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
namespace matador::utils {
|
||||
|
||||
enum class log_level
|
||||
{
|
||||
LVL_FATAL, /**< If a serious error occurred use FATAL level */
|
||||
LVL_ERROR, /**< On error use ERROR level */
|
||||
LVL_WARN, /**< Warnings should use WARN level */
|
||||
LVL_INFO, /**< Information should go with INFO level */
|
||||
LVL_DEBUG, /**< Debug output should use DEBUG level */
|
||||
LVL_TRACE, /**< Trace information should use TRACE level */
|
||||
LVL_ALL /**< This level represents all log levels and should be used for logging */
|
||||
};
|
||||
|
||||
/**
|
||||
* Simple file logger
|
||||
*/
|
||||
class logger
|
||||
{
|
||||
public:
|
||||
logger(const std::string &path, std::string source);
|
||||
logger(FILE *file, std::string source);
|
||||
logger(const logger &x);
|
||||
logger& operator=(const logger &x);
|
||||
logger(logger &&x) noexcept;
|
||||
logger& operator=(logger &&x) noexcept;
|
||||
|
||||
template<typename ... Args>
|
||||
void info(const std::string &what, Args const &... args) const { info(what.c_str(), args...); }
|
||||
template<typename ... Args>
|
||||
void info(const char *what, Args const &... args) const { log(log_level::LVL_INFO, what, args...); }
|
||||
|
||||
template<typename ... Args>
|
||||
void debug(const std::string &what, Args const &... args) const { debug(what.c_str(), args...); }
|
||||
template<typename ... Args>
|
||||
void debug(const char *what, Args const &... args) const { log(log_level::LVL_DEBUG, what, args...); }
|
||||
|
||||
template<typename ... Args>
|
||||
void warn(const std::string &what, Args const &... args) const { warn(what.c_str(), args...); }
|
||||
template<typename ... Args>
|
||||
void warn(const char *what, Args const &... args) const { log(log_level::LVL_WARN, what, args...); }
|
||||
|
||||
template<typename ... Args>
|
||||
void error(const std::string &what, Args const &... args) const { error(what.c_str(), args...); }
|
||||
template<typename ... Args>
|
||||
void error(const char *what, Args const &... args) const { log(log_level::LVL_ERROR, what, args...); }
|
||||
|
||||
template<typename ... Args>
|
||||
void fatal(const std::string &what, Args const &... args) const { fatal(what.c_str(), args...); }
|
||||
template<typename ... Args>
|
||||
void fatal(const char *what, Args const &... args) const { log(log_level::LVL_FATAL, what, args...); }
|
||||
|
||||
template<typename ... Args>
|
||||
void log(log_level lvl, const char *what, Args const &... args) const;
|
||||
void log(log_level lvl, const char *message) const;
|
||||
|
||||
private:
|
||||
void write(const char *message, size_t size) const;
|
||||
void close();
|
||||
|
||||
private:
|
||||
std::string path_;
|
||||
FILE *stream = nullptr;
|
||||
mutable std::mutex mutex_;
|
||||
std::string source_;
|
||||
};
|
||||
|
||||
template<typename... ARGS>
|
||||
void logger::log(log_level lvl, const char *what, ARGS const &... args) const
|
||||
{
|
||||
char message_buffer[16384];
|
||||
|
||||
#ifdef _MSC_VER
|
||||
sprintf_s(message_buffer, 16384, what, args...);
|
||||
#else
|
||||
sprintf(message_buffer, what, args...);
|
||||
#endif
|
||||
|
||||
log(lvl, message_buffer);
|
||||
}
|
||||
|
||||
}
|
||||
#endif //QUERY_LOGGER_HPP
|
||||
@@ -5,12 +5,40 @@
|
||||
|
||||
namespace matador::utils::os {
|
||||
|
||||
#ifdef _WIN32
|
||||
extern char DIR_SEPARATOR;
|
||||
extern const char* DIR_SEPARATOR_STRING;
|
||||
#else
|
||||
extern char DIR_SEPARATOR;
|
||||
extern const char* DIR_SEPARATOR_STRING;
|
||||
#endif
|
||||
|
||||
std::string error_string(unsigned long error);
|
||||
|
||||
std::string getenv(const char* name);
|
||||
|
||||
[[maybe_unused]] std::string getenv(const std::string &name);
|
||||
|
||||
FILE* fopen(const std::string &path, const char *modes);
|
||||
FILE* fopen(const char *path, const char *modes);
|
||||
|
||||
std::string get_current_dir();
|
||||
|
||||
bool mkdir(const std::string &dirname);
|
||||
bool mkdir(const char *dirname);
|
||||
|
||||
bool chdir(const std::string &dirname);
|
||||
bool chdir(const char *dirname);
|
||||
|
||||
bool rmdir(const std::string &dirname);
|
||||
bool rmdir(const char *dirname);
|
||||
|
||||
bool mkpath(const std::string &path);
|
||||
bool mkpath(const char *path);
|
||||
|
||||
bool rmpath(const std::string &path);
|
||||
bool rmpath(const char *path);
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_OS_HPP
|
||||
|
||||
@@ -2,9 +2,22 @@
|
||||
#define QUERY_STRING_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace matador::utils {
|
||||
|
||||
/**
|
||||
* Splits a string by a delimiter and
|
||||
* add the string tokens to a vector. The
|
||||
* size of the vector is returned.
|
||||
*
|
||||
* @param str The string to split.
|
||||
* @param delim The delimiter character.
|
||||
* @param values The result vector.
|
||||
* @return The size of the vector.
|
||||
*/
|
||||
size_t split(const std::string &str, char delim, std::vector<std::string> &values);
|
||||
|
||||
/**
|
||||
* Replaces all occurrences of string from in given string
|
||||
* with string to.
|
||||
|
||||
Reference in New Issue
Block a user