move Logging to LogHandler

This commit is contained in:
Stephen Birarda 2014-10-28 13:59:23 -07:00
parent 2711ff2b5f
commit d3bbd251db
14 changed files with 60 additions and 54 deletions

View file

@ -16,7 +16,7 @@
#include <AccountManager.h>
#include <Assignment.h>
#include <HifiConfigVariantMap.h>
#include <Logging.h>
#include <LogHandler.h>
#include <LogUtils.h>
#include <NodeList.h>
#include <PacketHeaders.h>
@ -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.");

View file

@ -9,7 +9,7 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <Logging.h>
#include <LogHandler.h>
#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();

View file

@ -41,7 +41,7 @@
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <Logging.h>
#include <LogHandler.h>
#include <NetworkAccessManager.h>
#include <NodeList.h>
#include <Node.h>

View file

@ -15,7 +15,7 @@
#include <QtCore/QTimer>
#include <QtCore/QThread>
#include <Logging.h>
#include <LogHandler.h>
#include <NodeList.h>
#include <PacketHeaders.h>
#include <SharedUtil.h>

View file

@ -9,7 +9,7 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <Logging.h>
#include <LogHandler.h>
#include <SharedUtil.h>
#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);

View file

@ -18,7 +18,7 @@
#include <AccountManager.h>
#include <HTTPConnection.h>
#include <Logging.h>
#include <LogHandler.h>
#include <UUID.h>
#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);

View file

@ -17,7 +17,7 @@
#include <QtCore/QCoreApplication>
#include <Logging.h>
#include <LogHandler.h>
#include <SharedUtil.h>
#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;

View file

@ -11,7 +11,7 @@
#include <QtCore/QCoreApplication>
#include <Logging.h>
#include <LogHandler.h>
#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();

View file

@ -58,7 +58,7 @@
#include <HFActionEvent.h>
#include <HFBackEvent.h>
#include <LocalVoxelsList.h>
#include <Logging.h>
#include <LogHandler.h>
#include <NetworkAccessManager.h>
#include <OctalCode.h>
#include <OctreeSceneStats.h>

View file

@ -19,10 +19,11 @@
#include <QtCore/QUrl>
#include <QtNetwork/QHostInfo>
#include <LogHandler.h>
#include "AccountManager.h"
#include "Assignment.h"
#include "HifiSockAddr.h"
#include "Logging.h"
#include "LimitedNodeList.h"
#include "PacketHeaders.h"
#include "SharedUtil.h"

View file

@ -15,10 +15,11 @@
#include <QtCore/QUrl>
#include <QtNetwork/QHostInfo>
#include <LogHandler.h>
#include "AccountManager.h"
#include "Assignment.h"
#include "HifiSockAddr.h"
#include "Logging.h"
#include "NodeList.h"
#include "PacketHeaders.h"
#include "SharedUtil.h"

View file

@ -14,7 +14,8 @@
#include <QtCore/QThread>
#include <QtCore/QTimer>
#include "Logging.h"
#include <LogHandler.h>
#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);

View file

@ -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 <qtimer.h>
#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<QString, int>::iterator message = _repeatMessageCountHash.begin();
while (message != _repeatMessageCountHash.end()) {
message = _repeatMessageCountHash.erase(message);
}
}

View file

@ -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 <qhash.h>
#include <qobject.h>
@ -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<QRegExp> _repeatedMessageRegexes;
QHash<QString, int> _repeatMessageCountHash;
};
#endif // hifi_Logging_h
#endif // hifi_LogHandler_h