added logger

This commit is contained in:
2023-11-23 18:04:02 +01:00
parent 1dbe4e6096
commit 72bc6db6b3
18 changed files with 621 additions and 30 deletions
+4 -10
View File
@@ -7,6 +7,8 @@
#include "matador/sql/query_result.hpp"
#include "matador/sql/record.hpp"
#include "matador/utils/logger.hpp"
#include <string>
namespace matador::sql {
@@ -32,21 +34,13 @@ public:
[[nodiscard]] record describe(const std::string &table_name) const;
[[nodiscard]] bool exists(const std::string &table_name) const;
template<class Type>
query_result<Type> fetch(const std::string &sql)
{
return query_result<Type>(connection_->fetch(sql));
}
[[nodiscard]] std::unique_ptr<query_result_impl> fetch(const std::string &sql) const;
[[nodiscard]] std::pair<size_t, std::string> execute(const std::string &sql) const;
private:
friend class session;
[[nodiscard]] std::unique_ptr<query_result_impl> call_fetch(const std::string &sql) const;
private:
connection_info connection_info_;
std::unique_ptr<connection_impl> connection_;
utils::logger logger_;
};
}
+1
View File
@@ -53,6 +53,7 @@ struct any_type_to_string_visitor
column alias(const std::string &column, const std::string &as);
column alias(column &&col, const std::string &as);
column count(const std::string &column);
column count_all();
enum class join_type_t {
INNER, OUTER, LEFT, RIGHT
+1 -2
View File
@@ -54,8 +54,7 @@ public:
template<typename Type>
Type fetch_value()
{
auto result = fetch_all();
return {};
return fetch_one().at(0).as<Type>();
}
private:
+1 -1
View File
@@ -48,7 +48,7 @@ public:
private:
friend class query_select_finish;
[[nodiscard]] std::unique_ptr<query_result_impl> call_fetch(const std::string &sql) const;
[[nodiscard]] std::unique_ptr<query_result_impl> fetch(const std::string &sql) const;
private:
connection_pool<connection> &pool_;
+88
View File
@@ -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
+28
View File
@@ -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
+13
View File
@@ -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.