Files
IfcOpenShell/src/ifcparse/IfcLogger.h
T

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

155 lines
6.8 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 "IfcBaseClass.h"
2017-12-22 13:51:11 +01:00
2023-09-17 12:30:17 +02:00
#include <boost/optional.hpp>
#include <boost/scope_exit.hpp>
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>
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,
const char (&code_prefix)[4],
uint16_t code_number,
const std::string& timestamp,
const std::string& message,
const IfcUtil::IfcBaseInterface* inst = 0,
const IfcUtil::IfcBaseClass* current_product = 0);
};
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_;
2023-09-17 12:30:17 +02:00
// To both stream variants need to exist at runtime or should this be a
// template argument of Logger or controlled using preprocessor directives?
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-06-11 21:04:44 +02:00
const IfcUtil::IfcBaseClass* current_product_ = nullptr;
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
boost::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_;
const IfcUtil::IfcBaseClass* current_product() const;
void current_product(const IfcUtil::IfcBaseClass* product);
2023-09-17 12:30:17 +02:00
public:
Logger() = default;
Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;
static Logger& Root();
void SetProduct(boost::optional<const IfcUtil::IfcBaseClass*> product);
2023-09-17 12:30:17 +02:00
/// Determines to what stream respectively progress and errors are logged
void SetOutput(std::wostream* stream1, std::wostream* stream2);
2023-09-17 12:30:17 +02:00
/// Determines to what stream respectively progress and errors are logged
void SetOutput(std::ostream* stream1, std::ostream* stream2);
2023-09-17 12:30:17 +02:00
/// Determines the types of log messages to get logged
void Verbosity(Severity severity);
Severity Verbosity() const;
Severity MaxSeverity() const;
2023-09-17 12:30:17 +02:00
/// Determines output format: plain text or sequence of JSON objects
void OutputFormat(Format format);
Format OutputFormat() const;
2023-09-17 12:30:17 +02:00
/// Log a message to the output stream
void Message(Severity type, const char (&code_prefix)[4], uint16_t code_number, const std::string& message, const IfcUtil::IfcBaseInterface* instance = 0);
void Message(Severity type, const char (&code_prefix)[4], uint16_t code_number, const std::exception& exception, const IfcUtil::IfcBaseInterface* instance = 0);
void Notice(const char (&code_prefix)[4], uint16_t code_number, const std::string& message, const IfcUtil::IfcBaseInterface* instance = 0) { 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 IfcUtil::IfcBaseInterface* instance = 0) { 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 IfcUtil::IfcBaseInterface* instance = 0) { Message(LOG_ERROR, code_prefix, code_number, message, instance); }
2023-09-17 12:30:17 +02:00
void Notice(const char (&code_prefix)[4], uint16_t code_number, const std::exception& exception, const IfcUtil::IfcBaseInterface* instance = 0) { 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 IfcUtil::IfcBaseInterface* instance = 0) { 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 IfcUtil::IfcBaseInterface* instance = 0) { 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 ProgressBar(int progress);
std::string GetLog();
2026-06-11 21:04:44 +02:00
void ClearLog();
void Append(Logger& logger);
void PrintPerformanceStats();
void PrintPerformanceStatsOnElement(bool b) { print_perf_stats_on_element_ = b; }
2026-06-11 21:04:44 +02:00
bool PrintPerformanceStatsOnElement() 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
2023-09-17 12:30:17 +02:00
#define PERF(x) \
\
Logger::Root().Message(Logger::LOG_PERF, "SYS", 1, x); \
2023-09-17 12:30:17 +02:00
\
BOOST_SCOPE_EXIT(void) { \
Logger::Root().Message(Logger::LOG_PERF, "SYS", 2, "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