From d3bbd251db27cc517002d41fbd6fe4c5debfd243 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 28 Oct 2014 13:59:23 -0700 Subject: [PATCH] move Logging to LogHandler --- assignment-client/src/AssignmentClient.cpp | 6 +-- .../src/AssignmentClientMonitor.cpp | 4 +- assignment-client/src/audio/AudioMixer.cpp | 2 +- assignment-client/src/avatars/AvatarMixer.cpp | 2 +- assignment-client/src/main.cpp | 4 +- assignment-client/src/octree/OctreeServer.cpp | 4 +- domain-server/src/main.cpp | 4 +- ice-server/src/main.cpp | 4 +- interface/src/Application.cpp | 2 +- libraries/networking/src/LimitedNodeList.cpp | 3 +- libraries/networking/src/NodeList.cpp | 3 +- .../networking/src/ThreadedAssignment.cpp | 5 ++- .../Logging.cpp => shared/src/LogHandler.cpp} | 41 ++++++++++--------- .../src/Logging.h => shared/src/LogHandler.h} | 30 +++++++------- 14 files changed, 60 insertions(+), 54 deletions(-) rename libraries/{networking/src/Logging.cpp => shared/src/LogHandler.cpp} (78%) rename libraries/{networking/src/Logging.h => shared/src/LogHandler.h} (63%) diff --git a/assignment-client/src/AssignmentClient.cpp b/assignment-client/src/AssignmentClient.cpp index b30cd355d1..e7ac7577b9 100644 --- a/assignment-client/src/AssignmentClient.cpp +++ b/assignment-client/src/AssignmentClient.cpp @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -51,7 +51,7 @@ AssignmentClient::AssignmentClient(int &argc, char **argv) : connect(&_shutdownEventListener, SIGNAL(receivedCloseEvent()), SLOT(quit())); // set the logging target to the the CHILD_TARGET_NAME - Logging::setTargetName(ASSIGNMENT_CLIENT_TARGET_NAME); + LogHandler::getInstance().setTargetName(ASSIGNMENT_CLIENT_TARGET_NAME); const QVariantMap argumentVariantMap = HifiConfigVariantMap::mergeCLParametersWithJSONConfig(arguments()); @@ -218,7 +218,7 @@ void AssignmentClient::handleAuthenticationRequest() { void AssignmentClient::assignmentCompleted() { // reset the logging target to the the CHILD_TARGET_NAME - Logging::setTargetName(ASSIGNMENT_CLIENT_TARGET_NAME); + LogHandler::getInstance().setTargetName(ASSIGNMENT_CLIENT_TARGET_NAME); qDebug("Assignment finished or never started - waiting for new assignment."); diff --git a/assignment-client/src/AssignmentClientMonitor.cpp b/assignment-client/src/AssignmentClientMonitor.cpp index af09ff1535..02a4ff04f0 100644 --- a/assignment-client/src/AssignmentClientMonitor.cpp +++ b/assignment-client/src/AssignmentClientMonitor.cpp @@ -9,7 +9,7 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include +#include #include "AssignmentClientMonitor.h" @@ -21,7 +21,7 @@ AssignmentClientMonitor::AssignmentClientMonitor(int &argc, char **argv, int num QCoreApplication(argc, argv) { // start the Logging class with the parent's target name - Logging::setTargetName(ASSIGNMENT_CLIENT_MONITOR_TARGET_NAME); + LogHandler::getInstance().setTargetName(ASSIGNMENT_CLIENT_MONITOR_TARGET_NAME); _childArguments = arguments(); diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index 8d87638434..cc834798d7 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -41,7 +41,7 @@ #include #include -#include +#include #include #include #include diff --git a/assignment-client/src/avatars/AvatarMixer.cpp b/assignment-client/src/avatars/AvatarMixer.cpp index 93f38e3608..3ec7c5cfbf 100644 --- a/assignment-client/src/avatars/AvatarMixer.cpp +++ b/assignment-client/src/avatars/AvatarMixer.cpp @@ -15,7 +15,7 @@ #include #include -#include +#include #include #include #include diff --git a/assignment-client/src/main.cpp b/assignment-client/src/main.cpp index 5e103cf767..3bf6990a74 100644 --- a/assignment-client/src/main.cpp +++ b/assignment-client/src/main.cpp @@ -9,7 +9,7 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include +#include #include #include "Assignment.h" @@ -22,7 +22,7 @@ int main(int argc, char* argv[]) { #endif // use the verbose message handler in Logging - qInstallMessageHandler(Logging::verboseMessageHandler); + qInstallMessageHandler(LogHandler::verboseMessageHandler); const char* numForksString = getCmdOption(argc, (const char**)argv, NUM_FORKS_PARAMETER); diff --git a/assignment-client/src/octree/OctreeServer.cpp b/assignment-client/src/octree/OctreeServer.cpp index d0a17287cb..121cac0273 100644 --- a/assignment-client/src/octree/OctreeServer.cpp +++ b/assignment-client/src/octree/OctreeServer.cpp @@ -18,7 +18,7 @@ #include #include -#include +#include #include #include "../AssignmentClient.h" @@ -923,7 +923,7 @@ void OctreeServer::run() { beforeRun(); // after payload has been processed - qInstallMessageHandler(Logging::verboseMessageHandler); + qInstallMessageHandler(LogHandler::verboseMessageHandler); const char* STATUS_PORT = "--statusPort"; const char* statusPort = getCmdOption(_argc, _argv, STATUS_PORT); diff --git a/domain-server/src/main.cpp b/domain-server/src/main.cpp index 82e4bf7cab..ba80e6fce0 100644 --- a/domain-server/src/main.cpp +++ b/domain-server/src/main.cpp @@ -17,7 +17,7 @@ #include -#include +#include #include #include "DomainServer.h" @@ -27,7 +27,7 @@ int main(int argc, char* argv[]) { setvbuf(stdout, NULL, _IOLBF, 0); #endif - qInstallMessageHandler(Logging::verboseMessageHandler); + qInstallMessageHandler(LogHandler::verboseMessageHandler); int currentExitCode = 0; diff --git a/ice-server/src/main.cpp b/ice-server/src/main.cpp index 21c8b563b1..2326984668 100644 --- a/ice-server/src/main.cpp +++ b/ice-server/src/main.cpp @@ -11,7 +11,7 @@ #include -#include +#include #include "IceServer.h" @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) { setvbuf(stdout, NULL, _IOLBF, 0); #endif - qInstallMessageHandler(Logging::verboseMessageHandler); + qInstallMessageHandler(LogHandler::verboseMessageHandler); IceServer iceServer(argc, argv); return iceServer.exec(); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index f0bd2fb66c..e1088136c4 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -58,7 +58,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/libraries/networking/src/LimitedNodeList.cpp b/libraries/networking/src/LimitedNodeList.cpp index 46ab4f4b0b..48a94e5444 100644 --- a/libraries/networking/src/LimitedNodeList.cpp +++ b/libraries/networking/src/LimitedNodeList.cpp @@ -19,10 +19,11 @@ #include #include +#include + #include "AccountManager.h" #include "Assignment.h" #include "HifiSockAddr.h" -#include "Logging.h" #include "LimitedNodeList.h" #include "PacketHeaders.h" #include "SharedUtil.h" diff --git a/libraries/networking/src/NodeList.cpp b/libraries/networking/src/NodeList.cpp index feeb13500a..ff3b86880d 100644 --- a/libraries/networking/src/NodeList.cpp +++ b/libraries/networking/src/NodeList.cpp @@ -15,10 +15,11 @@ #include #include +#include + #include "AccountManager.h" #include "Assignment.h" #include "HifiSockAddr.h" -#include "Logging.h" #include "NodeList.h" #include "PacketHeaders.h" #include "SharedUtil.h" diff --git a/libraries/networking/src/ThreadedAssignment.cpp b/libraries/networking/src/ThreadedAssignment.cpp index 6d2e366499..52644a9a4e 100644 --- a/libraries/networking/src/ThreadedAssignment.cpp +++ b/libraries/networking/src/ThreadedAssignment.cpp @@ -14,7 +14,8 @@ #include #include -#include "Logging.h" +#include + #include "ThreadedAssignment.h" ThreadedAssignment::ThreadedAssignment(const QByteArray& packet) : @@ -54,7 +55,7 @@ void ThreadedAssignment::setFinished(bool isFinished) { void ThreadedAssignment::commonInit(const QString& targetName, NodeType_t nodeType, bool shouldSendStats) { // change the logging target name while the assignment is running - Logging::setTargetName(targetName); + LogHandler::getInstance().setTargetName(targetName); NodeList* nodeList = NodeList::getInstance(); nodeList->setOwnerType(nodeType); diff --git a/libraries/networking/src/Logging.cpp b/libraries/shared/src/LogHandler.cpp similarity index 78% rename from libraries/networking/src/Logging.cpp rename to libraries/shared/src/LogHandler.cpp index bb704729af..123b418562 100644 --- a/libraries/networking/src/Logging.cpp +++ b/libraries/shared/src/LogHandler.cpp @@ -1,9 +1,10 @@ // -// Logging.cpp -// libraries/networking/src +// LogHandler.cpp +// libraries/shared/src // -// Created by Stephen Birarda on 6/11/13. -// Copyright 2013 High Fidelity, Inc. +// Created by Stephen Birarda on 2014-10-28. +// Migrated from Logging.cpp created on 6/11/13 +// Copyright 2014 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html @@ -18,19 +19,17 @@ #include -#include "Logging.h" +#include "LogHandler.h" -QString Logging::_targetName = QString(); - -Logging& Logging::getInstance() { - static Logging staticInstance; +LogHandler& LogHandler::getInstance() { + static LogHandler staticInstance; return staticInstance; } -Logging::Logging() { +LogHandler::LogHandler() { // setup our timer to flush the verbose logs every 5 seconds QTimer* logFlushTimer = new QTimer(this); - connect(logFlushTimer, &QTimer::timeout, this, &Logging::flushRepeatedMessages); + connect(logFlushTimer, &QTimer::timeout, this, &LogHandler::flushRepeatedMessages); logFlushTimer->start(VERBOSE_LOG_INTERVAL_SECONDS * 1000); } @@ -52,34 +51,34 @@ const char* stringForLogType(QtMsgType msgType) { // the following will produce 2000-10-02 13:55:36 -0700 const char DATE_STRING_FORMAT[] = "%Y-%m-%d %H:%M:%S %z"; -void Logging::verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { +void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { if (message.isEmpty()) { return; } // log prefix is in the following format // [DEBUG] [TIMESTAMP] [PID:PARENT_PID] [TARGET] logged string - + QString prefixString = QString("[%1]").arg(stringForLogType(type)); - + time_t rawTime; time(&rawTime); struct tm* localTime = localtime(&rawTime); - + char dateString[100]; strftime(dateString, sizeof(dateString), DATE_STRING_FORMAT, localTime); - + prefixString.append(QString(" [%1]").arg(dateString)); - + prefixString.append(QString(" [%1").arg(getpid())); - + pid_t parentProcessID = getppid(); if (parentProcessID != 0) { prefixString.append(QString(":%1]").arg(parentProcessID)); } else { prefixString.append("]"); } - + if (!getInstance()._targetName.isEmpty()) { prefixString.append(QString(" [%1]").arg(getInstance()._targetName)); } @@ -87,9 +86,11 @@ void Logging::verboseMessageHandler(QtMsgType type, const QMessageLogContext& co fprintf(stdout, "%s %s\n", prefixString.toLocal8Bit().constData(), message.toLocal8Bit().constData()); } -void Logging::flushRepeatedMessages() { +void LogHandler::flushRepeatedMessages() { QHash::iterator message = _repeatMessageCountHash.begin(); while (message != _repeatMessageCountHash.end()) { message = _repeatMessageCountHash.erase(message); } } + + diff --git a/libraries/networking/src/Logging.h b/libraries/shared/src/LogHandler.h similarity index 63% rename from libraries/networking/src/Logging.h rename to libraries/shared/src/LogHandler.h index 458cbf90e8..e89a739c6a 100644 --- a/libraries/networking/src/Logging.h +++ b/libraries/shared/src/LogHandler.h @@ -1,16 +1,17 @@ // -// Logging.h -// libraries/networking/src +// LogHandler.cpp +// libraries/shared/src // -// Created by Stephen Birarda on 6/11/13. -// Copyright 2013 High Fidelity, Inc. +// Created by Stephen Birarda on 2014-10-28. +// Migrated from Logging.cpp created on 6/11/13 +// Copyright 2014 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#ifndef hifi_Logging_h -#define hifi_Logging_h +#ifndef hifi_LogHandler_h +#define hifi_LogHandler_h #include #include @@ -21,27 +22,28 @@ const int VERBOSE_LOG_INTERVAL_SECONDS = 5; /// Handles custom message handling and sending of stats/logs to Logstash instance -class Logging : public QObject { +class LogHandler : public QObject { Q_OBJECT public: + static LogHandler& getInstance(); + /// sets the target name to output via the verboseMessageHandler, called once before logging begins /// \param targetName the desired target name to output in logs - static void setTargetName(const QString& targetName) { _targetName = targetName; } - + void setTargetName(const QString& targetName) { _targetName = targetName; } + /// a qtMessageHandler that can be hooked up to a target that links to Qt /// prints various process, message type, and time information static void verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString &message); - static void addRepeatedMessageRegex(const QRegExp& regex) { getInstance()._repeatedMessageRegexes.append(regex); } + void addRepeatedMessageRegex(const QRegExp& regex) { _repeatedMessageRegexes.append(regex); } private: - static Logging& getInstance(); - Logging(); + LogHandler(); void flushRepeatedMessages(); - static QString _targetName; + QString _targetName; QList _repeatedMessageRegexes; QHash _repeatMessageCountHash; }; -#endif // hifi_Logging_h +#endif // hifi_LogHandler_h \ No newline at end of file