ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Utilities.h
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 #pragma once
9 
10 #include <QString>
11 
12 #include <Python.h>
13 
14 #include <CVLog.h>
15 
23 template <enum CVLog::MessageLevelFlags level> class ccLogger
24 {
25  public:
27  {
28  m_message.reserve(255);
29  }
30 
31  virtual ~ccLogger()
32  {
33  flush();
34  }
35 
36  inline ccLogger &operator<<(const QString &message)
37  {
38  m_message += message;
39  return *this;
40  }
41 
42  inline ccLogger &operator<<(const char *message)
43  {
44  m_message += message;
45  return *this;
46  }
47 
48  inline ccLogger &operator<<(int value)
49  {
50  m_message += QString::number(value);
51  return *this;
52  }
53 
54  inline ccLogger &operator<<(const qsizetype value)
55  {
56  m_message += QString::number(value);
57  return *this;
58  }
59 
60  void flush()
61  {
63  m_message.clear();
64  }
65 
66  protected:
67  QString m_message;
68 };
69 
75 
85 template <enum CVLog::MessageLevelFlags level> class PluginLogger : public ccLogger<level>
86 {
87  public:
88  PluginLogger() : ccLogger<level>()
89  {
90  this->m_message += "[PythonRuntime] ";
91  }
92 
93  // friend PluginLogger& endl(PluginLogger<level>& logger) {
94  // logger.flush();
95  // return logger;
96  // }
97 };
98 
104 
106 inline wchar_t *QStringToWcharArray(const QString &string)
107 {
108  auto *wcharArray = new wchar_t[string.size() + 1];
109  const int len = string.toWCharArray(wcharArray);
110  Q_ASSERT(len <= string.size());
111  wcharArray[len] = '\0';
112  return wcharArray;
113 }
114 
116 inline void LogPythonPath()
117 {
118  const wchar_t *pythonPath = Py_GetPath();
119  if (pythonPath != nullptr)
120  {
121  size_t errPos{0};
122  char *cPythonPath = Py_EncodeLocale(pythonPath, &errPos);
123  if (cPythonPath)
124  {
125  CVLog::Print("[PythonRuntime] PythonPath is set to: %s", cPythonPath);
126  PyMem_Free(cPythonPath);
127  }
128  else
129  {
130  CVLog::Print("[PythonRuntime] Failed to convert the PythonPath");
131  }
132  }
133  else
134  {
135  CVLog::Print("[PythonRuntime] PythonPath is not set");
136  }
137 }
138 
140 inline void LogPythonHome()
141 {
142  const wchar_t *pythonHome = Py_GetPythonHome();
143  if (pythonHome != nullptr)
144  {
145  size_t errPos{0};
146  char *cPythonHome = Py_EncodeLocale(pythonHome, &errPos);
147  if (cPythonHome)
148  {
149  CVLog::Print("[PythonRuntime] PythonHome is set to: %s", cPythonHome);
150  PyMem_Free(cPythonHome);
151  }
152  else
153  {
154  CVLog::Print("[PythonRuntime]Failed to convert the PythonHome path");
155  }
156  }
157  else
158  {
159  CVLog::Print("[PythonRuntime] PythonHome is not set");
160  }
161 }
int size
void LogPythonPath()
Logs the PYTHON_PATH the log console of ACloudViewer.
Definition: Utilities.h:116
wchar_t * QStringToWcharArray(const QString &string)
Returns a newly allocated wchar_t array (null terminated) from a QString.
Definition: Utilities.h:106
void LogPythonHome()
Logs the PYTHON_HOME the log console of ACloudViewer.
Definition: Utilities.h:140
static bool Print(const char *format,...)
Prints out a formatted message in console.
Definition: CVLog.cpp:113
static void LogMessage(const QString &message, int level)
Static shortcut to CVLog::logMessage.
Definition: CVLog.cpp:64
QString m_message
Definition: Utilities.h:67
ccLogger()
Definition: Utilities.h:26
void flush()
Definition: Utilities.h:60
ccLogger & operator<<(int value)
Definition: Utilities.h:48
ccLogger & operator<<(const QString &message)
Definition: Utilities.h:36
ccLogger & operator<<(const char *message)
Definition: Utilities.h:42
virtual ~ccLogger()
Definition: Utilities.h:31
ccLogger & operator<<(const qsizetype value)
Definition: Utilities.h:54