25 #include <QApplication>
32 #include <QMessageBox>
33 #include <QStandardPaths>
45 #define getpid _getpid
62 : QListWidget(parent) {}
66 int itemsCount =
count();
68 for (
int i = 0; i < itemsCount; ++i) {
69 if (item(i)->isSelected()) {
70 strings << item(i)->text();
74 QApplication::clipboard()->setText(strings.join(
"\n"));
76 QListWidget::keyPressEvent(
event);
100 : m_textDisplay(nullptr),
101 m_parentWidget(nullptr),
102 m_parentWindow(nullptr),
103 m_logStream(nullptr) {}
110 const QMessageLogContext&
context,
111 const QString& msg) {
117 if (
type == QtDebugMsg) {
123 QString(
"[%1] ").arg(
context.function) +
133 message.prepend(
"[Qt WARNING] ");
137 message.prepend(
"[Qt CRITICAL] ");
141 message.prepend(
"[Qt FATAL] ");
145 message.prepend(
"[Qt INFO] ");
158 std::cout << message.toStdString() <<
std::endl;
163 std::cerr << message.toStdString() <<
std::endl;
179 QWidget* parentWidget ,
181 bool redirectToStdOut ) {
189 s_console.instance->m_textDisplay = textDisplay;
191 s_console.instance->m_parentWindow = parentWindow;
199 "QtMessagesEnabled",
false)
208 s_console.instance->setAutoRefresh(
true);
230 for (QVector<ConsoleItemType>::const_iterator it =
m_queue.constBegin();
231 it !=
m_queue.constEnd(); ++it) {
233 int level = it->second;
237 QListWidgetItem* item =
new QListWidgetItem(it->first);
246 item->setForeground(Qt::darkRed);
284 QString formatedMessage =
285 QStringLiteral(
"[") +
DATETIME + QStringLiteral(
"] ") + message;
288 printf(
"%s\n", qPrintable(message));
323 printf(
"WARN-DBG: ");
334 printf(
" %s\n", qPrintable(formatedMessage));
340 QThread::currentThread() == qApp->thread()) {
348 QDateTime::currentDateTime().toString(
"yyyyMMdd-HHmmss");
351 qint64 pid = getpid();
354 return QString(
"%1.%2.%3.log").arg(prefix).arg(timestamp).arg(pid);
358 QStringList candidatePaths;
362 candidatePaths << QCoreApplication::applicationDirPath() +
"/logs";
363 candidatePaths << QStandardPaths::writableLocation(
364 QStandardPaths::TempLocation) +
365 "/ACloudViewerCache/logs";
367 #elif defined(__APPLE__)
371 QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
372 if (!appLogPath.isEmpty()) {
376 QDir appDir(appLogPath);
379 if (appDir.cd(
"Logs")) {
380 candidatePaths << appDir.absolutePath() +
"/ACloudViewerCache";
385 if (!appLogPath.isEmpty()) {
386 candidatePaths << appLogPath +
"/logs";
391 QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
392 if (!homePath.isEmpty()) {
393 candidatePaths << homePath +
"/.ACloudViewerCache/logs";
397 candidatePaths << QCoreApplication::applicationDirPath() +
"/logs";
400 candidatePaths << QStandardPaths::writableLocation(
401 QStandardPaths::TempLocation) +
402 "/ACloudViewerCache/logs";
407 candidatePaths << QCoreApplication::applicationDirPath() +
"/logs";
412 QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
413 if (!dataPath.isEmpty()) {
414 candidatePaths << dataPath +
"/logs";
419 QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
420 if (!homePath.isEmpty()) {
421 candidatePaths << homePath +
"/.ACloudViewerCache/logs";
425 candidatePaths << QStandardPaths::writableLocation(
426 QStandardPaths::TempLocation) +
427 "/ACloudViewerCache/logs";
431 for (
const QString&
path : candidatePaths) {
436 if (dir.mkpath(
".")) {
442 QFileInfo dirInfo(
path);
443 if (dirInfo.isWritable()) {
451 return QStandardPaths::writableLocation(QStandardPaths::TempLocation);
467 if (!logPrefix.isEmpty()) {
475 QString logPath = logDir +
"/" + logFileName;
480 QString(
"[Console] Failed to open/create log file '%1'")
486 QString(
"[Console] Log file created: %1").arg(logPath);
491 *
m_logStream <<
"========================================"
495 << QDateTime::currentDateTime().toString(
496 "yyyy-MM-dd HH:mm:ss")
499 *
m_logStream <<
"========================================"
static bool PrintDebug(const char *format,...)
Same as Print, but works only in Debug mode.
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
static bool Print(const char *format,...)
Prints out a formatted message in console.
static CVLog * TheInstance()
Returns the static and unique instance.
static void RegisterInstance(CVLog *logInstance)
Registers a unique instance.
static bool Error(const char *format,...)
Display an error dialog with formatted message.
void forceConsoleDisplay() override
Forces display of console widget.
ecvConsole()
Default constructor.
static QString getLogDirectory()
Get appropriate log directory path (handles permissions on Ubuntu)
QWidget * m_parentWidget
Parent widget.
static void ReleaseInstance(bool flush=true)
Releases unique instance.
QMutex m_mutex
Mutex for concurrent thread access to console.
static void Init(QListWidget *textDisplay=nullptr, QWidget *parentWidget=nullptr, MainWindow *parentWindow=nullptr, bool redirectToStdOut=false)
Inits console (and optionaly associates it with a text output widget)
MainWindow * m_parentWindow
Parent window (if any)
static void EnableQtMessages(bool state)
Whether to show Qt messages (qDebug / qWarning / etc.) in Console.
QPair< QString, int > ConsoleItemType
Queue element type (message + color)
QTextStream * m_logStream
Log file stream.
bool setLogFile(const QString &logPrefix)
Sets log file with prefix (generates timestamped log file like glog)
QTimer m_timer
Timer for auto-refresh.
void refresh()
Refreshes console (display all messages still in queue)
static bool s_showQtMessagesInConsole
Whether to show Qt messages (qDebug / qWarning / etc.) in Console.
static bool QtMessagesEnabled()
~ecvConsole() override
Destructor.
static QString generateLogFileName(const QString &prefix)
Generate log file name with timestamp and pid.
QWidget * parentWidget()
Returns the parent widget (if any)
static bool s_redirectToStdOut
QListWidget * m_textDisplay
Associated text display widget.
void logMessage(const QString &message, int level) override
Generic message logging method.
void setAutoRefresh(bool state)
Sets auto-refresh state.
QVector< ConsoleItemType > m_queue
Queue for incoming messages.
static const QString Console()
static QVariant getValue(const QString §ion, const QString &key, const QVariant &defaultValue=QVariant())
static void setValue(const QString §ion, const QString &key, const QVariant &value)
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
static ecvSingleton< ecvConsole > s_console
QTextStream & endl(QTextStream &stream)
static QString LOGFILE_PREFIX
Tensor Append(const Tensor &self, const Tensor &other, const utility::optional< int64_t > &axis)
Appends the two tensors, along the given axis into a new tensor. Both the tensors must have same data...
static const std::string path
bool Copy(const std::string &from, const std::string &to, bool include_parent_dir=false, const std::string &extname="")
Copy a file or directory.
constexpr Rgb red(MAX, 0, 0)
constexpr Rgb blue(0, 0, MAX)
Generic singleton encapsulation structure.