Files
IfcOpenShell/src/ifcparse/logger.h
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

167 lines
8.0 KiB
C++
Raw Normal View History

2016-05-11 09:56:40 +01:00
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#ifndef IFCLOGGER_H
#define IFCLOGGER_H
2023-09-17 12:30:17 +02:00
#include "ifc_parse_api.h"
#include "express.h"
2017-12-22 13:51:11 +01:00
2023-09-17 12:30:17 +02:00
#include <boost/scope_exit.hpp>
2026-07-09 22:14:31 +02:00
#include <cstddef>
2026-06-10 09:14:22 +02:00
#include <cstdint>
2023-09-17 12:30:17 +02:00
#include <exception>
2022-01-07 15:12:32 +01:00
#include <map>
#include <mutex>
2026-07-09 13:30:48 +02:00
#include <optional>
2023-09-17 12:30:17 +02:00
#include <sstream>
2016-05-11 09:56:40 +01:00
#include <string>
2026-06-11 21:04:44 +02:00
#include <vector>
2016-05-11 09:56:40 +01:00
class IFC_PARSE_API log_message {
public:
char code[7];
int severity;
std::string timestamp, message, instance, product;
log_message(
int severity,
2026-07-09 13:30:48 +02:00
const std::string& code,
const std::string& timestamp,
const std::string& message,
2026-07-09 13:30:48 +02:00
const express::Base& instance = express::Base(),
const express::Base& current_product = express::Base());
};
2016-05-11 09:56:40 +01:00
2026-03-31 15:32:36 +02:00
class IFC_PARSE_API logger {
2023-09-17 12:30:17 +02:00
public:
typedef enum {
LOG_PERF,
LOG_DEBUG,
LOG_NOTICE,
LOG_WARNING,
LOG_ERROR
} Severity;
2023-09-17 12:30:17 +02:00
typedef enum {
FMT_PLAIN,
FMT_JSON,
FMT_INMEMORY
2023-09-17 12:30:17 +02:00
} Format;
private:
std::vector<log_message> log_messages_;
std::ostream* log1_ = nullptr;
std::ostream* log2_ = nullptr;
2023-09-17 12:30:17 +02:00
std::wostream* wlog1_ = nullptr;
std::wostream* wlog2_ = nullptr;
2023-09-17 12:30:17 +02:00
std::stringstream log_stream_;
2026-07-09 13:30:48 +02:00
express::Base current_product_;
2023-09-17 12:30:17 +02:00
Severity verbosity_ = LOG_NOTICE;
Format format_ = FMT_PLAIN;
Severity max_severity_ = LOG_NOTICE;
2023-09-17 12:30:17 +02:00
std::optional<long long> first_timepoint_;
std::map<std::string, double> performance_statistics_;
std::map<std::string, double> performance_signal_start_;
2023-09-17 12:30:17 +02:00
bool print_perf_stats_on_element_ = false;
std::mutex mutex_;
2026-07-09 13:30:48 +02:00
const express::Base& current_product() const;
void current_product(const express::Base& product);
void message(Severity type, const std::string& code, const std::string& message, const express::Base& instance);
2023-09-17 12:30:17 +02:00
public:
logger() = default;
logger(const logger&) = delete;
logger& operator=(const logger&) = delete;
2026-07-09 13:30:48 +02:00
static logger& root();
2026-07-09 13:30:48 +02:00
void set_product(std::optional<express::Base> product);
void set_product(const express::Base& product) { set_product(std::optional<express::Base>(product)); }
2023-09-17 12:30:17 +02:00
void set_output(std::wostream* stream1, std::wostream* stream2);
void set_output(std::ostream* stream1, std::ostream* stream2);
2023-09-17 12:30:17 +02:00
void verbosity(Severity severity);
Severity verbosity() const;
Severity max_severity() const;
2023-09-17 12:30:17 +02:00
void output_format(Format format);
Format output_format() const;
2023-09-17 12:30:17 +02:00
2026-07-09 13:30:48 +02:00
void message(Severity type, const std::string& message, const express::Base& instance = express::Base());
void message(Severity type, const std::exception& exception, const express::Base& instance = express::Base());
void message(Severity type, const char (&code_prefix)[4], uint16_t code_number, const std::string& message, const express::Base& instance = express::Base());
void message(Severity type, const char (&code_prefix)[4], uint16_t code_number, const std::exception& exception, const express::Base& instance = express::Base());
void notice(const std::string& message, const express::Base& instance = express::Base()) { this->message(LOG_NOTICE, message, instance); }
void warning(const std::string& message, const express::Base& instance = express::Base()) { this->message(LOG_WARNING, message, instance); }
void error(const std::string& message, const express::Base& instance = express::Base()) { this->message(LOG_ERROR, message, instance); }
void notice(const std::exception& exception, const express::Base& instance = express::Base()) { message(LOG_NOTICE, exception, instance); }
void warning(const std::exception& exception, const express::Base& instance = express::Base()) { message(LOG_WARNING, exception, instance); }
void error(const std::exception& exception, const express::Base& instance = express::Base()) { message(LOG_ERROR, exception, instance); }
2026-07-09 13:30:48 +02:00
void notice(const char (&code_prefix)[4], uint16_t code_number, const std::string& message, const express::Base& instance = express::Base()) { this->message(LOG_NOTICE, code_prefix, code_number, message, instance); }
void warning(const char (&code_prefix)[4], uint16_t code_number, const std::string& message, const express::Base& instance = express::Base()) { this->message(LOG_WARNING, code_prefix, code_number, message, instance); }
void error(const char (&code_prefix)[4], uint16_t code_number, const std::string& message, const express::Base& instance = express::Base()) { this->message(LOG_ERROR, code_prefix, code_number, message, instance); }
2023-09-17 12:30:17 +02:00
2026-07-09 13:30:48 +02:00
void notice(const char (&code_prefix)[4], uint16_t code_number, const std::exception& exception, const express::Base& instance = express::Base()) { message(LOG_NOTICE, code_prefix, code_number, exception, instance); }
void warning(const char (&code_prefix)[4], uint16_t code_number, const std::exception& exception, const express::Base& instance = express::Base()) { message(LOG_WARNING, code_prefix, code_number, exception, instance); }
void error(const char (&code_prefix)[4], uint16_t code_number, const std::exception& exception, const express::Base& instance = express::Base()) { message(LOG_ERROR, code_prefix, code_number, exception, instance); }
2017-08-01 16:45:11 +02:00
void status(const std::string& message, bool new_line = true);
2017-08-01 16:45:11 +02:00
void progress_bar(int progress);
std::string get_log();
2026-07-09 22:14:31 +02:00
std::size_t count(const std::string& code);
void clear();
2026-07-09 13:30:48 +02:00
void append(logger& other);
void print_performance_stats();
2026-07-09 13:30:48 +02:00
void print_performance_stats_on_element(bool enabled) { print_perf_stats_on_element_ = enabled; }
bool print_performance_stats_on_element() const { return print_perf_stats_on_element_; }
2016-05-11 09:56:40 +01:00
const std::vector<log_message>& log_messages() const { return log_messages_; }
2023-09-17 12:30:17 +02:00
};
2022-01-07 15:12:32 +01:00
// SWIG couldn't represent `logger::root()` default value using Python,
// so when translating signature it represents it just as `fn(*args)`, losing information about args.
// Using `logger* = nullptr` instead of `logger& = logger::root()` helps,
// since `nullptr` is convertable Python's `None`.
// `logger_or_root` is just covering the boilerplate for this pattern.
inline logger& logger_or_root(logger* logger) { return logger ? *logger : ::logger::root(); }
2023-09-17 12:30:17 +02:00
#define PERF(x) \
\
2026-07-09 13:30:48 +02:00
::logger::root().message(::logger::LOG_PERF, x); \
2023-09-17 12:30:17 +02:00
\
BOOST_SCOPE_EXIT(void) { \
2026-07-09 13:30:48 +02:00
::logger::root().message(::logger::LOG_PERF, "done " + std::string(x)); \
2023-09-17 12:30:17 +02:00
} \
BOOST_SCOPE_EXIT_END
2022-01-07 15:12:32 +01:00
#endif