ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Logging.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "Logging.h"
9 
10 #include <fmt/core.h>
11 #include <fmt/printf.h>
12 #include <fmt/ranges.h>
13 
14 namespace cloudViewer {
15 namespace utility {
16 
17 std::function<void(const std::string &)> Logger::Impl::console_print_fcn_ =
18  [](const std::string &msg) { std::cout << msg << std::endl; };
19 
20 Logger::Logger() : impl_(new Logger::Impl()) {
21  impl_->print_fcn_ = Logger::Impl::console_print_fcn_;
22  impl_->verbosity_level_ = VerbosityLevel::Info;
23 }
24 
26  static Logger instance;
27  return instance;
28 }
29 
30 void Logger::VError [[noreturn]] (const char *file,
31  int line,
32  const char *function,
33  const std::string &message) const {
34  std::string err_msg = fmt::format("[ Error] ({}) {}:{}: {}\n", function,
35  file, line, message);
36  err_msg = impl_->ColorString(err_msg, TextColor::Red, 1);
37 #ifdef _MSC_VER // Uncaught exception error messages not shown in Windows
38  std::cerr << err_msg << std::endl;
39 #endif
40  throw std::runtime_error(err_msg);
41 }
42 
43 void Logger::VWarning(const char *file,
44  int line,
45  const char *function,
46  const std::string &message) const {
47  std::string err_msg = fmt::format("[CloudViewer WARNING] {}", message);
48  err_msg = impl_->ColorString(err_msg, TextColor::Yellow, 1);
49  impl_->print_fcn_(err_msg);
50 }
51 
52 void Logger::VInfo(const char *file,
53  int line,
54  const char *function,
55  const std::string &message) const {
56  std::string err_msg = fmt::format("[CloudViewer INFO] {}", message);
57  impl_->print_fcn_(err_msg);
58 }
59 
60 void Logger::VDebug(const char *file,
61  int line,
62  const char *function,
63  const std::string &message) const {
64  std::string err_msg = fmt::format("[CloudViewer DEBUG] {}", message);
65  impl_->print_fcn_(err_msg);
66 }
67 
68 void Logger::SetPrintFunction(
69  std::function<void(const std::string &)> print_fcn) {
70  impl_->print_fcn_ = print_fcn;
71 }
72 
73 const std::function<void(const std::string &)> Logger::GetPrintFunction() {
74  return impl_->print_fcn_;
75 }
76 
77 void Logger::ResetPrintFunction() {
78  impl_->print_fcn_ = impl_->console_print_fcn_;
79 }
80 
82  impl_->verbosity_level_ = verbosity_level;
83 }
84 
86  return impl_->verbosity_level_;
87 }
88 
90  Logger::GetInstance().SetVerbosityLevel(level);
91 }
92 
94  return Logger::GetInstance().GetVerbosityLevel();
95 }
96 
97 ConsoleProgressBar::ConsoleProgressBar(size_t expected_count,
98  const std::string &progress_info,
99  bool active) {
100  reset(expected_count, progress_info, active);
101 }
102 
103 void ConsoleProgressBar::reset(size_t expected_count,
104  const std::string &progress_info,
105  bool active) {
106  expected_count_ = expected_count;
107  current_count_ = static_cast<size_t>(-1); // Guaranteed to wraparound
108  progress_info_ = progress_info;
109  progress_pixel_ = 0;
110  active_ = active;
111  operator++();
112 }
113 
114 ConsoleProgressBar &ConsoleProgressBar::operator++() {
115  setCurrentCount(current_count_ + 1);
116  return *this;
117 }
118 
119 void ConsoleProgressBar::setCurrentCount(size_t n) {
120  current_count_ = n;
121  if (!active_) {
122  return;
123  }
124  if (current_count_ >= expected_count_) {
125  fmt::print("{}[{}] 100%\n", progress_info_,
126  std::string(resolution_, '='));
127  } else {
128  size_t new_progress_pixel =
129  int(current_count_ * resolution_ / expected_count_);
130  if (new_progress_pixel > progress_pixel_) {
131  progress_pixel_ = new_progress_pixel;
132  int percent = int(current_count_ * 100 / expected_count_);
133  fmt::print("{}[{}>{}] {:d}%\r", progress_info_,
134  std::string(progress_pixel_, '='),
135  std::string(resolution_ - 1 - progress_pixel_, ' '),
136  percent);
137  fflush(stdout);
138  }
139  }
140 }
141 
142 } // namespace utility
143 } // namespace cloudViewer
filament::Texture::InternalFormat format
Logger class should be used as a global singleton object (GetInstance()).
Definition: Logging.h:129
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
ccGuiPythonInstance * GetInstance() noexcept
Definition: Runtime.cpp:72
void SetVerbosityLevel(VerbosityLevel level)
Definition: Logging.cpp:89
VerbosityLevel GetVerbosityLevel()
Get global verbosity level of CloudViewer.
Definition: Logging.cpp:93
Generic file read and write utility for python interface.
static std::function< void(const std::string &)> console_print_fcn_
Definition: Logging.h:136