Libs/Common/Logging.h
Namespaces
Name |
---|
shapeworks User usage reporting (telemetry) |
Classes
Name | |
---|---|
struct | fmt::formatter< QString > |
class | shapeworks::Logging ShapeWorks Logging Library. |
Functions
Name | |
---|---|
template <typename... Args> std::string |
safe_format(const std::string & fmt_str, const Args &... args) |
Defines
Name | |
---|---|
SW_LOG_STACK(message) Log stack macro. |
|
SW_LOG(message, ...) Log message macro. |
|
SW_WARN(message, ...) Log warning macro. |
|
SW_ERROR(message, ...) Log error macro. |
|
SW_DEBUG(message, ...) Log debug macro. |
|
SW_TRACE(x) Variable trace macro (e.g. output variable name = |
|
SW_MESSAGE(message, ...) Log show message macro. |
|
SW_STATUS(message, ...) Don't write to log, but set status (e.g. in the Studio statusbar) |
|
SW_PROGRESS(value, message, ...) | |
SW_CLOSE_LOG() Close session macro. |
|
SW_LOG_ONCE(message, ...) Log once macro, will only log the message once. |
Functions Documentation
function safe_format
template <typename... Args>
std::string safe_format(
const std::string & fmt_str,
const Args &... args
)
Macros Documentation
define SW_LOG_STACK
#define SW_LOG_STACK(
message
)
shapeworks::Logging::Instance().log_stack(message)
Log stack macro.
define SW_LOG
#define SW_LOG(
message,
...
)
shapeworks::Logging::Instance().log_message(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__);
Log message macro.
define SW_WARN
#define SW_WARN(
message,
...
)
shapeworks::Logging::Instance().log_warning(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__)
Log warning macro.
define SW_ERROR
#define SW_ERROR(
message,
...
)
shapeworks::Logging::Instance().log_error(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__)
Log error macro.
define SW_DEBUG
#define SW_DEBUG(
message,
...
)
shapeworks::Logging::Instance().log_debug(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__, __FUNCTION__)
Log debug macro.
define SW_TRACE
#define SW_TRACE(
x
)
SW_DEBUG(#x " = {}", x);
Variable trace macro (e.g. output variable name =
define SW_MESSAGE
#define SW_MESSAGE(
message,
...
)
shapeworks::Logging::Instance().show_message(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__)
Log show message macro.
define SW_STATUS
#define SW_STATUS(
message,
...
)
shapeworks::Logging::Instance().show_status(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__)
Don't write to log, but set status (e.g. in the Studio statusbar)
define SW_PROGRESS
#define SW_PROGRESS(
value,
message,
...
)
shapeworks::Logging::Instance().show_progress(value, safe_format(message, ##__VA_ARGS__));
define SW_CLOSE_LOG
#define SW_CLOSE_LOG(
)
shapeworks::Logging::Instance().close_log();
Close session macro.
define SW_LOG_ONCE
#define SW_LOG_ONCE(
message,
...
)
{ \
static bool logged = false; \
if (!logged) { \
SW_LOG(message, ##__VA_ARGS__); \
logged = true; \
} \
}
Log once macro, will only log the message once.
Source code
#pragma once
#include <spdlog/fmt/fmt.h>
#include <QString>
#include <functional>
#include <iostream>
template <>
struct fmt::formatter<QString> {
constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { return ctx.end(); }
template <typename FormatContext>
auto format(const QString& str, FormatContext& ctx) const -> decltype(ctx.out()) {
return fmt::format_to(ctx.out(), "{}", qUtf8Printable(str));
}
};
template <typename... Args>
std::string safe_format(const std::string& fmt_str, const Args&... args) {
std::string result;
try {
result = fmt::format(fmt_str, args...);
} catch (const std::exception& e) {
// Handle formatting errors here, example:
std::cerr << "Error formatting string: " << fmt_str << " : " << e.what() << std::endl;
}
return result;
}
namespace shapeworks {
class Logging {
public:
static Logging& Instance();
void open_file_log(const std::string& filename);
bool check_log_open() const;
std::string get_log_filename() const;
void log_message(const std::string& message, const int line, const char* file) const;
void log_stack(const std::string& message) const;
void log_error(const std::string& message, const int line, const char* file) const;
void show_message(const std::string& message, const int line, const char* file) const;
void show_status(const std::string& message, const int line, const char* file) const;
void show_progress(double value, const std::string& message);
void log_debug(const std::string& message, const int line, const char* file, const char* function) const;
void log_warning(const std::string& message, const int line, const char* file) const;
void close_log();
void set_error_callback(const std::function<void(std::string)>& callback);
void set_message_callback(const std::function<void(std::string)>& callback);
void set_warning_callback(const std::function<void(std::string)>& callback);
void set_debug_callback(const std::function<void(std::string)>& callback);
void set_status_callback(const std::function<void(std::string)>& callback);
void set_progress_callback(const std::function<void(double, std::string)>& callback);
private:
Logging();
std::string log_filename_;
bool log_open_ = false;
std::function<void(std::string)> error_callback_;
std::function<void(std::string)> message_callback_;
std::function<void(std::string)> warning_callback_;
std::function<void(std::string)> debug_callback_;
std::function<void(std::string)> status_callback_;
std::function<void(double, std::string)> progress_callback_;
};
#define SW_LOG_STACK(message) shapeworks::Logging::Instance().log_stack(message)
#define SW_LOG(message, ...) \
shapeworks::Logging::Instance().log_message(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__);
#define SW_WARN(message, ...) \
shapeworks::Logging::Instance().log_warning(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__)
#define SW_ERROR(message, ...) \
shapeworks::Logging::Instance().log_error(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__)
#define SW_DEBUG(message, ...) \
shapeworks::Logging::Instance().log_debug(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__, __FUNCTION__)
#define SW_TRACE(x) SW_DEBUG(#x " = {}", x);
#define SW_MESSAGE(message, ...) \
shapeworks::Logging::Instance().show_message(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__)
#define SW_STATUS(message, ...) \
shapeworks::Logging::Instance().show_status(safe_format(message, ##__VA_ARGS__), __LINE__, __FILE__)
#define SW_PROGRESS(value, message, ...) \
shapeworks::Logging::Instance().show_progress(value, safe_format(message, ##__VA_ARGS__));
#define SW_CLOSE_LOG() shapeworks::Logging::Instance().close_log();
#define SW_LOG_ONCE(message, ...) \
{ \
static bool logged = false; \
if (!logged) { \
SW_LOG(message, ##__VA_ARGS__); \
logged = true; \
} \
}
} // namespace shapeworks
Updated on 2024-11-11 at 19:51:46 +0000